Skip to contentSkip to conversation

var.c: check for valid variable name before printing in "export -p"

PATCH In review [email protected] 4 participants 7 messages Feb 14, 2012 – Feb 25, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 var.c: check for valid variable name before printing in "export -p"

From: Harald Hoyer <[email protected]>

"export -p" prints all environment variables, without checking if the environment variable is a valid dash variable name.

IMHO, the only valid usecase for "export -p" is to eval the output.

$ eval $(export -p); echo OK
OK

Without this patch the following test does error out with:

test.py:
import os
os.environ["test-test"]="test" os.environ["test_test"]="test"
os.execv("./dash", [ './dash', '-c', 'eval $(export -p); echo OK' ])

$ python test.py
./dash: 1: export: test-test: bad variable name

Of course the results can be more evil, if the environment variable name is crafted, that it injects valid shell code.

---
 src/var.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
src/var.c +5 -2
index 027beff..06771d3 100644
@@ -409,12 +409,15 @@ showvars(const char *prefix, int on, int off)
 	for (; ep < epend; ep++) {
 		const char *p;
 		const char *q;
-
+		const char *r;
+		r = endofname(*ep);
 		p = strchrnul(*ep, '=');
 		q = nullstr;
-		if (*p)
+		if (*p) {
+			if (p != r)
+				continue;
 			q = single_quote(++p);
#1 lore raw
Quoted 6 lines · On Tue, Feb 14, 2012 at 10:48:48AM +0000, [email protected] wrote:

From: Harald Hoyer <[email protected]>

"export -p" prints all environment variables, without checking if the environment variable is a valid dash variable name.

IMHO, the only valid usecase for "export -p" is to eval the output.

Thanks a lot for the report and patch.

I'd prefer to fix this up at entry into the shell rather than when we print out the variables. So how about this patch?

commit 46d3c1a614f11f0d40a7e73376359618ff07abcd

Author: Herbert Xu <[email protected]>
Date:   Sat Feb 25 15:35:18 2012 +0800

[VAR] Sanitise environment variable names on entry

On Tue, Feb 14, 2012 at 10:48:48AM +0000, [email protected] wrote:
>
> "export -p" prints all environment variables, without checking if the > environment variable is a valid dash variable name.
>
> IMHO, the only valid usecase for "export -p" is to eval the output. >
> $ eval $(export -p); echo OK
> OK
>
> Without this patch the following test does error out with:
>
> test.py:
> import os
> os.environ["test-test"]="test"
> os.environ["test_test"]="test"
> os.execv("./dash", [ './dash', '-c', 'eval $(export -p); echo OK' ]) >
> $ python test.py
> ./dash: 1: export: test-test: bad variable name
>
> Of course the results can be more evil, if the environment variable > name is crafted, that it injects valid shell code.

This patch fixes the issue by sanitising all environment variable names upon entry into the shell.

ChangeLog +4 -0
index d1e84c9..8686332 100644
@@ -1,3 +1,7 @@
+2012-02-25  Herbert Xu <[email protected]>
+
+	* Sanitise environment variable names on entry.
+
 2011-08-17  David S. Miller <[email protected]>
 
 	* Allow building without LINEO support.
src/var.c +2 -1
index 027beff..dc90249 100644
@@ -136,7 +136,8 @@ INIT {
 
 	initvar();
 	for (envp = environ ; *envp ; envp++) {
-		if (strchr(*envp, '=')) {
+		p = endofname(*envp);
+		if (p != *envp && *p == '=') {
 			setvareq(*envp, VEXPORT|VTEXTFIXED);
 		}
 	}

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 6 lines · Herbert Xu
Quoted 4 lines · On Tue, Feb 14, 2012 at 10:48:48AM +0000, [email protected] wrote:

From: Harald Hoyer <[email protected]>

"export -p" prints all environment variables, without checking if the environment variable is a valid dash variable name.

Quoted 6 lines
Quoted 1 line

IMHO, the only valid usecase for "export -p" is to eval the output.

Thanks a lot for the report and patch.

I'd prefer to fix this up at entry into the shell rather than when we print out the variables. So how about this patch?

Such a change would change other things than just "set" and "export -p". It would also not propagate environment variables with invalid names to child processes. For example:

env -i not-a-name=1 PATH="$PATH" dash -c env | grep not-a-name

Most shells pass the environment variable through, such as bash, zsh, ksh93 and most ash derivatives. However, the original Bourne shell and pdksh/mksh do not.

I think it is best to pass them through so that executing a utility with and without the shell are as similar as possible (most versions of make(1) assume this is the case by skipping the shell if the command is simple enough) and particularly for dash for historical/compatibility reasons.

I did something similar to the OP's patch in FreeBSD (SVN r223183):
"set" and "export -p" skip variables with invalid names.

Note that this problem cannot occur with "readonly -p" because only variables with a proper name can be marked read-only.

Signature · 1 line
Jilles Tjoelker
#3 lore raw
Quoted 4 lines · Jilles Tjoelker

Most shells pass the environment variable through, such as bash, zsh, ksh93 and most ash derivatives. However, the original Bourne shell and pdksh/mksh do not.

Do you know of any genuine uses of such environment variables?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 7 lines · Herbert Xu
Quoted 4 lines · Jilles Tjoelker

Most shells pass the environment variable through, such as bash, zsh, ksh93 and most ash derivatives. However, the original Bourne shell and pdksh/mksh do not.

Do you know of any genuine uses of such environment variables?

POSIX states that applications must not rely on such pass-through:
http://austingroupbugs.net/view.php?id=168

So while it might indeed be useful to pass through invalid names, such an application is broken for expecting it to work, and I'm okay with this patch as-is.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 7 lines · Eric Blake

POSIX states that applications must not rely on such pass-through:
http://austingroupbugs.net/view.php?id=168

So while it might indeed be useful to pass through invalid names, such an application is broken for expecting it to work, and I'm okay with this patch as-is.

Great. From a security point of view I'd rather not pass them through.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
In reply to Herbert Xu
Quoted 7 lines · Herbert Xu
Quoted 3 lines · Jilles Tjoelker

Most shells pass the environment variable through, such as bash, zsh, ksh93 and most ash derivatives. However, the original Bourne shell and pdksh/mksh do not.

Do you know of any genuine uses of such environment variables?

No, but I know that I do not know. I recommend noting the (small) potential for breakage in the changelog. In a large body of software, subtle features are often depended on somewhere, be it inadvertently.

Signature · 1 line
Jilles Tjoelker