1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/cmd/ksh93/include
Martijn Dekker b48e5b3365 Fix arbitrary command execution vuln in array subscripts in arith
This commit fixes an arbitrary command execution vulnerability in
array subscripts used within the arithmetic subsystem.

One of the possible reproducers is:
	var='1$(echo INJECTION >&2)' ksh -c \
		'typeset -A a; ((a[$var]++)); typeset -p a'

Output before this commit:
	INJECTION
	typeset -A a=([1]=1)
The 'echo' command has been surreptitiously executed from an
external environment variable.

Output after this commit:
	typeset -A a=(['1$(echo INJECTION >&2)']=1)
The value is correctly used as an array subscript and nothing in it
is parsed or executed. This is as it should be, as ksh93 supports
arbitrary subscripts for associative arrays.

If we think about it logically, the C-style arithmetic subsystem
simply has no business messing around with shell expansions or
quoting at all, because those don't belong to it. Shell expansions
and quotes are properly resolved by the main shell language before
the arithmetic subsystem is even invoked. It is particularly
important to maintain that separation because the shell expansion
mechanism also executes command substitutions.

Yet, the arithmetic subsystem subjected array subscripts that
contain `$` (and only array subscripts -- how oddly specific) to
an additional level of expansion and quote resolution. For some
unfathomable reason, there are two lines of code doing specifically
this. The vulnerability is fixed by simply removing those.

Incredibly, variants of this vulnerability are shared by bash, mksh
and zsh. Instead of fixing it, it got listed in Bash Pitfalls!
http://mywiki.wooledge.org/BashPitfalls#y.3D.24.28.28_array.5B.24x.5D_.29.29

src/cmd/ksh93/sh/arith.c:
- scope(): Remove these two lines that implement the vulnerability.
			if(strchr(sub,'$'))
				sub = sh_mactrim(shp,sub,0);
- scope(), arith(): Remove the NV_SUBQUOTE flag from two
  nv_endsubscript() calls. That flag causes the array subscript to
  retain the current level of shell quoting. The shell quotes
  everything as in "double quotes" before invoking the arithmetic
  subsystem, and the bad sh_mactrim() call removed one level of
  quoting. Since we're no longer doing that, this flag should no
  longer be passed, or subscripts may get extra backslash escapes.

src/cmd/ksh93/include/name.h,
src/cmd/ksh93/sh/array.c:
- nv_endsubscript(): The NV_SUBQUOTE flag was only passed from
  arith.c. Since it is now unused, remove it.

src/cmd/ksh93/tests/arith.sh:
- Tweak some tests: fix typos, report wrong values.
- Add 21 tests. Most are based on reproducers contributed by
  @stephane-chazelas and @hyenias. They verify that this
  vulnerability is gone and that no quoting bugs were introduced.

Resolves: https://github.com/ksh93/ksh/issues/152
2021-03-04 13:37:13 +00:00
..
argnod.h Fix bugs related to --posix shell option (re: 921bbcae, f45a0f16) 2021-02-14 23:51:19 +00:00
builtins.h Remove SHOPT_BASH; keep &> redir operator, '-o posix' option 2020-09-01 06:19:19 +01:00
defs.h Add more out of memory checks (re: 18529b88) (#192) 2021-02-27 21:21:58 +00:00
edit.h editors: fix broken SIGWINCH handling 2021-02-22 00:11:59 +00:00
fault.h Remove legacy code for older libast versions 2020-09-04 02:31:39 +02:00
fcin.h Version: 2012-08-01-master 2016-01-11 15:54:23 -05:00
history.h Version: 2012-08-01-master 2016-01-11 15:54:23 -05:00
io.h Fix: Closing a FD within a comsub broke output (rhbz#1116072) 2020-09-27 04:46:24 +02:00
jobs.h Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
lexstates.h Fix many spelling errors and word repetitions (#188) 2021-02-20 03:22:24 +00:00
name.h Fix arbitrary command execution vuln in array subscripts in arith 2021-03-04 13:37:13 +00:00
national.h Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
nval.h Fix floating point numerics having precision of 0 with assignments (#149) 2020-11-26 13:50:30 +00:00
path.h Fix bugs related to --posix shell option (re: 921bbcae, f45a0f16) 2021-02-14 23:51:19 +00:00
regress.h Version: 2012-08-01-master 2016-01-11 15:54:23 -05:00
shell.h Fix many compiler warnings and remove unused variables (#191) 2021-02-22 22:16:32 +00:00
shlex.h Remove SHOPT_BASH; keep &> redir operator, '-o posix' option 2020-09-01 06:19:19 +01:00
shnodes.h Version: 2012-08-01-master 2016-01-11 15:54:23 -05:00
shtable.h tests/options.sh: add forgotten SHOPT_BRACEPAT check (re: af5f7acf) 2021-02-15 01:57:17 +00:00
streval.h Remove legacy code for older libast versions 2020-09-04 02:31:39 +02:00
terminal.h Fixes for implicit declaration warnings 2020-06-14 09:55:08 -04:00
test.h restore 'test --man --' oddness (re: fa6a180f) 2020-08-31 23:43:22 +01:00
timeout.h Version: 2012-08-01-master 2016-01-11 15:54:23 -05:00
ulimit.h sh_setmatch(): fix node size calculation 2021-02-02 11:52:54 +00:00
variables.h Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
version.h Fix arbitrary command execution vuln in array subscripts in arith 2021-03-04 13:37:13 +00:00