Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh simplify and eliminate TOCTOU.



details:   https://anonhg.NetBSD.org/src/rev/a7e037199aee
branches:  trunk
changeset: 333220:a7e037199aee
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Oct 23 21:03:25 2014 +0000

description:
simplify and eliminate TOCTOU.

diffstat:

 bin/sh/redir.c |  23 ++++++++++-------------
 1 files changed, 10 insertions(+), 13 deletions(-)

diffs (47 lines):

diff -r 67c9275ec357 -r a7e037199aee bin/sh/redir.c
--- a/bin/sh/redir.c    Thu Oct 23 20:08:27 2014 +0000
+++ b/bin/sh/redir.c    Thu Oct 23 21:03:25 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: redir.c,v 1.36 2014/10/15 14:54:25 christos Exp $      */
+/*     $NetBSD: redir.c,v 1.37 2014/10/23 21:03:25 christos Exp $      */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)redir.c    8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: redir.c,v 1.36 2014/10/15 14:54:25 christos Exp $");
+__RCSID("$NetBSD: redir.c,v 1.37 2014/10/23 21:03:25 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -197,20 +197,17 @@
        case NTO:
                if (Cflag) {
                        fname = redir->nfile.expfname;
-                       if (stat(fname, &sb) == -1) {
+                       if ((f = open(fname, O_WRONLY)) == -1) {
                                if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL,
                                    0666)) < 0)
                                        goto ecreate;
-                       } else if (!S_ISREG(sb.st_mode)) {
-                               if ((f = open(fname, O_WRONLY, 0666)) < 0)
-                                       goto ecreate;
-                               if (fstat(f, &sb) != -1 &&
-                                   S_ISREG(sb.st_mode)) {
-                                       close(f);
-                                       errno = EEXIST;
-                                       goto ecreate;
-                               }
-                       } else {
+                       } else if (fstat(f, &sb) == -1) {
+                               int serrno = errno;
+                               close(f);
+                               errno = serrno;
+                               goto ecreate;
+                       } else if (S_ISREG(sb.st_mode)) {
+                               close(f);
                                errno = EEXIST;
                                goto ecreate;
                        }



Home | Main Index | Thread Index | Old Index