Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: validate STTY= value



This replaces workers/54517 (stty flusho for quieter history tests)
which I didn't apply because the -f option to stty turns out not to be
portable. A <$TTY redirection does work.

I've also tracked down why the zsh feature of prefixing commands with
STTY= didn't appear to work from in the test. That feature had initially
also operated with an imported STTY environment variable. It then
got limited to process group leaders (GETPGRP() == getpid()) because
"background jobs or any other job that wasn't allowed to read/write
the terminal would stop on SIGTTIN". In my view this check hasn't been
needed since an even later change to only use STTY= when it explicitly
prefixes a command. After removing it, STTY= would also work for
W01history.ztst. However, the functionality to restore stty settings
relies on job control being enabled. Setting MONITOR isn't enough. So
for the history test, this patch sticks with running stty directly.

Aside from dropping the process group leader test, the patch adds
checks of the parsed stty arguments value. The use of execstring()
means that zsh syntax such as expansions, pipes etc apply. With an
untrusted value passed to STTY, this would be bad and I raised this
on the -security list. But conclusion seems to be that that's fairly
academic, especially if the feature doesn't work from a script. We want
some parsing so things like STTY='eof "^E"' work. Checking the that the
generated wordcode matches an exact form is easier than adding an extra
parse routine and building the wordcode. I've not bothered with an error
message.

This also redirects $TTY (ttystrname) where that's set but isatty(0) is
false and adds "command" before "stty". It's also tempting to redirect
stdout and maybe stderr to /dev/null so that prefixing STTY='-a' doesn't
produce output.

Oliver

diff --git a/Src/exec.c b/Src/exec.c
index b0794abab..8555aed72 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -743,13 +743,55 @@ execute(LinkList args, int flags, int defpath)
      * parameter as it arguments. If the parameter is empty, we   *
      * do nothing, but this causes the terminal settings to be    *
      * restored later which can be useful.                        */
-    if ((s = STTYval) && *s && isatty(0) && (GETPGRP() == getpid())) {
-	char *t = tricat("stty", " ", s);
+    if ((s = STTYval) && *s && ttystrname) {
+	Eprog prog;
+	char *t, *sttycmd[] = {
+	    "command stty",
+	    s,
+	    isatty(0) ? NULL : "<",
+	    ttystrname,
+	    NULL
+        };
 
+	pushheap();
+	t = zjoin(sttycmd, ' ', 1);
 	STTYval = 0;	/* this prevents infinite recursion */
 	zsfree(s);
-	execstring(t, 1, 0, "stty");
-	zsfree(t);
+	if ((prog = parse_string(t, 0))) {
+	    /* Check the parsed code, allowing quoted strings and
+	     * the redirected $TTY but nothing else. */
+	    Wordcode end, p = prog->prog;
+	    if (!prog->npats &&
+		*p++ == wc_bld(WC_LIST, Z_END | Z_SYNC) &&
+		wc_code(*p) == WC_SUBLIST &&
+		WC_SUBLIST_FLAGS(*p++) == WC_SUBLIST_END &&
+		wc_code(*p) == WC_PIPE &&
+		WC_PIPE_TYPE(*p++) == WC_PIPE_END &&
+		(*p != wc_bld(WC_REDIR, REDIR_READ) || (
+		    !*++p && /* file descriptor 0 */
+		    !strcmp(ttystrname, ecrawstr(prog, ++p, 0)) &&
+		    p++ /* always true but need the increment */ )) &&
+		wc_code(*p) == WC_SIMPLE &&
+		(end = p + WC_SIMPLE_ARGC(*p) + 1, wc_code(*end) == WC_END))
+	    {
+		while (p < end) {
+		    for (t = ecrawstr(prog, p++, 0); *t; t++) {
+			if ((*t >= Hat && *t <= Comma) ||
+			    (*t == String && *++t != Snull))
+			    goto end;
+		    }
+		}
+
+		if (isset(VERBOSE)) {
+		    zputs(t, stderr);
+		    fputc('\n', stderr);
+		    fflush(stderr);
+		}
+		execode(prog, 1, 0, "stty");
+	    }
+	}
+    end:
+	popheap();
     } else if (s) {
 	STTYval = 0;
 	zsfree(s);
diff --git a/Test/W01history.ztst b/Test/W01history.ztst
index 665a37f2c..1d61237b7 100644
--- a/Test/W01history.ztst
+++ b/Test/W01history.ztst
@@ -6,18 +6,21 @@
 
 %test
 
+  stty flusho < $TTY
   PS1= $ZTST_testdir/../Src/zsh -fis <<<'
     print one two three four five six seven eight nine ten
     print !:$ !:10 !:9 !:1 !:0
     print one two three four five six seven eight nine ten
     print !:0-$ !:1-2
   '
+  stty -flusho < $TTY
 0:History word references
 >one two three four five six seven eight nine ten
 >ten ten nine one print
 >one two three four five six seven eight nine ten
 >print one two three four five six seven eight nine ten one two
 
+  stty flusho < $TTY
   PS1= $ZTST_testdir/../Src/zsh -fis <<<'
     print line one of an arbitrary series
     print issue two for some mystery sequence
@@ -26,6 +29,7 @@
     print !2:2
     print !-3:1-$
   '
+  stty -flusho < $TTY
 0:History line numbering
 >line one of an arbitrary series
 >issue two for some mystery sequence
@@ -34,12 +38,14 @@
 >two
 >mystery sequence
 
+  stty flusho < $TTY
   PS1= $ZTST_testdir/../Src/zsh -fis <<<'
     print All metaphor, Malachi, stilts and all
     print !1:2:s/,/\\\\?/ !1:2:s/m/shm/:s/,/\!/
     print !1:2:&
     print -l !1:2-3:gs/a/o/
   '
+  stty -flusho < $TTY
 0:History substitution
 >All metaphor, Malachi, stilts and all
 >metaphor? shmetaphor!
@@ -47,11 +53,13 @@
 >metophor,
 >Molochi,
 
+  stty flusho < $TTY
   PS1= $ZTST_testdir/../Src/zsh -fis <<<'
     echo foo bar
     echo $(!!) again
     echo more $( !! )
   '
+  stty -flusho < $TTY
 0:Regression test for history references in command substitution
 >foo bar
 >foo bar again
@@ -60,6 +68,7 @@
 F:Check that a history bug introduced by workers/34160 is working again.
 # Discarded line of error output consumes prompts printed by "zsh -i".
 
+ stty flusho < $TTY
  PS1= $ZTST_testdir/../Src/zsh -fis <<<'
    echo /my/path/for/testing
    echo !1:1:h10
@@ -72,6 +81,7 @@ F:Check that a history bug introduced by workers/34160 is working again.
    echo !1:1:t1
    echo !1:1:t3:h2
  '
+ stty -flusho < $TTY
 0:Modifiers :h and :t with arguments
 >/my/path/for/testing
 >/my/path/for/testing
@@ -84,10 +94,12 @@ F:Check that a history bug introduced by workers/34160 is working again.
 >testing
 >path/for
 
+ stty flusho < $TTY
  PS1= $ZTST_testdir/../Src/zsh -fis <<<'
    echo /my/path/for/testing
    echo !1:1:P
  '
+ stty -flusho < $TTY
 0:Modifier :P
 >/mggy/path/for/testing
 >/my/path/for/testing




Messages sorted by: Reverse Date, Date, Thread, Author