aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-01-25 13:57:08 +0000
committerRon Yorston <rmy@pobox.com>2021-01-25 14:15:02 +0000
commiteb0c2bbbaf0722103124a589e3dfe952c2664cbb (patch)
tree3ae3ec89bd7a6041ed5aed1d537f6b2fb3586ed7
parent6422b0ef38d0835c86b40e6e642f18cdfb933e85 (diff)
downloadbusybox-w32-eb0c2bbbaf0722103124a589e3dfe952c2664cbb.tar.gz
busybox-w32-eb0c2bbbaf0722103124a589e3dfe952c2664cbb.tar.bz2
busybox-w32-eb0c2bbbaf0722103124a589e3dfe952c2664cbb.zip
tls: avoid unnecessary changes to POSIX build, part 2
On reflection, the previous commit may have been ill-advised. There are many calls to open_read_close() and most shouldn't be able to access special devices. (Though in practice only a few are enabled in busybox-w32.) Nonetheless, I've implemented a new mechanism which uses the macro MINGW_SPECIAL() to mark calls to functions that are allowed to access special devices. An unrelated change is to avoid compiling fputs_stdout() in coreutils/printf.c for the POSIX build.
-rw-r--r--coreutils/dd.c6
-rw-r--r--coreutils/printf.c2
-rw-r--r--coreutils/shred.c8
-rw-r--r--include/libbb.h3
-rw-r--r--include/mingw.h4
-rw-r--r--libbb/read.c7
-rw-r--r--networking/tls.c2
-rw-r--r--win32/mingw.c9
8 files changed, 23 insertions, 18 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c
index c150ef5bc..bd799aa2b 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -505,10 +505,8 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
505 if (G.flags & FLAG_IDIRECT) 505 if (G.flags & FLAG_IDIRECT)
506 iflag |= O_DIRECT; 506 iflag |= O_DIRECT;
507#endif 507#endif
508#if !ENABLE_PLATFORM_MINGW32 508 xmove_fd(MINGW_SPECIAL(xopen)(infile, iflag), ifd);
509 xmove_fd(xopen(infile, iflag), ifd); 509#if ENABLE_PLATFORM_MINGW32
510#else
511 xmove_fd(mingw_xopen(infile, iflag), ifd);
512 update_dev_fd(get_dev_type(infile), ifd); 510 update_dev_fd(get_dev_type(infile), ifd);
513#endif 511#endif
514 } else { 512 } else {
diff --git a/coreutils/printf.c b/coreutils/printf.c
index aabc51e0d..d1d22f39c 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -151,10 +151,12 @@ static double my_xstrtod(const char *arg)
151 return result; 151 return result;
152} 152}
153 153
154#if ENABLE_PLATFORM_MINGW32
154static int fputs_stdout(const char *s) 155static int fputs_stdout(const char *s)
155{ 156{
156 return fputs(s, stdout); 157 return fputs(s, stdout);
157} 158}
159#endif
158 160
159/* Handles %b; return 1 if output is to be short-circuited by \c */ 161/* Handles %b; return 1 if output is to be short-circuited by \c */
160static int print_esc_string(const char *str) 162static int print_esc_string(const char *str)
diff --git a/coreutils/shred.c b/coreutils/shred.c
index 86d4b66b4..0b11b9491 100644
--- a/coreutils/shred.c
+++ b/coreutils/shred.c
@@ -38,10 +38,6 @@
38 38
39#include "libbb.h" 39#include "libbb.h"
40 40
41#if ENABLE_PLATFORM_MINGW32
42#define xopen mingw_xopen
43#endif
44
45int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 41int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
46int shred_main(int argc UNUSED_PARAM, char **argv) 42int shred_main(int argc UNUSED_PARAM, char **argv)
47{ 43{
@@ -61,9 +57,9 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
61 opt = getopt32(argv, "fuzn:+vx", &num_iter); 57 opt = getopt32(argv, "fuzn:+vx", &num_iter);
62 argv += optind; 58 argv += optind;
63 59
64 zero_fd = xopen("/dev/zero", O_RDONLY); 60 zero_fd = MINGW_SPECIAL(xopen)("/dev/zero", O_RDONLY);
65 if (num_iter != 0) 61 if (num_iter != 0)
66 rand_fd = xopen("/dev/urandom", O_RDONLY); 62 rand_fd = MINGW_SPECIAL(xopen)("/dev/urandom", O_RDONLY);
67 63
68 if (!*argv) 64 if (!*argv)
69 bb_show_usage(); 65 bb_show_usage();
diff --git a/include/libbb.h b/include/libbb.h
index 3d6a6a0cf..a7f32e21e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -202,6 +202,9 @@ int klogctl(int type, char *b, int len);
202 202
203#if ENABLE_PLATFORM_MINGW32 203#if ENABLE_PLATFORM_MINGW32
204# include "mingw.h" 204# include "mingw.h"
205# define MINGW_SPECIAL(a) mingw_ ## a
206#else
207# define MINGW_SPECIAL(a) a
205#endif 208#endif
206 209
207/* Busybox does not use threads, we can speed up stdio. */ 210/* Busybox does not use threads, we can speed up stdio. */
diff --git a/include/mingw.h b/include/mingw.h
index d1e638231..a1ba5f5af 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -429,7 +429,11 @@ enum {DEV_NULL, DEV_ZERO, DEV_URANDOM, NOT_DEVICE = -1};
429int get_dev_type(const char *filename); 429int get_dev_type(const char *filename);
430void update_dev_fd(int dev, int fd); 430void update_dev_fd(int dev, int fd);
431int mingw_open (const char *filename, int oflags, ...); 431int mingw_open (const char *filename, int oflags, ...);
432
433/* functions which add O_SPECIAL to open(2) to allow access to devices */
432int mingw_xopen(const char *filename, int oflags); 434int mingw_xopen(const char *filename, int oflags);
435ssize_t mingw_open_read_close(const char *fn, void *buf, size_t size) FAST_FUNC;
436
433ssize_t mingw_read(int fd, void *buf, size_t count); 437ssize_t mingw_read(int fd, void *buf, size_t count);
434int mingw_close(int fd); 438int mingw_close(int fd);
435int pipe(int filedes[2]); 439int pipe(int filedes[2]);
diff --git a/libbb/read.c b/libbb/read.c
index 2e4317cd5..a342506a8 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -73,14 +73,7 @@ ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
73 73
74ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) 74ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
75{ 75{
76#if !ENABLE_PLATFORM_MINGW32
77 int fd = open(filename, O_RDONLY); 76 int fd = open(filename, O_RDONLY);
78#else
79 int fd, flag;
80
81 flag = O_RDONLY | (get_dev_type(filename) == DEV_URANDOM ? O_SPECIAL : 0);
82 fd = mingw_open(filename, flag);
83#endif
84 if (fd < 0) 77 if (fd < 0)
85 return fd; 78 return fd;
86 return read_close(fd, buf, size); 79 return read_close(fd, buf, size);
diff --git a/networking/tls.c b/networking/tls.c
index e34acd69f..b05afe1c9 100644
--- a/networking/tls.c
+++ b/networking/tls.c
@@ -349,7 +349,7 @@ static void dump_tls_record(const void *vp, int len)
349 349
350void FAST_FUNC tls_get_random(void *buf, unsigned len) 350void FAST_FUNC tls_get_random(void *buf, unsigned len)
351{ 351{
352 if (len != open_read_close("/dev/urandom", buf, len)) 352 if (len != MINGW_SPECIAL(open_read_close)("/dev/urandom", buf, len))
353 xfunc_die(); 353 xfunc_die();
354} 354}
355 355
diff --git a/win32/mingw.c b/win32/mingw.c
index ed0989be2..474d9cdc6 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -229,6 +229,15 @@ int mingw_xopen(const char *pathname, int flags)
229 return ret; 229 return ret;
230} 230}
231 231
232ssize_t FAST_FUNC mingw_open_read_close(const char *fn, void *buf, size_t size)
233{
234 /* allow use of special devices */
235 int fd = mingw_open(fn, O_RDONLY|O_SPECIAL);
236 if (fd < 0)
237 return fd;
238 return read_close(fd, buf, size);
239}
240
232#undef fopen 241#undef fopen
233FILE *mingw_fopen (const char *filename, const char *otype) 242FILE *mingw_fopen (const char *filename, const char *otype)
234{ 243{