]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/commit
MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable.
authorjilles <jilles@ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f>
Sun, 25 Oct 2015 17:17:50 +0000 (17:17 +0000)
committerjilles <jilles@ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f>
Sun, 25 Oct 2015 17:17:50 +0000 (17:17 +0000)
commit65a5d64955a8d0f7bae65d4267106cc88bcac0e7
tree549fa8604538046131b5b363cc06210ecaa0270b
parentd2a020590a9d4e32db98e9fd281eb2d63c6fc897
MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable.

Shell syntax is too complicated to detect command substitution and unquoted
operators reliably without implementing much of sh's parser. Therefore, have
sh do this detection.

While changing sh's support anyway, also read input from a pipe instead of
arguments to avoid {ARG_MAX} limits and improve privacy, and output count
and length using 16 instead of 8 digits.

The basic concept is:
execl("/bin/sh", "sh", "-c", "freebsd_wordexp ${1:+\"$1\"} -f "$2",
    "", flags & WRDE_NOCMD ? "-p" : "", <pipe with words>);

The WRDE_BADCHAR error is still implemented in libc. POSIX requires us to
fail strings containing unquoted braces with code WRDE_BADCHAR. Since this
is normally not a syntax error in sh, there is still a need for checking
code in libc, we_check().

The new we_check() is an optimistic check that all the characters
  <newline> | & ; < > ( ) { }
are quoted. To avoid duplicating too much sh logic, such characters are
permitted when quoting characters are seen, even if the quoting characters
may themselves be quoted. This code reports all WRDE_BADCHAR errors; bad
characters that get past it and are a syntax error in sh return WRDE_SYNTAX.

Although many implementations of WRDE_NOCMD erroneously allow some command
substitutions (and ours even documented this), there appears to be code that
relies on its security (codesearch.debian.net shows quite a few uses).
Passing untrusted data to wordexp() still exposes a denial of service
possibility and a fairly large attack surface.

This is also a MFC of r286830 to reduce conflicts. I changed the code
somewhat to avoid changes from r286941; in particular, WRDE_BADVAL can still
only be returned if WRDE_UNDEF was passed.

Relnotes: yes
Security: fixes command execution with wordexp(untrusted, WRDE_NOCMD)

git-svn-id: svn://svn.freebsd.org/base/stable/10@289938 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f
bin/sh/builtins.def
bin/sh/expand.c
bin/sh/parser.c
bin/sh/parser.h
lib/libc/gen/wordexp.3
lib/libc/gen/wordexp.c