From 98ac59d5a1e09709bf72910de9f2775d69b6c03a Mon Sep 17 00:00:00 2001 From: guenther <> Date: Tue, 30 Aug 2016 14:44:45 +0000 Subject: [PATCH] Use O_CLOEXEC when opening fds local to a function ok jca@ krw@ --- src/lib/libutil/logwtmp.c | 4 ++-- src/lib/libutil/pty.c | 4 ++-- src/lib/libutil/readlabel.c | 8 ++++---- src/lib/libutil/uucplock.c | 9 +++++---- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/lib/libutil/logwtmp.c b/src/lib/libutil/logwtmp.c index 0f968c76..decde069 100644 --- a/src/lib/libutil/logwtmp.c +++ b/src/lib/libutil/logwtmp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: logwtmp.c,v 1.9 2005/08/02 21:46:23 espie Exp $ */ +/* $OpenBSD: logwtmp.c,v 1.10 2016/08/30 14:44:45 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -46,7 +46,7 @@ logwtmp(const char *line, const char *name, const char *host) struct utmp ut; int fd; - if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0) + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) < 0) return; if (fstat(fd, &buf) == 0) { (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line)); diff --git a/src/lib/libutil/pty.c b/src/lib/libutil/pty.c index 598b8fa1..2a19de81 100644 --- a/src/lib/libutil/pty.c +++ b/src/lib/libutil/pty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pty.c,v 1.19 2013/05/21 19:07:02 matthew Exp $ */ +/* $OpenBSD: pty.c,v 1.20 2016/08/30 14:44:45 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -54,7 +54,7 @@ openpty(int *amaster, int *aslave, char *name, struct termios *termp, * Use /dev/ptm and the PTMGET ioctl to get a properly set up and * owned pty/tty pair. */ - fd = open(PATH_PTMDEV, O_RDWR, 0); + fd = open(PATH_PTMDEV, O_RDWR|O_CLOEXEC); if (fd == -1) return (-1); if ((ioctl(fd, PTMGET, &ptm) == -1)) { diff --git a/src/lib/libutil/readlabel.c b/src/lib/libutil/readlabel.c index e2cee311..d53820e6 100644 --- a/src/lib/libutil/readlabel.c +++ b/src/lib/libutil/readlabel.c @@ -1,4 +1,4 @@ -/* $OpenBSD: readlabel.c,v 1.13 2015/01/16 16:48:52 deraadt Exp $ */ +/* $OpenBSD: readlabel.c,v 1.14 2016/08/30 14:44:45 guenther Exp $ */ /* * Copyright (c) 1996, Jason Downs. All rights reserved. @@ -59,7 +59,7 @@ readlabelfs(char *device, int verbose) /* Perform disk mapping if device is given as a DUID. */ if (isduid(device, 0)) { - if ((fd = open("/dev/diskmap", O_RDONLY)) != -1) { + if ((fd = open("/dev/diskmap", O_RDONLY|O_CLOEXEC)) != -1) { bzero(&dm, sizeof(struct dk_diskmap)); strlcpy(rpath, device, sizeof(rpath)); part = rpath[strlen(rpath) - 1]; @@ -105,12 +105,12 @@ readlabelfs(char *device, int verbose) } /* If rpath doesn't exist, change that partition back. */ - fd = open(rpath, O_RDONLY); + fd = open(rpath, O_RDONLY|O_CLOEXEC); if (fd < 0) { if (errno == ENOENT) { rpath[strlen(rpath) - 1] = part; - fd = open(rpath, O_RDONLY); + fd = open(rpath, O_RDONLY|O_CLOEXEC); if (fd < 0) { if (verbose) warn("%s", rpath); diff --git a/src/lib/libutil/uucplock.c b/src/lib/libutil/uucplock.c index f62273c8..bf63f775 100644 --- a/src/lib/libutil/uucplock.c +++ b/src/lib/libutil/uucplock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uucplock.c,v 1.17 2015/11/11 01:12:09 deraadt Exp $ */ +/* $OpenBSD: uucplock.c,v 1.18 2016/08/30 14:44:45 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -70,7 +70,8 @@ uu_lock(const char *ttyname) (long)pid); (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); - if ((tmpfd = open(lcktmpname, O_CREAT | O_TRUNC | O_WRONLY, 0664)) < 0) + tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664); + if (tmpfd < 0) GORET(0, UU_LOCK_CREAT_ERR); for (i = 0; i < MAXTRIES; i++) { @@ -82,7 +83,7 @@ uu_lock(const char *ttyname) * check to see if the process holding the lock * still exists */ - if ((fd = open(lckname, O_RDONLY)) < 0) + if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0) GORET(1, UU_LOCK_OPEN_ERR); if ((pid_old = get_pid(fd, &err)) == -1) @@ -126,7 +127,7 @@ uu_lock_txfr(const char *ttyname, pid_t pid) snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); - if ((fd = open(lckname, O_RDWR)) < 0) + if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0) return UU_LOCK_OWNER_ERR; if (get_pid(fd, &err) != getpid()) ret = UU_LOCK_OWNER_ERR;