freebsd-skq/bin/sh
jilles aff97c8f1f 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)
2015-10-25 17:17:50 +00:00
..
bltin sh: Fix duplicate prototypes for builtins. 2011-06-13 21:03:27 +00:00
funcs sh: Remove "kill" example function, which is superseded by the kill builtin 2012-01-21 20:39:17 +00:00
tests MFC r268436,273243,278806,280920,286971: sh: Add various tests. 2015-09-13 14:24:12 +00:00
alias.c MFC r284779: sh: Fix some arithmetic undefined behaviour. 2015-08-15 19:58:00 +00:00
alias.h sh: Fix duplicate prototypes for builtins. 2011-06-13 21:03:27 +00:00
arith_yacc.c Add prototypes, ANSIfy functions definitions to reduce WARNS=6 output. 2012-01-25 08:42:19 +00:00
arith_yacc.h sh: Import arithmetic expression code from dash. 2011-02-08 23:18:06 +00:00
arith_yylex.c sh: Reject ++ and -- in arithmetic. 2013-08-24 20:06:00 +00:00
arith.h sh: Fix duplicate prototypes for builtins. 2011-06-13 21:03:27 +00:00
builtins.def MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable. 2015-10-25 17:17:50 +00:00
cd.c Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
cd.h sh: Fix duplicate prototypes for builtins. 2011-06-13 21:03:27 +00:00
error.c sh: Add a function to print warnings (with command name and newline). 2010-12-21 20:47:06 +00:00
error.h sh: Allow EV_EXIT through function calls, make {...} <redir more consistent. 2011-04-23 22:28:56 +00:00
eval.c MFC r272575: sh: Eliminate some gotos. 2015-09-13 13:43:08 +00:00
eval.h sh: Make return return from the closest function or dot script. 2013-09-04 22:10:16 +00:00
exec.c MFC r284779: sh: Fix some arithmetic undefined behaviour. 2015-08-15 19:58:00 +00:00
exec.h sh: Expand assignment-like words specially for export/readonly/local. 2012-07-15 10:19:43 +00:00
expand.c MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable. 2015-10-25 17:17:50 +00:00
expand.h MFC r276037: sh: Remove EXP_REDIR. 2014-12-29 15:33:20 +00:00
histedit.c sh: Prefer internal nextopt() to libc getopt(). 2012-09-15 21:56:30 +00:00
input.c Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
input.h sh: Remove mkinit. 2013-07-25 15:08:41 +00:00
jobs.c MFC r272575: sh: Eliminate some gotos. 2015-09-13 13:43:08 +00:00
jobs.h sh: Fix race condition with signals and wait or set -T. 2013-09-02 21:57:46 +00:00
mail.c Consistently use "STATIC" for all functions in order to be able to set 2010-10-13 04:01:01 +00:00
mail.h
main.c sh: Make return return from the closest function or dot script. 2013-09-04 22:10:16 +00:00
main.h sh: Make various functions static. 2012-01-01 22:17:12 +00:00
Makefile Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
memalloc.c Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
memalloc.h sh: Remove linked list of stack marks. 2013-05-11 20:51:00 +00:00
miscbltin.c sh: Improve error handling in read builtin: 2013-05-03 15:28:31 +00:00
mkbuiltins Work better with how make/bmake works: 2012-05-24 19:48:15 +00:00
mknodes.c sh: Fix crash with empty functions (f() { }) introduced in r196483 2009-08-28 22:41:25 +00:00
mksyntax.c sh: Simplify mksyntax and make it fit for cross-compiling. 2013-02-07 22:42:33 +00:00
mktokens sh: Add case statement fallthrough (with ';&' instead of ';;'). 2011-06-17 13:03:49 +00:00
myhistedit.h sh: Make various functions static. 2012-01-01 22:17:12 +00:00
mystring.c Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
mystring.h Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
nodes.c.pat sh: Add const to nodesavestr(). 2013-04-07 16:28:36 +00:00
nodetypes Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
options.c sh: Remove XXX comment about removing nextopt(). 2012-09-08 19:24:03 +00:00
options.h sh: Add do-nothing -h option. 2011-06-18 23:43:28 +00:00
output.c sh: Remove output.c's reset() handler. 2013-07-25 13:09:17 +00:00
output.h sh: Detect and flag write errors on stdout in builtins. 2012-12-12 22:01:10 +00:00
parser.c MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable. 2015-10-25 17:17:50 +00:00
parser.h MFC r288430: wordexp: Rewrite to make WRDE_NOCMD reliable. 2015-10-25 17:17:50 +00:00
redir.c MFC r272575: sh: Eliminate some gotos. 2015-09-13 13:43:08 +00:00
redir.h sh: Remove unnecessary reset functions. 2013-08-16 20:24:41 +00:00
sh.1 MFC r274254: sh(1): Mention portability issue with shifting zero positional 2015-01-04 19:02:50 +00:00
shell.h sh: Remove #define MKINIT. 2013-07-25 19:48:15 +00:00
show.c Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
show.h sh: Constify various strings. 2009-12-24 18:41:14 +00:00
TOUR sh: Remove mkinit. 2013-07-25 15:08:41 +00:00
trap.c MFC r272575: sh: Eliminate some gotos. 2015-09-13 13:43:08 +00:00
trap.h Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00
var.c MFC r263847: sh: Fix memory leak with an assignment before a regular builtin 2014-04-15 18:29:05 +00:00
var.h Sync sh(1) in stable/10 to head. 2014-03-09 17:04:31 +00:00