]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
nsfs: add current_in_namespace()
authorChristian Brauner <brauner@kernel.org>
Fri, 12 Sep 2025 11:52:50 +0000 (13:52 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 19 Sep 2025 12:26:16 +0000 (14:26 +0200)
Add a helper to easily check whether a given namespace is the caller's
current namespace. This is currently open-coded in a lot of places.
Simply switch on the type and compare the results.

Reviewed-by: Aleksa Sarai <cyphar@cyphar.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/nsfs.h

index fb84aa538091ff9af11062bc16214cce28c13114..e5a5fa83d36bde36bae454806830e35d9f2b6390 100644 (file)
@@ -5,6 +5,8 @@
 #define _LINUX_NSFS_H
 
 #include <linux/ns_common.h>
+#include <linux/cred.h>
+#include <linux/pid_namespace.h>
 
 struct path;
 struct task_struct;
@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task,
                        const struct proc_ns_operations *ns_ops);
 void nsfs_init(void);
 
-#endif /* _LINUX_NSFS_H */
+#define __current_namespace_from_type(__ns)                            \
+       _Generic((__ns),                                                \
+               struct cgroup_namespace *: current->nsproxy->cgroup_ns, \
+               struct ipc_namespace *:    current->nsproxy->ipc_ns,    \
+               struct net *:              current->nsproxy->net_ns,    \
+               struct pid_namespace *:    task_active_pid_ns(current), \
+               struct mnt_namespace *:    current->nsproxy->mnt_ns,    \
+               struct time_namespace *:   current->nsproxy->time_ns,   \
+               struct user_namespace *:   current_user_ns(),           \
+               struct uts_namespace *:    current->nsproxy->uts_ns)
+
+#define current_in_namespace(__ns) (__current_namespace_from_type(__ns) == __ns)
 
+#endif /* _LINUX_NSFS_H */