From 05eb11cbc40a190f73ed1471d97062cabfc37d54 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Sat, 29 Sep 2012 11:54:34 +0000 Subject: [PATCH] libc: Use O_CLOEXEC for various internal file descriptors. This fixes a race condition where another thread may fork() before CLOEXEC is set, unintentionally passing the descriptor to the child process. This commit only adds O_CLOEXEC flags to open() or openat() calls where no fcntl(fd, F_SETFD, FD_CLOEXEC) follows. The separate fcntl() call still leaves a race window so it should be fixed later. --- lib/libc/gen/arc4random.c | 2 +- lib/libc/gen/getcap.c | 2 +- lib/libc/gen/getcwd.c | 2 +- lib/libc/gen/nlist.c | 2 +- lib/libc/gen/opendir.c | 3 ++- lib/libc/gen/pututxline.c | 6 +++--- lib/libc/gen/readpassphrase.c | 2 +- lib/libc/gen/sem_new.c | 2 +- lib/libc/gen/syslog.c | 3 ++- lib/libc/locale/ldpart.c | 2 +- lib/libc/nls/msgcat.c | 2 +- lib/libc/stdlib/rand.c | 2 +- lib/libc/stdlib/random.c | 2 +- lib/libc/yp/yplib.c | 2 +- 14 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c index 46059abdb433..59e410bc4386 100644 --- a/lib/libc/gen/arc4random.c +++ b/lib/libc/gen/arc4random.c @@ -153,7 +153,7 @@ arc4_stir(void) if (arc4_sysctl((u_char *)&rdat, KEYSIZE) == KEYSIZE) done = 1; if (!done) { - fd = _open(RANDOMDEV, O_RDONLY, 0); + fd = _open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0); if (fd >= 0) { if (_read(fd, &rdat, KEYSIZE) == KEYSIZE) done = 1; diff --git a/lib/libc/gen/getcap.c b/lib/libc/gen/getcap.c index 2700d3d10313..c321d122db12 100644 --- a/lib/libc/gen/getcap.c +++ b/lib/libc/gen/getcap.c @@ -264,7 +264,7 @@ getent(char **cap, u_int *len, char **db_array, int fd, const char *name, *cap = cbuf; return (retval); } else { - fd = _open(*db_p, O_RDONLY, 0); + fd = _open(*db_p, O_RDONLY | O_CLOEXEC, 0); if (fd < 0) continue; myfd = 1; diff --git a/lib/libc/gen/getcwd.c b/lib/libc/gen/getcwd.c index 40feedfa0f2a..da76d56824f4 100644 --- a/lib/libc/gen/getcwd.c +++ b/lib/libc/gen/getcwd.c @@ -142,7 +142,7 @@ getcwd(pt, size) /* Open and stat parent directory. */ fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD, - "..", O_RDONLY); + "..", O_RDONLY | O_CLOEXEC); if (fd == -1) goto err; if (dir) diff --git a/lib/libc/gen/nlist.c b/lib/libc/gen/nlist.c index 1361af3366c9..159731c4337b 100644 --- a/lib/libc/gen/nlist.c +++ b/lib/libc/gen/nlist.c @@ -66,7 +66,7 @@ nlist(name, list) { int fd, n; - fd = _open(name, O_RDONLY, 0); + fd = _open(name, O_RDONLY | O_CLOEXEC, 0); if (fd < 0) return (-1); n = __fdnlist(fd, list); diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 436639907ccc..2d505fc47899 100644 --- a/lib/libc/gen/opendir.c +++ b/lib/libc/gen/opendir.c @@ -199,7 +199,8 @@ __opendir_common(int fd, const char *name, int flags) * which has also been read -- see fts.c. */ if (flags & DTF_REWIND) { - if ((fd2 = _open(name, O_RDONLY | O_DIRECTORY)) == -1) { + if ((fd2 = _open(name, O_RDONLY | O_DIRECTORY | + O_CLOEXEC)) == -1) { saved_errno = errno; free(buf); free(dirp); diff --git a/lib/libc/gen/pututxline.c b/lib/libc/gen/pututxline.c index 98addee1f586..555386c549c0 100644 --- a/lib/libc/gen/pututxline.c +++ b/lib/libc/gen/pututxline.c @@ -47,7 +47,7 @@ futx_open(const char *file) struct stat sb; int fd; - fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK, 0644); + fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644); if (fd < 0) return (NULL); @@ -235,7 +235,7 @@ utx_lastlogin_upgrade(void) struct stat sb; int fd; - fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR, 0644); + fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR|O_CLOEXEC, 0644); if (fd < 0) return; @@ -269,7 +269,7 @@ utx_log_add(const struct futx *fu) vec[1].iov_len = l; l = htobe16(l); - fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND, 0644); + fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC, 0644); if (fd < 0) return (-1); if (_writev(fd, vec, 2) == -1) diff --git a/lib/libc/gen/readpassphrase.c b/lib/libc/gen/readpassphrase.c index d57e703e5fb4..86c7811d9fb1 100644 --- a/lib/libc/gen/readpassphrase.c +++ b/lib/libc/gen/readpassphrase.c @@ -68,7 +68,7 @@ restart: * stdin and write to stderr unless a tty is required. */ if ((flags & RPP_STDIN) || - (input = output = _open(_PATH_TTY, O_RDWR)) == -1) { + (input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) { if (flags & RPP_REQUIRE_TTY) { errno = ENOTTY; return(NULL); diff --git a/lib/libc/gen/sem_new.c b/lib/libc/gen/sem_new.c index b748b49235af..b11d54721ba5 100644 --- a/lib/libc/gen/sem_new.c +++ b/lib/libc/gen/sem_new.c @@ -198,7 +198,7 @@ _sem_open(const char *name, int flags, ...) goto error; } - fd = _open(path, flags|O_RDWR, mode); + fd = _open(path, flags|O_RDWR|O_CLOEXEC, mode); if (fd == -1) goto error; if (flock(fd, LOCK_EX) == -1) diff --git a/lib/libc/gen/syslog.c b/lib/libc/gen/syslog.c index 99308e992f8f..6357761cad43 100644 --- a/lib/libc/gen/syslog.c +++ b/lib/libc/gen/syslog.c @@ -300,7 +300,8 @@ vsyslog(int pri, const char *fmt, va_list ap) * Make sure the error reported is the one from the syslogd failure. */ if (LogStat & LOG_CONS && - (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) { + (fd = _open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK|O_CLOEXEC, 0)) >= + 0) { struct iovec iov[2]; struct iovec *v = iov; diff --git a/lib/libc/locale/ldpart.c b/lib/libc/locale/ldpart.c index ea7b3886ada8..ccf5e7f81fb9 100644 --- a/lib/libc/locale/ldpart.c +++ b/lib/libc/locale/ldpart.c @@ -87,7 +87,7 @@ __part_load_locale(const char *name, strcat(filename, name); strcat(filename, "/"); strcat(filename, category_filename); - if ((fd = _open(filename, O_RDONLY)) < 0) + if ((fd = _open(filename, O_RDONLY | O_CLOEXEC)) < 0) return (_LDP_ERROR); if (_fstat(fd, &st) != 0) goto bad_locale; diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c index 4532e909fa6a..44b144084092 100644 --- a/lib/libc/nls/msgcat.c +++ b/lib/libc/nls/msgcat.c @@ -384,7 +384,7 @@ load_msgcat(const char *path, const char *name, const char *lang) } UNLOCK; - if ((fd = _open(path, O_RDONLY)) == -1) { + if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) { SAVEFAIL(name, lang, errno); NLRETERR(errno); } diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index 077c1ba4ad19..3835976e4200 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -121,7 +121,7 @@ sranddev() int fd, done; done = 0; - fd = _open("/dev/random", O_RDONLY, 0); + fd = _open("/dev/random", O_RDONLY | O_CLOEXEC, 0); if (fd >= 0) { if (_read(fd, (void *) &next, sizeof(next)) == sizeof(next)) done = 1; diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index 6da541912825..4a1af5452fd9 100644 --- a/lib/libc/stdlib/random.c +++ b/lib/libc/stdlib/random.c @@ -303,7 +303,7 @@ srandomdev(void) len = rand_deg * sizeof state[0]; done = 0; - fd = _open("/dev/random", O_RDONLY, 0); + fd = _open("/dev/random", O_RDONLY | O_CLOEXEC, 0); if (fd >= 0) { if (_read(fd, (void *) state, len) == (ssize_t) len) done = 1; diff --git a/lib/libc/yp/yplib.c b/lib/libc/yp/yplib.c index 3dfc0aa55f2f..1778c6a6a3ba 100644 --- a/lib/libc/yp/yplib.c +++ b/lib/libc/yp/yplib.c @@ -375,7 +375,7 @@ again: ysd->dom_socket = -1; } snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2); - if ((fd = _open(path, O_RDONLY)) == -1) { + if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) { /* no binding file, YP is dead. */ /* Try to bring it back to life. */ _close(fd);