1
2#ifndef _LINUX_USER_NAMESPACE_H
3#define _LINUX_USER_NAMESPACE_H
4
5#include <linux/kref.h>
6#include <linux/nsproxy.h>
7#include <linux/ns_common.h>
8#include <linux/sched.h>
9#include <linux/workqueue.h>
10#include <linux/rwsem.h>
11#include <linux/sysctl.h>
12#include <linux/err.h>
13
14#define UID_GID_MAP_MAX_EXTENTS 5
15
16struct uid_gid_map {
17 u32 nr_extents;
18 struct uid_gid_extent {
19 u32 first;
20 u32 lower_first;
21 u32 count;
22 } extent[UID_GID_MAP_MAX_EXTENTS];
23};
24
25#define USERNS_SETGROUPS_ALLOWED 1UL
26
27#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
28
29struct ucounts;
30
31enum ucount_type {
32 UCOUNT_USER_NAMESPACES,
33 UCOUNT_PID_NAMESPACES,
34 UCOUNT_UTS_NAMESPACES,
35 UCOUNT_IPC_NAMESPACES,
36 UCOUNT_NET_NAMESPACES,
37 UCOUNT_MNT_NAMESPACES,
38 UCOUNT_CGROUP_NAMESPACES,
39#ifdef CONFIG_INOTIFY_USER
40 UCOUNT_INOTIFY_INSTANCES,
41 UCOUNT_INOTIFY_WATCHES,
42#endif
43 UCOUNT_COUNTS,
44};
45
46struct user_namespace {
47 struct uid_gid_map uid_map;
48 struct uid_gid_map gid_map;
49 struct uid_gid_map projid_map;
50 atomic_t count;
51 struct user_namespace *parent;
52 int level;
53 kuid_t owner;
54 kgid_t group;
55 struct ns_common ns;
56 unsigned long flags;
57
58
59#ifdef CONFIG_PERSISTENT_KEYRINGS
60 struct key *persistent_keyring_register;
61 struct rw_semaphore persistent_keyring_register_sem;
62#endif
63 struct work_struct work;
64#ifdef CONFIG_SYSCTL
65 struct ctl_table_set set;
66 struct ctl_table_header *sysctls;
67#endif
68 struct ucounts *ucounts;
69 int ucount_max[UCOUNT_COUNTS];
70} __randomize_layout;
71
72struct ucounts {
73 struct hlist_node node;
74 struct user_namespace *ns;
75 kuid_t uid;
76 int count;
77 atomic_t ucount[UCOUNT_COUNTS];
78};
79
80extern struct user_namespace init_user_ns;
81
82bool setup_userns_sysctls(struct user_namespace *ns);
83void retire_userns_sysctls(struct user_namespace *ns);
84struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
85void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
86
87#ifdef CONFIG_USER_NS
88
89static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
90{
91 if (ns)
92 atomic_inc(&ns->count);
93 return ns;
94}
95
96extern int create_user_ns(struct cred *new);
97extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
98extern void __put_user_ns(struct user_namespace *ns);
99
100static inline void put_user_ns(struct user_namespace *ns)
101{
102 if (ns && atomic_dec_and_test(&ns->count))
103 __put_user_ns(ns);
104}
105
106struct seq_operations;
107extern const struct seq_operations proc_uid_seq_operations;
108extern const struct seq_operations proc_gid_seq_operations;
109extern const struct seq_operations proc_projid_seq_operations;
110extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
111extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
112extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
113extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
114extern int proc_setgroups_show(struct seq_file *m, void *v);
115extern bool userns_may_setgroups(const struct user_namespace *ns);
116extern bool in_userns(const struct user_namespace *ancestor,
117 const struct user_namespace *child);
118extern bool current_in_userns(const struct user_namespace *target_ns);
119struct ns_common *ns_get_owner(struct ns_common *ns);
120#else
121
122static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
123{
124 return &init_user_ns;
125}
126
127static inline int create_user_ns(struct cred *new)
128{
129 return -EINVAL;
130}
131
132static inline int unshare_userns(unsigned long unshare_flags,
133 struct cred **new_cred)
134{
135 if (unshare_flags & CLONE_NEWUSER)
136 return -EINVAL;
137 return 0;
138}
139
140static inline void put_user_ns(struct user_namespace *ns)
141{
142}
143
144static inline bool userns_may_setgroups(const struct user_namespace *ns)
145{
146 return true;
147}
148
149static inline bool in_userns(const struct user_namespace *ancestor,
150 const struct user_namespace *child)
151{
152 return true;
153}
154
155static inline bool current_in_userns(const struct user_namespace *target_ns)
156{
157 return true;
158}
159
160static inline struct ns_common *ns_get_owner(struct ns_common *ns)
161{
162 return ERR_PTR(-EPERM);
163}
164#endif
165
166#endif
167