1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
261a58c6cSSukadev Bhattiprolu #ifndef _LINUX_PID_NS_H
361a58c6cSSukadev Bhattiprolu #define _LINUX_PID_NS_H
461a58c6cSSukadev Bhattiprolu
561a58c6cSSukadev Bhattiprolu #include <linux/sched.h>
6187f1882SPaul Gortmaker #include <linux/bug.h>
761a58c6cSSukadev Bhattiprolu #include <linux/mm.h>
8a27bb332SKent Overstreet #include <linux/workqueue.h>
961a58c6cSSukadev Bhattiprolu #include <linux/threads.h>
109a575a92SCedric Le Goater #include <linux/nsproxy.h>
11435d5f4bSAl Viro #include <linux/ns_common.h>
1295846ecfSGargi Sharma #include <linux/idr.h>
1361a58c6cSSukadev Bhattiprolu
1449cb2fc4SAdrian Reber /* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
1549cb2fc4SAdrian Reber #define MAX_PID_NS_LEVEL 32
1661a58c6cSSukadev Bhattiprolu
1759eda0e0SAl Viro struct fs_pin;
1820fad13aSPavel Emelyanov
19105ff533SJeff Xu #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
20202e1422SAleksa Sarai /* modes for vm.memfd_noexec sysctl */
21202e1422SAleksa Sarai #define MEMFD_NOEXEC_SCOPE_EXEC 0 /* MFD_EXEC implied if unset */
22202e1422SAleksa Sarai #define MEMFD_NOEXEC_SCOPE_NOEXEC_SEAL 1 /* MFD_NOEXEC_SEAL implied if unset */
23202e1422SAleksa Sarai #define MEMFD_NOEXEC_SCOPE_NOEXEC_ENFORCED 2 /* same as 1, except MFD_EXEC rejected */
24105ff533SJeff Xu #endif
25105ff533SJeff Xu
2661a58c6cSSukadev Bhattiprolu struct pid_namespace {
2795846ecfSGargi Sharma struct idr idr;
281adfcb03SAl Viro struct rcu_head rcu;
29e8cfbc24SGargi Sharma unsigned int pid_allocated;
3084d73786SSukadev Bhattiprolu struct task_struct *child_reaper;
31baf8f0f8SPavel Emelianov struct kmem_cache *pid_cachep;
32caafa432SPavel Emelyanov unsigned int level;
33*7863dcc7SChristian Brauner int pid_max;
34faacbfd3SPavel Emelyanov struct pid_namespace *parent;
3520fad13aSPavel Emelyanov #ifdef CONFIG_BSD_PROCESS_ACCT
3659eda0e0SAl Viro struct fs_pin *bacct;
3720fad13aSPavel Emelyanov #endif
3849f4d8b9SEric W. Biederman struct user_namespace *user_ns;
39f333c700SEric W. Biederman struct ucounts *ucounts;
40cf3f8921SDaniel Lezcano int reboot; /* group exit code if this pidns was rebooted */
41435d5f4bSAl Viro struct ns_common ns;
42*7863dcc7SChristian Brauner struct work_struct work;
43*7863dcc7SChristian Brauner #ifdef CONFIG_SYSCTL
44*7863dcc7SChristian Brauner struct ctl_table_set set;
45*7863dcc7SChristian Brauner struct ctl_table_header *sysctls;
46*7863dcc7SChristian Brauner #if defined(CONFIG_MEMFD_CREATE)
47105ff533SJeff Xu int memfd_noexec_scope;
48105ff533SJeff Xu #endif
49*7863dcc7SChristian Brauner #endif
503859a271SKees Cook } __randomize_layout;
5161a58c6cSSukadev Bhattiprolu
5261a58c6cSSukadev Bhattiprolu extern struct pid_namespace init_pid_ns;
5361a58c6cSSukadev Bhattiprolu
54e8cfbc24SGargi Sharma #define PIDNS_ADDING (1U << 31)
55c876ad76SEric W. Biederman
5657d5f66bSEric W. Biederman #ifdef CONFIG_PID_NS
get_pid_ns(struct pid_namespace * ns)57a05f7b15SPavel Emelianov static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
589a575a92SCedric Le Goater {
59b461cc03SPavel Emelyanov if (ns != &init_pid_ns)
608eb71d95SKirill Tkhai refcount_inc(&ns->ns.count);
61a05f7b15SPavel Emelianov return ns;
629a575a92SCedric Le Goater }
639a575a92SCedric Le Goater
649876cfe8SAleksa Sarai #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
pidns_memfd_noexec_scope(struct pid_namespace * ns)659876cfe8SAleksa Sarai static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
669876cfe8SAleksa Sarai {
679876cfe8SAleksa Sarai int scope = MEMFD_NOEXEC_SCOPE_EXEC;
689876cfe8SAleksa Sarai
699876cfe8SAleksa Sarai for (; ns; ns = ns->parent)
709876cfe8SAleksa Sarai scope = max(scope, READ_ONCE(ns->memfd_noexec_scope));
719876cfe8SAleksa Sarai
729876cfe8SAleksa Sarai return scope;
739876cfe8SAleksa Sarai }
749876cfe8SAleksa Sarai #else
pidns_memfd_noexec_scope(struct pid_namespace * ns)759876cfe8SAleksa Sarai static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
769876cfe8SAleksa Sarai {
779876cfe8SAleksa Sarai return 0;
789876cfe8SAleksa Sarai }
799876cfe8SAleksa Sarai #endif
809876cfe8SAleksa Sarai
8149f4d8b9SEric W. Biederman extern struct pid_namespace *copy_pid_ns(unsigned long flags,
8249f4d8b9SEric W. Biederman struct user_namespace *user_ns, struct pid_namespace *ns);
8374bd59bbSPavel Emelyanov extern void zap_pid_ns_processes(struct pid_namespace *pid_ns);
84cf3f8921SDaniel Lezcano extern int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd);
85bbc2e3efSCyrill Gorcunov extern void put_pid_ns(struct pid_namespace *ns);
869a575a92SCedric Le Goater
8757d5f66bSEric W. Biederman #else /* !CONFIG_PID_NS */
8857d5f66bSEric W. Biederman #include <linux/err.h>
8957d5f66bSEric W. Biederman
get_pid_ns(struct pid_namespace * ns)9057d5f66bSEric W. Biederman static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
9157d5f66bSEric W. Biederman {
9257d5f66bSEric W. Biederman return ns;
9357d5f66bSEric W. Biederman }
9457d5f66bSEric W. Biederman
pidns_memfd_noexec_scope(struct pid_namespace * ns)959876cfe8SAleksa Sarai static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
969876cfe8SAleksa Sarai {
979876cfe8SAleksa Sarai return 0;
989876cfe8SAleksa Sarai }
999876cfe8SAleksa Sarai
copy_pid_ns(unsigned long flags,struct user_namespace * user_ns,struct pid_namespace * ns)10049f4d8b9SEric W. Biederman static inline struct pid_namespace *copy_pid_ns(unsigned long flags,
10149f4d8b9SEric W. Biederman struct user_namespace *user_ns, struct pid_namespace *ns)
10257d5f66bSEric W. Biederman {
10357d5f66bSEric W. Biederman if (flags & CLONE_NEWPID)
10457d5f66bSEric W. Biederman ns = ERR_PTR(-EINVAL);
10557d5f66bSEric W. Biederman return ns;
10657d5f66bSEric W. Biederman }
10757d5f66bSEric W. Biederman
put_pid_ns(struct pid_namespace * ns)10857d5f66bSEric W. Biederman static inline void put_pid_ns(struct pid_namespace *ns)
10957d5f66bSEric W. Biederman {
11057d5f66bSEric W. Biederman }
11157d5f66bSEric W. Biederman
zap_pid_ns_processes(struct pid_namespace * ns)11274bd59bbSPavel Emelyanov static inline void zap_pid_ns_processes(struct pid_namespace *ns)
11374bd59bbSPavel Emelyanov {
11474bd59bbSPavel Emelyanov BUG();
11574bd59bbSPavel Emelyanov }
116cf3f8921SDaniel Lezcano
reboot_pid_ns(struct pid_namespace * pid_ns,int cmd)117cf3f8921SDaniel Lezcano static inline int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
118cf3f8921SDaniel Lezcano {
119cf3f8921SDaniel Lezcano return 0;
120cf3f8921SDaniel Lezcano }
12157d5f66bSEric W. Biederman #endif /* CONFIG_PID_NS */
12257d5f66bSEric W. Biederman
12361bce0f1SEric W. Biederman extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk);
1243ae4eed3SAdrian Bunk void pidhash_init(void);
12595846ecfSGargi Sharma void pid_idr_init(void);
126*7863dcc7SChristian Brauner int register_pidns_sysctls(struct pid_namespace *pidns);
127*7863dcc7SChristian Brauner void unregister_pidns_sysctls(struct pid_namespace *pidns);
1283ae4eed3SAdrian Bunk
task_is_in_init_pid_ns(struct task_struct * tsk)129d7e4f854SLeo Yan static inline bool task_is_in_init_pid_ns(struct task_struct *tsk)
130d7e4f854SLeo Yan {
131d7e4f854SLeo Yan return task_active_pid_ns(tsk) == &init_pid_ns;
132d7e4f854SLeo Yan }
133d7e4f854SLeo Yan
13461a58c6cSSukadev Bhattiprolu #endif /* _LINUX_PID_NS_H */
135