In someone's reply to one of my posts (which I forgot), I remember
bash -c "somecommand \$1" bash $somevariable
instead of
bash -c "somecommand $somevariable"
I saw this example again in findutils manual
find -exec sh -c 'something "$@"' sh {} \;
instead of
find -exec sh -c "something {}" \;
Do the two examples have the same reason to use the first solution instead of the other solution? If yes, what is it?
Inspired Why does command injection not work in this example? and Is the following the only form of command injection in bash?
– Tim Kennedy Jun 07 '18 at 15:26The reason for this is that the ‘{}’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.
{}
anywhere in the strings, and according to documentation, so do thefind
s on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of thefind
s in use. – ilkkachu Jun 07 '18 at 19:10find
implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion. – Stéphane Chazelas Jun 08 '18 at 14:59uname -v
anduname -r
. HP-UX is also not really AT&T based. Today one of the most implortant implementations is libfind that is used by many programs in order to implement afind(1)
compatible CLI interface inside programs. – schily Jun 08 '18 at 15:12