xref: /linux/include/linux/pid_namespace.h (revision 9876cfe8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PID_NS_H
3 #define _LINUX_PID_NS_H
4 
5 #include <linux/sched.h>
6 #include <linux/bug.h>
7 #include <linux/mm.h>
8 #include <linux/workqueue.h>
9 #include <linux/threads.h>
10 #include <linux/nsproxy.h>
11 #include <linux/ns_common.h>
12 #include <linux/idr.h>
13 
14 /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
15 #define MAX_PID_NS_LEVEL 32
16 
17 struct fs_pin;
18 
19 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
20 /* modes for vm.memfd_noexec sysctl */
21 #define MEMFD_NOEXEC_SCOPE_EXEC			0 /* MFD_EXEC implied if unset */
22 #define MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL		1 /* MFD_NOEXEC_SEAL implied if unset */
23 #define MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED	2 /* same as 1, except MFD_EXEC rejected */
24 #endif
25 
26 struct pid_namespace {
27 	struct idr idr;
28 	struct rcu_head rcu;
29 	unsigned int pid_allocated;
30 	struct task_struct *child_reaper;
31 	struct kmem_cache *pid_cachep;
32 	unsigned int level;
33 	struct pid_namespace *parent;
34 #ifdef CONFIG_BSD_PROCESS_ACCT
35 	struct fs_pin *bacct;
36 #endif
37 	struct user_namespace *user_ns;
38 	struct ucounts *ucounts;
39 	int reboot;	/* group exit code if this pidns was rebooted */
40 	struct ns_common ns;
41 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
42 	int memfd_noexec_scope;
43 #endif
44 } __randomize_layout;
45 
46 extern struct pid_namespace init_pid_ns;
47 
48 #define PIDNS_ADDING (1U << 31)
49 
50 #ifdef CONFIG_PID_NS
get_pid_ns(struct pid_namespace * ns)51 static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
52 {
53 	if (ns != &init_pid_ns)
54 		refcount_inc(&ns->ns.count);
55 	return ns;
56 }
57 
58 #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
pidns_memfd_noexec_scope(struct pid_namespace * ns)59 static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
60 {
61 	int scope = MEMFD_NOEXEC_SCOPE_EXEC;
62 
63 	for (; ns; ns = ns->parent)
64 		scope = max(scope, READ_ONCE(ns->memfd_noexec_scope));
65 
66 	return scope;
67 }
68 #else
pidns_memfd_noexec_scope(struct pid_namespace * ns)69 static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
70 {
71 	return 0;
72 }
73 #endif
74 
75 extern struct pid_namespace *copy_pid_ns(unsigned long flags,
76 	struct user_namespace *user_ns, struct pid_namespace *ns);
77 extern void zap_pid_ns_processes(struct pid_namespace *pid_ns);
78 extern int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd);
79 extern void put_pid_ns(struct pid_namespace *ns);
80 
81 #else /* !CONFIG_PID_NS */
82 #include <linux/err.h>
83 
get_pid_ns(struct pid_namespace * ns)84 static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
85 {
86 	return ns;
87 }
88 
pidns_memfd_noexec_scope(struct pid_namespace * ns)89 static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
90 {
91 	return 0;
92 }
93 
copy_pid_ns(unsigned long flags,struct user_namespace * user_ns,struct pid_namespace * ns)94 static inline struct pid_namespace *copy_pid_ns(unsigned long flags,
95 	struct user_namespace *user_ns, struct pid_namespace *ns)
96 {
97 	if (flags & CLONE_NEWPID)
98 		ns = ERR_PTR(-EINVAL);
99 	return ns;
100 }
101 
put_pid_ns(struct pid_namespace * ns)102 static inline void put_pid_ns(struct pid_namespace *ns)
103 {
104 }
105 
zap_pid_ns_processes(struct pid_namespace * ns)106 static inline void zap_pid_ns_processes(struct pid_namespace *ns)
107 {
108 	BUG();
109 }
110 
reboot_pid_ns(struct pid_namespace * pid_ns,int cmd)111 static inline int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
112 {
113 	return 0;
114 }
115 #endif /* CONFIG_PID_NS */
116 
117 extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk);
118 void pidhash_init(void);
119 void pid_idr_init(void);
120 
task_is_in_init_pid_ns(struct task_struct * tsk)121 static inline bool task_is_in_init_pid_ns(struct task_struct *tsk)
122 {
123 	return task_active_pid_ns(tsk) == &init_pid_ns;
124 }
125 
126 #endif /* _LINUX_PID_NS_H */
127