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']
Share:
10,119
fl00r
Author by

fl00r

I am not funny

Updated on June 04, 2022

Comments

  • fl00r
    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
    Fred Foo about 12 years
    The string returned by pwd is useless after chroot, because it alters the filename lookup rules.
  • Nowaker
    Nowaker over 10 years
    How 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
    Fred Foo over 10 years
    @DamianNowak: as I explained in the answer, you need to chdir into the chroot 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
    Nowaker over 10 years
    OK, I get it now. Thanks.