From: Luyao Huang Date: Mon, 25 Sep 2017 18:27:07 +0000 (+0200) Subject: tests: Do not ignore mode parameter in mocked open() X-Git-Tag: CVE-2017-1000256~112 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3e581d150a28f5c8f577216220d19011c3bb7fbc;p=thirdparty%2Flibvirt.git tests: Do not ignore mode parameter in mocked open() This is normally not an issue since the tests which use mocked open() do not create files. But once coverage build is enabled, gcov_open will use O_CREATE and real_open will read random data rather than the actual mode argument. Signed-off-by: Jiri Denemark --- diff --git a/tests/virfilewrapper.c b/tests/virfilewrapper.c index fede7b2e89..1d1d182708 100644 --- a/tests/virfilewrapper.c +++ b/tests/virfilewrapper.c @@ -257,10 +257,21 @@ int open(const char *path, int flags, ...) { int ret = -1; char *newpath = NULL; + va_list ap; + mode_t mode = 0; PATH_OVERRIDE(newpath, path); - ret = real_open(newpath, flags); + /* The mode argument is mandatory when O_CREAT is set in flags, + * otherwise the argument is ignored. + */ + if (flags & O_CREAT) { + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + } + + ret = real_open(newpath, flags, mode); VIR_FREE(newpath); diff --git a/tests/virusbmock.c b/tests/virusbmock.c index 8d60664944..f430a2edad 100644 --- a/tests/virusbmock.c +++ b/tests/virusbmock.c @@ -87,13 +87,26 @@ int open(const char *pathname, int flags, ...) { char *path; int ret; + va_list ap; + mode_t mode = 0; init_syms(); path = get_fake_path(pathname); if (!path) return -1; - ret = realopen(path, flags); + + /* The mode argument is mandatory when O_CREAT is set in flags, + * otherwise the argument is ignored. + */ + if (flags & O_CREAT) { + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + } + + ret = realopen(path, flags, mode); + VIR_FREE(path); return ret; }