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:
+29
-5
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user