]> sipb.mit.edu Git - wiki.git/commitdiff
(no commit message)
authorAlexander W Dehnert <adehnert@mit.edu>
Fri, 5 Apr 2013 05:51:13 +0000 (01:51 -0400)
committersipb-www <sipb-www@lucky-star.mit.edu>
Fri, 5 Apr 2013 05:51:13 +0000 (01:51 -0400)
doc/safe-shell.mdwn

index 7b8f60114a3a8c1d31d64a9138d8451fbb67fa60..4913abfd081af90f05c95e192a38c212237cf686 100644 (file)
@@ -101,6 +101,16 @@ manual](http://www.gnu.org/software/bash/manual/html_node/Special-Parameters.htm
 for details on the distinction between `$*`, `$@`, and `"$@"` &mdash; the first
 and second are rarely what you want in a safe shell script.
 
+## Passing filenames or other positional arguments to commands
+
+If you get filenames from the user or from shell globbing, or any other kind of positional arguments, you should be aware that those could start with a "-". Even if you quote correctly, this may still act differently from what you intended. For example, consider a script that allows somebody to run commands as `nobody` (exposed over `remctl`, perhaps), consisting of just `sudo -u nobody "$@"`. The quoting is fine, but if a user passes `-u root reboot`, `sudo` will catch the second `-u` and run it as `root`.
+
+Fixing this depends on what command you're running.
+
+For many, however, `--` is accepted to indicate that any options are done, and future arguments should be parsed as positional parameters --- even if they look like options. In the `sudo` example above, `sudo -u nobody -- "$@"` would avoid this attack (though obviously limiting this in the `sudo` configuration should be done as well).
+
+Another approach is to prefix each filename with `./`, if the filenames are expected to be in the current directory.
+
 ## Temporary files
 
 TODO: mumble `mktemp`?