From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 543B760A6F78 for ; Sat, 5 Dec 2026 03:28:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jneK5gW9 Received: by kanga.kvack.org (Postfix) id 8E7DD6B05C1; Fri, 4 Dec 2026 22:27:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C0466B05C2; Fri, 4 Dec 2026 22:27:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 787286B05C3; Fri, 4 Dec 2026 22:27:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 41B8F6B05C2 for ; Fri, 4 Dec 2026 22:27:21 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DA51BC02CD for ; Sat, 5 Dec 2026 03:27:19 +0000 (UTC) X-FDA: 85508493798.09.D0ED52F Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf02.hostedemail.com (Postfix) with ESMTP id 3981480010 for ; Sat, 5 Dec 2026 03:27:18 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=jneK5gW9; spf=pass (imf02.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796441238; b=wFPZkxembQ22sHmoyBCH9Zq+JxFVxeyFp78AgnuWiISX1hSolu7aXmTN0OrJiSXw4WYFH9 pz/HontSvx+1N0WX/qdSOoXpfqkibsevlW0EeBs0O4NA/iz8d1nR4BrZn5myhdWG2yXcrZ VJTTUEEO2ntz+RyXrMxaSETEhV8gk5M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796441238; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aHrL00gYpyKvvkHjUxiDlfIjZQIb4clX6EJOpNfI+CM=; b=hwFQQ1uvFQA1geLuilKXBoQQUblFpDGe7wNiwTTaYG/TBB+bucwDEDUKo+Bky9kBW+lfYJ CkgYo9e8SaNgbGwkmS90CxZLnvdK59zmiJopZOBTMjDxMFU4CCJL0ZbD2sSweY3BkHjHXW 2iG4axTV3f8cM1JYtx1PDZBds0H41kQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=jneK5gW9; spf=pass (imf02.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id 834B341942; Sat, 5 Dec 2026 03:27:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id AC52C1F0092B; Sat, 5 Dec 2026 03:27:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441237; bh=aHrL00gYpyKvvkHjUxiDlfIjZQIb4clX6EJOpNfI+CM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jneK5gW9L8MruJa4VhzbMlSjidQTvZjH7shzFimGiy5YAm54zPUeXvt1IcsPyilDZ jaeA73a7At9UrGWePjzcD1g1CziUPnQDhKeoeQLBq1yFGMw3gmA8zebrxQmPgYwER0 GvVKUtM2nbknYOQqjvGDyciy37/44hcHhX1X/gpuKmYxpHmvyY0h72P32Ok5zcL5xl IP0X/4x7l+PtwWuh+JKoXi2Si9dcz2eobdyB7Wb3IpCLMmrCoL7odoPHdHWc/a0eN2 qkGqx2jK9Z2ytuxM29biwgPG9lorbYxeIBtWpWMQYOtDFZC05EQIrSTfw9fz4cFSe0 oFQIzUdULCiYw== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?UTF-8?q?G=C3=BCnther=20Noack?= , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: [RFC PATCH v10 07/28] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Date: Sat, 5 Dec 2026 05:26:12 +0200 Message-ID: <20261205032633.1702772-8-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-1-jarkko@kernel.org> References: <20261205032633.1702772-1-jarkko@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3981480010 X-Rspam-User: X-Stat-Signature: riuzwwub96hbmhehzj5sxrzu9x9x689t X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796441238-107534 X-HE-Meta: U2FsdGVkX1+Ri+8nKPazDggKqC338jj0ZJpYpsdYMO5QklT/KOpoOcByOEkNGMrT/p8n9hFNSuD2XdQgMfW2usawRSI4yN/jKCmulfT/1x8iIJzuFW7bXjgRLgfbbl+xWmIiFXfM5+HrPBec/T17jkXCQIOPcbdgdMs14/ggB+G+Sh4uMX462lGFbUSpMykNhX3HBKEHqq4QHIDdcfztepkXvjhDda3CNCS+l2y4Gh2VAHkn2QSd88ADFBqcqTRF92t9wetNMhwriehOO1nwJlhaiQ0CiE1cCvhuV54ULN3uywsp6JfZxZ6Y06Ijvd3ismTskRwLK+Fl8yU4G6qeUlzJee5sf6cKbHi4z90nJwWcT3SRTUCr2/4URbSX92dUHNoYjJ6hz4lIF/VsfV0gfB7TQkU3bkthMrod/vEFAT/tLKRVLuryxU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NoW7ksW8xSK Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Unprivileged container managers cannot use open_tree(OPEN_TREE_CLONE) in the host mount namespace because it requires may_mount() against the caller's current mount namespace rather than the destination namespace. Add MOVE_MOUNT_CLONE for use with MOVE_MOUNT_T_MNTNS so move_mount() can recursively loopback-clone a path from the caller's mount namespace directly into the target mount namespace file descriptor. Authorize the operation against CAP_SYS_ADMIN in target_mnt_ns->user_ns, apply CL_SLAVE | CL_COPY_UNBINDABLE | CL_LOCK_SOURCE when crossing into a foreign user namespace, and lock cross-user-namespace cloned subtrees with MNT_LOCKED inside attach_recursive_mnt(). Signed-off-by: Jarkko Sakkinen --- fs/namespace.c | 93 ++++++++++++++++++++++++++++++-------- include/uapi/linux/mount.h | 3 +- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index d73a215c9ae5..ced125eff59b 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2577,6 +2577,7 @@ enum mnt_tree_flags_t { MNT_TREE_PROPAGATION = BIT(1), MNT_TREE_MNTNS = BIT(2), MNT_TREE_MNTNS_INIT = BIT(3), + MNT_TREE_CLONE = BIT(4), }; /** @@ -3809,31 +3810,28 @@ static int do_move_mount(const struct path *old_path, { struct mount *old = real_mount(old_path->mnt); struct user_namespace *src_user_ns = NULL; + struct mount *cloned = NULL; struct mnt_namespace *dest_mnt_ns; int err; bool beneath = flags & MNT_TREE_BENEATH; - if (!path_mounted(old_path)) + if (!(flags & MNT_TREE_CLONE) && !path_mounted(old_path)) return -EINVAL; if (d_is_dir(new_path->dentry) != d_is_dir(old_path->dentry)) return -EINVAL; + if ((flags & MNT_TREE_CLONE) && + mnt_ns_loop_target(old_path->dentry, target_mnt_ns)) + return -EINVAL; + LOCK_MOUNT_MAYBE_BENEATH(mp, new_path, beneath); if (IS_ERR(mp.parent)) return PTR_ERR(mp.parent); if (flags & MNT_TREE_MNTNS) { - u64 seq; - - if (!anon_ns_root(old) || old->mnt_ns == mp.parent->mnt_ns || - mp.parent->mnt_ns != target_mnt_ns) - return -EINVAL; - seq = old->mnt_ns->seq_origin; - if (seq && seq != target_mnt_ns->ns.ns_id && - seq != current->nsproxy->mnt_ns->ns.ns_id) + if (mp.parent->mnt_ns != target_mnt_ns) return -EINVAL; - src_user_ns = old->mnt_ns->user_ns; if (flags & MNT_TREE_MNTNS_INIT) { if (mp.parent != target_mnt_ns->root) return -EBUSY; @@ -3846,6 +3844,38 @@ static int do_move_mount(const struct path *old_path, if (m == target_mnt_ns->root) return beneath ? -EINVAL : -EBUSY; } + if (flags & MNT_TREE_CLONE) { + unsigned int copy_flags; + + if (!check_mnt(old) || old->mnt_ns == target_mnt_ns || + (old->mnt.mnt_sb->s_flags & SB_NOUSER)) + return -EINVAL; + src_user_ns = old->mnt_ns->user_ns; + if (!in_userns(target_mnt_ns->user_ns, src_user_ns)) + copy_flags = CL_SLAVE | CL_COPY_UNBINDABLE | + CL_LOCK_SOURCE; + else + copy_flags = CL_COPY_MNT_NS_FILE; + cloned = __do_loopback(old_path, true, copy_flags); + if (IS_ERR(cloned)) + return PTR_ERR(cloned); + old = cloned; + if (d_is_symlink(old->mnt.mnt_root)) { + err = -EINVAL; + goto out_cloned; + } + } else { + u64 seq; + + if (!anon_ns_root(old) || + old->mnt_ns == mp.parent->mnt_ns) + return -EINVAL; + seq = old->mnt_ns->seq_origin; + if (seq && seq != target_mnt_ns->ns.ns_id && + seq != current->nsproxy->mnt_ns->ns.ns_id) + return -EINVAL; + src_user_ns = old->mnt_ns->user_ns; + } } else if (check_mnt(old)) { /* if the source is in our namespace... */ /* ... it should be detachable from parent */ @@ -3885,23 +3915,36 @@ static int do_move_mount(const struct path *old_path, over = mp.parent->overmount; err = can_move_mount_beneath(old, over, &mp); if (err) - return err; + goto out_cloned; } /* * Don't move a mount tree containing unbindable mounts to a destination * mount which is shared. */ - if (IS_MNT_SHARED(mp.parent) && tree_contains_unbindable(old)) - return -EINVAL; + if (IS_MNT_SHARED(mp.parent) && tree_contains_unbindable(old)) { + err = -EINVAL; + goto out_cloned; + } dest_mnt_ns = is_anon_ns(mp.parent->mnt_ns) ? current->nsproxy->mnt_ns : mp.parent->mnt_ns; - if (!check_for_nsfs_mounts(old, dest_mnt_ns)) - return -ELOOP; - if (mount_is_ancestor(old, mp.parent)) - return -ELOOP; + if (!check_for_nsfs_mounts(old, dest_mnt_ns)) { + err = -ELOOP; + goto out_cloned; + } + if (mount_is_ancestor(old, mp.parent)) { + err = -ELOOP; + goto out_cloned; + } - return attach_recursive_mnt(old, &mp, src_user_ns); + err = attach_recursive_mnt(old, &mp, src_user_ns); +out_cloned: + if (err && cloned) { + lock_mount_hash(); + umount_tree(cloned, UMOUNT_SYNC); + unlock_mount_hash(); + } + return err; } static int do_move_mount_old(const struct path *path, const char *old_name) @@ -4828,8 +4871,8 @@ static int lookup_move_mount_from(int from_dfd, /** * DOC: MOVE_MOUNT_T_MNTNS * - * ``MOVE_MOUNT_T_MNTNS`` installs detached anonymous mount trees into a target - * mount namespace ``nsfs`` file descriptor supplied as ``to_dfd``: + * ``MOVE_MOUNT_T_MNTNS`` installs mount trees into a target mount namespace + * ``nsfs`` file descriptor supplied as ``to_dfd``: * * 1. ``to_pathname`` of ``"/"`` attaches the initial root mount onto the * ``nullfs`` root of an empty mount namespace at most once. @@ -4837,6 +4880,11 @@ static int lookup_move_mount_from(int from_dfd, * 2. Relative ``to_pathname`` values resolve beneath the live root of the * target mount namespace. * + * 3. Combining ``MOVE_MOUNT_CLONE`` with ``MOVE_MOUNT_T_MNTNS`` recursively + * loopback-clones ``from_pathname`` from the caller's mount namespace into + * ``to_dfd`` without requiring ``OPEN_TREE_CLONE`` or ``may_mount()`` in + * the caller's own mount namespace. + * * Capability is checked against ``CAP_SYS_ADMIN`` in the target mount * namespace's owning user namespace, and source trees crossing user namespace * boundaries are locked with ``MNT_LOCKED`` and locked mount flags. @@ -4938,12 +4986,17 @@ SYSCALL_DEFINE5(move_mount, if (flags & ~MOVE_MOUNT__MASK) return -EINVAL; + if ((flags & MOVE_MOUNT_CLONE) && !(flags & MOVE_MOUNT_T_MNTNS)) + return -EINVAL; + if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) == (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) return -EINVAL; if (flags & MOVE_MOUNT_SET_GROUP) mflags |= MNT_TREE_PROPAGATION; if (flags & MOVE_MOUNT_BENEATH) mflags |= MNT_TREE_BENEATH; + if (flags & MOVE_MOUNT_CLONE) + mflags |= MNT_TREE_CLONE; if (unlikely(flags & MOVE_MOUNT_T_MNTNS)) { if (flags & (MOVE_MOUNT_T_EMPTY_PATH | MOVE_MOUNT_SET_GROUP)) diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h index 7d413418426a..cb4f7175db02 100644 --- a/include/uapi/linux/mount.h +++ b/include/uapi/linux/mount.h @@ -78,7 +78,8 @@ #define MOVE_MOUNT_T_MNTNS 0x00000080 /* Resolve to_dfd as mntns fd */ #define MOVE_MOUNT_SET_GROUP 0x00000100 /* Set sharing group instead */ #define MOVE_MOUNT_BENEATH 0x00000200 /* Mount beneath top mount */ -#define MOVE_MOUNT__MASK 0x000003f7 +#define MOVE_MOUNT_CLONE 0x00000400 /* Recursively clone from path */ +#define MOVE_MOUNT__MASK 0x000007f7 /* * fsopen() flags. -- 2.47.3