How to exit from chroot
10,119
The point of a chroot
is that you can't get out. However, if you don't chdir
to /var/chroot/mychroot
, then you can still access the outside filesystem using .
, and ..
I don't know much Ruby, but here's the trick in Python:
/tmp$ sudo python
>>> import os
>>> os.chroot("/var")
>>> os.listdir("/") # list our new root
['backups', 'log', 'opt', 'cache', 'spool', 'lib', 'local', 'run', 'lock', 'games', 'mail', 'tmp']
>>> os.listdir(".") # list a directory outside our jail
['.X0-lock', '.ICE-unix', '.X11-unix']
>>> os.listdir("..") # list the outside root
['lost+found', 'bin', 'mnt', 'boot', 'opt', 'scratch', 'var', 'proc', 'usr', 'etc', 'lib', 'srv', 'sys', 'media', 'root', 'selinux', 'vmlinuz', 'dev', 'tmp', 'home', 'sbin']
Comments
-
fl00r almost 2 years
I can chroot with
Dir.chroot
Dir.chroot("/var/chroot/mychroot")
But how can I return back from chroot?
-
Fred Foo about 12 yearsThe string returned by
pwd
is useless afterchroot
, because it alters the filename lookup rules. -
Nowaker over 10 yearsHow does it work? Is it a real chroot? Or maybe Python's chroot is just garbage? The idea of chroot is to remain in chroot forever.
-
Fred Foo over 10 years@DamianNowak: as I explained in the answer, you need to
chdir
into thechroot
for it to take effect. You also need to close file descriptors on outside directories.chroot
is not a magic bullet, it should be used with care. -
Nowaker over 10 yearsOK, I get it now. Thanks.