From b873219f06e896327efacbca3dc9b7e3892b11c8 Mon Sep 17 00:00:00 2001 From: cheloha Date: Sat, 29 Jan 2022 00:06:26 +0000 Subject: [PATCH] touch(1): don't leak file descriptor if futimens(2) fails 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/usr.bin/touch/touch.c b/usr.bin/touch/touch.c index 114b6759f6a..6cbdfcfaaec 100644 --- a/usr.bin/touch/touch.c +++ b/usr.bin/touch/touch.c @@ -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; -- 2.20.1