From 94cf1f830d25409ba80b0933075e026e41fe0e3c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 15 Mar 2018 09:01:45 +0000 Subject: win32: restrict visibility of special devices Handling of the special devices /dev/zero and /dev/urandom was inconsistent: - they could be used as arguments to 'cat' but not 'od'; - they could not be used in shell redirection. Restrict the use of these devices to two places: - as input files to 'dd' with the 'if=' argument; - internally within 'shred'. See GitHub issue #98. --- coreutils/dd.c | 4 +++- coreutils/shred.c | 4 ++++ include/mingw.h | 2 ++ win32/mingw.c | 31 ++++++++++++++++++++++++------- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/coreutils/dd.c b/coreutils/dd.c index 4362ae798..10066575e 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -429,8 +429,10 @@ int dd_main(int argc UNUSED_PARAM, char **argv) #endif if (infile) { +#if !ENABLE_PLATFORM_MINGW32 xmove_fd(xopen(infile, O_RDONLY), ifd); -#if ENABLE_PLATFORM_MINGW32 +#else + xmove_fd(mingw_xopen(infile, O_RDONLY), ifd); if (!strcmp(infile, "/dev/zero")) { mingw_read_zero(ifd); } diff --git a/coreutils/shred.c b/coreutils/shred.c index 1b65a359e..0ebbc39ca 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -38,6 +38,10 @@ #include "libbb.h" +#if ENABLE_PLATFORM_MINGW32 +#define xopen mingw_xopen +#endif + int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int shred_main(int argc UNUSED_PARAM, char **argv) { diff --git a/include/mingw.h b/include/mingw.h index 09903d582..386540b37 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -30,6 +30,7 @@ int inet_pton(int af, const char *src, void *dst); #define FD_CLOEXEC 0x1 #define O_NONBLOCK 0 #define O_NOFOLLOW 0 +#define O_SPECIAL 0x800000 /* * grp.h @@ -367,6 +368,7 @@ int kill(pid_t pid, int sig); int link(const char *oldpath, const char *newpath); NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); int mingw_open (const char *filename, int oflags, ...); +int mingw_xopen(const char *filename, int oflags); void mingw_read_zero(int fd); void mingw_read_random(int fd); ssize_t mingw_read(int fd, void *buf, size_t count); diff --git a/win32/mingw.c b/win32/mingw.c index f9967f1c4..981c50415 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -157,6 +157,7 @@ int mingw_open (const char *filename, int oflags, ...) va_list args; unsigned mode; int fd; + int special = 0; int devnull = 0; int devzero = 0; int devrand = 0; @@ -165,16 +166,20 @@ int mingw_open (const char *filename, int oflags, ...) mode = va_arg(args, int); va_end(args); - if (oflags & O_NONBLOCK) { - oflags &= ~O_NONBLOCK; + if (oflags & O_SPECIAL) { + oflags &= ~O_SPECIAL; + special = 1; } if (filename && !strncmp(filename, "/dev/", 5)) { - if (!strcmp(filename+5, "null")) + if (!strcmp(filename+5, "null")) { devnull = 1; - else if (!strcmp(filename+5, "zero")) - devzero = 1; - else if (!strcmp(filename+5, "urandom")) - devrand = 1; + } + else if (special) { + if (!strcmp(filename+5, "zero")) + devzero = 1; + else if (!strcmp(filename+5, "urandom")) + devrand = 1; + } if (devnull || devzero || devrand ) filename = "nul"; @@ -194,6 +199,18 @@ int mingw_open (const char *filename, int oflags, ...) return fd; } +int mingw_xopen(const char *pathname, int flags) +{ + int ret; + + /* allow use of special devices */ + ret = mingw_open(pathname, flags|O_SPECIAL); + if (ret < 0) { + bb_perror_msg_and_die("can't open '%s'", pathname); + } + return ret; +} + #undef fopen FILE *mingw_fopen (const char *filename, const char *otype) { -- cgit v1.2.3-55-g6feb