The `sudo chroot /chroot su - user sh -c "cmd args"` trick
catonmat.net
catonmat.net
This "trick" is absolutely trivial. What's next, the trick about using grep to search through files? The author's lack of competence is evident due to the improper way of using su, the extremely weird idiom of sourcing the profile where it is not required and representing su as being useful for running untrusted binaries.
Simulating a login is usually not what you want. Sometimes it is, most often it is not. The author simulates a login but does not tell you why would you want to do it.
Su takes arguments that can be passed to the started shell. In particular -c can be passed to the original shell started bu su[1]; no need to start a second shell. This is not used here. Starting two shells instead of one is hardly a crime (I always do cat file | grep something instead of the more effective grep something file) but misrepresenting how su works is. In particular su does not work like "su - user cmd", as he claims, su works like "su [login [args]]" where args are passed to the started shell. The author does not understand su therefore he is not capable of writing an article about it.
The fact that he just simulated a full login only to run a non-interactive shell that manually sources .profile is hilarious, but the fact that he says chroots are good for running untrusted code is dangerously misleading.
[1] Interestingly GNU su takes -c by itself rather than passing it to the shell, but the effect of su user -c cmd is the same in BSD and Linux, although for different reasons.
Peteris is a solid HN contributor with 216 submissions to date, many of which focus on math or programming or shell tricks, which puts him head and shoulders above those that submit the daily sewage from techcrunch.com (or those, like me and you, that don't submit at all).
Here's a quick sample of some of his top HN submissions:
1. Announcing his business, making virtual machines accessible over the web (and open-sourcing the code behind it): http://news.ycombinator.com/item?id=1534973
2. An introduction to pipe viewer: http://news.ycombinator.com/item?id=462244
3. Top Ten One-Liners from CommandLineFu Explained: http://news.ycombinator.com/item?id=1200900
4. Low-level bit hacks: http://news.ycombinator.com/item?id=1811104
5. Using Fibonacci Numbers to Convert from Miles to Kilometers and Vice Versa: http://news.ycombinator.com/item?id=1050151
6. How I went to Silicon Valley and raised $55,000 for Browserling: http://news.ycombinator.com/item?id=2808314
7. How to steal a botnet: http://news.ycombinator.com/item?id=1072225
etc.
And, his submissions often provoke even greater discussion in comment threads, even when they contain a mistake (as in http://news.ycombinator.com/item?id=3684515).
Peteris is legit (and a really nice guy besides). No matter how much you know, it's likely that he's written about something you don't know. He has earned enough respect from the HN community that he deserves to have corrections to his articles submitted gracefully and with tact, not with accusations of incompetence.
I found sourcing the profile trick useful in case the chrooted users need a customized environment such as custom PATHs or something else user specific.
http://unix.stackexchange.com/questions/38175/difference-bet...
http://serverfault.com/questions/8882/what-is-the-difference...
1. This requires a pretty complete system environment to be set up in the /chroot directory: a shell, /etc/passwd, su, and all the libraries needed to make the program you're running work. In my experience this is the hard part.
2. chroot only isolates filesystem access but an untrusted executable might make use of your networking, for example. (Furthermore, if you're not careful an unprivileged user can become root inside the jail, and root can trivially escape chroot jails.)
Edit: I suppose the article is really about command composition and not chroot jails, but I think that a better example wouldn't involve a lot of prerequisite work.
[1] http://wiki.debian.org/Debootstrap [2] https://wiki.ubuntu.com/DebootstrapChroot
> This is great way to execute unknown executables and code in a safe environment!
LOL wut? No. No it is not a great nor a safe way to "execute unknown executables and code". Chroots are so very inadequate for this.
I agree with agwa. I only use chroot for building live systems, not "security". And I use su and sh -c sometimes for overcoming braindead attempts at security on Linux distributions.
But I have always thought that if you were going to use chroot as some sort of security mechanism ("jails"), then you have to strip all the unneeded functionality out of the environment. It must all be removed from the system. If there is a program on the system that can do something you don't want the user to do, then either remove that program or edit the code to remove that functionality (e.g. a reduced functionality shell, etc.) and recompile.
Imagine the user uses SSH to login to a machine that only has a crippled shell (you have edited the code to remove undesired functionality) and a couple of other crippled programs that can't do much of anything. These might be combined into the same binary, similar to busybox. Any functionality that you don't want the user to have simply does not exist omnn the system. That would be my idea of "security".
Second, "This is great way to execute unknown executables and code in a safe environment!" is nonsense that I can't believe I'm still seeing in 2012. (No, schroot does not do this either.)
Why is that?
Also, if all he wants is to run a command, why not use sudo? Isn't that what sudo is supposed to do (i.e. pretty much what su -c does)?
Because he just did a login:
silver:aram$ sudo su aram -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
silver:aram$ sudo su - aram -c 'echo $PATH'
.:/home/aram/bin:/home/aram/bin/linux:/home/aram/bin/linux/amd64:/home/aram/bin/linux/386:/home/aram/go/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/games:/usr/local/bin:/usr/local/sbin:/home/aram/plan9/bin
silver:aram$ sudo su - aram -c '. ~/.profile; echo $PATH'
.:/home/aram/bin:/home/aram/bin/linux:/home/aram/bin/linux/amd64:/home/aram/bin/linux/386:/home/aram/go/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/games:/usr/local/bin:/usr/local/sbin:/home/aram/plan9/bin
> why not use sudo? Isn't that what sudo is supposed to do (i.e. pretty much what su -c does)?Sure, he could have used sudo.
It makes it awful easy to execute something you forgot/didn't know was in the current working directory.
UPenn gives a pretty good explanation here: http://www.seas.upenn.edu/cets/answers/dot-path.html
The security issue is most relevant on multi-user systems, including hosted services (e.g. a web server with an upload feature and misconfigured umask).
I don't do it on my personal system either because I am my own worst enemy and am frequently saved by the Unix "rule of least surprise."
It's the usual convenience/risk balance. To each his own.