Author: jilles Date: Wed Feb 12 21:10:40 2014 New Revision: 261813 URL: http://svnweb.freebsd.org/changeset/base/261813
Log: MFC r241046: libc: Use O_CLOEXEC for various internal file descriptors. This fixes a race condition where another thread may fork(), 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. Modified: stable/9/lib/libc/gen/getcap.c stable/9/lib/libc/gen/getcwd.c stable/9/lib/libc/gen/nlist.c stable/9/lib/libc/gen/opendir.c stable/9/lib/libc/gen/pututxline.c stable/9/lib/libc/gen/readpassphrase.c stable/9/lib/libc/gen/sem_new.c stable/9/lib/libc/gen/syslog.c stable/9/lib/libc/locale/ldpart.c stable/9/lib/libc/nls/msgcat.c stable/9/lib/libc/yp/yplib.c Directory Properties: stable/9/lib/libc/ (props changed) Modified: stable/9/lib/libc/gen/getcap.c ============================================================================== --- stable/9/lib/libc/gen/getcap.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/getcap.c Wed Feb 12 21:10:40 2014 (r261813) @@ -264,7 +264,7 @@ getent(char **cap, u_int *len, char **db *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; Modified: stable/9/lib/libc/gen/getcwd.c ============================================================================== --- stable/9/lib/libc/gen/getcwd.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/getcwd.c Wed Feb 12 21:10:40 2014 (r261813) @@ -140,7 +140,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) Modified: stable/9/lib/libc/gen/nlist.c ============================================================================== --- stable/9/lib/libc/gen/nlist.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/nlist.c Wed Feb 12 21:10:40 2014 (r261813) @@ -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); Modified: stable/9/lib/libc/gen/opendir.c ============================================================================== --- stable/9/lib/libc/gen/opendir.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/opendir.c Wed Feb 12 21:10:40 2014 (r261813) @@ -190,7 +190,8 @@ __opendir_common(int fd, const char *nam */ if (flags & DTF_REWIND) { (void)_close(fd); - if ((fd = _open(name, O_RDONLY | O_DIRECTORY)) == -1) { + if ((fd = _open(name, O_RDONLY | O_DIRECTORY | + O_CLOEXEC)) == -1) { saved_errno = errno; free(buf); free(dirp); Modified: stable/9/lib/libc/gen/pututxline.c ============================================================================== --- stable/9/lib/libc/gen/pututxline.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/pututxline.c Wed Feb 12 21:10:40 2014 (r261813) @@ -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); @@ -219,7 +219,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; @@ -253,7 +253,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) Modified: stable/9/lib/libc/gen/readpassphrase.c ============================================================================== --- stable/9/lib/libc/gen/readpassphrase.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/readpassphrase.c Wed Feb 12 21:10:40 2014 (r261813) @@ -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); Modified: stable/9/lib/libc/gen/sem_new.c ============================================================================== --- stable/9/lib/libc/gen/sem_new.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/sem_new.c Wed Feb 12 21:10:40 2014 (r261813) @@ -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) Modified: stable/9/lib/libc/gen/syslog.c ============================================================================== --- stable/9/lib/libc/gen/syslog.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/gen/syslog.c Wed Feb 12 21:10:40 2014 (r261813) @@ -300,7 +300,8 @@ vsyslog(int pri, const char *fmt, va_lis * 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; Modified: stable/9/lib/libc/locale/ldpart.c ============================================================================== --- stable/9/lib/libc/locale/ldpart.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/locale/ldpart.c Wed Feb 12 21:10:40 2014 (r261813) @@ -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; Modified: stable/9/lib/libc/nls/msgcat.c ============================================================================== --- stable/9/lib/libc/nls/msgcat.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/nls/msgcat.c Wed Feb 12 21:10:40 2014 (r261813) @@ -384,7 +384,7 @@ load_msgcat(const char *path, const char } UNLOCK; - if ((fd = _open(path, O_RDONLY)) == -1) { + if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) { SAVEFAIL(name, lang, errno); NLRETERR(errno); } Modified: stable/9/lib/libc/yp/yplib.c ============================================================================== --- stable/9/lib/libc/yp/yplib.c Wed Feb 12 20:21:12 2014 (r261812) +++ stable/9/lib/libc/yp/yplib.c Wed Feb 12 21:10:40 2014 (r261813) @@ -373,7 +373,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); _______________________________________________ svn-src-all@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"