From: Jim Meyering Date: Mon, 19 Dec 2005 18:16:07 +0000 (+0000) Subject: (main) Avoid a minor race condition when `-m MODE' is specified, by using X-Git-Tag: v6.0~1070 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=17f521adfdd3b415f68fc30b61eb115c1e379a79;p=thirdparty%2Fcoreutils.git (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. --- 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]));