diff options
author | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-10-03 12:30:44 +0000 |
---|---|---|
committer | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-10-03 12:30:44 +0000 |
commit | ae60c1c6b0fa997314e9c82471a6e0ac2a7f1ea1 (patch) | |
tree | d083c7c24dcb1e38bada1c3514b1bfcf60ce2010 /usr.sbin/ntpd/util.c | |
parent | 77ba510b67f525bc2922ee3eb2b5df6b0eb4c199 (diff) |
Fix a possible bug that will happen with dup2() when oldd == newd. In that
case the dup2() would fail silently and the descriptor would remain with
the CLOEXEC flag causing the exec*()d child process to have unexpected
behavior.
ok guenther@
Diffstat (limited to 'usr.sbin/ntpd/util.c')
-rw-r--r-- | usr.sbin/ntpd/util.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/usr.sbin/ntpd/util.c b/usr.sbin/ntpd/util.c index 146834b5eca..b3ba2874a6a 100644 --- a/usr.sbin/ntpd/util.c +++ b/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.22 2016/09/14 13:20:16 rzalamena Exp $ */ +/* $OpenBSD: util.c,v 1.23 2016/10/03 12:30:43 rzalamena Exp $ */ /* * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org> @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <fcntl.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> @@ -184,7 +185,11 @@ start_child(char *pname, int cfd, int argc, char **argv) break; case 0: /* Prepare the parent socket and execute. */ - dup2(cfd, PARENT_SOCK_FILENO); + if (cfd != PARENT_SOCK_FILENO) { + if (dup2(cfd, PARENT_SOCK_FILENO) == -1) + fatal("dup2"); + } else if (fcntl(cfd, F_SETFD, 0) == -1) + fatal("fcntl"); execvp(argv[0], nargv); fatal("%s: execvp", __func__); |