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