<p>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.</p>

<p><code>[edit 20080916 142430 EST]</code> 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.</p>

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

[{'answer_id': 75310, 'author': 'sammyo', 'author_id': 10826, 'author_profile': 'https://Stackoverflow.com/users/10826', 'pm_score': 2, 'selected': False, 'text': "<p>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. </p>\n"}, {'answer_id': 75390, 'author': 'SpoonMeiser', 'author_id': 1577190, 'author_profile': 'https://Stackoverflow.com/users/1577190', 'pm_score': 2, 'selected': False, 'text': "<p>I guess it depends why you might be in a chroot, and whether any effort has gone into disguising it.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Similrarly, check /sys.</p>\n"}, {'answer_id': 88716, 'author': 'user10392', 'author_id': 10392, 'author_profile': 'https://Stackoverflow.com/users/10392', 'pm_score': 5, 'selected': True, 'text': "<p>The inode for / will always be 2 if it's the root directory of an ext2/ext3/ext4 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.</p>\n"}, {'answer_id': 215381, 'author': 'Joshua', 'author_id': 14768, 'author_profile': 'https://Stackoverflow.com/users/14768', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>Checking the root for inode #2 is also a good one.</p>\n\n<p>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.</p>\n"}, {'answer_id': 8070267, 'author': "Gilles 'SO- stop being evil'", 'author_id': 387076, 'author_profile': 'https://Stackoverflow.com/users/387076', 'pm_score': 3, 'selected': False, 'text': '\n\n<p>On Linux with root permissions, test if the root directory of the init process is your root directory. Although <code>/proc/1/root</code> is always a symbolic link to <code>/</code>, following it leads to the “master” root directory (assuming the init process is not chrooted, but that\'s hardly ever done). If <code>/proc</code> isn\'t mounted, you can bet you\'re in a chroot.</p>\n\n<pre><code>[ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]\n# With ash/bash/ksh/zsh\n! [ -x /proc/1/root/. ] || [ /proc/1/root/. -ef / ]\n</code></pre>\n\n<p>This is more precise than <a href="https://stackoverflow.com/questions/75182/detecting-a-chroot-jail-from-within/215381#215381">looking at <code>/proc/1/exe</code></a> because that could be different outside a chroot if <code>init</code> has been upgraded since the last boot or if the chroot is on the main root filesystem and <code>init</code> is hard linked in it.</p>\n\n<p>If you do not have root permissions, you can look at <code>/proc/1/mountinfo</code> and <code>/proc/$$/mountinfo</code> (briefly documented in <a href="http://www.kernel.org/doc/Documentation/filesystems/proc.txt" rel="nofollow noreferrer"><code>filesystems/proc.txt</code> in the Linux kernel documentation</a>). This file is world-readable and contains a lot of information about each mount point in the process\'s view of the filesystem. The paths in that file are restricted by the chroot affecting the reader process, if any. If the process reading <code>/proc/1/mountinfo</code> is chrooted into a filesystem that\'s different from the global root (assuming pid 1\'s root is the global root), then no entry for <code>/</code> appears in <code>/proc/1/mountinfo</code>. If the process reading <code>/proc/1/mountinfo</code> is chrooted to a directory on the global root filesystem, then an entry for <code>/</code> appears in <code>/proc/1/mountinfo</code>, but with a different mount id. Incidentally, the root field (<code>$4</code>) indicates where the chroot is in its master filesystem. Again, this is specific to Linux.</p>\n\n<pre><code>[ "$(awk \'$5=="/" {print $1}\' &lt;/proc/1/mountinfo)" != "$(awk \'$5=="/" {print $1}\' &lt;/proc/$$/mountinfo)" ]\n</code></pre>\n'}, {'answer_id': 17851543, 'author': 'Alex', 'author_id': 920842, 'author_profile': 'https://Stackoverflow.com/users/920842', 'pm_score': 0, 'selected': False, 'text': '<p>If you entered the chroot with schroot, then you can check the value of $debian_chroot.</p>\n'}, {'answer_id': 24245912, 'author': 'Jérôme Pouiller', 'author_id': 301717, 'author_profile': 'https://Stackoverflow.com/users/301717', 'pm_score': 3, 'selected': False, 'text': "<p>If you are not in a chroot and the root filesystem is ext2/ext3/ext4, the inode for / will always be 2. You may check that using</p>\n<pre><code>stat -c %i /\n</code></pre>\n<p>or</p>\n<pre><code>ls -id /\n</code></pre>\n<p>Interresting, but let's try to find path of chroot directory. Ask to <code>stat</code> on which device / is located:</p>\n<pre><code>stat -c %04D /\n</code></pre>\n<p>First byte is major of device and lest byte is minor. For example, 0802, means major 8, minor 1. If you check in /dev, you will see this device is /dev/sda2. If you are root you can directly create correspondong device in your chroot:</p>\n<pre><code>mknode /tmp/root_dev b 8 1\n</code></pre>\n<p>Now, let's find inode associated to our chroot. debugfs allows list contents of files using inode numbers. For exemple, <code>ls -id /</code> returned 923960:</p>\n<pre><code>sudo debugfs /tmp/root_dev -R 'ls &lt;923960&gt;'\n 923960  (12) .       915821  (32) ..     5636100  (12) var   \n5636319  (12) lib    5636322  (12) usr    5636345  (12) tmp   \n5636346  (12) sys    5636347  (12) sbin   5636348  (12) run   \n5636349  (12) root   5636350  (12) proc   5636351  (12) mnt   \n5636352  (12) home   5636353  (12) dev    5636354  (12) boot   \n5636355  (12) bin    5636356  (12) etc    5638152  (16) selinux   \n5769366  (12) srv    5769367  (12) opt    5769375  (3832) media \n</code></pre>\n<p>Interesting information is inode of <code>..</code> entry: 915821. I can ask its content:</p>\n<pre><code>sudo debugfs /tmp/root_dev -R 'ls &lt;915821&gt;'\n915821  (12) .              2  (12) ..    923960  (20) debian-jail   \n923961  (4052) other-jail  \n</code></pre>\n<p>Directory called <code>debian-jail</code> has inode 923960. So last component of my chroot dir is <code>debian-jail</code>. Let's see parent directory (inode 2) now:</p>\n<pre><code>sudo debugfs /tmp/root_dev -R 'ls &lt;2&gt;'\n      2  (12) .           2  (12) ..          11  (20) lost+found    1046529  (12) home   \n 130817  (12) etc    784897  (16) media     3603  (20) initrd.img   \n 261633  (12) var    654081  (12) usr     392449  (12) sys            392450  (12) lib   \n 784898  (12) root   915715  (12) sbin   1046530  (12) tmp   \n1046531  (12) bin    784899  (12) dev     392451  (12) mnt   \n 915716  (12) run        12  (12) proc   1046532  (12) boot               13  (16) lib64   \n 784945  (12) srv    915821  (12) opt       3604  (3796) vmlinuz \n</code></pre>\n<p>Directory called <code>opt</code> has inode 915821 and inode 2 is root of filesystem. So my chroot directory is <code>/opt/debian-jail</code>. Sure, <code>/dev/sda1</code>  may be mounted on another filesystem. You need to check that (use lsof or directly picking information <code>/proc</code>).</p>\n"}, {'answer_id': 48844758, 'author': 'Samuel Harmer', 'author_id': 594137, 'author_profile': 'https://Stackoverflow.com/users/594137', 'pm_score': 2, 'selected': False, 'text': '<p>I wanted the same information for a jail running on FreeBSD (as Ansible doesn\'t seem to detect this scenario).</p>\n\n<p>On the FreeNAS distribution of FreeBSD 11, <code>/proc</code> is not mounted on the host, but it is within the jail. Whether this is also true on regular FreeBSD I don\'t know for sure, but <a href="https://www.freebsd.org/doc/en/articles/linux-users/procfs.html" rel="nofollow noreferrer">procfs: Gone But Not Forgotten</a> seems to suggest it is. Either way, you probably wouldn\'t want to try mounting it just to detect jail status and therefore I\'m not certain it can be used as a reliable predictor of being within a jail.</p>\n\n<p>I also ruled out using stat on <code>/</code> as certainly on FreeNAS all jails are given their own file system (i.e. <a href="https://www.freebsd.org/doc/handbook/zfs-term.html" rel="nofollow noreferrer">a ZFS dataset</a>) and therefore the <code>/</code> node on the host and in the jail both have inode 4. I expect this is common on FreeBSD 11 in general.</p>\n\n<p>So the approach I settled on was using <a href="https://www.freebsd.org/cgi/man.cgi?procstat" rel="nofollow noreferrer">procstat</a> on pid 0.</p>\n\n<pre><code>[root@host ~]# procstat 0\n  PID  PPID  PGID   SID  TSID THR LOGIN    WCHAN     EMUL          COMM        \n    0     0     0     0     0 1234 -        swapin    -             kernel      \n[root@host ~]# echo $?\n0\n[root@host ~]# jexec guest tcsh\nroot@guest:/ # procstat 0\nprocstat: sysctl(kern.proc): No such process\nprocstat: procstat_getprocs()\nroot@guest:/ # echo $?\n1\n</code></pre>\n\n<p>I am making an assumption here that pid 0 will always be the kernel on the host, and there won\'t be a pid 0 inside the jail.</p>\n'}]
['https://Stackoverflow.com/questions/75182', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13277/']