1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_USER_NAMESPACE_H
3 #define _LINUX_USER_NAMESPACE_H
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>
14 #define UID_GID_MAP_MAX_BASE_EXTENTS 5
15 #define UID_GID_MAP_MAX_EXTENTS 340
17 struct uid_gid_extent
{
23 struct uid_gid_map
{ /* 64 bytes -- 1 cache line */
26 struct uid_gid_extent extent
[UID_GID_MAP_MAX_BASE_EXTENTS
];
28 struct uid_gid_extent
*forward
;
29 struct uid_gid_extent
*reverse
;
34 #define USERNS_SETGROUPS_ALLOWED 1UL
36 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
41 UCOUNT_USER_NAMESPACES
,
42 UCOUNT_PID_NAMESPACES
,
43 UCOUNT_UTS_NAMESPACES
,
44 UCOUNT_IPC_NAMESPACES
,
45 UCOUNT_NET_NAMESPACES
,
46 UCOUNT_MNT_NAMESPACES
,
47 UCOUNT_CGROUP_NAMESPACES
,
48 UCOUNT_TIME_NAMESPACES
,
49 #ifdef CONFIG_INOTIFY_USER
50 UCOUNT_INOTIFY_INSTANCES
,
51 UCOUNT_INOTIFY_WATCHES
,
53 #ifdef CONFIG_FANOTIFY
54 UCOUNT_FANOTIFY_GROUPS
,
55 UCOUNT_FANOTIFY_MARKS
,
60 struct user_namespace
{
61 struct uid_gid_map uid_map
;
62 struct uid_gid_map gid_map
;
63 struct uid_gid_map projid_map
;
64 struct user_namespace
*parent
;
70 /* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
71 * in its effective capability set at the child ns creation time. */
72 bool parent_could_setfcap
;
75 /* List of joinable keyrings in this namespace. Modification access of
76 * these pointers is controlled by keyring_sem. Once
77 * user_keyring_register is set, it won't be changed, so it can be
78 * accessed directly with READ_ONCE().
80 struct list_head keyring_name_list
;
81 struct key
*user_keyring_register
;
82 struct rw_semaphore keyring_sem
;
85 /* Register of per-UID persistent keyrings for this namespace */
86 #ifdef CONFIG_PERSISTENT_KEYRINGS
87 struct key
*persistent_keyring_register
;
89 struct work_struct work
;
91 struct ctl_table_set set
;
92 struct ctl_table_header
*sysctls
;
94 struct ucounts
*ucounts
;
95 int ucount_max
[UCOUNT_COUNTS
];
99 struct hlist_node node
;
100 struct user_namespace
*ns
;
103 atomic_t ucount
[UCOUNT_COUNTS
];
106 extern struct user_namespace init_user_ns
;
108 bool setup_userns_sysctls(struct user_namespace
*ns
);
109 void retire_userns_sysctls(struct user_namespace
*ns
);
110 struct ucounts
*inc_ucount(struct user_namespace
*ns
, kuid_t uid
, enum ucount_type type
);
111 void dec_ucount(struct ucounts
*ucounts
, enum ucount_type type
);
113 #ifdef CONFIG_USER_NS
115 static inline struct user_namespace
*get_user_ns(struct user_namespace
*ns
)
118 refcount_inc(&ns
->ns
.count
);
122 extern int create_user_ns(struct cred
*new);
123 extern int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
);
124 extern void __put_user_ns(struct user_namespace
*ns
);
126 static inline void put_user_ns(struct user_namespace
*ns
)
128 if (ns
&& refcount_dec_and_test(&ns
->ns
.count
))
132 struct seq_operations
;
133 extern const struct seq_operations proc_uid_seq_operations
;
134 extern const struct seq_operations proc_gid_seq_operations
;
135 extern const struct seq_operations proc_projid_seq_operations
;
136 extern ssize_t
proc_uid_map_write(struct file
*, const char __user
*, size_t, loff_t
*);
137 extern ssize_t
proc_gid_map_write(struct file
*, const char __user
*, size_t, loff_t
*);
138 extern ssize_t
proc_projid_map_write(struct file
*, const char __user
*, size_t, loff_t
*);
139 extern ssize_t
proc_setgroups_write(struct file
*, const char __user
*, size_t, loff_t
*);
140 extern int proc_setgroups_show(struct seq_file
*m
, void *v
);
141 extern bool userns_may_setgroups(const struct user_namespace
*ns
);
142 extern bool in_userns(const struct user_namespace
*ancestor
,
143 const struct user_namespace
*child
);
144 extern bool current_in_userns(const struct user_namespace
*target_ns
);
145 struct ns_common
*ns_get_owner(struct ns_common
*ns
);
148 static inline struct user_namespace
*get_user_ns(struct user_namespace
*ns
)
150 return &init_user_ns
;
153 static inline int create_user_ns(struct cred
*new)
158 static inline int unshare_userns(unsigned long unshare_flags
,
159 struct cred
**new_cred
)
161 if (unshare_flags
& CLONE_NEWUSER
)
166 static inline void put_user_ns(struct user_namespace
*ns
)
170 static inline bool userns_may_setgroups(const struct user_namespace
*ns
)
175 static inline bool in_userns(const struct user_namespace
*ancestor
,
176 const struct user_namespace
*child
)
181 static inline bool current_in_userns(const struct user_namespace
*target_ns
)
186 static inline struct ns_common
*ns_get_owner(struct ns_common
*ns
)
188 return ERR_PTR(-EPERM
);
192 #endif /* _LINUX_USER_H */