From patchwork Thu Jun 27 14:11:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13714463 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10C28195B33 for ; Thu, 27 Jun 2024 14:11:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497519; cv=none; b=OTfnO8vUSG/UI4UJC6vGkfGzWA9zFrPYOtOrD+pVqlAh43tK69NSoDkB+cdXlp9HojeVm7BKCzR6R7X0PzUyuhsSxwcb/cIWLEffw1SKAHZr8S41kaitJT1zvEiGtvvbOr6Dn2GK1SmEcxb9wnT01q3nryFLtjOcFjdSXhYVSc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497519; c=relaxed/simple; bh=A7w7/irj2RmpEr99sp3WGnTDNScYARDxHYfDbPIBR14=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gie8KB5bc7h1lWorDDYX7keCYydk8SzJjBVPdj7cOsgU/dmseTBtmcBXSaLrNmpRX/hsAjA8FerpQT7h0p+n9cMu35I0tzvyPKX+Jv8fN7oNNHmz7dmcPQv3qPaNdg+yF+yj34Q8HmsGZEis7tbybPXW7j8kPGe7sBdId2MP4To= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jPwDBj2I; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jPwDBj2I" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDEF2C4AF07; Thu, 27 Jun 2024 14:11:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719497518; bh=A7w7/irj2RmpEr99sp3WGnTDNScYARDxHYfDbPIBR14=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jPwDBj2IDZe2sJtjimDT//VKb6m1M+f1EzbiVSBXLaaVjJLjqzTRceqfzmRDCOfit Svqc+g91yt7qrnJRZq8kNWH4Tho8c25tAF4zSxk+KMBTxM3TV52jt31+tl9Dwl8z5e CQIiiQor30ziNX4ul+JbnpmCalpz3cbuq8WRdNYnzfDuyMxIktL6faJVIboEBJefFN gyM0c6pxBRM6mvFAazda7XFgsJEWvHitaL22J/7t7gVgB20GnMC52k8Uj/ZTfrSshM 2Mchuf+2zUxHwSwdD/s2aF3CT4CAS5+rG6d0noqR3VHK3xaM33HYDMMnJ3xJ8BvuBn OEjXl5P4nT0cQ== From: Christian Brauner Date: Thu, 27 Jun 2024 16:11:39 +0200 Subject: [PATCH RFC 1/4] file: add take_fd() cleanup helper Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240627-work-pidfs-v1-1-7e9ab6cc3bb1@kernel.org> References: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> In-Reply-To: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Josef Bacik , Seth Forshee , Stephane Graber , Jeff Layton , Aleksa Sarai , Alexander Mikhalitsyn , Christian Brauner X-Mailer: b4 0.15-dev-13183 X-Developer-Signature: v=1; a=openpgp-sha256; l=1823; i=brauner@kernel.org; h=from:subject:message-id; bh=A7w7/irj2RmpEr99sp3WGnTDNScYARDxHYfDbPIBR14=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaTVFmuGu2maqU15enxlsHtMgk2wh0DX9dmT+u7dWdK6x VDwWXRTRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwETiXjIyXNho0nzSdLlQUERq y8XP0hH6EzcrffhdFjZ1h/y1VGO9Ewz/zLZtnf+xScqsyux32pmoD/0brn83m5A1Pbg9piHc+ls NNwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add a helper that returns the file descriptor and ensures that the old variable contains a negative value. This makes it easy to rely on CLASS(get_unused_fd). Signed-off-by: Christian Brauner --- include/linux/cleanup.h | 13 ++++++++----- include/linux/file.h | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index c2d09bc4f976..80c4181e194a 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -63,17 +63,20 @@ #define __free(_name) __cleanup(__free_##_name) -#define __get_and_null_ptr(p) \ - ({ __auto_type __ptr = &(p); \ - __auto_type __val = *__ptr; \ - *__ptr = NULL; __val; }) +#define __get_and_null(p, nullvalue) \ + ({ \ + __auto_type __ptr = &(p); \ + __auto_type __val = *__ptr; \ + *__ptr = nullvalue; \ + __val; \ + }) static inline __must_check const volatile void * __must_check_fn(const volatile void *val) { return val; } #define no_free_ptr(p) \ - ((typeof(p)) __must_check_fn(__get_and_null_ptr(p))) + ((typeof(p)) __must_check_fn(__get_and_null(p, NULL))) #define return_ptr(p) return no_free_ptr(p) diff --git a/include/linux/file.h b/include/linux/file.h index 45d0f4800abd..3ea7f2452f20 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -97,6 +97,8 @@ extern void put_unused_fd(unsigned int fd); DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T), get_unused_fd_flags(flags), unsigned flags) +#define take_fd(fd) __get_and_null(fd, -EBADF) + extern void fd_install(unsigned int fd, struct file *file); int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags); From patchwork Thu Jun 27 14:11:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13714464 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 609C4195B33 for ; Thu, 27 Jun 2024 14:12:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497521; cv=none; b=q738XUWxM/zdoeuXMX/YBKfE9Z7MmKab9fyUnR7QfvsJocDqaTiVPGptPREa0Ct+wpQaPcAPP6Enwodchg4eje6UXn0B1fU1Ab1cz0kDl69hkR77VLJzrS8v+2z3g8/YJy/LJ1xm6IBCc4jDgJwqmSJkj8We9iaupK6Es+tiy4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497521; c=relaxed/simple; bh=R5+PBNxjeTS8Zs34p+Q7gbq9G9p8wRePVEYAraVFMZY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rt8ANtElLsz2WgjaU/aOhCerO8SRbpT1ZMp443bomx+Wplo/0OHSuB2h/7hOx867nzAeNHDErWalTh2wv15fmddQJN2YRMkjL88G45wDLcjDo4cDOlEv4+BMQoZ/cCovMp2+lUI/j7sAk8L28NeE1q7+mW7C+5whdPXJht/E9Es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZlLRrzI2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZlLRrzI2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28D40C32786; Thu, 27 Jun 2024 14:11:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719497521; bh=R5+PBNxjeTS8Zs34p+Q7gbq9G9p8wRePVEYAraVFMZY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZlLRrzI2HTKPkblTi4xa9pppIQt46hjNVZ+1/UIMQqEVn2lOBsuXZCE5tXfPQUDKa XfT9oJJiqLoF95lmIbWIuiuWsrrWH8GuQbxf14n3gzKpX6qowpGqN78sMJxlg6KCr9 pWBdDPdL2DmQOHbjL80QxMX67P88/Agov7ZIscL1p9/GuTvusKnunxKbGaUmWTdyrF 7KQGcaHZmfo7ziolqRegYmtLzWf09vhIWtqz4+F/shHIOht+RbgrMH7CtJ9il6ap1E Vf2+4vKG5iHbDqULaF/mQkuPGrFC2JJkubvXepqEeToVNKUBzOkRd+MF17DDkt58+v Vo6OabRZ8QG+A== From: Christian Brauner Date: Thu, 27 Jun 2024 16:11:40 +0200 Subject: [PATCH RFC 2/4] nsproxy: add a cleanup helper for nsproxy Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240627-work-pidfs-v1-2-7e9ab6cc3bb1@kernel.org> References: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> In-Reply-To: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Josef Bacik , Seth Forshee , Stephane Graber , Jeff Layton , Aleksa Sarai , Alexander Mikhalitsyn , Christian Brauner X-Mailer: b4 0.15-dev-13183 X-Developer-Signature: v=1; a=openpgp-sha256; l=545; i=brauner@kernel.org; h=from:subject:message-id; bh=R5+PBNxjeTS8Zs34p+Q7gbq9G9p8wRePVEYAraVFMZY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaTVFmt9szp+zErklLLRhl/b/SuvrFns4x5R6bPa9LRe9 htDE+HAjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgImwHWf4X9m5s/xoZzhbVeNl kQ37/6268sjquN5KN97aZ2fkXiv9n8bIcI5pYm598/2l1192Hw5+kPu9cJ3sMkGhvqcdt5nldEt NOAA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add a simple cleanup helper for nsproxy. Signed-off-by: Christian Brauner --- include/linux/nsproxy.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/nsproxy.h b/include/linux/nsproxy.h index 5601d14e2886..e6bec522b139 100644 --- a/include/linux/nsproxy.h +++ b/include/linux/nsproxy.h @@ -112,4 +112,6 @@ static inline void get_nsproxy(struct nsproxy *ns) refcount_inc(&ns->count); } +DEFINE_FREE(put_nsproxy, struct nsproxy *, if (_T) put_nsproxy(_T)) + #endif From patchwork Thu Jun 27 14:11:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13714465 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBC78198A0B for ; Thu, 27 Jun 2024 14:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497523; cv=none; b=UgofxidAn9qlNzs0BAqDgNWqXBWLqv/bWZ+BzcF7M3P6OCBUZu6ca5BYh0pMA3pcyHOOQVF8sNYkuIFHDPfeXxTyEWBspGfArFRqaohSjCyFnYpnkRy/JObxeZoWOhnsB3hltNTHF3bM4F4RwQH2V5/ho19114fZ3SEDRJADOek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497523; c=relaxed/simple; bh=nSTWSTrDybQHR+lX2cMJgo58KHuSllMHwXZNln6F9VI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BSklLjsGRj6VR4bxv/dmie4HyhKTackFFV5uC2kYujGYhSwt6w3r686vCn9492GeC8uKVj8DSrgBbfljTAG9ESJ8dbfx/of7DlEc8mJVQvyUuGIlQkay2tNTLV6V6wfPL1CFrU0nW5vV3nhqKlMGyEsypTgTXFPFhXahNydYoLU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BcYpQz2m; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BcYpQz2m" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 872AEC32786; Thu, 27 Jun 2024 14:12:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719497523; bh=nSTWSTrDybQHR+lX2cMJgo58KHuSllMHwXZNln6F9VI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BcYpQz2mg6KfF9vguIRUbb2ttbzsXifPH0X8FR142Ob9mAUO4f58MRQqbIerqpcm9 04SlQyv/6zdJB8p6je+rildwPULNBNu0Pf6ZuuYGYAjVetsLvH53Gpik+VPpqFKX+8 e78lGfYfFZhLTnlH+Clnlj/EhGldg3BYym/eG4kTZUuz1kpu8G7VghepBw6+s4ENhK Q+d1c2+kOmsZGb3R6rwzlOP6pljFpety7AdbceruJVPdnWc30JiLoLW1JWFnFi3zRa dgE9rHqd85rpHXN54CESpprzM0E4tjwItHhEXrp5AX68iPxCb78jsekeKsEQaFypMY rjcBW3PQqYR9Q== From: Christian Brauner Date: Thu, 27 Jun 2024 16:11:41 +0200 Subject: [PATCH RFC 3/4] nsfs: add open_namespace() Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240627-work-pidfs-v1-3-7e9ab6cc3bb1@kernel.org> References: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> In-Reply-To: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Josef Bacik , Seth Forshee , Stephane Graber , Jeff Layton , Aleksa Sarai , Alexander Mikhalitsyn , Christian Brauner X-Mailer: b4 0.15-dev-13183 X-Developer-Signature: v=1; a=openpgp-sha256; l=2540; i=brauner@kernel.org; h=from:subject:message-id; bh=nSTWSTrDybQHR+lX2cMJgo58KHuSllMHwXZNln6F9VI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaTVFmvtiw2t2l1/rfPgngCtW26vPp9itTSdz7j0I/uyX qNsZ9HPHaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABMpmcDI8KZ45ZwvF+8IMXSK yOUXWKzj7rn7Yc/sC7la8WW3nh28wcjwP319iVP6sbzAauMvzceEOKYvfdt46PjL6PuZtzpjCs+ eYwcA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 and call it from open_related_ns(). Signed-off-by: Christian Brauner --- fs/internal.h | 1 + fs/nsfs.c | 55 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/fs/internal.h b/fs/internal.h index ab2225136f60..1ece6a3d34cb 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -321,3 +321,4 @@ struct stashed_operations { int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data, struct path *path); void stashed_dentry_prune(struct dentry *dentry); +int open_namespace(struct ns_common *ns); diff --git a/fs/nsfs.c b/fs/nsfs.c index af352dadffe1..c35bc3ca466f 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -83,40 +83,47 @@ int ns_get_path(struct path *path, struct task_struct *task, return ns_get_path_cb(path, ns_get_path_task, &args); } -int open_related_ns(struct ns_common *ns, - struct ns_common *(*get_ns)(struct ns_common *ns)) +/** + * open_namespace - open a namespace + * @ns: the namespace to open + * + * This will consume a reference to @ns indendent of success or failure. + * + * Return: A file descriptor on success or a negative error code on failure. + */ +int open_namespace(struct ns_common *ns) { - struct path path = {}; - struct ns_common *relative; + struct path path __free(path_put) = {}; struct file *f; int err; - int fd; - fd = get_unused_fd_flags(O_CLOEXEC); + /* call first to consume reference */ + err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path); + if (err < 0) + return err; + + CLASS(get_unused_fd, fd)(O_CLOEXEC); if (fd < 0) return fd; + f = dentry_open(&path, O_RDONLY, current_cred()); + if (IS_ERR(f)) + return PTR_ERR(f); + + fd_install(fd, f); + return take_fd(fd); +} + +int open_related_ns(struct ns_common *ns, + struct ns_common *(*get_ns)(struct ns_common *ns)) +{ + struct ns_common *relative; + relative = get_ns(ns); - if (IS_ERR(relative)) { - put_unused_fd(fd); + if (IS_ERR(relative)) return PTR_ERR(relative); - } - err = path_from_stashed(&relative->stashed, nsfs_mnt, relative, &path); - if (err < 0) { - put_unused_fd(fd); - return err; - } - - f = dentry_open(&path, O_RDONLY, current_cred()); - path_put(&path); - if (IS_ERR(f)) { - put_unused_fd(fd); - fd = PTR_ERR(f); - } else - fd_install(fd, f); - - return fd; + return open_namespace(relative); } EXPORT_SYMBOL_GPL(open_related_ns); From patchwork Thu Jun 27 14:11:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13714466 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27588198A17 for ; Thu, 27 Jun 2024 14:12:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497526; cv=none; b=TJuv2dwX8fUdlv4wojPIkgfyLhFX5ksqeMOjOf8WZYTzIoaoSM/fzDadPJOxZN/zIELK5fWiefJ2kRy5ahKlj/PtENW9MCH5lpT4Z+/oAckku8mwLJGAKVKGRvdbO3W/aUNBo3JI7uhTwiI2thIKHUfyXWAGZjRDUXXDHv7bryI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719497526; c=relaxed/simple; bh=Qmbw4XMvnxGQLiSZgzcB/v1d+nHzcDHYvQenqTXMVWk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RqbRYSe2o4F1LNi4VJjhtG35JNpouzrdfCI3ByQQl0yCt7be6vI6+H0TfLwsl/PL5G+9kdzyLGkdXqFV2KrKE9lMo+XYvzmiCIX8WGc31SXqegJX4kWMRFoCS2/2KBKytElMRu6xjX/2LSTi+gh6PcET5pdqWpAt6vg65+rd6Rg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z9ccZP+6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Z9ccZP+6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE01DC4AF0A; Thu, 27 Jun 2024 14:12:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719497525; bh=Qmbw4XMvnxGQLiSZgzcB/v1d+nHzcDHYvQenqTXMVWk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Z9ccZP+6Dgy+oKtzA35duhtiyx80TMKzwcHHQ7EOevXW0sihtD/QvUkI7Z8IbA9dd Zq9Mj4nCXHALDeDZIRcdR6ePnHsmN/IrqM0GNSagJbeDuY1rPgEiu5SHTQk6SPltF2 psHLw1t+C2pyKWhUv/BJhg8N+PlcjrRj6nJX7gaCS0FDmKwyQ0jhOf/lfkbRqK0iGw BFqmydsoY9KaGH3cH5AvNEksgcF+wLdjutg4MkzKofjbAysxyubDLDMsxYPObYzjS6 Ab6tN4IPW2Kdd0ViJtdlLNAnDvbWOB4t0t78xezeeVVm1RMd8qOpv/Pmc6EjNwRXR0 dyvfh2tswfpzA== From: Christian Brauner Date: Thu, 27 Jun 2024 16:11:42 +0200 Subject: [PATCH RFC 4/4] pidfs: allow retrieval of namespace file descriptors Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240627-work-pidfs-v1-4-7e9ab6cc3bb1@kernel.org> References: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> In-Reply-To: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Josef Bacik , Seth Forshee , Stephane Graber , Jeff Layton , Aleksa Sarai , Alexander Mikhalitsyn , Christian Brauner X-Mailer: b4 0.15-dev-13183 X-Developer-Signature: v=1; a=openpgp-sha256; l=5166; i=brauner@kernel.org; h=from:subject:message-id; bh=Qmbw4XMvnxGQLiSZgzcB/v1d+nHzcDHYvQenqTXMVWk=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaTVFmuF3LEv4bbPYZXWvtGatzc+0LvkRpPlinrmBxYz3 HiVW5d3lLIwiHExyIopsji0m4TLLeep2GyUqQEzh5UJZAgDF6cATCRgG8P/oKyHLApy8ayzmqpT sxmTniS9nPxMMHizuMkHjVmh72TYGRkeTzN59vZY9xKDmRziR+3Pfgu/f+R19jq/zqR1kXdEvwS yAgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 For users that hold a reference to a pidfd procfs might not even be available nor is it desirable to parse through procfs just for the sake of getting namespace file descriptors for a process. Make it possible to directly retrieve namespace file descriptors from a pidfd. Pidfds already can be used with setns() to change a set of namespaces atomically. Signed-off-by: Christian Brauner --- fs/pidfs.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/pidfd.h | 14 +++++++ 2 files changed, 106 insertions(+) diff --git a/fs/pidfs.c b/fs/pidfs.c index dbb9d854d1c5..957284e8b2dd 100644 --- a/fs/pidfs.c +++ b/fs/pidfs.c @@ -11,10 +11,16 @@ #include #include #include +#include #include #include +#include +#include +#include +#include #include "internal.h" +#include "mount.h" #ifdef CONFIG_PROC_FS /** @@ -108,11 +114,97 @@ static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) return poll_flags; } +static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct task_struct *task __free(put_task) = NULL; + struct nsproxy *nsp __free(put_nsproxy) = NULL; + struct user_namespace *user_ns = NULL; + struct pid_namespace *pid_ns = NULL; + struct pid *pid = pidfd_pid(file); + struct ns_common *ns_common; + + if (arg) + return -EINVAL; + + task = get_pid_task(pid, PIDTYPE_PID); + if (!task) + return -ESRCH; + + scoped_guard(task_lock, task) { + nsp = task->nsproxy; + if (nsp) + get_nsproxy(nsp); + } + if (!nsp) + return -ESRCH; /* just pretend it didn't exist */ + + /* + * We're trying to open a file descriptor to the namespace so perform a + * filesystem cred ptrace check. Also, we mirror nsfs behavior. + */ + if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) + return -EACCES; + + switch (cmd) { + case PIDFD_GET_CGROUP_NAMESPACE: + get_cgroup_ns(nsp->cgroup_ns); + ns_common = &nsp->cgroup_ns->ns; + break; + case PIDFD_GET_IPC_NAMESPACE: + get_ipc_ns(nsp->ipc_ns); + ns_common = &nsp->ipc_ns->ns; + break; + case PIDFD_GET_MNT_NAMESPACE: + get_mnt_ns(nsp->mnt_ns); + ns_common = &nsp->mnt_ns->ns; + break; + case PIDFD_GET_NET_NAMESPACE: + ns_common = &nsp->net_ns->ns; + get_net_ns(ns_common); + break; + case PIDFD_GET_PID_NAMESPACE: + rcu_read_lock(); + pid_ns = get_pid_ns(task_active_pid_ns(task)); + rcu_read_unlock(); + ns_common = &pid_ns->ns; + break; + case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE: + get_pid_ns(nsp->pid_ns_for_children); + ns_common = &nsp->pid_ns_for_children->ns; + break; + case PIDFD_GET_TIME_NAMESPACE: + get_time_ns(nsp->time_ns); + ns_common = &nsp->time_ns->ns; + break; + case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE: + get_time_ns(nsp->time_ns_for_children); + ns_common = &nsp->time_ns_for_children->ns; + break; + case PIDFD_GET_USER_NAMESPACE: + rcu_read_lock(); + user_ns = get_user_ns(task_cred_xxx(task, user_ns)); + rcu_read_unlock(); + ns_common = &user_ns->ns; + break; + case PIDFD_GET_UTS_NAMESPACE: + get_uts_ns(nsp->uts_ns); + ns_common = &nsp->uts_ns->ns; + break; + default: + return -ENOIOCTLCMD; + } + + /* open_namespace() unconditionally consumes the reference */ + return open_namespace(ns_common); +} + static const struct file_operations pidfs_file_operations = { .poll = pidfd_poll, #ifdef CONFIG_PROC_FS .show_fdinfo = pidfd_show_fdinfo, #endif + .unlocked_ioctl = pidfd_ioctl, + .compat_ioctl = compat_ptr_ioctl, }; struct pid *pidfd_pid(const struct file *file) diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h index 72ec000a97cd..565fc0629fff 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -5,6 +5,7 @@ #include #include +#include /* Flags for pidfd_open(). */ #define PIDFD_NONBLOCK O_NONBLOCK @@ -15,4 +16,17 @@ #define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1) #define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2) +#define PIDFS_IOCTL_MAGIC 0xFF + +#define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1) +#define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2) +#define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3) +#define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4) +#define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5) +#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6) +#define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7) +#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8) +#define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9) +#define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10) + #endif /* _UAPI_LINUX_PIDFD_H */