Login | Register
My pages Projects Community openCollabNet
Click here to access Issue tracker direct from Eclipse or Visual Studio
and to obtain other CollabNet integrations.
Project highlights:

14 Nov 2017: Release 3.0.1 is now available at the download page.

18 Sep 2017: Release 3.0.0 is now available at the download page.

03 Nov 2016: Release 2.5.1 is now available at the download page.

scons
Issue 1689

Query | Reports

Issue 1689

Issue #: 1689   Platform: All   Reporter: broonie (Mark Brown)
Component: scons   OS: All  
Subcomponent: scons   Version: 0.97   CC:
Remove selected CCs
Status: RESOLVED   Priority: P2  
Resolution: FIXED   Issue type: DEFECT  
 Target milestone:1.0.1 
Assigned to: gregnoel (Greg Noel)
QA Contact: issues@scons
URL: https://bugs.launchpad.net/launchpad-buildd/+bug/87077/
* Summary: Uses os.system() without sanitising environment
Status whiteboard:
Keywords:
Attachments:
Date/filename: Description: Submitted by:
Thu Sep 4 22:27:00 -0800 2008: review Patch to escape variable names (text/plain) Greg Noel
Issue 1689 depends on: Show dependency tree
Issue 1689 blocks:
Votes for issue 1689:    Vote for this issue

View issue activity   |   Format for printing   |   Format as XML

Description: Opened: Tue Jul 10 07:06:00 -0800 2007 Sort by: Oldest first | Newest first

As discussed on the ubuntu-devel list[1] on POSIX platforms without spawnvpe but
which do have the env utility SCons uses that together with os.system() to pass
the environment to child processes. When doing so it does fully not escape
special characters in the environment, causing build failures. This is
particularly common where SConstructs blindly pass the entire system environment
into the Environment.

The Ubunutu build daemons trigger this by defining an environment variable
HASH(0x12345678).

[1] https://lists.ubuntu.com/archives/ubuntu-devel/2007-July/023901.html

------- Additional comments from Mark Brown Wed Jul 11 10:49:27 -0800 2007 -------

This is also being tracked in Launchpad:

  https://bugs.launchpad.net/launchpad-buildd/+bug/87077/

------- Additional comments from Greg Noel Thu Jun 5 15:34:55 -0800 2008 -------

Bug party triage: Need to find out exactly where this is taking place (command
execution, backtick, something else?) and start replacing these instances with
subprocess.

------- Additional comments from Mark Brown Fri Jun 6 01:22:09 -0800 2008 -------

This is caused by not quoting the environment when passing it to env in the
os.system() call - os.system() runs things using the shell so needs appropriate
quoting for that. The offending code is engine/SCons/Platform/posix.py.

I'm not sure how good the benchmark results were but it wasn't clear to me that
it was even worth trying to use this method if you have to do quoting since the
time taken to quote the strings may remove any benefit.

------- Additional comments from Gary Oberbrunner Fri Jun 6 03:33:10 -0800 2008 -------

From looking at the launchpad bug, it looks like env_spawn is only quoting the
VALUES, not the NAMES of the env vars.  The bug comes from having an env var
with a name of "HASH(0x12345678)" (who even knew that was possible!) 

------- Additional comments from Mark Brown Fri Jun 6 04:24:19 -0800 2008 -------

I rather suspect that more characters need to be escaped even in the values, but
ICBW.

In any case, this is something that systems support so SCons should handle it.

------- Additional comments from Gary Oberbrunner Fri Jun 6 05:17:58 -0800 2008 -------

Hi Mark; I was just looking at that and I'm pretty sure you're right.  The
default escape() function in posix.py only backslash-escapes backslash,
double-quote, and dollar.  I think that would be correct *if* the result were
going to be used in a double-quoted string, but it's not; it just gets passed to
the shell as is.

Two ways to fix it, then: (1) make escape() escape all shell metachars, or (2)
doublequote the results of escape().  I'm not sure of the ramifications of
either.  We have to be very careful in this area.

------- Additional comments from Greg Noel Fri Jun 6 09:31:17 -0800 2008 -------

Hi Mark, Gary -- The question that came up during the bug party was who should
take the bug.  If you had encountered it from ParseConfig/ParseFlages (which
call backtick), it made sense for me to work on it.  If you encountered it when
executing a command, Steven made sense.  If it was interwoven with other quoting
issues, Jim made sense.  At that point, I hadn't studied the code, so I wasn't sure.

Now that I've been pushed to look at the code, I want to run a couple of timing
tests, but I'd bet that quoting the environment values makes os.system slower
than fork/exec in all cases, in which case, something like your patch makes a
lot of sense (although I'd take out the referenced functions as well).

Now if we could only cure that race between import and fork...

BTW, Gary, all *IX shells are absolutely uniform in the way they do quoting,
which means that quoting with a backslash is the only absolutely reliable way.
The simplest (but extreme) way is to quote every character; a less extreme way
is to quote only the shell metacharacters.  From your comment, the set of quoted
characters is insufficient, and if the escape() routine survives, it should be
expanded to handle all shell metacharacters.

------- Additional comments from Mark Brown Fri Jun 6 10:22:40 -0800 2008 -------

This was encountered executing jobs.

If the patch were integrated upstream it'd need to drop the referenced
functions, yes. The patch doesn't do that since when carrying a diff in a
distribution it's better to carry the minimum diff since this avoids unneeded
rejects due to changes in the upstream code when porting diffs forwards.

------- Additional comments from Greg Noel Thu Sep 4 22:27:34 -0800 2008 -------

Created an attachment (id=493)
Patch to escape variable names

------- Additional comments from Greg Noel Thu Sep 4 22:38:18 -0800 2008 -------

This patch is a hack to solve the immediate symptoms.  It passes the regression
tests, but needs a test case of its own, so this issue shouldn't be closed until
that is complete.

------- Additional comments from Greg Noel Thu Sep 4 22:46:07 -0800 2008 -------

Oh, the escape function should be extended to include any other characters that
need to be quoted: <>{}[]!#&*~`?'| and possibly others.  It's possible that
those could have side-effects, so I didn't do them for the initial patch.

Patch committed as revision 3340.

------- Additional comments from Steven Knight Sat Sep 6 06:35:03 -0800 2008 -------

Reassigning to gregnoel to record who fixed it.

------- Additional comments from Steven Knight Sat Sep 6 06:35:23 -0800 2008 -------

Regression test added by the following checkin as r3360:

http://scons.tigris.org/servlets/ReadMsg?list=dev&msgNo=6336
http://scons.tigris.org/servlets/ReadMsg?list=commits&msgNo=1916

Pushed both r3340 and r3360 to the /checkpoint and /release branches for 1.0.1:

http://scons.tigris.org/servlets/ReadMsg?list=commits&msgNo=1917
http://scons.tigris.org/servlets/ReadMsg?list=commits&msgNo=1918

Updating target milestone here to 1.0.1, just for consistency.