hush: protect against self-modifying trap code
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 30 Jan 2018 03:29:03 +0000 (04:29 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 30 Jan 2018 03:29:03 +0000 (04:29 +0100)
function                                             old     new   delta
check_and_run_traps                                  211     236     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c

index ddf377355a760d1863b3523c1f5bf10d71c40005..585c51bd54dff81ca1a84a71430bc46bd1cc55d2 100644 (file)
@@ -2004,10 +2004,12 @@ static int check_and_run_traps(void)
                                smalluint save_rcode;
                                char *argv[3];
                                /* argv[0] is unused */
-                               argv[1] = G_traps[sig];
+                               argv[1] = xstrdup(G_traps[sig]);
+                               /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */
                                argv[2] = NULL;
                                save_rcode = G.last_exitcode;
                                builtin_eval(argv);
+                               free(argv[1]);
 //FIXME: shouldn't it be set to 128 + sig instead?
                                G.last_exitcode = save_rcode;
                                last_sig = sig;