]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-util/sysext: Clone sub mounts as private to preserve nested ones
authorKai Lüke <kai@amutable.com>
Wed, 29 Apr 2026 14:53:40 +0000 (23:53 +0900)
committerKai Lüke <pothos@users.noreply.github.com>
Fri, 10 Jul 2026 01:34:54 +0000 (03:34 +0200)
When nested mounts appear under a sysext hierarchy like this:
  mkdir -p /opt/trigger/
  mount -t tmpfs tmpfs /opt/trigger
  mkdir -p /opt/trigger/inner
  mount -t tmpfs tmpfs /opt/trigger/inner
Then systemd-sysext merge will lose the inner mount because it uses a
regular bind mount with propagation and then unmounts the source,
unmounting all children with it which propagates (as found out in
https://github.com/flatcar/Flatcar/issues/2111).
To solve this, clone the sub mount with MS_PRIVATE to decouple sub
mounts from the original mount. Then attach the cloned mount instead of
doing regular bind mounts. For old kernels we still attach the cloned
mount but we fallback to cloning without MS_PRIVATE. This change also
affects mount_private_apivfs which is used for private /proc, /sys, and
cgroupfs but I think it makes sense there, too, instead of only doing
mount_setattr for sysext alone because, e.g., a container and the host
should not be leaking mount actions into each other for these mounts.

src/shared/mount-util.c
src/sysext/sysext.c
test/units/TEST-50-DISSECT.sysext.sh

index 1654e49d84fc5d9bebc8db106f199d519a7c1560..20eda5b0ef3a978e41ab80efdfc5a6f1a8b42eac 100644 (file)
@@ -1838,11 +1838,35 @@ int get_sub_mounts(const char *prefix, SubMount **ret_mounts, size_t *ret_n_moun
                         continue;
                 }
 
-                mount_fd = RET_NERRNO(open_tree(AT_FDCWD, path, OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_RECURSIVE));
-                if (mount_fd == -ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
-                        continue;
-                if (mount_fd < 0)
-                        return log_debug_errno(mount_fd, "Failed to open subtree of mounted filesystem '%s': %m", path);
+                /* If possible on a newer kernel, use MS_PRIVATE to decouple it from the original mount.
+                 * Otherwise MNT_DETACH of the source path could propagate through and unmount the
+                 * just-moved nested children at the destination (relevant for preserving nested mounts
+                 * under sysext hierarchies). */
+                static bool mount_attr_unsupported = false;
+
+                if (!mount_attr_unsupported) {
+                        mount_fd = open_tree_attr_with_fallback(
+                                        AT_FDCWD, path,
+                                        OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_RECURSIVE,
+                                        &(struct mount_attr) { .propagation = MS_PRIVATE });
+                        if (mount_fd == -ENOENT) /* The path may be hidden by another over-mount or already unmounted. */
+                                continue;
+                        if (mount_fd < 0 && ERRNO_IS_NEG_NOT_SUPPORTED(mount_fd)) {
+                                /* On a kernel older than 5.12 without mount_setattr() we do the regular
+                                 * clone. Nested mounts under sysext and similar cases may get lost. */
+                                log_debug_errno(mount_fd, "mount_setattr() not supported, falling back to plain open_tree() without MS_PRIVATE: %m");
+                                mount_attr_unsupported = true;
+                        } else if (mount_fd < 0)
+                                return log_debug_errno(mount_fd, "Failed to open subtree of mounted filesystem '%s': %m", path);
+                }
+
+                if (mount_attr_unsupported) {
+                        mount_fd = RET_NERRNO(open_tree(AT_FDCWD, path, OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC|AT_RECURSIVE));
+                        if (mount_fd == -ENOENT)
+                                continue;
+                        if (mount_fd < 0)
+                                return log_debug_errno(mount_fd, "Failed to open subtree of mounted filesystem '%s': %m", path);
+                }
 
                 p = strdup(path);
                 if (!p)
index 4cc98836281b11554cff067a65d701e4f6645ea0..e789af2e2f5b05ac566c7fe3a645baa089ec613b 100644 (file)
@@ -480,9 +480,13 @@ static int move_submounts(const char *src, const char *dst) {
                 if (child_fd < 0)
                         return log_error_errno(errno, "Failed to pin mountpoint %s: %m", t);
 
-                r = mount_follow_verbose(LOG_ERR, m->path, FORMAT_PROC_FD_PATH(child_fd), /* fstype= */ NULL, MS_BIND|MS_REC, /* options= */ NULL);
+                /* Instead of a bind mount we attach the detached clone produced by
+                 * open_tree_attr_with_fallback() from get_sub_mounts() because that has no propagation
+                 * relationship with the original anymore and the MNT_DETACH below won't propagate for
+                 * nested mounts. */
+                r = RET_NERRNO(move_mount(m->mount_fd, "", child_fd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH));
                 if (r < 0)
-                        return r;
+                        return log_error_errno(r, "Failed to move mount '%s' to '%s': %m", m->path, t);
 
                 (void) umount_verbose(LOG_WARNING, m->path, MNT_DETACH);
         }
index fd62dcfe81740e40ad54674c73e96b636d7351ff..fd7a164478bc0314f9d43cb31ecb1165485303a1 100755 (executable)
@@ -1863,6 +1863,57 @@ if systemctl --quiet is-active "$ext_unit"; then
 fi
 )
 
+( init_trap
+: "Nested tmpfs submounts under the hierarchy survive merge/refresh/unmerge round-trip"
+fake_root=${roots_dir:+"$roots_dir/nested-submounts"}
+hierarchy=/opt
+
+# Don't run the test if the inner mount won't be preserved due to an old kernel
+if ! systemd-analyze compare-versions "$(uname -r)" ge 5.12; then
+    echo >&2 "Kernel too old for mount_setattr (need >= 5.12), skipping nested submount test"
+    exit 0
+fi
+
+prepare_root "$fake_root" "$hierarchy"
+prepare_extension_image "$fake_root" "$hierarchy"
+prepare_hierarchy "$fake_root" "$hierarchy"
+
+# Two tmpfs mounts, one nested in the hierarchy under the other. Reproduces the nested mount layout from
+# https://github.com/flatcar/Flatcar/issues/2111 and verifies that we preserve nested mounts across merge,
+# refresh, and unmerge.
+outer_mp="$fake_root$hierarchy/outer"
+inner_mp="$outer_mp/inner"
+mkdir -p "$outer_mp"
+mount -t tmpfs tmpfs "$outer_mp"
+prepend_trap "umount -l ${outer_mp@Q} 2>/dev/null || true"
+mkdir -p "$inner_mp"
+mount -t tmpfs tmpfs "$inner_mp"
+prepend_trap "umount -l ${inner_mp@Q} 2>/dev/null || true"
+touch "$outer_mp/outer-marker"
+touch "$inner_mp/inner-marker"
+
+run_systemd_sysext "$fake_root" merge
+extension_verify_after_merge "$fake_root" "$hierarchy" -e -h
+mountpoint "$outer_mp"
+mountpoint "$inner_mp"
+test -f "$outer_mp/outer-marker"
+test -f "$inner_mp/inner-marker"
+
+run_systemd_sysext "$fake_root" refresh --always-refresh=yes
+extension_verify_after_merge "$fake_root" "$hierarchy" -e -h
+mountpoint "$outer_mp"
+mountpoint "$inner_mp"
+test -f "$outer_mp/outer-marker"
+test -f "$inner_mp/inner-marker"
+
+run_systemd_sysext "$fake_root" unmerge
+extension_verify_after_unmerge "$fake_root" "$hierarchy" -h
+mountpoint "$outer_mp"
+mountpoint "$inner_mp"
+test -f "$outer_mp/outer-marker"
+test -f "$inner_mp/inner-marker"
+)
+
 } # End of run_sysext_tests