rubychroot

How to exit from chroot


I can chroot with Dir.chroot

Dir.chroot("/var/chroot/mychroot")

But how can I return back from chroot?


Solution

  • 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']