Module Name:    src
Committed By:   christos
Date:           Mon Nov 11 16:39:21 UTC 2013

Modified Files:
        src/usr.sbin/syslogd: syslogd.c

Log Message:
CID 1128379: Fix TOCTOU, always open the file first with non-blocking I/O,
do that stat later and leave the file as non-blocking because it does not
make a difference for plain files.


To generate a diff of this commit:
cvs rdiff -u -r1.117 -r1.118 src/usr.sbin/syslogd/syslogd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/syslogd/syslogd.c
diff -u src/usr.sbin/syslogd/syslogd.c:1.117 src/usr.sbin/syslogd/syslogd.c:1.118
--- src/usr.sbin/syslogd/syslogd.c:1.117	Sat Nov  9 14:00:18 2013
+++ src/usr.sbin/syslogd/syslogd.c	Mon Nov 11 11:39:21 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: syslogd.c,v 1.117 2013/11/09 19:00:18 christos Exp $	*/
+/*	$NetBSD: syslogd.c,v 1.118 2013/11/11 16:39:21 christos Exp $	*/
 
 /*
  * Copyright (c) 1983, 1988, 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 19
 #if 0
 static char sccsid[] = "@(#)syslogd.c	8.3 (Berkeley) 4/4/94";
 #else
-__RCSID("$NetBSD: syslogd.c,v 1.117 2013/11/09 19:00:18 christos Exp $");
+__RCSID("$NetBSD: syslogd.c,v 1.118 2013/11/11 16:39:21 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -3898,26 +3898,25 @@ cfline(size_t linenum, const char *line,
 			f->f_flags |= FFLAG_SIGN;
 #endif /* !DISABLE_SIGN */
 		(void)strlcpy(f->f_un.f_fname, p, sizeof(f->f_un.f_fname));
-		if (!stat(p, &sb) && S_ISFIFO(sb.st_mode)) {
+		if ((f->f_file = open(p, O_WRONLY|O_APPEND|O_NONBLOCK, 0)) < 0)
+		{
+			f->f_type = F_UNUSED;
+			logerror("%s", p);
+			break;
+		}
+		if (!fstat(f->f_file, &sb) && S_ISFIFO(sb.st_mode)) {
 			f->f_file = -1;
 			f->f_type = F_FIFO;
 			break;
 		}
 
-		if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) {
-			f->f_type = F_UNUSED;
-			logerror("%s", p);
-			break;
-		}
 		if (isatty(f->f_file)) {
 			f->f_type = F_TTY;
 			if (strcmp(p, ctty) == 0)
 				f->f_type = F_CONSOLE;
-			if (fcntl(f->f_file, F_SETFL, O_NONBLOCK) == -1)
-				logerror("Warning: cannot change tty fd for"
-				    " `%s' to non-blocking.", p);
 		} else
 			f->f_type = F_FILE;
+
 		if (syncfile)
 			f->f_flags |= FFLAG_SYNC;
 		break;

Reply via email to