Is it possible to exit the chroot(2) environment?

xtouqh at hotmail.com xtouqh at hotmail.com
Sun Sep 27 19:12:16 UTC 2020


Kyle Evans wrote:
> On Sun, Sep 27, 2020 at 2:03 PM Warner Losh <imp at bsdimp.com> wrote:
>>
>> On Sun, Sep 27, 2020 at 12:30 PM Yuri <yuri at rawbw.com> wrote:
>>
>>> This line
>>>
>>> https://github.com/rpm-software-management/rpm/blob/master/lib/rpmchroot.c#L155
>>> calls chroot(".") in order to exit from the chroot environment.
>>>
>>
>> Interesting. FreeBSD doesn't allow that.
>>
>>
>>> It apparently succeeds on Linux (this is rpm), but it fails on FreeBSD
>>> with "Operation not permitted", while executed under sudo.
>>>
>>> The chroot(2) man page doesn't mention anything about exiting the chroot
>>> environment.
>>>
>>
>> True. Such behavior is undefined. There's no defined notion of exiting a
>> chroot. It doesn't seem to be documented in the few examples of the
>> chroot(2) call linux man pages I've found. Do you have documentation on
>> what, exactly, it's supposed to do?
>>
> 
> I'm almost certain they just aren't restricting you from chrooting to
> a directory out of the chroot if you have a reference to it, so it
> probably does something like:
> 
> chdir("/");
> chroot("/some/root");
> /* Do stuff, but never chdir */
> chroot("."); /* Working directory is still the real root. */

Reading the illumos chroot(2) suggests something similar:

    The ".." entry in the root directory is interpreted to mean the root
    directory itself. Therefore, ".." cannot be used to access files
    outside the subtree rooted at the root directory. Instead, fchroot()
    can be used to reset the root to a directory that was opened before
    the root directory was changed.


More information about the freebsd-hackers mailing list