Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp4844763ybe; Mon, 9 Sep 2019 15:51:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqwzRqmDf8imr/TCbpZ9pKtec9VlZ2nKvMH/69/Jdgp/rPfckf4sCjoho+qLwMyS29U8qqQ7 X-Received: by 2002:a50:858a:: with SMTP id a10mr27683111edh.284.1568069485987; Mon, 09 Sep 2019 15:51:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568069485; cv=none; d=google.com; s=arc-20160816; b=PBtlkZrn4VTgb4U3T3jba0J3dYiwGKeT5uDOIAQbJnaUePdFonljxOsezBrFdfiSJi LUv7q0mYFWLJVUqRzNVBPS9CjpH58ptYWndERw45OC6Q7qxzKNGcsRna8miKciEQ+m/X YxSMirwaXT3x0NNIFpiQws6EYbNHr7zG9MPoNoAvvrll+q2CNF0SWuRXW/oz+BSUc3Iq wgJ4kjdLuclTdu2s2OYtQDiixqn75KO4zGCCrmEhbhdduiQSEZ6Su4aZRwurnrgJNzHQ nxUVRR3LrPq7cz1ELJiOTr/ICLkOX6Ncl4hTVgYA5kj7ho7/jt1EXYD4nHp3jSsLBY5X Wc4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:mime-version:user-agent :message-id:in-reply-to:date:references:cc:to:from; bh=Kr7djp6U9i5D2PuwqItih1sJ+k0ECYGO0Pa76tiukzo=; b=Wlb7nX3mmPwWhdnYWvCnI+CWSLpbotAAXccRyDyAGW1GQ7GbogcuJ04jTio2bupNgr aIuOlrIYsc3bmbA54RJd7p57kqGdGkBWtZ2A60yUk1Cmw4ypUz3kPC97WsiUDddjQAVv YlSmjgNTTIaCF0gobqi+TUwFJ7SNVs5e4hxEI5yXSxlxJm7iI3bNtb4jVguKIO1Gjao9 qi8KdDx04pim1y2at9UD9lrG9tLstknj/x41VEh3371mleIoEMcTi9fi54thBv+0kDTI VxYXX9iEQ8bqqS5QmvNMu1wwvCj8Pjv3X8ZI26FyB1oqtLkeG6clElm01B6pUH2g5sHP t3iA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o16si8304513ejr.190.2019.09.09.15.51.02; Mon, 09 Sep 2019 15:51:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729549AbfIIKkc (ORCPT + 99 others); Mon, 9 Sep 2019 06:40:32 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:44900 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726407AbfIIKkb (ORCPT ); Mon, 9 Sep 2019 06:40:31 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out02.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1i7H5v-00043v-EB; Mon, 09 Sep 2019 04:40:27 -0600 Received: from 110.8.30.213.rev.vodafone.pt ([213.30.8.110] helo=x220.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.87) (envelope-from ) id 1i7H5t-00072P-An; Mon, 09 Sep 2019 04:40:27 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: "Michael Kerrisk \(man-pages\)" Cc: Philipp Wendler , linux-man , Containers , lkml , Andy Lutomirski , Jordan Ogas , werner@almesberger.net, Al Viro References: <20190805103630.tu4kytsbi5evfrhi@mikami> <3a96c631-6595-b75e-f6a7-db703bf89bcf@gmail.com> Date: Mon, 09 Sep 2019 05:40:05 -0500 In-Reply-To: (Michael Kerrisk's message of "Tue, 6 Aug 2019 14:03:13 +0200") Message-ID: <87r24piwhm.fsf@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1i7H5t-00072P-An;;;mid=<87r24piwhm.fsf@x220.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=213.30.8.110;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX18c6MeuT4/YbjCiKID4z0Z9syV2XRq6dMM= X-SA-Exim-Connect-IP: 213.30.8.110 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa07.xmission.com X-Spam-Level: X-Spam-Status: No, score=-0.2 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,TVD_RCVD_IP,T_TM2_M_HEADER_IN_MSG,T_TooManySym_01, T_TooManySym_02 autolearn=disabled version=3.4.2 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.4566] * 0.0 TVD_RCVD_IP Message was received from an IP address * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa07 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_02 5+ unique symbols in subject * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: XMission; sa07 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;"Michael Kerrisk \(man-pages\)" X-Spam-Relay-Country: X-Spam-Timing: total 1533 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 2.8 (0.2%), b_tie_ro: 1.95 (0.1%), parse: 1.20 (0.1%), extract_message_metadata: 13 (0.8%), get_uri_detail_list: 2.2 (0.1%), tests_pri_-1000: 4.9 (0.3%), tests_pri_-950: 1.31 (0.1%), tests_pri_-900: 1.08 (0.1%), tests_pri_-90: 32 (2.1%), check_bayes: 30 (2.0%), b_tokenize: 8 (0.5%), b_tok_get_all: 11 (0.7%), b_comp_prob: 2.4 (0.2%), b_tok_touch_all: 4.9 (0.3%), b_finish: 2.0 (0.1%), tests_pri_0: 521 (34.0%), check_dkim_signature: 0.76 (0.0%), check_dkim_adsp: 2.8 (0.2%), poll_dns_idle: 935 (61.0%), tests_pri_10: 3.0 (0.2%), tests_pri_500: 950 (61.9%), rewrite_mail: 0.00 (0.0%) Subject: Re: pivot_root(".", ".") and the fchdir() dance X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org "Michael Kerrisk (man-pages)" writes: > Hello Philipp, > > On Tue, 6 Aug 2019 at 10:12, Philipp Wendler wrote: >> >> Hello Michael, hello Aleksa, >> >> Am 05.08.19 um 14:29 schrieb Michael Kerrisk (man-pages): >> >> > On 8/5/19 12:36 PM, Aleksa Sarai wrote: >> >> On 2019-08-01, Michael Kerrisk (man-pages) wrote: >> >>> I'd like to add some documentation about the pivot_root(".", ".") >> >>> idea, but I have a doubt/question. In the lxc_pivot_root() code we >> >>> have these steps >> >>> >> >>> oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); >> >>> newroot = open(rootfs, O_DIRECTORY | O_RDONLY | O_CLOEXEC); >> >>> >> >>> fchdir(newroot); >> >>> pivot_root(".", "."); >> >>> >> >>> fchdir(oldroot); // **** >> >>> >> >>> mount("", ".", "", MS_SLAVE | MS_REC, NULL); >> >>> umount2(".", MNT_DETACH); >> >> >> >>> fchdir(newroot); // **** >> >> >> >> And this one is required because we are in @oldroot at this point, due >> >> to the first fchdir(2). If we don't have the first one, then switching >> >> from "." to "/" in the mount/umount2 calls should fix the issue. >> > >> > See my notes above for why I therefore think that the second fchdir() >> > is also not needed (and therefore why switching from "." to "/" in the >> > mount()/umount2() calls is unnecessary. >> > >> > Do you agree with my analysis? >> >> If both the second and third fchdir are not required, >> then we do not need to bother with file descriptors at all, right? > > Exactly. > >> Indeed, my tests show that the following seems to work fine: >> >> chdir(rootfs) >> pivot_root(".", ".") >> umount2(".", MNT_DETACH) > > Thanks for the confirmation, That's also exactly what I tested. > >> I tested that with my own tool[1] that uses user namespaces and marks >> everything MS_PRIVATE before, so I do not need the mount(MS_SLAVE) here. >> >> And it works the same with both umount2("/") and umount2("."). > > Yes. > >> Did I overlook something that makes the file descriptors required? > > No. > >> If not, wouldn't the above snippet make sense as example in the man page? > > I have exactly that snippet in a pending change for the manual page :-). I have just spotted this conversation and I expect if you are going to use this example it is probably good to document what is going on so that people can follow along. >> chdir(rootfs) >> pivot_root(".", ".") At this point the mount stack should be: old_root new_root rootfs With "." and "/" pointing to new_root. >> umount2(".", MNT_DETACH) At this point resolving "." starts with new_root and follows up the mount stack to old-root. Ordinarily if you unmount "/" as is happening above you then need to call chroot and possibly chdir to ensure neither "/" nor "." point to somewhere other than the unmounted root filesystem. In this specific case because "/" and "." resolve to new_root under the filesystem that is being unmounted that all is well. Eric