When I open up dmenu and start typing out the program I want to open it provides some autocomplete suggestions based on the programs in my PATH.
I realised that there are hundreds of these, most of which I have never heard of before in my life. I realised that without a concerted effort I'd actually never end up learning anything about most of them - so I wrote a quick one liner to choose a random utility from my PATH and open up the man page:
$ (for folder in `echo $PATH | sed "s/:/\\n/g"`; do ls -1 $folder; done; ) | shuf -n 1 | xargs manHow this works is basically splitting my PATH into new lines using sed, then listing the contents using one calling ls -1 and selecting one from the whole lot using shuf and attempting to open its manpage using man.
The first one I picked up was ...
However that's doesn't exactly help us understand the whole picture. So let's start by asking "what is a chroot"? Well say we have a system that has a pretty straightforward set of folders in its root directory like the following
$ echo $PATH /usr/bin:/bin $ ls / bin boot dev etc home initrd.img lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var vmlinuz
So when we run a command like 'ls' it'll look for the utility in the two folders in our PATH (/bin and /usr/bin), then execute it on root directory of our file system. If we want however we can do a little trickery, and use the utility chroot so that when ls runs it sees an entirely different root altogether.
To try this we'll need to prepare our fake root directory:
$ sudo mkdir /opt/fakeroot $ sudo debootstrap --arch i386 jessie /opt/fakeroot http://httpredir.debian.org/debian I: Retrieving Release I: Retrieving Release.gpg I: Checking Release signature I: Valid Release signature (key id 75DDC3C4A499F1A18CB5F3C8CBF8D6FD518E17E1) ... many lines later ... I: Configuring tasksel... I: Configuring tasksel-data... I: Configuring libc-bin... I: Configuring systemd... I: Base system installed successfully. $ ls / bin boot dev etc home initrd.img lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var vmlinuz $ ls /opt/fakeroot/ bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
OK so we now have a similar looking set of folders in /opt/fakeroot that we'd have in a freshly built Debian Jessie system - however we can run ls / using the chroot command so that it sees /opt/fakeroot as its root directory:
$ sudo chroot /opt/fakeroot ls / bin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
OK nice, I can trick a process I've launched into thinking that any directory is root. However if I am launching a process like ls using chroot then surely I have no need for a tool like ischroot? Well the painful truth is that you may be running in a chroot "jail" without even knowing it. Some crafty (or security conscious) sysadmin may have set the system up so that, for example, when you connect via SSH your session is run inside a chroot. So assuming you've been entrusted with sudo priviledges, you can run ischroot and you'll be able to find out whether you are or not.
So just to replay the man page information, the return value of running ischroot should indicate whether we're running inside a chroot or not - the possible return values are:
|running in a chroot
|not running in a chroot
So to test this out, let's run ischroot outside a chroot jail ...
root@hanoi:/opt# ischroot root@hanoi:/opt# echo $? 1
Cool, so that is exactly as expected. So if we switch over to our newly created chroot and run the same command...
root@hanoi:/opt# sudo chroot /opt/fakeroot root@hanoi:/# ischroot root@hanoi:/# echo $? 2
Ah, so instead of returning 1 we got 2 - indicating an error. After a bit of messing around it turns out we have just hit debian bug #685034 -" debianutils: ischroot fails to detect if it is running in a chroot". However we can determine if we definitely are not running in a chroot, but we have a bit of difficulty determining whether or not we are - a return value of 2 could be thought of as a "maybe". Since we don't like "maybe" in some cases, we can force ischroot to interpret "maybe" as "yes" using the -t flag:
root@hanoi:/# ischroot -t root@hanoi:/# echo $? 0
So not exactly a great start! However I am glad I learned about creating a chroot jail, even if ischroot isn't working as expected. Hopefully the next utility I pick up works a little better (and involves a smaller writeup).