From 76f2606181eabc8ab20aa7297bbddc6e78bb549d Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Thu, 16 Aug 2018 21:36:19 +0000 Subject: [PATCH] Fix Coverity warnings about mkstemp in tests umask(2) should always be used prior to mkstemp(3) so the temporary file won't be created with insecure permissions. Reported by: Coverity CID: 1331605 1347173 1375366 1339800 1331604 1296056 1296060 CID: 1296057 1296062 MFC after: 2 weeks --- tests/sys/aio/aio_kqueue_test.c | 2 ++ tests/sys/aio/lio_kqueue_test.c | 2 ++ tests/sys/file/dup_test.c | 2 ++ tests/sys/file/flock_helper.c | 1 + tests/sys/kern/kern_copyin.c | 2 ++ tests/sys/kern/pipe/pipe_overcommit2_test.c | 2 ++ tests/sys/kern/ptrace_test.c | 2 ++ tests/sys/kern/unix_passfd_test.c | 1 + tests/sys/posixshm/posixshm_test.c | 1 + 9 files changed, 15 insertions(+) diff --git a/tests/sys/aio/aio_kqueue_test.c b/tests/sys/aio/aio_kqueue_test.c index f3e63376eb1..e855653a273 100644 --- a/tests/sys/aio/aio_kqueue_test.c +++ b/tests/sys/aio/aio_kqueue_test.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -90,6 +91,7 @@ main (int argc, char *argv[]) if (argc == 1) { strcpy(pathname, PATH_TEMPLATE); + umask(0077); fd = mkstemp(pathname); file = pathname; tmp_file = 1; diff --git a/tests/sys/aio/lio_kqueue_test.c b/tests/sys/aio/lio_kqueue_test.c index 3226573721c..1a7fe8b2d0b 100644 --- a/tests/sys/aio/lio_kqueue_test.c +++ b/tests/sys/aio/lio_kqueue_test.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -86,6 +87,7 @@ main(int argc, char *argv[]) if (argc == 1) { strcpy(pathname, PATH_TEMPLATE); + umask(0077); fd = mkstemp(pathname); file = pathname; tmp_file = 1; diff --git a/tests/sys/file/dup_test.c b/tests/sys/file/dup_test.c index 81738188962..825f796ef27 100644 --- a/tests/sys/file/dup_test.c +++ b/tests/sys/file/dup_test.c @@ -50,6 +50,7 @@ * open files limit work. */ +#include #include #include #include @@ -68,6 +69,7 @@ getafile(void) int fd; char temp[] = "/tmp/dup2XXXXXXXXX"; + umask(0077); if ((fd = mkstemp(temp)) < 0) err(1, "mkstemp"); remove(temp); diff --git a/tests/sys/file/flock_helper.c b/tests/sys/file/flock_helper.c index 0fca15e48ef..4f2ffcea8b2 100644 --- a/tests/sys/file/flock_helper.c +++ b/tests/sys/file/flock_helper.c @@ -92,6 +92,7 @@ make_file(const char *pathname, off_t sz) filename = malloc(len); strcpy(filename, pathname); strcat(filename, template); + umask(0077); fd = mkstemp(filename); if (fd < 0) err(1, "mkstemp"); diff --git a/tests/sys/kern/kern_copyin.c b/tests/sys/kern/kern_copyin.c index a8cf0422eec..98d330f6ade 100644 --- a/tests/sys/kern/kern_copyin.c +++ b/tests/sys/kern/kern_copyin.c @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include @@ -69,6 +70,7 @@ ATF_TC_BODY(kern_copyin, tc) atf_tc_skip("Platform is not supported."); #endif + umask(0077); scratch_file = mkstemp(template); ATF_REQUIRE(scratch_file != -1); unlink(template); diff --git a/tests/sys/kern/pipe/pipe_overcommit2_test.c b/tests/sys/kern/pipe/pipe_overcommit2_test.c index ee1e714d0d8..6ee0707981a 100644 --- a/tests/sys/kern/pipe/pipe_overcommit2_test.c +++ b/tests/sys/kern/pipe/pipe_overcommit2_test.c @@ -27,6 +27,7 @@ */ #include +#include #include #include #include @@ -50,6 +51,7 @@ main(void) lastfd = -1; + umask(0077); if (mkstemp(template) == -1) err(1, "mkstemp failed"); diff --git a/tests/sys/kern/ptrace_test.c b/tests/sys/kern/ptrace_test.c index 9589bb62922..7773b1f2673 100644 --- a/tests/sys/kern/ptrace_test.c +++ b/tests/sys/kern/ptrace_test.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -3347,6 +3348,7 @@ ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc) ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0); (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX"); + umask(0077); fd = mkstemp(tmpfile); ATF_REQUIRE(fd >= 0); diff --git a/tests/sys/kern/unix_passfd_test.c b/tests/sys/kern/unix_passfd_test.c index 7267fe931b4..5ca452526a6 100644 --- a/tests/sys/kern/unix_passfd_test.c +++ b/tests/sys/kern/unix_passfd_test.c @@ -87,6 +87,7 @@ tempfile(int *fdp) snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX", getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR")); + umask(0077); fd = mkstemp(path); ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path); (void)unlink(path); diff --git a/tests/sys/posixshm/posixshm_test.c b/tests/sys/posixshm/posixshm_test.c index 05774df2b28..32d5a898a40 100644 --- a/tests/sys/posixshm/posixshm_test.c +++ b/tests/sys/posixshm/posixshm_test.c @@ -54,6 +54,7 @@ gen_test_path(void) snprintf(test_path, sizeof(test_path), "%s/tmp.XXXXXX", getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR")); test_path[sizeof(test_path) - 1] = '\0'; + umask(0077); ATF_REQUIRE_MSG(mkstemp(test_path) != -1, "mkstemp failed; errno=%d", errno); ATF_REQUIRE_MSG(unlink(test_path) == 0,