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