History log of /freebsd-10-stable/lib/libc/gen/wordexp.3
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# 289938 25-Oct-2015 jilles

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)


# 256281 10-Oct-2013 gjb

Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.

Approved by: re (implicit)
Sponsored by: The FreeBSD Foundation

# 157852 18-Apr-2006 ceri

Correct usage example.

PR: docs/95059
Submitted by: Jos Backus
MFC after: 4 days


# 132798 28-Jul-2004 tjr

Document incorrect handling of multibyte characters due to limitations
of sh(1).


# 131331 30-Jun-2004 tjr

Fix typo: WRDE_DOOFS -> WRDE_DOOFFS.

Noticed by: Stoned Elipot


# 119893 08-Sep-2003 ru

mdoc(7): Use the new feature of the .In macro.


# 115631 01-Jun-2003 ru

Assorted mdoc(7) fixes.


# 112190 13-Mar-2003 tjr

Document return type of wordfree() (void). Reduce the space between
struct member names and the corresponding comments so the lines don't
wrap on 80-column terminals.


# 108597 03-Jan-2003 tjr

It is no longer necessary to include sys/types.h before wordexp.h.


# 108310 27-Dec-2002 tjr

Fix a small typo.


# 108288 26-Dec-2002 tjr

Add an implementation of the POSIX wordexp() and wordfree() functions,
which perform shell-style word expansion on strings. This is still a
little rough around the edges.

PR: 13420