Ways to provide arguments to a command executed by `bash -c`

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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?
bash command
 |Â
show 1 more comment
up vote
1
down vote
favorite
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?
bash command
2
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
@schily, depends on what you mean with "usually". GNU find replacesanywhere in the strings, and according to documentation, so do thefinds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of thefinds in use.
â ilkkachu
Jun 7 at 19:10
1
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
1
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of thefindimplementations 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 8 at 14:59
 |Â
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
bash command
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?
bash command
edited Jun 8 at 13:43
asked Jun 7 at 15:15
Tim
22.5k61222401
22.5k61222401
2
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
@schily, depends on what you mean with "usually". GNU find replacesanywhere in the strings, and according to documentation, so do thefinds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of thefinds in use.
â ilkkachu
Jun 7 at 19:10
1
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
1
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of thefindimplementations 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 8 at 14:59
 |Â
show 1 more comment
2
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
@schily, depends on what you mean with "usually". GNU find replacesanywhere in the strings, and according to documentation, so do thefinds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of thefinds in use.
â ilkkachu
Jun 7 at 19:10
1
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
1
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of thefindimplementations 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 8 at 14:59
2
2
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
@schily, depends on what you mean with "usually". GNU find replaces
anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.â ilkkachu
Jun 7 at 19:10
@schily, depends on what you mean with "usually". GNU find replaces
anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.â ilkkachu
Jun 7 at 19:10
1
1
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
1
1
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the
find 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 8 at 14:59
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the
find 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 8 at 14:59
 |Â
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448453%2fways-to-provide-arguments-to-a-command-executed-by-bash-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The 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.`
â Tim Kennedy
Jun 7 at 15:26
The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
â schily
Jun 7 at 15:52
@schily, depends on what you mean with "usually". GNU find replaces
anywhere in the strings, and according to documentation, so do thefinds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of thefinds in use.â ilkkachu
Jun 7 at 19:10
1
AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
â schily
Jun 7 at 19:22
1
@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the
findimplementations 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 8 at 14:59