Live data from Hacker News

TIL: Some surprising code execution sources in bash

yossarian.net

31–40 of 51 posts

Re: TIL: Some surprising code execution sources in bash

#31

So many footguns in bash. When do we finally get serious about ditching this language as an industry in the same way we are about memory safety?

Oh no!!! But what about all those Docker files, and k8 clusters!!!

Serious, please view the curled file from a link before piping it to bash/sh.

Re: TIL: Some surprising code execution sources in bash

#32
Question: why does the evaluation inside a[] (which does not produce a value) not result in a bad array subscript error in this case?

if you try to evaluate this kind of things as an arithmetic expression directly, it will fail with an error of a bad subscript (mind you, the attack will still work though).

Re: TIL: Some surprising code execution sources in bash

#33

So many footguns in bash. When do we finally get serious about ditching this language as an industry in the same way we are about memory safety?

You underestimate the effort of doing this.

We all want bash gone, but it is an essential piece of infrastructure. The introduction of dash was a huge step in this direction (of ditching bash).

Do you want to help? Try to remove bash from the toolchain bootstrap. It is one of the lowest hanging fruits right now.

Re: TIL: Some surprising code execution sources in bash

#34
post #17

Earlier quoted context omitted.

The first function one is not particularly well-written, but harmless. The quoting of ${num} is completely useless. Inside [[ bash does not do any word splitting after variable expansion. Double quotes never prevent variable expansion. I am not sure what the author is talking about. Shellcheck is correct to not complain. I stopped reading there.

> Double quotes never prevent variable expansion. I am not sure what the author is talking about. Shellcheck is correct to not complain. I stopped reading there. I think it would behoove you to read the rest of the post. The double quotes are not the operative part of example there; they're only there to demonstrate that the code execution doesn't come from splatting or word splitting. The actual code execution in Ca…

So -eq triggers evaluation? Sounds like typical bash magic. I would use [ an the problem goes away.

Showing -eq is not the best example, it can just be replaced by = and the problem goes away.

But if you need -gt or similar there is no replacement. So one should stick to [.

If I follow correctly the dangerous combination is [[ and arithmetic comparisons?

Re: TIL: Some surprising code execution sources in bash

#35
post #2

My first insinct would be to remove the bashisms first: https://gist.github.com/alganet/a4198158651f3b2dc43ce658052e... Then, if we run it: "line 3: test: a[$(cat /etc/passwd > /tmp/pwned)] + 42: integer expression expected"

(Author of the post.) Yep, this is specifically a bashism (by way of being a kshism). However, it's worth noting that the second variant (`type -v`) will work in `[` and `test`. (It's also a still a bashism, but IME people don't realize how little of `type` is actually POSIX.)

You are defining a function and then you use it interactively. That does not demonstrate that bash scripting is dangerous. Can you demonstrate the problem in a script?

Yes, you can do dangerous things in bash scripts. This might be one of them. Not at my computer now and no time to experiment.

Re: TIL: Some surprising code execution sources in bash

#36

So many footguns in bash. When do we finally get serious about ditching this language as an industry in the same way we are about memory safety?

Curious what you use instead of bash? When you spin up a server somewhere, what's the first thing you like to install that replaces what we typically use bash for?

Re: TIL: Some surprising code execution sources in bash

#37

Question: why does the evaluation inside a[] (which does not produce a value) not result in a bad array subscript error in this case? if you try to evaluate this kind of things as an arithmetic expression directly, it will fail with an error of a bad subscript (mind you, the attack will still work though).

[deleted]

Re: TIL: Some surprising code execution sources in bash

#38
post #34

Earlier quoted context omitted.

> Double quotes never prevent variable expansion. I am not sure what the author is talking about. Shellcheck is correct to not complain. I stopped reading there. I think it would behoove you to read the rest of the post. The double quotes are not the operative part of example there; they're only there to demonstrate that the code execution doesn't come from splatting or word splitting. The actual code execution in Ca…

So -eq triggers evaluation? Sounds like typical bash magic. I would use [ an the problem goes away. Showing -eq is not the best example, it can just be replaced by = and the problem goes away. But if you need -gt or similar there is no replacement. So one should stick to [. If I follow correctly the dangerous combination is [[ and arithmetic comparisons?

`-eq` is for arithmetic comparison; `=` is for string comparison. They don't do the same thing, and it's unsound to uniformly replace either with the other.

The dangerous thing here is that an undefined number of contexts exist where Bash treats strings as arithmetic expressions, which can contain arbitrary code despite not being quoted for expansion. `-eq` is just one example of that; others have linked other examples.

(This is all for case #1. With case #2, `[` and `test` are also susceptible so long as their builtin variants are used.)

Re: TIL: Some surprising code execution sources in bash

#39

So many footguns in bash. When do we finally get serious about ditching this language as an industry in the same way we are about memory safety?

Curious what you use instead of bash? When you spin up a server somewhere, what's the first thing you like to install that replaces what we typically use bash for?

Do these apply to NuShell? I think something like that is the way forward. Something with real data types rather than implicitly doing weird array processing. I would be pretty happy with something similar to Python but with easier IO redirection and subprocess management.

xonsh is neat in principle, but painful in actual usage ime. And I suspect vulnerable to similar issues around the Python-bash interop.

Re: TIL: Some surprising code execution sources in bash

#40

Honestly I just don't write shell scripts anymore, bash or otherwise. By the time any system I use is up, Python is available. I don't know if I've found a true need for shell in anything application level. I'll even fire up a Python shell for something simple like mass renaming files, simply because the string manipulation is so much easier.

Or, use the One True Shell[1]: https://vivekhaldar.com/articles/emacs-as-a-login-shell/

As a less joke but also more joke: https://www.gnu.org/software/emacs/manual/html_mono/eshell.h...

1: it was going to be much funnier if I could have found the link where someone used emacs as Xsession or similar but this one will do

Post reply on HN