From c82eb97e2b60857c449aed8d359610a432e0861b Mon Sep 17 00:00:00 2001 From: rzalamena <> Date: Mon, 3 Oct 2016 12:30:43 +0000 Subject: [PATCH] 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@ --- src/usr.sbin/ntpd/util.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/usr.sbin/ntpd/util.c b/src/usr.sbin/ntpd/util.c index 146834b5..b3ba2874 100644 --- a/src/usr.sbin/ntpd/util.c +++ b/src/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 @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include #include #include #include @@ -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__);