From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87FD930E84A for ; Tue, 18 Nov 2025 08:49:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763455749; cv=none; b=CZOI7oMgkpDmxrgDxE1nL2i96xRjZvwLfJI7xROKE+up3xCWel4Trr+uUHWvrdk4+QFrhAwBWh1h4kuAg/iY3r9pKjjQ63Pvq404FuqxdiZ/39ZepfbYKvtyzLsKYHqMOv/7usFClY0tawcK5J7wefAJGMATF8U+45m9gaS1R+8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763455749; c=relaxed/simple; bh=W3x1PTmtZ3Z/D0FhN+NbR9sOC5qsrgt2Qb/qnCIsgHs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dkAs7h9CMKM4TWmfvbrcjsI9twy6mEzosiOCS6zzqvUmANneNZYnPDepVjORhlkpXc8J3L6XJ8p98OQGTebNhAlBg2qYKJbMf9yKlnB2zrp92PWO1QyhJZohfTWel1Gk2kh0ruY+QcJGao56luKrpenKHvTZlwmIppR50HnThrA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hYnDJYYj; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hYnDJYYj" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-343684a06b2so4754677a91.1 for ; Tue, 18 Nov 2025 00:49:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763455747; x=1764060547; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fd67JIqjBnffJd56/mtC8XiwZ8ONmUWOMWGf8uhbslw=; b=hYnDJYYjztFLehNvvggdpiPi+A3DnHQlzkq11sYu+bRiK73JoKWzeQpKcbEvFnaxL/ U5Jai57YIB6W0aFbOG0E6Bmxs+GV/aPnq4Z4EweQupAOPSemLZBWD6LP9mMfucz+GjjC xJlfgNl0GGtDqEmjLQsRX7hytp+6tFFC4YkaHtHj5MjJwrw7m1De2+VAq//VMube3mcK vlnQxwKsmDA5d1GGkpZvfNNr0K1Hylgqnqu+ZZVYGVVgfr2eVPyzzP8bG4w/61AGZmwJ qNndiqLh51u+FHvQ+rfNPsdxJmTxSSDYQMYSRKS42MoG/QOrcC84jEFFqP0NyJNZAWqD zjwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763455747; x=1764060547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fd67JIqjBnffJd56/mtC8XiwZ8ONmUWOMWGf8uhbslw=; b=rR6ES1BYqutF27LgusfkZYQhOl3Ma7ZHcDHKoV1ICS2yNzy3TjIjePsLlmpkLPsfXy tW/hjcLSWi7H42PTukXlqBQ+A+Cj2GHzy6jx+oBIhmfUKbr4bqLy+47XHV/eeH4bkHfG DRANS+AwRsAg5+9KgXQSEPsoqMXiOtObe3FFqZQiyTmJfnKH4eHOk2IyxsnxUGRc4jZs SrnVEg7c0Lk9OuCjLYBh77128di/3gO8JQRREn4RnFH7YJ+KlM7XudBXpzywQCi7+MHh IZwiGf2YX6rsJ/LLZ+f6mfwmdS59owBWK1aBWkSoQulRCGxKuhykl4WEQTFTtATduKwz 1qSg== X-Forwarded-Encrypted: i=1; AJvYcCUtAEZyQPeon5TxRjgZ8b8lTMX+Ly/yghejrlk6rB6ATDCpahJDb3u/RRGuR+n+Tek17rAO@lists.linux.dev X-Gm-Message-State: AOJu0YyRU4h69QYQ6WH/idEt38qJT1popq9IsAcBsj7ifnYZzQHt9z9F HkaY3eX+wIzEShZwnypP3MhjcRVSCAJInlXKv2VdgJH4OfbGm27d3aqQ X-Gm-Gg: ASbGncvnnNErundXy9bc69aJ6TMmLaCVEZLKGPhW0d0f2j1Re4TCTZzmxOiq7t/5HlC eaKnGJG0QH3yWlDV9hqEtTAb5oYJV0jfiIz7TuBXSae0S70Q5/hxdYxd+ewnjGt7mtbzg8aalxp dFnfdCAckiHYDW6zrSjEFxOeaf+L1RVEUXhzQkvcPCF9I833fw97NA5hRZghcbQT71c+VIHJQZK t1PLMZp+wgwcAzGLl6feqTsrJEEKBNJDLRBT2Jvtensz9OigQ0tSy6stQHh5hrCK41gTHym9X0k xUmfLfOTbnu5w7u6Elra5zFHlISCnjciNXCONMW1DnkowHueNuzeKNiHNzLTSSG6flDz/KmVsWv kFxHwyoIW2rtmJW4shQBCaNVQdqEbAE8xITolx6yhNPFWlKixvx8kvfKd5jjJKGlj/wQamaBhbI 2LfdYR6bkfy0w= X-Google-Smtp-Source: AGHT+IEgBwuS1DJAsmgPcY9fauxnHgEWvKncpN2aql5DMZ9kFcZP+AJ8w+3vmDc0Q8yljTSaUfNvQw== X-Received: by 2002:a17:90b:4d0d:b0:343:688e:3252 with SMTP id 98e67ed59e1d1-343f9ebca28mr17752724a91.12.1763455746633; Tue, 18 Nov 2025 00:49:06 -0800 (PST) Received: from fedora ([2405:201:3017:184:2d1c:8c4c:2945:3f7c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-345b01d934csm964041a91.1.2025.11.18.00.49.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 00:49:06 -0800 (PST) From: Bhavik Sachdev To: Alexander Viro , Christian Brauner Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, criu@lists.linux.dev, Aleksa Sarai , Bhavik Sachdev , Pavel Tikhomirov , Jan Kara , John Garry , Arnaldo Carvalho de Melo , "Darrick J . Wong" , Namhyung Kim , Ingo Molnar , Andrei Vagin , Alexander Mikhalitsyn , Miklos Szeredi Subject: [PATCH v6 2/2] statmount: accept fd as a parameter Date: Tue, 18 Nov 2025 14:16:42 +0530 Message-ID: <20251118084836.2114503-3-b.sachdev1904@gmail.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251118084836.2114503-1-b.sachdev1904@gmail.com> References: <20251118084836.2114503-1-b.sachdev1904@gmail.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Extend `struct mnt_id_req` to take in a fd and introduce STATMOUNT_BY_FD flag. When a valid fd is provided and STATMOUNT_BY_FD is set, statmount will return mountinfo about the mount the fd is on. This even works for "unmounted" mounts (mounts that have been umounted using umount2(mnt, MNT_DETACH)), if you have access to a file descriptor on that mount. These "umounted" mounts will have no mountpoint and no valid mount namespace. Hence, we unset the STATMOUNT_MNT_POINT and STATMOUNT_MNT_NS_ID in statmount.mask for "unmounted" mounts. In case of STATMOUNT_BY_FD, given that we already have access to an fd on the mount, accessing mount information without a capability check seems fine because of the following reasons: - All fs related information is available via fstatfs() without any capability check. - Mount information is also available via /proc/pid/mountinfo (without any capability check). - Given that we have access to a fd on the mount which tells us that we had access to the mount at some point (or someone that had access gave us the fd). So, we should be able to access mount info. Co-developed-by: Pavel Tikhomirov Signed-off-by: Pavel Tikhomirov Signed-off-by: Bhavik Sachdev --- fs/namespace.c | 99 ++++++++++++++++++++++++++------------ include/uapi/linux/mount.h | 7 ++- 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index ee36d67f1ac2..1c41c6e2304a 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -5563,29 +5563,41 @@ static int grab_requested_root(struct mnt_namespace *ns, struct path *root) /* locks: namespace_shared */ static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id, - struct mnt_namespace *ns) + struct mnt_namespace *ns, unsigned int flags) { struct mount *m; int err; - /* Has the namespace already been emptied? */ - if (mnt_ns_id && mnt_ns_empty(ns)) - return -ENOENT; + /* caller sets s->mnt in case of STATMOUNT_BY_FD */ + if (!(flags & STATMOUNT_BY_FD)) { + /* Has the namespace already been emptied? */ + if (mnt_ns_id && mnt_ns_empty(ns)) + return -ENOENT; - s->mnt = lookup_mnt_in_ns(mnt_id, ns); - if (!s->mnt) - return -ENOENT; + s->mnt = lookup_mnt_in_ns(mnt_id, ns); + if (!s->mnt) + return -ENOENT; + } - err = grab_requested_root(ns, &s->root); - if (err) - return err; + if (ns) { + err = grab_requested_root(ns, &s->root); + if (err) + return err; + } else { + /* + * We can't set mount point and mnt_ns_id since we don't have a + * ns for the mount. This can happen if the mount is unmounted + * with MNT_DETACH. + */ + s->mask &= ~(STATMOUNT_MNT_POINT | STATMOUNT_MNT_NS_ID); + } /* * Don't trigger audit denials. We just want to determine what * mounts to show users. */ m = real_mount(s->mnt); - if (!is_path_reachable(m, m->mnt.mnt_root, &s->root) && + if (ns && !is_path_reachable(m, m->mnt.mnt_root, &s->root) && !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) return -EPERM; @@ -5709,7 +5721,7 @@ static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq, } static int copy_mnt_id_req(const struct mnt_id_req __user *req, - struct mnt_id_req *kreq) + struct mnt_id_req *kreq, unsigned int flags) { int ret; size_t usize; @@ -5727,11 +5739,18 @@ static int copy_mnt_id_req(const struct mnt_id_req __user *req, ret = copy_struct_from_user(kreq, sizeof(*kreq), req, usize); if (ret) return ret; - if (kreq->mnt_ns_fd != 0 && kreq->mnt_ns_id) - return -EINVAL; - /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */ - if (kreq->mnt_id <= MNT_UNIQUE_ID_OFFSET) - return -EINVAL; + + if (flags & STATMOUNT_BY_FD) { + if (kreq->mnt_id || kreq->mnt_ns_id) + return -EINVAL; + } else { + if (kreq->fd != 0 && kreq->mnt_ns_id) + return -EINVAL; + + /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */ + if (kreq->mnt_id <= MNT_UNIQUE_ID_OFFSET) + return -EINVAL; + } return 0; } @@ -5740,16 +5759,18 @@ static int copy_mnt_id_req(const struct mnt_id_req __user *req, * that, or if not simply grab a passive reference on our mount namespace and * return that. */ -static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq) +static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq, + unsigned int flags) { struct mnt_namespace *mnt_ns; if (kreq->mnt_ns_id) { mnt_ns = lookup_mnt_ns(kreq->mnt_ns_id); - } else if (kreq->mnt_ns_fd) { + /* caller sets mnt_ns in case of STATMOUNT_BY_FD */ + } else if (!(flags & STATMOUNT_BY_FD) && kreq->fd) { struct ns_common *ns; - CLASS(fd, f)(kreq->mnt_ns_fd); + CLASS(fd, f)(kreq->fd); if (fd_empty(f)) return ERR_PTR(-EBADF); @@ -5777,25 +5798,38 @@ SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req, { struct mnt_namespace *ns __free(mnt_ns_release) = NULL; struct kstatmount *ks __free(kfree) = NULL; + struct file *file_from_fd __free(fput) = NULL; + struct vfsmount *fd_mnt; struct mnt_id_req kreq; /* We currently support retrieval of 3 strings. */ size_t seq_size = 3 * PATH_MAX; int ret; - if (flags) + if (flags & ~STATMOUNT_BY_FD) return -EINVAL; - ret = copy_mnt_id_req(req, &kreq); + ret = copy_mnt_id_req(req, &kreq, flags); if (ret) return ret; - ns = grab_requested_mnt_ns(&kreq); - if (IS_ERR(ns)) - return PTR_ERR(ns); + if (flags & STATMOUNT_BY_FD) { + file_from_fd = fget_raw(kreq.fd); + if (!file_from_fd) + return -EBADF; - if (kreq.mnt_ns_id && (ns != current->nsproxy->mnt_ns) && - !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) - return -EPERM; + fd_mnt = file_from_fd->f_path.mnt; + ns = real_mount(fd_mnt)->mnt_ns; + if (ns) + refcount_inc(&ns->passive); + } else { + ns = grab_requested_mnt_ns(&kreq, 0); + if (IS_ERR(ns)) + return PTR_ERR(ns); + + if (kreq.mnt_ns_id && (ns != current->nsproxy->mnt_ns) && + !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + } ks = kmalloc(sizeof(*ks), GFP_KERNEL_ACCOUNT); if (!ks) @@ -5806,8 +5840,11 @@ SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req, if (ret) return ret; + if (flags & STATMOUNT_BY_FD) + ks->mnt = fd_mnt; + scoped_guard(namespace_shared) - ret = do_statmount(ks, kreq.mnt_id, kreq.mnt_ns_id, ns); + ret = do_statmount(ks, kreq.mnt_id, kreq.mnt_ns_id, ns, flags); if (!ret) ret = copy_statmount_to_user(ks); @@ -5916,7 +5953,7 @@ static inline int prepare_klistmount(struct klistmount *kls, struct mnt_id_req * if (!kls->kmnt_ids) return -ENOMEM; - ns = grab_requested_mnt_ns(kreq); + ns = grab_requested_mnt_ns(kreq, 0); if (IS_ERR(ns)) return PTR_ERR(ns); kls->ns = ns; @@ -5947,7 +5984,7 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, if (!access_ok(mnt_ids, nr_mnt_ids * sizeof(*mnt_ids))) return -EFAULT; - ret = copy_mnt_id_req(req, &kreq); + ret = copy_mnt_id_req(req, &kreq, 0); if (ret) return ret; diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h index 5d3f8c9e3a62..a2156599ddc6 100644 --- a/include/uapi/linux/mount.h +++ b/include/uapi/linux/mount.h @@ -197,7 +197,7 @@ struct statmount { */ struct mnt_id_req { __u32 size; - __u32 mnt_ns_fd; + __u32 fd; __u64 mnt_id; __u64 param; __u64 mnt_ns_id; @@ -232,4 +232,9 @@ struct mnt_id_req { #define LSMT_ROOT 0xffffffffffffffff /* root mount */ #define LISTMOUNT_REVERSE (1 << 0) /* List later mounts first */ +/* + * @flag bits for statmount(2) + */ +#define STATMOUNT_BY_FD 0x00000001U /* want mountinfo for given fd */ + #endif /* _UAPI_LINUX_MOUNT_H */ -- 2.51.1