Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp4013788ybh; Tue, 6 Aug 2019 05:04:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqysb4Pyrub4gG2EMLovyoO32W7S1khIXyQYXHZVrvEcvDnFK1X8uO3iSA3EUFQI1wiVV2OU X-Received: by 2002:a63:784c:: with SMTP id t73mr2850080pgc.268.1565093066782; Tue, 06 Aug 2019 05:04:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565093066; cv=none; d=google.com; s=arc-20160816; b=u+BXCfZXDydOQXFFY/r3O38PzEurCUTlV/ENkVnUUW7Y8R0Soab/13B4RXoyD3qSZh MCLO3jM1Rto+66AnEZVioQmXSZZR+zm8wHiEDGcSfb+yJh4eMa0vfVRjoBnvnV5AUlrd FpgKdcoVFXACMIcKo/ueFhgDqMhKSnpNB66Qa7zzzup3dsQzlfMmLfa9Ik9YFBVeASDr 15zwQh0qW6RfM251jmQrdRk2wZsINk/HUs7CxBW5tX7/fG8Dz225pCSYMDlJ39Vvx1yq WJrfA54St59o2xYhlvffjsmhVSnZOQmCrdnZpdcunEgm1cBV01OdYUjpitUzAv3Xxn0i Bshw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:dkim-signature; bh=0ibbfjjlcjqJhBjocgI23MMIU0ZjpMBB8zJJ1s3Y/U8=; b=Xu7gJHHBidOVmiE1XOVh6XGnw798ZpKoMCvUMb+oQOCGz4uv7RiMNzSrRcWRe/ktOY +HNAMSOGEGhAARHtk1p63IQx2Gss2esNV8nQmYavq+SQVkg6v3ocrNF3JlEgCJAjBtD5 4CUO4G6ShZQ+aOtzeSU+gnPvh5n3IR8aRYrzkNDtkyDSgoPO2ww+UNs4CgpuhCb0BVoW 2Y/xHF9IMVb6t1/toMlI6l9fRoKrEwx1Fz8JSmy4NM4FdlqKzJD1xp8GGIhvUhDnAfcq iYc4r0gBmK8yxufROTmqOkPqFfXNUtWIzqEko7xw2ALfcChCwQ3j+hLTBeTJm3pvZp2A dC9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Vg2eXa17; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l35si43056974plb.186.2019.08.06.05.04.09; Tue, 06 Aug 2019 05:04:26 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Vg2eXa17; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729168AbfHFMD1 (ORCPT + 99 others); Tue, 6 Aug 2019 08:03:27 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:40057 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726092AbfHFMD0 (ORCPT ); Tue, 6 Aug 2019 08:03:26 -0400 Received: by mail-ed1-f67.google.com with SMTP id k8so82026365eds.7; Tue, 06 Aug 2019 05:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to:cc; bh=0ibbfjjlcjqJhBjocgI23MMIU0ZjpMBB8zJJ1s3Y/U8=; b=Vg2eXa17xUoxRlChCoci37B6L/lyFDQ9plgCkdnEBxPZZckdHS7JzDElMclSVP4BCs 6zMFs6pqODtDeABCJeeU8KejTPacxF2bnpBiWL0zZ4WuZHTbI3AiQgRv+9IuUww2QfGA vkFHmcU2hTkcqElqT7JuyGeYI0KDIFC76+jKny/eH/HHqDFxyn9fAQhfAmDTKr9BVPlk rDlITcAs+hZFUgu85dxCcJnGdZJTcyYd2Ied31JG7/FFawFT2gZyh+dqq3tGpSdMejCL 8avIearjdAdvw8Xvg87WzqmymgZWriEhz/yPMnrZJgCNILb0RwDD6/vb98/taFJdtVwF +D3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=0ibbfjjlcjqJhBjocgI23MMIU0ZjpMBB8zJJ1s3Y/U8=; b=pK7PU4n0vhfSPZ+7jQClH+K9xt8lWRW26LSzbrNvb/lqWjIwX9aosSnUL2UfhvT9Np eBXaZ23yVhSBWFmwORpvFcuE151RSQXWKd4lPdkAJZJ3HM7LSSGMfNcd7ejKnMpYD9A+ YVVsG07TuVvCtFDc8oaPeZV90Kk9Rr+QiKLAM4Su/cg5Bouv2M3lnBPoqbzelsaFjc9f GLjKz3lHXGvAUPhAIqjL4WW3MhmAxgzgArQPxaafeLE4Fgd/5ELFSbQtiKBhB/0p9/SJ ddLBSTQMy8NWRM8Hj5Rz91SHGjy6aTY+5/b5AouZY2QFW5Exp5p6dgx2YyxxjvtseXJK CTdw== X-Gm-Message-State: APjAAAXUQVqF+Vq0wruIN95YAS8Sz5KNkiTOUMd5HLqQtK81ndk+W6rv 8fd7pYvnCZ/IiCyuTT5Efen5X9DaODJswz2K/2s= X-Received: by 2002:a17:906:154f:: with SMTP id c15mr2756640ejd.268.1565093005065; Tue, 06 Aug 2019 05:03:25 -0700 (PDT) MIME-Version: 1.0 References: <20190805103630.tu4kytsbi5evfrhi@mikami> <3a96c631-6595-b75e-f6a7-db703bf89bcf@gmail.com> In-Reply-To: Reply-To: mtk.manpages@gmail.com From: "Michael Kerrisk (man-pages)" Date: Tue, 6 Aug 2019 14:03:13 +0200 Message-ID: Subject: Re: pivot_root(".", ".") and the fchdir() dance To: Philipp Wendler Cc: Aleksa Sarai , linux-man , Containers , lkml , Andy Lutomirski , Jordan Ogas , werner@almesberger.net, Al Viro Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 :-). Cheers, Michael -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/