views:

988

answers:

5

How can one detect being in a chroot jail without root privileges? Assume a standard BSD or Linux system. The best I came up with was to look at the inode value for "/" and to consider whether it is reasonably low, but I would like a more accurate method for detection.

[edit 20080916 142430 EST] Simply looking around the filesystem isn't sufficient, as it's not difficult to duplicate things like /boot and /dev to fool the jailed user.

[edit 20080916 142950 EST] For Linux systems, checking for unexpected values within /proc is reasonable, but what about systems that don't support /proc in the first place?

A: 
test -x /sbin/init

Should return '0', for there's no reason for 'init' to exist in a chroot jail, unless of course the chroot jail is a single user mode emergency recovery situation. But in that case, I think you'd know whether you're in a chroot environment.

jtimberman
My chroot jail has /sbin/init for monkeys like you.
Joshua
+1  A: 

Preventing stuff like that is the whole point. If it's your code that's supposed to run in the chroot, have it set a flag on startup. If you're hacking, hack: check for several common things in known locations, count the files in /etc, something in /dev.

sammyo
+1  A: 

I guess it depends why you might be in a chroot, and whether any effort has gone into disguising it.

I'd check /proc, these files are automatically generated system information files. The kernel will populate these in the root filesystem, but it's possible that they don't exist in the chroot filesystem.

If the root filesystem's /proc has been bound to /proc in the chroot, then it is likely that there are some discrepancies between that information and the chroot environment. Check /proc/mounts for example.

Similrarly, check /sys.

SpoonMeiser
While it's easy to bind /proc, discrepancies within that data would be cumbersome to mask. Answer accepted.
Topaz
Actually, scratch that - what about non-linux systems that don't have /proc and friends to begin with?
Topaz
The question says to assume a standard Linux or BSD system. To the best of my knowledge, both have /proc.
SpoonMeiser
I'm staring at a BSD system right now, no /proc here.
Topaz
+4  A: 

The inode for / will always be 2 if it's the root directory of a filesystem, but you may be chrooted inside a complete filesystem. If it's just chroot (and not some other virtualization), you could run mount and compare the mounted filesystems against what you see. Verify that every mount point has inode 2.

Out of curiosity, what gets inode 1?
Topaz
bad blocks (historical/legacy)
A: 

On BSD systems (check with uname -a), proc should always be present. Check if the dev/inode pair of /proc/1/exe (use stat on that path, it won't follow the symlink by text but by the underlying hook) matches /sbin/init.

Checking the root for inode #2 is also a good one.

On most other systems, a root user can find out much faster by attempting the fchdir root-breaking trick. If it goes anywhere you are in a chroot jail.

Joshua