diff options
-rw-r--r-- | coreutils/chroot.c | 4 | ||||
-rw-r--r-- | include/libbb.h | 5 | ||||
-rw-r--r-- | include/usage.h | 2 | ||||
-rw-r--r-- | libbb/full_write.c | 8 | ||||
-rw-r--r-- | libbb/isdirectory.c | 11 | ||||
-rw-r--r-- | libbb/read.c | 10 | ||||
-rw-r--r-- | libbb/xfuncs.c | 6 | ||||
-rw-r--r-- | printutils/lpd.c | 148 | ||||
-rw-r--r-- | printutils/lpr.c | 25 | ||||
-rw-r--r-- | runit/chpst.c | 3 | ||||
-rw-r--r-- | util-linux/switch_root.c | 3 |
11 files changed, 143 insertions, 82 deletions
diff --git a/coreutils/chroot.c b/coreutils/chroot.c index a3e70e925..1198a415a 100644 --- a/coreutils/chroot.c +++ b/coreutils/chroot.c | |||
@@ -19,9 +19,7 @@ int chroot_main(int argc, char **argv) | |||
19 | } | 19 | } |
20 | 20 | ||
21 | ++argv; | 21 | ++argv; |
22 | if (chroot(*argv)) { | 22 | xchroot(*argv); |
23 | bb_perror_msg_and_die("cannot change root directory to %s", *argv); | ||
24 | } | ||
25 | xchdir("/"); | 23 | xchdir("/"); |
26 | 24 | ||
27 | ++argv; | 25 | ++argv; |
diff --git a/include/libbb.h b/include/libbb.h index 48937c4b1..1ea2e35be 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -315,6 +315,7 @@ void kill_myself_with_sig(int sig) ATTRIBUTE_NORETURN; | |||
315 | void xsetgid(gid_t gid); | 315 | void xsetgid(gid_t gid); |
316 | void xsetuid(uid_t uid); | 316 | void xsetuid(uid_t uid); |
317 | void xchdir(const char *path); | 317 | void xchdir(const char *path); |
318 | void xchroot(const char *path); | ||
318 | void xsetenv(const char *key, const char *value); | 319 | void xsetenv(const char *key, const char *value); |
319 | void xunlink(const char *pathname); | 320 | void xunlink(const char *pathname); |
320 | void xstat(const char *pathname, struct stat *buf); | 321 | void xstat(const char *pathname, struct stat *buf); |
@@ -500,6 +501,8 @@ extern void *xrealloc(void *old, size_t size); | |||
500 | 501 | ||
501 | extern ssize_t safe_read(int fd, void *buf, size_t count); | 502 | extern ssize_t safe_read(int fd, void *buf, size_t count); |
502 | extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count); | 503 | extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count); |
504 | // NB: will return short read on error, not -1, | ||
505 | // if some data was read before error occurred | ||
503 | extern ssize_t full_read(int fd, void *buf, size_t count); | 506 | extern ssize_t full_read(int fd, void *buf, size_t count); |
504 | extern void xread(int fd, void *buf, size_t count); | 507 | extern void xread(int fd, void *buf, size_t count); |
505 | extern unsigned char xread_char(int fd); | 508 | extern unsigned char xread_char(int fd); |
@@ -514,6 +517,8 @@ extern ssize_t open_read_close(const char *filename, void *buf, size_t count); | |||
514 | extern void *xmalloc_open_read_close(const char *filename, size_t *sizep); | 517 | extern void *xmalloc_open_read_close(const char *filename, size_t *sizep); |
515 | 518 | ||
516 | extern ssize_t safe_write(int fd, const void *buf, size_t count); | 519 | extern ssize_t safe_write(int fd, const void *buf, size_t count); |
520 | // NB: will return short write on error, not -1, | ||
521 | // if some data was written before error occurred | ||
517 | extern ssize_t full_write(int fd, const void *buf, size_t count); | 522 | extern ssize_t full_write(int fd, const void *buf, size_t count); |
518 | extern void xwrite(int fd, const void *buf, size_t count); | 523 | extern void xwrite(int fd, const void *buf, size_t count); |
519 | 524 | ||
diff --git a/include/usage.h b/include/usage.h index 7c9a90e77..359f88d27 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -2055,7 +2055,7 @@ USE_FEATURE_BRCTL_FANCY("\n" \ | |||
2055 | "SPOOLDIR" | 2055 | "SPOOLDIR" |
2056 | #define lpd_full_usage \ | 2056 | #define lpd_full_usage \ |
2057 | "Example:" \ | 2057 | "Example:" \ |
2058 | "\n tcpsvd -E localhost 515 lpd /var/spool" | 2058 | "\n tcpsvd -E 0 515 softlimit -m 99999 lpd /var/spool" |
2059 | 2059 | ||
2060 | #define lpq_trivial_usage \ | 2060 | #define lpq_trivial_usage \ |
2061 | "[-P queue[@host[:port]]] [-U USERNAME] [-d JOBID...] [-fs]" | 2061 | "[-P queue[@host[:port]]] [-U USERNAME] [-d JOBID...] [-fs]" |
diff --git a/libbb/full_write.c b/libbb/full_write.c index 7bbacb8ac..7503c8b42 100644 --- a/libbb/full_write.c +++ b/libbb/full_write.c | |||
@@ -24,8 +24,14 @@ ssize_t full_write(int fd, const void *buf, size_t len) | |||
24 | while (len) { | 24 | while (len) { |
25 | cc = safe_write(fd, buf, len); | 25 | cc = safe_write(fd, buf, len); |
26 | 26 | ||
27 | if (cc < 0) | 27 | if (cc < 0) { |
28 | if (total) { | ||
29 | /* we already wrote some! */ | ||
30 | /* user can do another write to know the error code */ | ||
31 | return total; | ||
32 | } | ||
28 | return cc; /* write() returns -1 on failure. */ | 33 | return cc; /* write() returns -1 on failure. */ |
34 | } | ||
29 | 35 | ||
30 | total += cc; | 36 | total += cc; |
31 | buf = ((const char *)buf) + cc; | 37 | buf = ((const char *)buf) + cc; |
diff --git a/libbb/isdirectory.c b/libbb/isdirectory.c index b35919869..1d2477f47 100644 --- a/libbb/isdirectory.c +++ b/libbb/isdirectory.c | |||
@@ -12,7 +12,7 @@ | |||
12 | #include "libbb.h" | 12 | #include "libbb.h" |
13 | 13 | ||
14 | /* | 14 | /* |
15 | * Return TRUE if a fileName is a directory. | 15 | * Return TRUE if fileName is a directory. |
16 | * Nonexistent files return FALSE. | 16 | * Nonexistent files return FALSE. |
17 | */ | 17 | */ |
18 | int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) | 18 | int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) |
@@ -21,8 +21,8 @@ int is_directory(const char *fileName, const int followLinks, struct stat *statB | |||
21 | struct stat astatBuf; | 21 | struct stat astatBuf; |
22 | 22 | ||
23 | if (statBuf == NULL) { | 23 | if (statBuf == NULL) { |
24 | /* set from auto stack buffer */ | 24 | /* use auto stack buffer */ |
25 | statBuf = &astatBuf; | 25 | statBuf = &astatBuf; |
26 | } | 26 | } |
27 | 27 | ||
28 | if (followLinks) | 28 | if (followLinks) |
@@ -30,10 +30,7 @@ int is_directory(const char *fileName, const int followLinks, struct stat *statB | |||
30 | else | 30 | else |
31 | status = lstat(fileName, statBuf); | 31 | status = lstat(fileName, statBuf); |
32 | 32 | ||
33 | if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { | 33 | status = (status == 0 && S_ISDIR(statBuf->st_mode)); |
34 | status = FALSE; | ||
35 | } | ||
36 | else status = TRUE; | ||
37 | 34 | ||
38 | return status; | 35 | return status; |
39 | } | 36 | } |
diff --git a/libbb/read.c b/libbb/read.c index 4ad41d589..575446536 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
@@ -88,8 +88,14 @@ ssize_t full_read(int fd, void *buf, size_t len) | |||
88 | while (len) { | 88 | while (len) { |
89 | cc = safe_read(fd, buf, len); | 89 | cc = safe_read(fd, buf, len); |
90 | 90 | ||
91 | if (cc < 0) | 91 | if (cc < 0) { |
92 | return cc; /* read() returns -1 on failure. */ | 92 | if (total) { |
93 | /* we already have some! */ | ||
94 | /* user can do another read to know the error code */ | ||
95 | return total; | ||
96 | } | ||
97 | return cc; /* read() returns -1 on failure. */ | ||
98 | } | ||
93 | if (cc == 0) | 99 | if (cc == 0) |
94 | break; | 100 | break; |
95 | buf = ((char *)buf) + cc; | 101 | buf = ((char *)buf) + cc; |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 17760a3c3..18e696a7a 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -577,6 +577,12 @@ void xchdir(const char *path) | |||
577 | bb_perror_msg_and_die("chdir(%s)", path); | 577 | bb_perror_msg_and_die("chdir(%s)", path); |
578 | } | 578 | } |
579 | 579 | ||
580 | void xchroot(const char *path) | ||
581 | { | ||
582 | if (chroot(path)) | ||
583 | bb_perror_msg_and_die("can't change root directory to %s", path); | ||
584 | } | ||
585 | |||
580 | // Print a warning message if opendir() fails, but don't die. | 586 | // Print a warning message if opendir() fails, but don't die. |
581 | DIR *warn_opendir(const char *path) | 587 | DIR *warn_opendir(const char *path) |
582 | { | 588 | { |
diff --git a/printutils/lpd.c b/printutils/lpd.c index ed3d9d100..916fd6213 100644 --- a/printutils/lpd.c +++ b/printutils/lpd.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * micro lpd - a small non-queueing lpd | 3 | * micro lpd |
4 | * | 4 | * |
5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> | 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
6 | * | 6 | * |
@@ -8,72 +8,108 @@ | |||
8 | */ | 8 | */ |
9 | #include "libbb.h" | 9 | #include "libbb.h" |
10 | 10 | ||
11 | // TODO: xmalloc_reads is vulnerable to remote OOM attack! | ||
12 | |||
11 | int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; | 13 | int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; |
12 | int lpd_main(int argc, char *argv[]) | 14 | int lpd_main(int argc, char *argv[]) |
13 | { | 15 | { |
14 | char *s; | 16 | int spooling; |
17 | char *s, *queue; | ||
18 | |||
19 | // read command | ||
20 | s = xmalloc_reads(STDIN_FILENO, NULL); | ||
21 | |||
22 | // we understand only "receive job" command | ||
23 | if (2 != *s) { | ||
24 | unsupported_cmd: | ||
25 | printf("Command %02x %s\n", | ||
26 | (unsigned char)s[0], "is not supported"); | ||
27 | return EXIT_FAILURE; | ||
28 | } | ||
15 | 29 | ||
16 | // goto spool directory | ||
17 | // spool directory contains either links to real printer devices or just simple files | 30 | // spool directory contains either links to real printer devices or just simple files |
18 | // these links or files are called "queues" | 31 | // these links or files are called "queues" |
19 | if (!argv[1]) | 32 | // OR |
20 | bb_show_usage(); | 33 | // if a directory named as given queue exists within spool directory |
34 | // then LPD enters spooling mode and just dumps both control and data files to it | ||
21 | 35 | ||
22 | xchdir(argv[1]); | 36 | // goto spool directory |
37 | if (argv[1]) | ||
38 | xchdir(argv[1]); | ||
23 | 39 | ||
24 | xdup2(1, 2); | 40 | // parse command: "\x2QUEUE_NAME\n" |
41 | queue = s + 1; | ||
42 | *strchrnul(s, '\n') = '\0'; | ||
25 | 43 | ||
26 | // read command | 44 | // protect against "/../" attacks |
27 | s = xmalloc_reads(STDIN_FILENO, NULL); | 45 | if (queue[0] == '.' || strstr(queue, "/.")) |
46 | return EXIT_FAILURE; | ||
28 | 47 | ||
29 | // N.B. we keep things simple | 48 | // queue is a directory -> chdir to it and enter spooling mode |
30 | // only "receive job" command is meaningful here... | 49 | spooling = chdir(queue) + 1; /* 0: cannot chdir, 1: done */ |
31 | if (2 == *s) { | ||
32 | char *queue; | ||
33 | 50 | ||
34 | // parse command: "\x2QUEUE_NAME\n" | 51 | xdup2(STDOUT_FILENO, STDERR_FILENO); |
35 | queue = s + 1; | ||
36 | *strchrnul(s, '\n') = '\0'; | ||
37 | 52 | ||
38 | while (1) { | 53 | while (1) { |
39 | // signal OK | 54 | char *fname; |
40 | write(STDOUT_FILENO, "", 1); | 55 | int fd; |
41 | // get subcommand | 56 | // int is easier than ssize_t: can use xatoi_u, |
42 | s = xmalloc_reads(STDIN_FILENO, NULL); | 57 | // and can correctly display error returns (-1) |
43 | if (!s) | 58 | int expected_len, real_len; |
44 | return EXIT_SUCCESS; // EOF (probably) | 59 | |
45 | // valid s must be of form: SUBCMD | LEN | SP | FNAME | 60 | // signal OK |
46 | // N.B. we bail out on any error | 61 | write(STDOUT_FILENO, "", 1); |
47 | // control or data file follows | 62 | |
48 | if (2 == s[0] || 3 == s[0]) { | 63 | // get subcommand |
49 | int fd; | 64 | s = xmalloc_reads(STDIN_FILENO, NULL); |
50 | size_t len; | 65 | if (!s) |
51 | // 2: control file (ignoring), 3: data file | 66 | return EXIT_SUCCESS; // probably EOF |
52 | fd = -1; | 67 | // we understand only "control file" or "data file" cmds |
53 | if (3 == s[0]) | 68 | if (2 != s[0] && 3 != s[0]) |
54 | fd = xopen(queue, O_RDWR | O_APPEND); | 69 | goto unsupported_cmd; |
55 | // get data length | ||
56 | *strchrnul(s, ' ') = '\0'; | ||
57 | len = xatou(s + 1); | ||
58 | // dump exactly len bytes to file, or die | ||
59 | bb_copyfd_exact_size(STDIN_FILENO, fd, len); | ||
60 | close(fd); // NB: can do close(-1). Who cares? | ||
61 | free(s); | ||
62 | // got no ACK? -> bail out | ||
63 | if (safe_read(STDIN_FILENO, s, 1) <= 0 || s[0]) { | ||
64 | // don't talk to peer - it obviously | ||
65 | // don't follow the protocol | ||
66 | return EXIT_FAILURE; | ||
67 | } | ||
68 | } else { | ||
69 | // any other subcommand aborts receiving job | ||
70 | // N.B. abort subcommand itself doesn't contain | ||
71 | // fname so it failed earlier... | ||
72 | break; | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | 70 | ||
77 | printf("Command %02x not supported\n", (unsigned char)*s); | 71 | *strchrnul(s, '\n') = '\0'; |
78 | return EXIT_FAILURE; | 72 | // valid s must be of form: SUBCMD | LEN | SP | FNAME |
73 | // N.B. we bail out on any error | ||
74 | fname = strchr(s, ' '); | ||
75 | if (!fname) { | ||
76 | printf("Command %02x %s\n", | ||
77 | (unsigned char)s[0], "lacks filename"); | ||
78 | return EXIT_FAILURE; | ||
79 | } | ||
80 | *fname++ = '\0'; | ||
81 | if (spooling) { | ||
82 | // spooling mode: dump both files | ||
83 | // make "/../" attacks in file names ineffective | ||
84 | xchroot("."); | ||
85 | // job in flight has mode 0200 "only writable" | ||
86 | fd = xopen3(fname, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0200); | ||
87 | } else { | ||
88 | // non-spooling mode: | ||
89 | // 2: control file (ignoring), 3: data file | ||
90 | fd = -1; | ||
91 | if (3 == s[0]) | ||
92 | fd = xopen(queue, O_RDWR | O_APPEND); | ||
93 | } | ||
94 | expected_len = xatoi_u(s + 1); | ||
95 | real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len); | ||
96 | if (spooling && real_len != expected_len) { | ||
97 | unlink(fname); // don't keep corrupted files | ||
98 | printf("Expected %d but got %d bytes\n", | ||
99 | expected_len, real_len); | ||
100 | return EXIT_FAILURE; | ||
101 | } | ||
102 | // get ACK and see whether it is NUL (ok) | ||
103 | if (read(STDIN_FILENO, s, 1) != 1 || s[0] != 0) { | ||
104 | // don't send error msg to peer - it obviously | ||
105 | // don't follow the protocol, so probably | ||
106 | // it can't understand us either | ||
107 | return EXIT_FAILURE; | ||
108 | } | ||
109 | // chmod completely downloaded job as "readable+writable" | ||
110 | if (spooling) | ||
111 | fchmod(fd, 0600); | ||
112 | close(fd); // NB: can do close(-1). Who cares? | ||
113 | free(s); | ||
114 | } /* while (1) */ | ||
79 | } | 115 | } |
diff --git a/printutils/lpr.c b/printutils/lpr.c index 52983bfb7..09fbc6ad1 100644 --- a/printutils/lpr.c +++ b/printutils/lpr.c | |||
@@ -19,19 +19,26 @@ | |||
19 | */ | 19 | */ |
20 | static void get_response_or_say_and_die(const char *errmsg) | 20 | static void get_response_or_say_and_die(const char *errmsg) |
21 | { | 21 | { |
22 | static const char newline = '\n'; | 22 | ssize_t sz; |
23 | char buf = ' '; | 23 | char buf[128]; |
24 | 24 | ||
25 | fflush(stdout); | 25 | fflush(stdout); |
26 | 26 | ||
27 | safe_read(STDOUT_FILENO, &buf, 1); | 27 | buf[0] = ' '; |
28 | if ('\0' != buf) { | 28 | sz = safe_read(STDOUT_FILENO, buf, 1); |
29 | if ('\0' != buf[0]) { | ||
29 | // request has failed | 30 | // request has failed |
30 | bb_error_msg("error while %s. Server said:", errmsg); | 31 | // try to make sure last char is '\n', but do not add |
31 | safe_write(STDERR_FILENO, &buf, 1); | 32 | // superfluous one |
32 | logmode = 0; /* no error messages from bb_copyfd_eof() pls */ | 33 | sz = full_read(STDOUT_FILENO, buf + 1, 126); |
33 | bb_copyfd_eof(STDOUT_FILENO, STDERR_FILENO); | 34 | bb_error_msg("error while %s%s", errmsg, |
34 | safe_write(STDERR_FILENO, &newline, 1); | 35 | (sz > 0 ? ". Server said:" : "")); |
36 | if (sz > 0) { | ||
37 | // sz = (bytes in buf) - 1 | ||
38 | if (buf[sz] != '\n') | ||
39 | buf[++sz] = '\n'; | ||
40 | safe_write(STDERR_FILENO, buf, sz + 1); | ||
41 | } | ||
35 | xfunc_die(); | 42 | xfunc_die(); |
36 | } | 43 | } |
37 | } | 44 | } |
diff --git a/runit/chpst.c b/runit/chpst.c index 0f605cc45..7b70a4d5a 100644 --- a/runit/chpst.c +++ b/runit/chpst.c | |||
@@ -332,8 +332,7 @@ int chpst_main(int argc, char **argv) | |||
332 | if (env_dir) edir(env_dir); | 332 | if (env_dir) edir(env_dir); |
333 | if (root) { | 333 | if (root) { |
334 | xchdir(root); | 334 | xchdir(root); |
335 | if (chroot(".") == -1) | 335 | xchroot("."); |
336 | bb_perror_msg_and_die("chroot"); | ||
337 | } | 336 | } |
338 | slimit(); | 337 | slimit(); |
339 | if (nicelvl) { | 338 | if (nicelvl) { |
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c index bd1e9d5ce..3a1741179 100644 --- a/util-linux/switch_root.c +++ b/util-linux/switch_root.c | |||
@@ -105,8 +105,9 @@ int switch_root_main(int argc, char **argv) | |||
105 | // Overmount / with newdir and chroot into it. The chdir is needed to | 105 | // Overmount / with newdir and chroot into it. The chdir is needed to |
106 | // recalculate "." and ".." links. | 106 | // recalculate "." and ".." links. |
107 | 107 | ||
108 | if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".")) | 108 | if (mount(".", "/", NULL, MS_MOVE, NULL)) |
109 | bb_error_msg_and_die("error moving root"); | 109 | bb_error_msg_and_die("error moving root"); |
110 | xchroot("."); | ||
110 | xchdir("/"); | 111 | xchdir("/"); |
111 | 112 | ||
112 | // If a new console specified, redirect stdin/stdout/stderr to that. | 113 | // If a new console specified, redirect stdin/stdout/stderr to that. |