From 17f521adfdd3b415f68fc30b61eb115c1e379a79 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 19 Dec 2005 18:16:07 +0000 Subject: [PATCH] (main) Avoid a minor race condition when `-m MODE' is specified, by using open, fchown, and close rather than just chown. To do that reliably (even with an overly restrictive umask), ensure that each mknod/mkfifo call uses a mode including at least owner-read access. --- src/mkfifo.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/mkfifo.c b/src/mkfifo.c index baed853f8f..d31412b054 100644 --- a/src/mkfifo.c +++ b/src/mkfifo.c @@ -23,6 +23,7 @@ #include #include "system.h" +#include "chmod-safer.h" #include "error.h" #include "modechange.h" #include "quote.h" @@ -75,6 +76,7 @@ int main (int argc, char **argv) { mode_t newmode; + mode_t tmp_mode; const char *specified_mode; int exit_status = EXIT_SUCCESS; int optc; @@ -122,9 +124,14 @@ main (int argc, char **argv) free (change); } + /* This is the mode we'll use in the mknod or mkfifo call. + If it doesn't include S_IRUSR, use S_IRUSR so the final + open-for-fchmod will succeed. */ + tmp_mode = (newmode & S_IRUSR) ? newmode : S_IRUSR; + for (; optind < argc; ++optind) { - int fail = mkfifo (argv[optind], newmode); + int fail = mkfifo (argv[optind], tmp_mode); if (fail) error (0, errno, _("cannot create fifo %s"), quote (argv[optind])); @@ -133,7 +140,7 @@ main (int argc, char **argv) if (fail == 0 && specified_mode) { - fail = chmod (argv[optind], newmode); + fail = chmod_safer (argv[optind], newmode, 0, S_IFIFO); if (fail) error (0, errno, _("cannot set permissions of fifo %s"), quote (argv[optind])); -- 2.47.3