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