From 72c5b7d85d06d6f71960ff00e780b87ca9d33d78 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Fri, 6 Jan 2006 14:50:44 +1100 Subject: =?UTF-8?q?=20=20=20-=20djm@cvs.openbsd.org=202006/01/05=2023:43:5?= =?UTF-8?q?3=20=20=20=20=20=20[misc.c]=20=20=20=20=20=20check=20that=20std?= =?UTF-8?q?io=20file=20descriptors=20are=20actually=20closed=20before=20cl?= =?UTF-8?q?obbering=20=20=20=20=20=20them=20in=20sanitise=5Fstdfd().=20pro?= =?UTF-8?q?blems=20occurred=20when=20a=20lower=20numbered=20fd=20was=20=20?= =?UTF-8?q?=20=20=20=20closed,=20but=20higher=20ones=20weren't.=20spotted?= =?UTF-8?q?=20by,=20and=20patch=20tested=20by=20=20=20=20=20=20Fr=C3=A9d?= =?UTF-8?q?=C3=A9ric=20Olivi=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'misc.c') diff --git a/misc.c b/misc.c index 0339cede..b876c003 100644 --- a/misc.c +++ b/misc.c @@ -24,7 +24,7 @@ */ #include "includes.h" -RCSID("$OpenBSD: misc.c,v 1.40 2006/01/02 07:53:44 reyk Exp $"); +RCSID("$OpenBSD: misc.c,v 1.41 2006/01/05 23:43:53 djm Exp $"); #ifdef SSH_TUN_OPENBSD #include @@ -616,18 +616,20 @@ tun_open(int tun, int mode) void sanitise_stdfd(void) { - int nullfd; + int nullfd, dupfd; - if ((nullfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { + if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno)); exit(1); } - while (nullfd < 2) { - if (dup2(nullfd, nullfd + 1) == -1) { + while (++dupfd <= 2) { + /* Only clobber closed fds */ + if (fcntl(dupfd, F_GETFL, 0) >= 0) + continue; + if (dup2(nullfd, dupfd) == -1) { fprintf(stderr, "dup2: %s", strerror(errno)); exit(1); } - nullfd++; } if (nullfd > 2) close(nullfd); -- cgit v1.2.3