Compare commits

..
10 Commits
Author SHA1 Message Date
Denys Vlasenko b03007455c httpd: fix handling of range requests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-09-18 14:08:04 +02:00
Denys Vlasenko ca26f1c238 hush: fix a bug with "stdio rewind on exit". Closes 9186
With FEATURE_SH_STANDALONE=y, run this in a "sh SCRIPT":

sha256sum /dev/null
echo END

sha256sum is a NOEXEC applet. It runs in a forked child. Then child exit()s.
By this time, entire script is read, and buffered in a FILE object
from fopen("SCRIPT"). But fgetc() did not consume entire input.
exit() lseeks back by -9 bytes, from <eof> to 'e' in 'echo'.
(this may be libc-specific).
This change of fd position *is shared with the parent*!

Now parent can read more, and it thinks there is another "echo END".
End result: two "echo END"s are run.

`cmd` and arithmetic also need the fix for this, even without
FEATURE_SH_STANDALONE.

Fix this by _exit()ing instead.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-19 20:24:49 +02:00
Mike Frysinger 8191abd146 ash_test: ignore generated files
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit b9b7aa1910)
2016-03-22 18:22:51 -04:00
Ron Yorston c1565a04f2 ash: simplify EOF/newline handling in list parser
Processing of here documents in ash has had a couple of breakages
which are now the subject of tests.  This commit should fix both.

It is based on the following commit in dash git by Herbert Xu:

   <7c245aa> [PARSER] Simplify EOF/newline handling in list parser

(See git://git.kernel.org/pub/scm/utils/dash/dash.git)

Reported-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit c0e007663d)
2016-03-22 18:21:45 -04:00
Ron Yorston acad7392ee Revert "ash: fix a SEGV case in an invalid heredoc" xxx
This reverts commit 7e66102f76 but
leaves the test in place as it's still valid.

Reported-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit 6bd2fabc52)
2016-03-22 18:21:28 -04:00
Ron Yorston 4c07ad9130 ash: add test for issue with here document
This used to work but doesn't now:

foo () {
cat <<EOF && { echo "$1" ; }
$1
EOF
}

foo "bar"

Reported-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit 9121510dc6)
2016-03-22 18:21:06 -04:00
Ron Yorston 856af4e3dc ash: fix corruption of ${#var} if $var contains UTF-8 characters
As reported in bug 8506:

   $ X=abcdÉfghÍjklmnÓpqrstÚvwcyz
   $ echo ${#X}
   abcd26

The result should be 26.

This regression was introduced by:

   <d68d1fb> 2015-05-18 [Ron Yorston]  ash: code shrink around varvalue

The length in characters was being used to discard the contents of
the variable instead of the length in bytes.

URL: https://bugs.busybox.net/8506
Reported-by: Martijn Dekker <martijn@inlv.org>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit 3e3bfb896e)
2016-03-22 18:20:04 -04:00
Ron Yorston 4194c28753 ash: fix error during recursive processing of here document
Save the value of the checkkwd flag to prevent it being clobbered
during recursion.

Based on commit ec2c84d from git://git.kernel.org/pub/scm/utils/dash/dash.git
by Herbert Xu.

function                                             old     new   delta
readtoken                                            190     203     +13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 13/0)               Total: 13 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit 713f07d906)
2016-03-21 14:28:06 -04:00
Denys Vlasenko 3a76bb5136 udhcp: fix a SEGV on malformed RFC1035-encoded domain name
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit d474ffc682)
2016-03-21 14:23:12 -04:00
Denys Vlasenko 3c4de6e36c udhcpc: fix OPTION_6RD parsing (could overflow its malloced buffer)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
(cherry picked from commit 352f79acbd)
2016-03-21 14:23:05 -04:00
15 changed files with 137 additions and 74 deletions

No files matched your search

+3 -1
View File
@@ -771,7 +771,9 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
}
if (ENABLE_FEATURE_SUID)
check_suid(applet_no);
exit(applet_main[applet_no](argc, argv));
xfunc_error_retval = applet_main[applet_no](argc, argv);
/* Note: applet_main() may also not return (die on a xfunc or such) */
xfunc_die();
}
void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
+1 -1
View File
@@ -2196,7 +2196,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
if (STRNCASECMP(iobuf, "Range:") == 0) {
/* We know only bytes=NNN-[MMM] */
char *s = skip_whitespace(iobuf + sizeof("Range:")-1);
if (is_prefixed_with(s, "bytes=") == 0) {
if (is_prefixed_with(s, "bytes=")) {
s += sizeof("bytes=")-1;
range_start = BB_STRTOOFF(s, &s, 10);
if (s[0] != '-' || range_start < 0) {
+13 -2
View File
@@ -142,7 +142,7 @@ const char dhcp_option_strings[] ALIGN1 =
* udhcp_str2optset: to determine how many bytes to allocate.
* xmalloc_optname_optval: to estimate string length
* from binary option length: (option[LEN] / dhcp_option_lengths[opt_type])
* is the number of elements, multiply in by one element's string width
* is the number of elements, multiply it by one element's string width
* (len_of_option_as_string[opt_type]) and you know how wide string you need.
*/
const uint8_t dhcp_option_lengths[] ALIGN1 = {
@@ -162,7 +162,18 @@ const uint8_t dhcp_option_lengths[] ALIGN1 = {
[OPTION_S32] = 4,
/* Just like OPTION_STRING, we use minimum length here */
[OPTION_STATIC_ROUTES] = 5,
[OPTION_6RD] = 22, /* ignored by udhcp_str2optset */
[OPTION_6RD] = 12, /* ignored by udhcp_str2optset */
/* The above value was chosen as follows:
* len_of_option_as_string[] for this option is >60: it's a string of the form
* "32 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 ".
* Each additional ipv4 address takes 4 bytes in binary option and appends
* another "255.255.255.255 " 16-byte string. We can set [OPTION_6RD] = 4
* but this severely overestimates string length: instead of 16 bytes,
* it adds >60 for every 4 bytes in binary option.
* We cheat and declare here that option is in units of 12 bytes.
* This adds more than 60 bytes for every three ipv4 addresses - more than enough.
* (Even 16 instead of 12 should work, but let's be paranoid).
*/
};
+2 -2
View File
@@ -113,7 +113,7 @@ static const uint8_t len_of_option_as_string[] = {
[OPTION_IP ] = sizeof("255.255.255.255 "),
[OPTION_IP_PAIR ] = sizeof("255.255.255.255 ") * 2,
[OPTION_STATIC_ROUTES ] = sizeof("255.255.255.255/32 255.255.255.255 "),
[OPTION_6RD ] = sizeof("32 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 "),
[OPTION_6RD ] = sizeof("132 128 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 255.255.255.255 "),
[OPTION_STRING ] = 1,
[OPTION_STRING_HOST ] = 1,
#if ENABLE_FEATURE_UDHCP_RFC3397
@@ -220,7 +220,7 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_
type = optflag->flags & OPTION_TYPE_MASK;
optlen = dhcp_option_lengths[type];
upper_length = len_of_option_as_string[type]
* ((unsigned)(len + optlen - 1) / (unsigned)optlen);
* ((unsigned)(len + optlen) / (unsigned)optlen);
dest = ret = xmalloc(upper_length + strlen(opt_name) + 2);
dest += sprintf(ret, "%s=", opt_name);
+9 -4
View File
@@ -63,11 +63,10 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
if (crtpos + *c + 1 > clen) /* label too long? abort */
return NULL;
if (dst)
memcpy(dst + len, c + 1, *c);
/* \3com ---> "com." */
((char*)mempcpy(dst + len, c + 1, *c))[0] = '.';
len += *c + 1;
crtpos += *c + 1;
if (dst)
dst[len - 1] = '.';
} else {
/* NUL: end of current domain name */
if (retpos == 0) {
@@ -78,7 +77,10 @@ char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
crtpos = retpos;
retpos = depth = 0;
}
if (dst)
if (dst && len != 0)
/* \4host\3com\0\4host and we are at \0:
* \3com was converted to "com.", change dot to space.
*/
dst[len - 1] = ' ';
}
@@ -228,6 +230,9 @@ int main(int argc, char **argv)
int len;
uint8_t *encoded;
uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 };
printf("NUL:'%s'\n", dname_dec(str, 6, ""));
#define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:"));
printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
+35 -44
View File
@@ -6693,6 +6693,8 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
if (subtype == VSLENGTH && len > 0) {
reinit_unicode_for_ash();
if (unicode_status == UNICODE_ON) {
STADJUST(-len, expdest);
discard = 0;
len = unicode_strlen(p);
}
}
@@ -10516,7 +10518,7 @@ static union node *andor(void);
static union node *pipeline(void);
static union node *parse_command(void);
static void parseheredoc(void);
static char nexttoken_ends_list(void);
static int peektoken(void);
static int readtoken(void);
static union node *
@@ -10525,11 +10527,27 @@ list(int nlflag)
union node *n1, *n2, *n3;
int tok;
checkkwd = CHKNL | CHKKWD | CHKALIAS;
if (nlflag == 2 && nexttoken_ends_list())
return NULL;
n1 = NULL;
for (;;) {
switch (peektoken()) {
case TNL:
if (!(nlflag & 1))
break;
parseheredoc();
return n1;
case TEOF:
if (!n1 && (nlflag & 1))
n1 = NODE_EOF;
parseheredoc();
return n1;
}
checkkwd = CHKNL | CHKKWD | CHKALIAS;
if (nlflag == 2 && tokname_array[peektoken()][0])
return n1;
nlflag |= 2;
n2 = andor();
tok = readtoken();
if (tok == TBACKGND) {
@@ -10555,37 +10573,15 @@ list(int nlflag)
n1 = n3;
}
switch (tok) {
case TNL:
case TEOF:
tokpushback = 1;
/* fall through */
case TBACKGND:
case TSEMI:
tok = readtoken();
/* fall through */
case TNL:
if (tok == TNL) {
parseheredoc();
if (nlflag == 1)
return n1;
} else {
tokpushback = 1;
}
checkkwd = CHKNL | CHKKWD | CHKALIAS;
if (nexttoken_ends_list()) {
/* Testcase: "<<EOF; then <W".
* It used to segfault w/o this check:
*/
if (heredoclist) {
raise_error_unexpected_syntax(-1);
}
return n1;
}
break;
case TEOF:
if (heredoclist)
parseheredoc();
else
pungetc(); /* push back EOF on input */
return n1;
default:
if (nlflag == 1)
if ((nlflag & 1))
raise_error_unexpected_syntax(-1);
tokpushback = 1;
return n1;
@@ -11893,6 +11889,7 @@ static int
readtoken(void)
{
int t;
int kwd = checkkwd;
#if DEBUG
smallint alreadyseen = tokpushback;
#endif
@@ -11906,7 +11903,7 @@ readtoken(void)
/*
* eat newlines
*/
if (checkkwd & CHKNL) {
if (kwd & CHKNL) {
while (t == TNL) {
parseheredoc();
t = xxreadtoken();
@@ -11920,7 +11917,7 @@ readtoken(void)
/*
* check for keywords
*/
if (checkkwd & CHKKWD) {
if (kwd & CHKKWD) {
const char *const *pp;
pp = findkwd(wordtext);
@@ -11954,14 +11951,14 @@ readtoken(void)
return t;
}
static char
nexttoken_ends_list(void)
static int
peektoken(void)
{
int t;
t = readtoken();
tokpushback = 1;
return tokname_array[t][0];
return t;
}
/*
@@ -11971,18 +11968,12 @@ nexttoken_ends_list(void)
static union node *
parsecmd(int interact)
{
int t;
tokpushback = 0;
checkkwd = 0;
heredoclist = 0;
doprompt = interact;
setprompt_if(doprompt, doprompt);
needprompt = 0;
t = readtoken();
if (t == TEOF)
return NODE_EOF;
if (t == TNL)
return NULL;
tokpushback = 1;
return list(1);
}
+7
View File
@@ -0,0 +1,7 @@
/ash
/printenv
/recho
/zecho
/*.fail
*.xx
@@ -0,0 +1,2 @@
bar
bar
+7
View File
@@ -0,0 +1,7 @@
foo () {
cat <<EOF && { echo "$1" ; }
$1
EOF
}
foo "bar"
@@ -0,0 +1 @@
hello
+9
View File
@@ -0,0 +1,9 @@
echo hello >greeting
cat <<EOF &&
$(cat greeting)
EOF
{
echo $?
cat greeting
} >/dev/null
rm greeting
@@ -0,0 +1 @@
26
+2
View File
@@ -0,0 +1,2 @@
X=abcdÉfghÍjklmnÓpqrstÚvwcyz
echo ${#X}
+43 -19
View File
@@ -1477,19 +1477,50 @@ static sighandler_t install_sighandler(int sig, sighandler_t handler)
return old_sa.sa_handler;
}
static void hush_exit(int exitcode) NORETURN;
static void fflush_and__exit(void) NORETURN;
static void restore_ttypgrp_and__exit(void) NORETURN;
static void restore_ttypgrp_and__exit(void)
{
/* xfunc has failed! die die die */
/* no EXIT traps, this is an escape hatch! */
G.exiting = 1;
hush_exit(xfunc_error_retval);
}
/* Needed only on some libc:
* It was observed that on exit(), fgetc'ed buffered data
* gets "unwound" via lseek(fd, -NUM, SEEK_CUR).
* With the net effect that even after fork(), not vfork(),
* exit() in NOEXECed applet in "sh SCRIPT":
* noexec_applet_here
* echo END_OF_SCRIPT
* lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT".
* This makes "echo END_OF_SCRIPT" executed twice.
* Similar problems can be seen with die_if_script() -> xfunc_die()
* and in `cmd` handling.
* If set as die_func(), this makes xfunc_die() exit via _exit(), not exit():
*/
static void fflush_and__exit(void)
{
fflush_all();
_exit(xfunc_error_retval);
}
#if ENABLE_HUSH_JOB
static void xfunc_has_died(void);
/* After [v]fork, in child: do not restore tty pgrp on xfunc death */
# define disable_restore_tty_pgrp_on_exit() (die_func = NULL)
# define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit)
/* After [v]fork, in parent: restore tty pgrp on xfunc death */
# define enable_restore_tty_pgrp_on_exit() (die_func = xfunc_has_died)
# define enable_restore_tty_pgrp_on_exit() (die_func = restore_ttypgrp_and__exit)
/* Restores tty foreground process group, and exits.
* May be called as signal handler for fatal signal
* (will resend signal to itself, producing correct exit state)
* or called directly with -EXITCODE.
* We also call it if xfunc is exiting. */
* We also call it if xfunc is exiting.
*/
static void sigexit(int sig) NORETURN;
static void sigexit(int sig)
{
@@ -1544,7 +1575,6 @@ static sighandler_t pick_sighandler(unsigned sig)
}
/* Restores tty foreground process group, and exits. */
static void hush_exit(int exitcode) NORETURN;
static void hush_exit(int exitcode)
{
#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
@@ -1580,23 +1610,14 @@ static void hush_exit(int exitcode)
}
#endif
#if ENABLE_HUSH_JOB
fflush_all();
#if ENABLE_HUSH_JOB
sigexit(- (exitcode & 0xff));
#else
exit(exitcode);
_exit(exitcode);
#endif
}
static void xfunc_has_died(void) NORETURN;
static void xfunc_has_died(void)
{
/* xfunc has failed! die die die */
/* no EXIT traps, this is an escape hatch! */
G.exiting = 1;
hush_exit(xfunc_error_retval);
}
//TODO: return a mask of ALL handled sigs?
static int check_and_run_traps(void)
@@ -5913,7 +5934,8 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
) {
static const char *const argv[] = { NULL, NULL };
builtin_trap((char**)argv);
exit(0); /* not _exit() - we need to fflush */
fflush_all(); /* important */
_exit(0);
}
# if BB_MMU
reset_traps_to_defaults();
@@ -6466,7 +6488,8 @@ static void dump_cmd_in_x_mode(char **argv)
* Never returns.
* Don't exit() here. If you don't exec, use _exit instead.
* The at_exit handlers apparently confuse the calling process,
* in particular stdin handling. Not sure why? -- because of vfork! (vda) */
* in particular stdin handling. Not sure why? -- because of vfork! (vda)
*/
static void pseudo_exec_argv(nommu_save_t *nommu_save,
char **argv, int assignment_cnt,
char **argv_expanded) NORETURN;
@@ -7787,6 +7810,7 @@ int hush_main(int argc, char **argv)
INIT_G();
if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */
G.last_exitcode = EXIT_SUCCESS;
#if ENABLE_HUSH_FAST
G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */
#endif
@@ -7876,7 +7900,7 @@ int hush_main(int argc, char **argv)
/* Initialize some more globals to non-zero values */
cmdedit_update_prompt();
die_func = xfunc_has_died;
die_func = restore_ttypgrp_and__exit;
/* Shell is non-interactive at first. We need to call
* install_special_sighandlers() if we are going to execute "sh <script>",
+2 -1
View File
@@ -64,11 +64,12 @@ do_test()
echo -n "$1/$x:"
(
"$THIS_SH" "./$x" >"$name.xx" 2>&1
r=$?
# filter C library differences
sed -i \
-e "/: invalid option /s:'::g" \
"$name.xx"
test $? -eq 77 && rm -f "../$1-$x.fail" && exit 77
test $r -eq 77 && rm -f "../$1-$x.fail" && exit 77
diff -u "$name.xx" "$name.right" >"../$1-$x.fail" && rm -f "$name.xx" "../$1-$x.fail"
)
case $? in