mount-util/sysext: Clone sub mounts as private to preserve nested ones

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.
This commit is contained in:
Kai Lüke
2026-07-10 03:34:54 +02:00
committed by Kai Lüke
parent 7eabeaed0e
commit f6a7a9ec3d
3 changed files with 86 additions and 7 deletions
+29 -5
View 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)
+6 -2
View 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);
}