Compare commits

...
11 Commits
Author SHA1 Message Date
Samuel Sapalski dbd3b883a8 decompress_gunzip: Fix DoS if gzip is corrupt
On certain corrupt gzip files, huft_build will set the error bit on
the result pointer. If afterwards abort_unzip is called huft_free
might run into a segmentation fault or an invalid pointer to
free(p).

In order to mitigate this, we check in huft_free if the error bit
is set and clear it before the linked list is freed.

Signed-off-by: Samuel Sapalski <samuel.sapalski@nokia.com>
Signed-off-by: Peter Kaestle <peter.kaestle@nokia.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-03-31 15:00:27 +02:00
Denys Vlasenko 30ffa2bb15 ash: fix ${unset_var/pattern/repl}
function                                             old     new   delta
subevalvar                                          1349    1353      +4

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-03-30 15:17:12 +02:00
Denys Vlasenko d811a8cd01 Bump version to 1.32.1
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:43:57 +01:00
Denys Vlasenko 71afe6c513 hush: output bash-compat killing signal names
This significantly syncronises ash-signals and hush-signals tests.

function                                             old     new   delta
process_wait_result                                  449     450      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko 0f668aad7f hush: do not print killing signal name in cmd_whihc_dies_on_signal
function                                             old     new   delta
process_wait_result                                  438     449     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko 06f7cf1487 shell: remove FAST_FUNC from a static function
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko 58cbf385e0 shell: add testsuite for "wait $pid" waiting for other tasks
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko e7629ffbf5 ash: jobs: Fix waitcmd busy loop
Upstream commit:

    Date: Tue, 2 Jun 2020 23:46:48 +1000
    jobs: Fix waitcmd busy loop

    We need to clear gotsigchld in waitproc because it is used as
    a loop conditional for the waitcmd case.  Without it waitcmd
    may busy loop after a SIGCHLD.

    This patch also changes gotsigchld into a volatile sig_atomic_t
    to prevent compilers from optimising its accesses away.

    Fixes: 6c691b3e5099 ("jobs: Only clear gotsigchld when waiting...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

This change also incorporates other changes to bring us closer to upstream.

function                                             old     new   delta
dowait                                               553     636     +83

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko e4c7cf3f8c ash: jobs: Fix infinite loop in waitproc
Upstream commit:

    Date: Fri, 10 Apr 2020 21:03:09 +1000
    jobs: Fix infinite loop in waitproc

    After we changed the resetting of gotsigchld so that it is only
    done if jp is NULL, we can now get an infinite loop in waitproc
    if gotsigchld is set but there is no outstanding child because
    everything had been waited for previously without gotsigchld being
    zeroed.

    This patch fixes it by always zeroing gotsigchld as we did before.
    The bug that the previous patch was trying to fix is now resolved
    by switching the blocking mode to DOWAIT_NORMAL after the specified
    job has been completed so that we really do wait for all outstanding
    dead children.

    Reported-by: Harald van Dijk <harald@gigawatt.nl>
    Fixes: 6c691b3e5099 ("jobs: Only clear gotsigchld when waiting...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
dowait                                               553     631     +78

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Denys Vlasenko 4ac4f60942 ash: remove a tentative TODO, it's a wrong idea
Upstream tried it, and then reverted by:

    From: Herbert Xu <herbert@gondor.apana.org.au>
    Date: Tue, 26 May 2020 23:19:05 +1000
    parser: Fix double-backslash nl in old-style command sub

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
Christian Eggers 94f6d1ece0 shell: Fix "read -d ''" behavior
With bash's read builtin it is possible to read from a file (e.g.
device-tree) until the first '\0' character:

IFS= read -r -d '' VARIABLE < file

In busybox ash the -d extension is also implemented, but checking the
read character for '\0' has to be performed after comparing with the
delimiter.

Signed-off-by: Christian Eggers <ceggers@arri.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-01-01 14:30:58 +01:00
26 changed files with 201 additions and 84 deletions

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
VERSION = 1
PATCHLEVEL = 32
SUBLEVEL = 0
SUBLEVEL = 1
EXTRAVERSION =
NAME = Unnamed
+10 -2
View File
@@ -220,10 +220,20 @@ static const uint8_t border[] ALIGN1 = {
* each table.
* t: table to free
*/
#define BAD_HUFT(p) ((uintptr_t)(p) & 1)
#define ERR_RET ((huft_t*)(uintptr_t)1)
static void huft_free(huft_t *p)
{
huft_t *q;
/*
* If 'p' has the error bit set we have to clear it, otherwise we might run
* into a segmentation fault or an invalid pointer to free(p)
*/
if (BAD_HUFT(p)) {
p = (huft_t*)((uintptr_t)(p) ^ (uintptr_t)(ERR_RET));
}
/* Go through linked list, freeing from the malloced (t[-1]) address. */
while (p) {
q = (--p)->v.t;
@@ -289,8 +299,6 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current
* or a valid pointer to a Huffman table, ORed with 0x1 if incompete table
* is given: "fixed inflate" decoder feeds us such data.
*/
#define BAD_HUFT(p) ((uintptr_t)(p) & 1)
#define ERR_RET ((huft_t*)(uintptr_t)1)
static huft_t* huft_build(const unsigned *b, const unsigned n,
const unsigned s, const struct cp_ext *cp_ext,
unsigned *m)
+60 -69
View File
@@ -4273,50 +4273,6 @@ sprint_status48(char *os, int status, int sigonly)
return s - os;
}
static int
wait_block_or_sig(int *status)
{
int pid;
do {
sigset_t mask;
/* Poll all children for changes in their state */
got_sigchld = 0;
/* if job control is active, accept stopped processes too */
pid = waitpid(-1, status, doing_jobctl ? (WNOHANG|WUNTRACED) : WNOHANG);
if (pid != 0)
break; /* Error (e.g. EINTR, ECHILD) or pid */
/* Children exist, but none are ready. Sleep until interesting signal */
#if 1
sigfillset(&mask);
sigprocmask2(SIG_SETMASK, &mask); /* mask is updated */
while (!got_sigchld && !pending_sig) {
sigsuspend(&mask);
/* ^^^ add "sigdelset(&mask, SIGCHLD);" before sigsuspend
* to make sure SIGCHLD is not masked off?
* It was reported that this:
* fn() { : | return; }
* shopt -s lastpipe
* fn
* exec ash SCRIPT
* under bash 4.4.23 runs SCRIPT with SIGCHLD masked,
* making "wait" commands in SCRIPT block forever.
*/
}
sigprocmask(SIG_SETMASK, &mask, NULL);
#else /* unsafe: a signal can set pending_sig after check, but before pause() */
while (!got_sigchld && !pending_sig)
pause();
#endif
/* If it was SIGCHLD, poll children again */
} while (got_sigchld);
return pid;
}
#define DOWAIT_NONBLOCK 0
#define DOWAIT_BLOCK 1
#define DOWAIT_BLOCK_OR_SIG 2
@@ -4324,13 +4280,48 @@ wait_block_or_sig(int *status)
# define DOWAIT_JOBSTATUS 0x10 /* OR this to get job's exitstatus instead of pid */
#endif
static int
waitproc(int block, int *status)
{
sigset_t oldmask;
int flags = block == DOWAIT_BLOCK ? 0 : WNOHANG;
int err;
#if JOBS
if (doing_jobctl)
flags |= WUNTRACED;
#endif
do {
got_sigchld = 0;
do
err = waitpid(-1, status, flags);
while (err < 0 && errno == EINTR);
if (err || (err = -!block))
break;
sigfillset(&oldmask);
sigprocmask2(SIG_SETMASK, &oldmask); /* mask is updated */
while (!got_sigchld && !pending_sig)
sigsuspend(&oldmask);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
//simpler, but unsafe: a signal can set pending_sig after check, but before pause():
//while (!got_sigchld && !pending_sig)
// pause();
} while (got_sigchld);
return err;
}
static int
waitone(int block, struct job *job)
{
int pid;
int status;
struct job *jp;
struct job *thisjob;
struct job *thisjob = NULL;
#if BASH_WAIT_N
bool want_jobexitstatus = (block & DOWAIT_JOBSTATUS);
block = (block & ~DOWAIT_JOBSTATUS);
@@ -4357,21 +4348,8 @@ waitone(int block, struct job *job)
* SIG_DFL handler does not wake sigsuspend().
*/
INT_OFF;
if (block == DOWAIT_BLOCK_OR_SIG) {
pid = wait_block_or_sig(&status);
} else {
int wait_flags = 0;
if (block == DOWAIT_NONBLOCK)
wait_flags = WNOHANG;
/* if job control is active, accept stopped processes too */
if (doing_jobctl)
wait_flags |= WUNTRACED;
/* NB: _not_ safe_waitpid, we need to detect EINTR */
pid = waitpid(-1, &status, wait_flags);
}
TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
pid, status, errno, strerror(errno)));
thisjob = NULL;
pid = waitproc(block, &status);
TRACE(("wait returns pid %d, status=%d\n", pid, status));
if (pid <= 0)
goto out;
@@ -4453,15 +4431,27 @@ waitone(int block, struct job *job)
static int
dowait(int block, struct job *jp)
{
int pid = block == DOWAIT_NONBLOCK ? got_sigchld : 1;
smallint gotchld = *(volatile smallint *)&got_sigchld;
int rpid;
int pid;
while (jp ? jp->state == JOBRUNNING : pid > 0) {
if (!jp)
got_sigchld = 0;
if (jp && jp->state != JOBRUNNING)
block = DOWAIT_NONBLOCK;
if (block == DOWAIT_NONBLOCK && !gotchld)
return 1;
rpid = 1;
do {
pid = waitone(block, jp);
}
rpid &= !!pid;
return pid;
if (!pid || (jp && jp->state != JOBRUNNING))
block = DOWAIT_NONBLOCK;
} while (pid >= 0);
return rpid;
}
#if JOBS
@@ -4708,7 +4698,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
job = getjob(*argv, 0);
}
/* loop until process terminated or stopped */
dowait(DOWAIT_BLOCK_OR_SIG, NULL);
dowait(DOWAIT_BLOCK_OR_SIG, job);
if (pending_sig)
goto sigout;
job->waited = 1;
@@ -7021,7 +7011,8 @@ subevalvar(char *start, char *str, int strloc,
slash_pos = -1;
if (repl) {
slash_pos = expdest - ((char *)stackblock() + strloc);
STPUTC('/', expdest);
if (!(flag & EXP_DISCARD))
STPUTC('/', expdest);
//bb_error_msg("repl+1:'%s'", repl + 1);
p = argstr(repl + 1, (flag & EXP_DISCARD) | EXP_TILDE); /* EXP_TILDE: echo "${v/x/~}" expands ~ ! */
*repl = '/';
@@ -12811,7 +12802,7 @@ parsebackq: {
goto done;
case '\\':
pc = pgetc(); /* or pgetc_eatbnl()? why (example)? */
pc = pgetc(); /* not pgetc_eatbnl! */
if (pc != '\\' && pc != '`' && pc != '$'
&& (!synstack->dblquote || pc != '"')
) {
+2
View File
@@ -0,0 +1,2 @@
Background1
Ok:0
+7
View File
@@ -0,0 +1,7 @@
sleep 1 && echo "Background1" &
pid=$!
sleep 3 && echo "Background2: BUG!" &
# Shouldn't wait for 2nd bkgd:
wait $pid
kill $!
echo Ok:$?
+1
View File
@@ -0,0 +1 @@
test
+1
View File
@@ -0,0 +1 @@
printf 'test\0zest\n' | (read -d '' reply; echo "$reply")
+14
View File
@@ -0,0 +1,14 @@
___
___
___
trap -- 'a' EXIT
trap -- 'a' INT
trap -- 'a' USR1
trap -- 'a' USR2
___
___
trap -- 'a' USR1
trap -- 'a' USR2
___
___
trap -- 'a' USR2
+23
View File
@@ -0,0 +1,23 @@
# no output -- default state
echo ___
trap
# assign some traps
echo ___
trap "a" EXIT INT USR1 USR2
# show them all
echo ___
trap
# clear one
echo ___
trap 0 INT
echo ___
trap
# clear another
echo ___
trap "-" USR1
echo ___
trap
@@ -1,2 +1,3 @@
Ok:0
@@ -1,3 +1,5 @@
unset v
echo ${v/*/w}
v=''
echo ${v/*/w}
echo Ok:$?
+9 -3
View File
@@ -8698,9 +8698,15 @@ static int process_wait_result(struct pipe *fg_pipe, pid_t childpid, int status)
*/
if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
if (i == fg_pipe->num_cmds-1)
/* TODO: use strsignal() instead for bash compat? but that's bloat... */
puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig));
if (G.run_list_level == 1
/* ^^^^^ Do not print in nested contexts, example:
* echo `sleep 1; sh -c 'kill -9 $$'` - prints "137", NOT "Killed 137"
*/
&& i == fg_pipe->num_cmds-1
) {
/* strsignal() is for bash compat. ~600 bloat versus bbox's get_signame() */
puts(sig == SIGINT || sig == SIGPIPE ? "" : strsignal(sig));
}
/* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */
/* TODO: MIPS has 128 sigs (1..128), what if sig==128 here?
* Maybe we need to use sig | 128? */
+2 -2
View File
@@ -1,5 +1,5 @@
KILL
Killed
137:137
KILL
Killed
0:0
Done
+2
View File
@@ -0,0 +1,2 @@
Background1
Ok:0
+7
View File
@@ -0,0 +1,7 @@
sleep 1 && echo "Background1" &
pid=$!
sleep 3 && echo "Background2: BUG!" &
# Shouldn't wait for 2nd bkgd:
wait $pid
kill $!
echo Ok:$?
+1
View File
@@ -0,0 +1 @@
137
+1
View File
@@ -0,0 +1 @@
true; echo `sh -c 'kill -9 $$'` $?
+1
View File
@@ -0,0 +1 @@
test
+1
View File
@@ -0,0 +1 @@
printf 'test\0zest\n' | (read -d '' reply; echo "$reply")
+1 -1
View File
@@ -2,4 +2,4 @@ sending USR2
caught
sending USR2
sending USR2
USR2
User defined signal 2
@@ -0,0 +1,20 @@
got signal
trap -- 'echo got signal' USR1
sent 1 signal
got signal
wait interrupted
trap -- 'echo got signal' USR1
sent 2 signal
got signal
wait interrupted
trap -- 'echo got signal' USR1
sent 3 signal
got signal
wait interrupted
trap -- 'echo got signal' USR1
sent 4 signal
got signal
wait interrupted
trap -- 'echo got signal' USR1
sent 5 signal
sleep completed
+28
View File
@@ -0,0 +1,28 @@
trap "echo got signal" USR1
for try in 1 2 3 4 5; do
kill -USR1 $$
sleep 0.2
echo "sent $try signal"
done &
# Ensure "wait" has something to wait for
sleep 2 &
# Ensure we do not execute "trap" below before "kill -USR1" above
# (was getting failure on loaded machine without this)
sleep 0.1
sleeping=true
while $sleeping; do
trap
if wait %%; then
echo "sleep completed"
sleeping=false
elif [ $? = 127 ]; then
echo "BUG: no processes to wait for?!"
sleeping=false
else
echo "wait interrupted"
fi
done
@@ -1,2 +1,2 @@
HUP
Hangup
Done:129
+1 -1
View File
@@ -17,5 +17,5 @@ Ok
trap -- '' HUP
trap -- '' QUIT
trap -- '' SYS
TERM
Terminated
Done
+2 -2
View File
@@ -251,7 +251,7 @@ typedef struct remembered_name {
} remembered_name;
static arith_t FAST_FUNC
static arith_t
evaluate_string(arith_state_t *math_state, const char *expr);
static const char*
@@ -582,7 +582,7 @@ static arith_t strto_arith_t(const char *nptr, char **endptr)
# endif
#endif
static arith_t FAST_FUNC
static arith_t
evaluate_string(arith_state_t *math_state, const char *expr)
{
operator lasttok;
+2 -2
View File
@@ -209,8 +209,6 @@ shell_builtin_read(struct builtin_read_params *params)
}
c = buffer[bufpos];
if (c == '\0')
continue;
if (!(read_flags & BUILTIN_READ_RAW)) {
if (backslash) {
backslash = 0;
@@ -225,6 +223,8 @@ shell_builtin_read(struct builtin_read_params *params)
}
if (c == delim) /* '\n' or -d CHAR */
break;
if (c == '\0')
continue;
/* $IFS splitting. NOT done if we run "read"
* without variable names (bash compat).