Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Apr 2017 21:42:27 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r316942 - in stable/10/bin/sh: . tests/expansion
Message-ID:  <201704142142.v3ELgRMK052395@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Fri Apr 14 21:42:27 2017
New Revision: 316942
URL: https://svnweb.freebsd.org/changeset/base/316942

Log:
  MFC r314686: sh: Fix crash if a -T trap is taken during command substitution.
  
  Code like  t=$(stat -f %m "$file")  segfaulted if -T was active and a trap
  was taken while the shell was waiting for the child process to finish.
  
  What happened was that the dotrap() call in waitforjob() was hit. This
  re-entered command execution (including expand.c) at a point not expected by
  expbackq(), and global state (unallocated stack string and argbackq) was
  corrupted.
  
  To fix this, change expbackq() to prepare for command execution to be
  re-entered.
  
  In stable/10, there is more global state that needs to be restored than in
  stable/11 and head.
  
  Reported by:	bdrewery

Added:
  stable/10/bin/sh/tests/expansion/cmdsubst21.0
     - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst21.0
  stable/10/bin/sh/tests/expansion/cmdsubst22.0
     - copied unchanged from r314686, head/bin/sh/tests/expansion/cmdsubst22.0
Modified:
  stable/10/bin/sh/expand.c
  stable/10/bin/sh/tests/expansion/Makefile
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/bin/sh/expand.c
==============================================================================
--- stable/10/bin/sh/expand.c	Fri Apr 14 20:15:34 2017	(r316941)
+++ stable/10/bin/sh/expand.c	Fri Apr 14 21:42:27 2017	(r316942)
@@ -439,9 +439,6 @@ expbackq(union node *cmd, int quoted, in
 	p = grabstackstr(dest);
 	evalbackcmd(cmd, &in);
 	ungrabstackstr(p, dest);
-	ifsfirst = saveifs;
-	ifslastp = savelastp;
-	argbackq = saveargbackq;
 
 	p = in.buf;
 	lastc = '\0';
@@ -479,14 +476,20 @@ expbackq(union node *cmd, int quoted, in
 		close(in.fd);
 	if (in.buf)
 		ckfree(in.buf);
-	if (in.jp)
+	if (in.jp) {
+		p = grabstackstr(dest);
 		exitstatus = waitforjob(in.jp, (int *)NULL);
-	if (quoted == 0)
-		recordregion(startloc, dest - stackblock(), 0);
+		ungrabstackstr(p, dest);
+	}
 	TRACE(("expbackq: size=%td: \"%.*s\"\n",
 		((dest - stackblock()) - startloc),
 		(int)((dest - stackblock()) - startloc),
 		stackblock() + startloc));
+	ifsfirst = saveifs;
+	ifslastp = savelastp;
+	if (quoted == 0)
+		recordregion(startloc, dest - stackblock(), 0);
+	argbackq = saveargbackq;
 	expdest = dest;
 	INTON;
 }

Modified: stable/10/bin/sh/tests/expansion/Makefile
==============================================================================
--- stable/10/bin/sh/tests/expansion/Makefile	Fri Apr 14 20:15:34 2017	(r316941)
+++ stable/10/bin/sh/tests/expansion/Makefile	Fri Apr 14 21:42:27 2017	(r316942)
@@ -41,6 +41,8 @@ FILES+=		cmdsubst17.0
 FILES+=		cmdsubst18.0
 FILES+=		cmdsubst19.0
 FILES+=		cmdsubst20.0
+FILES+=		cmdsubst21.0
+FILES+=		cmdsubst22.0
 FILES+=		export1.0
 FILES+=		export2.0
 FILES+=		export3.0

Copied: stable/10/bin/sh/tests/expansion/cmdsubst21.0 (from r314686, head/bin/sh/tests/expansion/cmdsubst21.0)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/10/bin/sh/tests/expansion/cmdsubst21.0	Fri Apr 14 21:42:27 2017	(r316942, copy of r314686, head/bin/sh/tests/expansion/cmdsubst21.0)
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+set -T
+trapped=''
+trap "trapped=x$trapped" TERM
+[ "x$($SH -c "kill $$")y" = xy ] && [ "$trapped" = x ]

Copied: stable/10/bin/sh/tests/expansion/cmdsubst22.0 (from r314686, head/bin/sh/tests/expansion/cmdsubst22.0)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/10/bin/sh/tests/expansion/cmdsubst22.0	Fri Apr 14 21:42:27 2017	(r316942, copy of r314686, head/bin/sh/tests/expansion/cmdsubst22.0)
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+set -T
+trapped=''
+trap "trapped=x$trapped" TERM
+[ "x$(:; kill $$)y" = xy ] && [ "$trapped" = x ]



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201704142142.v3ELgRMK052395>