touch(1): don't leak file descriptor if futimens(2) fails
authorcheloha <cheloha@openbsd.org>
Sat, 29 Jan 2022 00:06:26 +0000 (00:06 +0000)
committercheloha <cheloha@openbsd.org>
Sat, 29 Jan 2022 00:06:26 +0000 (00:06 +0000)
This conditional chain short-circuits if futimens(2) fails, leaving the
file descriptor open.  We need to evaluate each system call in the chain
separately to ensure we attempt to close(2) the descriptor.

With input from guenther@ and millert@.

Thread: https://marc.info/?l=openbsd-tech&m=164332809900558&w=2

ok millert@, probably ok guenther@

usr.bin/touch/touch.c

index 114b675..6cbdfcf 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: touch.c,v 1.26 2019/03/10 15:11:52 schwarze Exp $     */
+/*     $OpenBSD: touch.c,v 1.27 2022/01/29 00:06:26 cheloha Exp $      */
 /*     $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $   */
 
 /*
@@ -137,9 +137,18 @@ main(int argc, char *argv[])
 
                /* Create the file. */
                fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE);
-               if (fd == -1 || futimens(fd, ts) || close(fd)) {
+               if (fd == -1) {
                        rval = 1;
                        warn("%s", *argv);
+                       continue;
+               }
+               if (futimens(fd, ts) == -1) {
+                       warn("%s", *argv);
+                       rval = 1;
+               }
+               if (close(fd) == -1) {
+                       warn("%s", *argv);
+                       rval = 1;
                }
        }
        return rval;