mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it
@ 2026-11-19 13:25 Christian Brauner
  2026-11-19 13:25 ` [PATCH 1/2] fs: let pivot_root() move userspace_init_fs when nobody shares it Christian Brauner
  2026-11-19 13:25 ` [PATCH 2/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Christian Brauner @ 2026-11-19 13:25 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Akisaira, linux-kernel, regressions,
	Christian Brauner (Amutable),
	stable

In Android's second stage init StartPropertyService() spawns two
threads. They share PID 1's fs_struct via CLONE_FS. So they share
userspace_init_fs. Afterwards SetupMountNamespaces() calls
unshare(CLONE_NEWNS) on the main thread to create a second mount
namespace and setns()s right back into the initial mount namespace.
unshare(CLONE_NEWNS) implies CLONE_FS so PID 1 switches to a private
copy of its fs_struct.

Ever since v7.3 validate_fs_switch() notices that PID 1 stopped sharing
userspace_init_fs and resets root and pwd of userspace_init_fs to
nullfs. But it does that in place and the two property service threads
still use userspace_init_fs. So they suddenly find themselves in nullfs,
libselinux can't open /sys/fs/selinux/access anymore, every property
write is denied and init stalls.

Before commit 14adbd5341aa ("fs: stop sharing fs_struct between
init_task and pid 1") PID 1 shared init_fs with init_task. So
userspace_init_fs was init_fs and the reset was a no-op as the first
stage chroot had already updated init_fs.root.

There are a couple of fixes to not leave stale state behind but it seems
that there is legitimate userspace out there that does need this. So
stop resetting userspace_init_fs for now. I can probably rework this to
something sane but I'm not sure it's worth it. Kthreads and
usermodehelpers keep using what PID 1 left behind, just like they kept
using init_fs before PID 1 got its own fs_struct. Keep the notice
though.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (2):
      fs: let pivot_root() move userspace_init_fs when nobody shares it
      fs: don't reset userspace_init_fs when PID 1 stops sharing it

 fs/fs_struct.c | 72 ++++++++++++++++++++++++----------------------------------
 1 file changed, 30 insertions(+), 42 deletions(-)
---
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
change-id: 20261119-work-fs-pid1_unshare-946f3ff2d1ab


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] fs: let pivot_root() move userspace_init_fs when nobody shares it
  2026-11-19 13:25 [PATCH 0/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner
@ 2026-11-19 13:25 ` Christian Brauner
  2026-11-19 13:25 ` [PATCH 2/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2026-11-19 13:25 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Akisaira, linux-kernel, regressions,
	Christian Brauner (Amutable),
	stable

chroot_fs_refs() only finds userspace_init_fs through the tasks that
share it. Currently that doesn't matter because validate_fs_switch()
resets userspace_init_fs to nullfs as soon as PID 1 stops sharing it.

But in the next patch we'll stop resetting userspace_init_fs and it will
keep what PID 1 left behind. If no other task shares it either a later
pivot_root() would leave kthreads and usermodehelpers behind on the old
root and keep the old root pinned. A plain umount() of the old root
would then fail with EBUSY.

Before PID 1 got its own fs_struct chroot_fs_refs() found init_fs
through the kthreads. So handle userspace_init_fs explicitly after
walking the tasks. If the walk already updated it through a task that
shares it nothing happens.

Fixes: 14adbd5341aa ("fs: stop sharing fs_struct between init_task and pid 1")
Cc: stable@vger.kernel.org # v7.3+
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/fs_struct.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/fs/fs_struct.c b/fs/fs_struct.c
index 34699f3b6f881..538eaaecbadf5 100644
--- a/fs/fs_struct.c
+++ b/fs/fs_struct.c
@@ -53,6 +53,21 @@ static inline int replace_path(struct path *p, const struct path *old, const str
 	return 1;
 }
 
+static inline int replace_fs_paths(struct fs_struct *fs,
+				   const struct path *old_root,
+				   const struct path *new_root)
+{
+	int hits = 0;
+
+	write_seqlock(&fs->seq);
+	hits += replace_path(&fs->root, old_root, new_root);
+	hits += replace_path(&fs->pwd, old_root, new_root);
+	for (int i = 0; i < hits; i++)
+		path_get(new_root);
+	write_sequnlock(&fs->seq);
+	return hits;
+}
+
 void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 {
 	struct task_struct *g, *p;
@@ -66,20 +81,15 @@ void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 
 		task_lock(p);
 		fs = p->real_fs;
-		if (fs) {
-			int hits = 0;
-			write_seqlock(&fs->seq);
-			hits += replace_path(&fs->root, old_root, new_root);
-			hits += replace_path(&fs->pwd, old_root, new_root);
-			while (hits--) {
-				count++;
-				path_get(new_root);
-			}
-			write_sequnlock(&fs->seq);
-		}
+		if (fs)
+			count += replace_fs_paths(fs, old_root, new_root);
 		task_unlock(p);
 	}
 	read_unlock(&tasklist_lock);
+
+	/* Kernel lookups and usermodehelpers use it even if no task shares it. */
+	count += replace_fs_paths(userspace_init_fs, old_root, new_root);
+
 	while (count--)
 		path_put(old_root);
 }

-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it
  2026-11-19 13:25 [PATCH 0/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner
  2026-11-19 13:25 ` [PATCH 1/2] fs: let pivot_root() move userspace_init_fs when nobody shares it Christian Brauner
@ 2026-11-19 13:25 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2026-11-19 13:25 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Alexander Viro, Jan Kara, Akisaira, linux-kernel, regressions,
	Christian Brauner (Amutable),
	stable

In Android's second stage init StartPropertyService() spawns two
threads. They share PID 1's fs_struct via CLONE_FS. So they share
userspace_init_fs. Afterwards SetupMountNamespaces() calls
unshare(CLONE_NEWNS) on the main thread to create a second mount
namespace and setns()s right back into the initial mount namespace.
unshare(CLONE_NEWNS) implies CLONE_FS so PID 1 switches to a private
copy of its fs_struct.

Ever since v7.3 validate_fs_switch() notices that PID 1 stopped sharing
userspace_init_fs and resets root and pwd of userspace_init_fs to
nullfs. But it does that in place and the two property service threads
still use userspace_init_fs. So they suddenly find themselves in nullfs,
libselinux can't open /sys/fs/selinux/access anymore, every property
write is denied and init stalls.

Before commit 14adbd5341aa ("fs: stop sharing fs_struct between
init_task and pid 1") PID 1 shared init_fs with init_task. So
userspace_init_fs was init_fs and the reset was a no-op as the first
stage chroot had already updated init_fs.root.

There are a couple of fixes to not leave stale state behind but it seems
that there is legitimate userspace out there that does need this. So
stop resetting userspace_init_fs for now. I can probably rework this to
something sane but I'm not sure it's worth it. Kthreads and
usermodehelpers keep using what PID 1 left behind, just like they kept
using init_fs before PID 1 got its own fs_struct. Keep the notice
though but downgrade it to pr_info().

Fixes: 9a8e29695888 ("fs: make userspace_init_fs a dynamically-initialized pointer")
Fixes: 14adbd5341aa ("fs: stop sharing fs_struct between init_task and pid 1")
Reported-by: Akisaira <akisaira@outlook.com>
Closes: https://lore.kernel.org/TY4PR01MB14006C85EBAAA186836A6E05CA67A2@TY4PR01MB14006.jpnprd01.prod.outlook.com
Cc: stable@vger.kernel.org # v7.3+
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/fs_struct.c | 40 +++++++++-------------------------------
 1 file changed, 9 insertions(+), 31 deletions(-)

diff --git a/fs/fs_struct.c b/fs/fs_struct.c
index 538eaaecbadf5..56dc79b065bd4 100644
--- a/fs/fs_struct.c
+++ b/fs/fs_struct.c
@@ -165,46 +165,24 @@ int unshare_fs_struct(void)
 EXPORT_SYMBOL_GPL(unshare_fs_struct);
 
 /*
- * PID 1 may choose to stop sharing fs_struct state with us.
- * Either via unshare(CLONE_FS) or unshare(CLONE_NEWNS). Of
- * course, PID 1 could have chosen to create arbitrary process
- * trees that all share fs_struct state via CLONE_FS. This is a
- * strong statement: We only care about PID 1 aka the thread-group
- * leader so subthread's fs_struct state doesn't matter.
+ * PID 1 may stop sharing userspace_init_fs via unshare(CLONE_FS) or
+ * unshare(CLONE_NEWNS) which implies CLONE_FS. That doesn't mean it's
+ * done with it. Android's init unshares to create a second mount
+ * namespace and setns()s right back while its threads keep sharing
+ * userspace_init_fs.
  *
- * PID 1 unsharing fs_struct state is a bug. PID 1 relies on
- * various kthreads to be able to perform work based on its
- * fs_struct state. Breaking that contract sucks for both sides.
- * So just don't bother with extra work for this. No sane init
- * system should ever do this.
- *
- * On older kernels if PID 1 unshared its filesystem state with us the
- * kernel simply used the stale fs_struct state implicitly pinning
- * anything that PID 1 had last used. Even if PID 1 might've moved on to
- * some completely different fs_struct state and might've even unmounted
- * the old root.
- *
- * This has hilarious consequences: Think continuing to dump coredump
- * state into an implicitly pinned directory somewhere. Calling random
- * binaries in the old rootfs via usermodehelpers.
- *
- * Be aggressive about this: We simply reject operating on stale
- * fs_struct state by reverting to nullfs. Every kworker that does
- * lookups after this point will fail. Every usermodehelper call will
- * fail. Tough luck but let's be kind and emit a warning to userspace.
+ * So never change userspace_init_fs behind userspace's back. Kthreads
+ * and usermodehelpers keep using what PID 1 left behind, just like
+ * they kept using init_fs before PID 1 got its own fs_struct.
  */
 static inline void validate_fs_switch(struct fs_struct *old_fs)
 {
-	might_sleep();
-
 	if (likely(current->pid != 1))
 		return;
 	/* @old_fs may be dangling but for comparison it's fine */
 	if (old_fs != userspace_init_fs)
 		return;
-	pr_warn("VFS: Pid 1 stopped sharing filesystem state\n");
-	set_fs_root(userspace_init_fs, &init_fs.root);
-	set_fs_pwd(userspace_init_fs, &init_fs.root);
+	pr_info("VFS: Pid 1 stopped sharing filesystem state\n");
 }
 
 struct fs_struct *switch_fs_struct(struct fs_struct *new_fs)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-19 13:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 13:25 [PATCH 0/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner
2026-11-19 13:25 ` [PATCH 1/2] fs: let pivot_root() move userspace_init_fs when nobody shares it Christian Brauner
2026-11-19 13:25 ` [PATCH 2/2] fs: don't reset userspace_init_fs when PID 1 stops sharing it Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®