dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Bug in Dash's unquoting of backslashes within backquoted strings
@ 2020-05-21 20:06 Matt Whitlock
  2020-05-21 21:38 ` Ron Yorston
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Whitlock @ 2020-05-21 20:06 UTC (permalink / raw)
  To: dash

A minimal example:

: `: "\\\\
\\$(bug)"`

That's one command, containing a newline character, embedded in a 
double-quoted string, embedded in a backquoted string. The command is an 
invocation of the : (colon) built-in, passing arguments obtained by word 
splitting the result of the backquoted command substitution.

The Open Group says: "Within the backquoted style of command substitution, 
backslash shall retain its literal meaning, except when followed by: '$', 
'`', or '\' (dollar sign, backquote, backslash)." Thus, each pair of 
backslashes between the backquotes is to be reduced to a single backslash.

Thus, the subcommand to be executed is:

: "\\
\$(bug)"

This subcommand is an invocation of the : (colon) built-in command, passing 
a single argument obtained by performing quote removal on the double-quoted 
string. After quote removal, the resulting argument consists of these eight 
characters: backslash, newline, dollar sign, open parenthesis, b, u, g, 
close parenthesis.

If the above subcommand is entered directly at the Dash command line, all 
is well. However, when it appears inside a backquoted subcommand (with the 
backslash characters being appropriately escaped), such as given at the top 
of this report, then Dash processes it incorrectly:

/bin/sh: 1: bug: not found

Dash apparently skips over the backslash immediately following the newline 
embedded in the double-quoted string. Thus, Dash sees the dollar sign as 
introducing a command substitution rather than as a literal character.

This does not happen if the backslashes preceding the embedded newline are 
absent. It also does not happen if any other character, including linear 
whitespace, is inserted immediately after the embedded newline.

Both Bash and Busybox Ash get it right. Dash is non-conformant.

This bug has potentially grave security implications since Dash is 
interpreting a string literal as though it were a command and executing it. 
If the word "bug" in my examples above had been "rm -rf /" instead, the 
results would have been catastrophic.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-05-26 13:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-21 20:06 Bug in Dash's unquoting of backslashes within backquoted strings Matt Whitlock
2020-05-21 21:38 ` Ron Yorston
2020-05-21 21:57   ` Ron Yorston
2020-05-26 13:19     ` parser: Fix double-backslash nl in old-style command sub Herbert Xu
2020-05-21 22:02   ` Bug in Dash's unquoting of backslashes within backquoted strings Harald van Dijk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).