Index: head/bin/sh/tests/builtins/Makefile =================================================================== --- head/bin/sh/tests/builtins/Makefile (revision 297359) +++ head/bin/sh/tests/builtins/Makefile (revision 297360) @@ -1,177 +1,178 @@ # $FreeBSD$ .include TESTSDIR= ${TESTSBASE}/bin/sh/${.CURDIR:T} .PATH: ${.CURDIR:H} ATF_TESTS_SH= functional_test FILESDIR= ${TESTSDIR} FILES= alias.0 alias.0.stdout FILES+= alias.1 alias.1.stderr FILES+= alias3.0 alias3.0.stdout FILES+= alias4.0 FILES+= break1.0 FILES+= break2.0 break2.0.stdout FILES+= break3.0 FILES+= break4.4 FILES+= break5.4 FILES+= break6.0 FILES+= builtin1.0 FILES+= case1.0 FILES+= case2.0 FILES+= case3.0 FILES+= case4.0 FILES+= case5.0 FILES+= case6.0 FILES+= case7.0 FILES+= case8.0 FILES+= case9.0 FILES+= case10.0 FILES+= case11.0 FILES+= case12.0 FILES+= case13.0 FILES+= case14.0 FILES+= case15.0 FILES+= case16.0 FILES+= case17.0 FILES+= case18.0 FILES+= case19.0 FILES+= case20.0 FILES+= cd1.0 FILES+= cd2.0 FILES+= cd3.0 FILES+= cd4.0 FILES+= cd5.0 FILES+= cd6.0 FILES+= cd7.0 FILES+= cd8.0 FILES+= cd9.0 cd9.0.stdout FILES+= command1.0 FILES+= command2.0 FILES+= command3.0 FILES+= command3.0.stdout FILES+= command4.0 FILES+= command5.0 FILES+= command5.0.stdout FILES+= command6.0 FILES+= command6.0.stdout FILES+= command7.0 FILES+= command8.0 FILES+= command9.0 FILES+= command10.0 FILES+= command11.0 FILES+= command12.0 FILES+= dot1.0 FILES+= dot2.0 FILES+= dot3.0 FILES+= dot4.0 FILES+= eval1.0 FILES+= eval2.0 FILES+= eval3.0 FILES+= eval4.0 FILES+= eval5.0 FILES+= eval6.0 FILES+= eval7.0 FILES+= eval8.7 FILES+= exec1.0 FILES+= exec2.0 FILES+= exit1.0 FILES+= exit2.8 FILES+= exit3.0 FILES+= export1.0 FILES+= fc1.0 FILES+= fc2.0 FILES+= for1.0 FILES+= for2.0 FILES+= for3.0 FILES+= getopts1.0 getopts1.0.stdout FILES+= getopts2.0 getopts2.0.stdout FILES+= getopts3.0 FILES+= getopts4.0 FILES+= getopts5.0 FILES+= getopts6.0 FILES+= getopts7.0 FILES+= getopts8.0 getopts8.0.stdout FILES+= getopts9.0 getopts9.0.stdout FILES+= getopts10.0 FILES+= hash1.0 hash1.0.stdout FILES+= hash2.0 hash2.0.stdout FILES+= hash3.0 hash3.0.stdout FILES+= hash4.0 FILES+= jobid1.0 FILES+= jobid2.0 FILES+= kill1.0 kill2.0 FILES+= lineno.0 lineno.0.stdout FILES+= lineno2.0 FILES+= lineno3.0 lineno3.0.stdout FILES+= local1.0 FILES+= local2.0 FILES+= local3.0 FILES+= local4.0 FILES+= local5.0 FILES+= local6.0 FILES+= local7.0 .if ${MK_NLS} != "no" FILES+= locale1.0 .endif FILES+= printf1.0 FILES+= printf2.0 FILES+= printf3.0 FILES+= printf4.0 FILES+= read1.0 read1.0.stdout FILES+= read2.0 FILES+= read3.0 read3.0.stdout FILES+= read4.0 read4.0.stdout FILES+= read5.0 FILES+= read6.0 FILES+= read7.0 FILES+= read8.0 FILES+= read9.0 FILES+= return1.0 FILES+= return2.1 FILES+= return3.1 FILES+= return4.0 FILES+= return5.0 FILES+= return6.4 FILES+= return7.4 FILES+= return8.0 FILES+= set1.0 FILES+= set2.0 FILES+= set3.0 FILES+= trap1.0 FILES+= trap10.0 FILES+= trap11.0 FILES+= trap12.0 FILES+= trap13.0 FILES+= trap14.0 FILES+= trap15.0 FILES+= trap16.0 +FILES+= trap17.0 FILES+= trap2.0 FILES+= trap3.0 FILES+= trap4.0 FILES+= trap5.0 FILES+= trap6.0 FILES+= trap7.0 FILES+= trap8.0 FILES+= trap9.0 FILES+= type1.0 type1.0.stderr FILES+= type2.0 FILES+= type3.0 FILES+= unalias.0 FILES+= var-assign.0 FILES+= var-assign2.0 FILES+= wait1.0 FILES+= wait2.0 FILES+= wait3.0 FILES+= wait4.0 FILES+= wait5.0 FILES+= wait6.0 FILES+= wait7.0 FILES+= wait8.0 FILES+= wait9.127 FILES+= wait10.0 .include Index: head/bin/sh/tests/builtins/trap17.0 =================================================================== --- head/bin/sh/tests/builtins/trap17.0 (nonexistent) +++ head/bin/sh/tests/builtins/trap17.0 (revision 297360) @@ -0,0 +1,10 @@ +# $FreeBSD$ +# This use-after-free bug probably needs non-default settings to show up. + +v1=nothing v2=nothing +trap 'trap "echo bad" USR1 +v1=trap_received +v2=trap_invoked +:' USR1 +kill -USR1 "$$" +[ "$v1.$v2" = trap_received.trap_invoked ] Property changes on: head/bin/sh/tests/builtins/trap17.0 ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: head/bin/sh/trap.c =================================================================== --- head/bin/sh/trap.c (revision 297359) +++ head/bin/sh/trap.c (revision 297360) @@ -1,551 +1,554 @@ /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Kenneth Almquist. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include "shell.h" #include "main.h" #include "nodes.h" /* for other headers */ #include "eval.h" #include "jobs.h" #include "show.h" #include "options.h" #include "syntax.h" #include "output.h" #include "memalloc.h" #include "error.h" #include "trap.h" #include "mystring.h" #include "builtins.h" #include "myhistedit.h" /* * Sigmode records the current value of the signal handlers for the various * modes. A value of zero means that the current handler is not known. * S_HARD_IGN indicates that the signal was ignored on entry to the shell, */ #define S_DFL 1 /* default signal handling (SIG_DFL) */ #define S_CATCH 2 /* signal is caught */ #define S_IGN 3 /* signal is ignored (SIG_IGN) */ #define S_HARD_IGN 4 /* signal is ignored permanently */ #define S_RESET 5 /* temporary - to reset a hard ignored sig */ static char sigmode[NSIG]; /* current value of signal */ volatile sig_atomic_t pendingsig; /* indicates some signal received */ volatile sig_atomic_t pendingsig_waitcmd; /* indicates wait builtin should be interrupted */ static int in_dotrap; /* do we execute in a trap handler? */ static char *volatile trap[NSIG]; /* trap handler commands */ static volatile sig_atomic_t gotsig[NSIG]; /* indicates specified signal received */ static int ignore_sigchld; /* Used while handling SIGCHLD traps. */ static int last_trapsig; static int exiting; /* exitshell() has been called */ static int exiting_exitstatus; /* value passed to exitshell() */ static int getsigaction(int, sig_t *); /* * Map a string to a signal number. * * Note: the signal number may exceed NSIG. */ static int sigstring_to_signum(char *sig) { if (is_number(sig)) { int signo; signo = atoi(sig); return ((signo >= 0 && signo < NSIG) ? signo : (-1)); } else if (strcasecmp(sig, "EXIT") == 0) { return (0); } else { int n; if (strncasecmp(sig, "SIG", 3) == 0) sig += 3; for (n = 1; n < sys_nsig; n++) if (sys_signame[n] && strcasecmp(sys_signame[n], sig) == 0) return (n); } return (-1); } /* * Print a list of valid signal names. */ static void printsignals(void) { int n, outlen; outlen = 0; for (n = 1; n < sys_nsig; n++) { if (sys_signame[n]) { out1fmt("%s", sys_signame[n]); outlen += strlen(sys_signame[n]); } else { out1fmt("%d", n); outlen += 3; /* good enough */ } ++outlen; if (outlen > 71 || n == sys_nsig - 1) { out1str("\n"); outlen = 0; } else { out1c(' '); } } } /* * The trap builtin. */ int trapcmd(int argc __unused, char **argv) { char *action; int signo; int errors = 0; int i; while ((i = nextopt("l")) != '\0') { switch (i) { case 'l': printsignals(); return (0); } } argv = argptr; if (*argv == NULL) { for (signo = 0 ; signo < sys_nsig ; signo++) { if (signo < NSIG && trap[signo] != NULL) { out1str("trap -- "); out1qstr(trap[signo]); if (signo == 0) { out1str(" EXIT\n"); } else if (sys_signame[signo]) { out1fmt(" %s\n", sys_signame[signo]); } else { out1fmt(" %d\n", signo); } } } return 0; } action = NULL; if (*argv && !is_number(*argv)) { if (strcmp(*argv, "-") == 0) argv++; else { action = *argv; argv++; } } for (; *argv; argv++) { if ((signo = sigstring_to_signum(*argv)) == -1) { warning("bad signal %s", *argv); errors = 1; continue; } INTOFF; if (action) action = savestr(action); if (trap[signo]) ckfree(trap[signo]); trap[signo] = action; if (signo != 0) setsignal(signo); INTON; } return errors; } /* * Clear traps on a fork. */ void clear_traps(void) { char *volatile *tp; for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { if (*tp && **tp) { /* trap not NULL or SIG_IGN */ INTOFF; ckfree(*tp); *tp = NULL; if (tp != &trap[0]) setsignal(tp - trap); INTON; } } } /* * Check if we have any traps enabled. */ int have_traps(void) { char *volatile *tp; for (tp = trap ; tp <= &trap[NSIG - 1] ; tp++) { if (*tp && **tp) /* trap not NULL or SIG_IGN */ return 1; } return 0; } /* * Set the signal handler for the specified signal. The routine figures * out what it should be set to. */ void setsignal(int signo) { int action; sig_t sigact = SIG_DFL; struct sigaction sa; char *t; if ((t = trap[signo]) == NULL) action = S_DFL; else if (*t != '\0') action = S_CATCH; else action = S_IGN; if (action == S_DFL) { switch (signo) { case SIGINT: action = S_CATCH; break; case SIGQUIT: #ifdef DEBUG { extern int debug; if (debug) break; } #endif action = S_CATCH; break; case SIGTERM: if (rootshell && iflag) action = S_IGN; break; #if JOBS case SIGTSTP: case SIGTTOU: if (rootshell && mflag) action = S_IGN; break; #endif } } t = &sigmode[signo]; if (*t == 0) { /* * current setting unknown */ if (!getsigaction(signo, &sigact)) { /* * Pretend it worked; maybe we should give a warning * here, but other shells don't. We don't alter * sigmode, so that we retry every time. */ return; } if (sigact == SIG_IGN) { if (mflag && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)) { *t = S_IGN; /* don't hard ignore these */ } else *t = S_HARD_IGN; } else { *t = S_RESET; /* force to be set */ } } if (*t == S_HARD_IGN || *t == action) return; switch (action) { case S_DFL: sigact = SIG_DFL; break; case S_CATCH: sigact = onsig; break; case S_IGN: sigact = SIG_IGN; break; } *t = action; sa.sa_handler = sigact; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sigaction(signo, &sa, NULL); } /* * Return the current setting for sig w/o changing it. */ static int getsigaction(int signo, sig_t *sigact) { struct sigaction sa; if (sigaction(signo, (struct sigaction *)0, &sa) == -1) return 0; *sigact = (sig_t) sa.sa_handler; return 1; } /* * Ignore a signal. */ void ignoresig(int signo) { if (sigmode[signo] == 0) setsignal(signo); if (sigmode[signo] != S_IGN && sigmode[signo] != S_HARD_IGN) { signal(signo, SIG_IGN); sigmode[signo] = S_IGN; } } int issigchldtrapped(void) { return (trap[SIGCHLD] != NULL && *trap[SIGCHLD] != '\0'); } /* * Signal handler. */ void onsig(int signo) { if (signo == SIGINT && trap[SIGINT] == NULL) { /* * The !in_dotrap here is safe. The only way we can arrive * here with in_dotrap set is that a trap handler set SIGINT to * SIG_DFL and killed itself. */ if (suppressint && !in_dotrap) SET_PENDING_INT; else onint(); return; } /* If we are currently in a wait builtin, prepare to break it */ if (signo == SIGINT || signo == SIGQUIT) pendingsig_waitcmd = signo; if (trap[signo] != NULL && trap[signo][0] != '\0' && (signo != SIGCHLD || !ignore_sigchld)) { gotsig[signo] = 1; pendingsig = signo; pendingsig_waitcmd = signo; } } /* * Called to execute a trap. Perhaps we should avoid entering new trap * handlers while we are executing a trap handler. */ void dotrap(void) { + struct stackmark smark; int i; int savestatus, prev_evalskip, prev_skipcount; in_dotrap++; for (;;) { pendingsig = 0; pendingsig_waitcmd = 0; for (i = 1; i < NSIG; i++) { if (gotsig[i]) { gotsig[i] = 0; if (trap[i]) { /* * Ignore SIGCHLD to avoid infinite * recursion if the trap action does * a fork. */ if (i == SIGCHLD) ignore_sigchld++; /* * Backup current evalskip * state and reset it before * executing a trap, so that the * trap is not disturbed by an * ongoing break/continue/return * statement. */ prev_evalskip = evalskip; prev_skipcount = skipcount; evalskip = 0; last_trapsig = i; savestatus = exitstatus; - evalstring(trap[i], 0); + setstackmark(&smark); + evalstring(stsavestr(trap[i]), 0); + popstackmark(&smark); /* * If such a command was not * already in progress, allow a * break/continue/return in the * trap action to have an effect * outside of it. */ if (evalskip == 0 || prev_evalskip != 0) { evalskip = prev_evalskip; skipcount = prev_skipcount; exitstatus = savestatus; } if (i == SIGCHLD) ignore_sigchld--; } break; } } if (i >= NSIG) break; } in_dotrap--; } /* * Controls whether the shell is interactive or not. */ void setinteractive(int on) { static int is_interactive = -1; if (on == is_interactive) return; setsignal(SIGINT); setsignal(SIGQUIT); setsignal(SIGTERM); is_interactive = on; } /* * Called to exit the shell. */ void exitshell(int status) { TRACE(("exitshell(%d) pid=%d\n", status, getpid())); exiting = 1; exiting_exitstatus = status; exitshell_savedstatus(); } void exitshell_savedstatus(void) { struct jmploc loc1, loc2; char *p; int sig = 0; sigset_t sigs; if (!exiting) { if (in_dotrap && last_trapsig) { sig = last_trapsig; exiting_exitstatus = sig + 128; } else exiting_exitstatus = oexitstatus; } exitstatus = oexitstatus = exiting_exitstatus; if (!setjmp(loc1.loc)) { handler = &loc1; if ((p = trap[0]) != NULL && *p != '\0') { /* * Reset evalskip, or the trap on EXIT could be * interrupted if the last command was a "return". */ evalskip = 0; trap[0] = NULL; evalstring(p, 0); } } if (!setjmp(loc2.loc)) { handler = &loc2; /* probably unnecessary */ flushall(); #if JOBS setjobctl(0); #endif } if (sig != 0 && sig != SIGSTOP && sig != SIGTSTP && sig != SIGTTIN && sig != SIGTTOU) { signal(sig, SIG_DFL); sigemptyset(&sigs); sigaddset(&sigs, sig); sigprocmask(SIG_UNBLOCK, &sigs, NULL); kill(getpid(), sig); /* If the default action is to ignore, fall back to _exit(). */ } _exit(exiting_exitstatus); }