From owner-freebsd-hackers@freebsd.org Sun Sep 27 19:42:44 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 65D2C4260BC for ; Sun, 27 Sep 2020 19:42:44 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BzwzS1z22z4Fjg for ; Sun, 27 Sep 2020 19:42:44 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) (Authenticated sender: kevans) by smtp.freebsd.org (Postfix) with ESMTPSA id 21A7F140C3 for ; Sun, 27 Sep 2020 19:42:44 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: by mail-qt1-f181.google.com with SMTP id e7so6589992qtj.11 for ; Sun, 27 Sep 2020 12:42:44 -0700 (PDT) X-Gm-Message-State: AOAM531w9VEIqOKU8+FhFCez7RSGSATSttM4ixUs4oQViMdG0RxilxmR 7YjgYMgNym5uWgP7ktO/QbzCPMRr+VMQlzOtGlQ= X-Received: by 2002:ac8:3f3d:: with SMTP id c58mt9615707qtk.53.1601235763673; Sun, 27 Sep 2020 12:42:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Kyle Evans Date: Sun, 27 Sep 2020 14:42:32 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Is it possible to exit the chroot(2) environment? Cc: Warner Losh , Yuri , Freebsd hackers list Content-Type: text/plain; charset="UTF-8" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 27 Sep 2020 19:42:44 -0000 On Sun, Sep 27, 2020 at 2:07 PM Kyle Evans wrote: > > On Sun, Sep 27, 2020 at 2:03 PM Warner Losh wrote: > > > > On Sun, Sep 27, 2020 at 12:30 PM Yuri 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. */ > I think the original report needs a ktrace to narrow down what's really going on... kevans@shiva:~/grep$ cat chr.c #include #include int main(int argc, char *argv[]) { printf("chdir %d\n", chdir("/")); printf("chroot %d\n", chroot("/tmp")); printf("chroot %d\n", chroot(".")); return (0); } kevans@shiva:~/grep$ sudo ./a.out chdir 0 chroot 0 chroot 0 Sprinkling some stat() calls in between reveals that chroot(2) is working in both cases and properly entering/exiting the chroot.