From mboxrd@z Thu Jan 1 00:00:00 1970 From: Aleksa Sarai Subject: Re: pivot_root(".", ".") and the fchdir() dance Date: Mon, 5 Aug 2019 20:36:30 +1000 Message-ID: <20190805103630.tu4kytsbi5evfrhi@mikami> References: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="6qypzrljzt2mkjjn" Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org To: "Michael Kerrisk (man-pages)" Cc: "Serge E. Hallyn" , linux-man , Containers , lkml , Andy Lutomirski , Jordan Ogas , Al Viro List-Id: linux-man@vger.kernel.org --6qypzrljzt2mkjjn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 >=20 > oldroot =3D open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); > newroot =3D open(rootfs, O_DIRECTORY | O_RDONLY | O_CLOEXEC); >=20 > fchdir(newroot); > pivot_root(".", "."); >=20 > fchdir(oldroot); // **** This one is "required" because (as the pivot_root(2) man page states), it's technically not guaranteed by the kernel that the process's cwd will be the same after pivot_root(2): > pivot_root() may or may not change the current root and the current > working directory of any processes or threads which use the old root > directory. Now, if it turns out that we can rely on the current behaviour (and the man page you're improving is actually inaccurate on this point) then you're right that this fchdir(2) isn't required. > 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 =66rom "." to "/" in the mount/umount2 calls should fix the issue. We do something very similar to this in runc as well[1] (though, as the commit message says, I "borrowed" the idea from LXC). [1]: https://github.com/opencontainers/runc/commit/f8e6b5af5e120ab7599885bd= 13a932d970ccc748 --=20 Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH --6qypzrljzt2mkjjn Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEXzbGxhtUYBJKdfWmnhiqJn3bjbQFAl1IBqsACgkQnhiqJn3b jbTjfw/+Pnh6zFLbDf4UKFZiPT2tLK91Q5TuWjlmjWxJxZMtGNxvDyQiyYXaNXn7 rskiOzCFTl2i3N5BJZCuT3LPfT9+jdHQ1lYowlGZh0AVSQrWksAUsEEumyMFxqpo /ltTRWeQG51pqiqW+Nutd5VH3qsaHT2WfNYHnGSuzyHn+lykwgLymnE7MzjgGPZV vm9IGDEQmpHMZuc6cAxKzC74Fa98QDmKS0K0R32GaLFc7Y28HNnDq/fnNbzAkVYO OwGDY1p97vVk6Zc0MOQuyjAYng+lnaD+wp1uEYai8hpPud5AZ6WBxFke5l83TKjk lJgYx+CT+/inlUcRZ7X8/8MofET/P8K65fteEL+SvoSpBakVmztMVKIvasll+eja 81rffEeIy+3X8pRhSSN8R3yFLk29uw+oQxj5FJlr6IGX0uiRnk1m93KuphlsBg4w jedZ9O+LE2DbjHT43Xz7u0oeeUYVF+Hs2aZXOXbxRGeJl1HiQYlJqW1mdWGwQKKf 1r3zSxaTfXz8svNsHA0fRlZi7S5psoUMRdNEhsQGW/GKr3KTtojUvEqeGGKhW6H4 KgJR6OHlmfRwwgCtiaLx09736H3/m/zG5gPsGTKZGP51LnQWYjpUaUMm+55tSUg9 waJ52wsLxsmJiJ6zYGHzih7sK6LJZMT0ZvqWUXAO5DPK5P2UPqc= =Ti2z -----END PGP SIGNATURE----- --6qypzrljzt2mkjjn--