Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / include / linux / user_namespace.h
blobd6b74b91096b4db0f1fdca10990f9f21bb42d690
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 {
18 u32 first;
19 u32 lower_first;
20 u32 count;
23 struct uid_gid_map { /* 64 bytes -- 1 cache line */
24 u32 nr_extents;
25 union {
26 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
27 struct {
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
38 struct ucounts;
40 enum ucount_type {
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 #ifdef CONFIG_INOTIFY_USER
49 UCOUNT_INOTIFY_INSTANCES,
50 UCOUNT_INOTIFY_WATCHES,
51 #endif
52 UCOUNT_COUNTS,
55 struct user_namespace {
56 struct uid_gid_map uid_map;
57 struct uid_gid_map gid_map;
58 struct uid_gid_map projid_map;
59 atomic_t count;
60 struct user_namespace *parent;
61 int level;
62 kuid_t owner;
63 kgid_t group;
64 struct ns_common ns;
65 unsigned long flags;
67 /* Register of per-UID persistent keyrings for this namespace */
68 #ifdef CONFIG_PERSISTENT_KEYRINGS
69 struct key *persistent_keyring_register;
70 struct rw_semaphore persistent_keyring_register_sem;
71 #endif
72 struct work_struct work;
73 #ifdef CONFIG_SYSCTL
74 struct ctl_table_set set;
75 struct ctl_table_header *sysctls;
76 #endif
77 struct ucounts *ucounts;
78 int ucount_max[UCOUNT_COUNTS];
79 } __randomize_layout;
81 struct ucounts {
82 struct hlist_node node;
83 struct user_namespace *ns;
84 kuid_t uid;
85 int count;
86 atomic_t ucount[UCOUNT_COUNTS];
89 extern struct user_namespace init_user_ns;
91 bool setup_userns_sysctls(struct user_namespace *ns);
92 void retire_userns_sysctls(struct user_namespace *ns);
93 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
94 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
96 #ifdef CONFIG_USER_NS
98 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
100 if (ns)
101 atomic_inc(&ns->count);
102 return ns;
105 extern int create_user_ns(struct cred *new);
106 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
107 extern void __put_user_ns(struct user_namespace *ns);
109 static inline void put_user_ns(struct user_namespace *ns)
111 if (ns && atomic_dec_and_test(&ns->count))
112 __put_user_ns(ns);
115 struct seq_operations;
116 extern const struct seq_operations proc_uid_seq_operations;
117 extern const struct seq_operations proc_gid_seq_operations;
118 extern const struct seq_operations proc_projid_seq_operations;
119 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
120 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
121 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
122 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
123 extern int proc_setgroups_show(struct seq_file *m, void *v);
124 extern bool userns_may_setgroups(const struct user_namespace *ns);
125 extern bool in_userns(const struct user_namespace *ancestor,
126 const struct user_namespace *child);
127 extern bool current_in_userns(const struct user_namespace *target_ns);
128 struct ns_common *ns_get_owner(struct ns_common *ns);
129 #else
131 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
133 return &init_user_ns;
136 static inline int create_user_ns(struct cred *new)
138 return -EINVAL;
141 static inline int unshare_userns(unsigned long unshare_flags,
142 struct cred **new_cred)
144 if (unshare_flags & CLONE_NEWUSER)
145 return -EINVAL;
146 return 0;
149 static inline void put_user_ns(struct user_namespace *ns)
153 static inline bool userns_may_setgroups(const struct user_namespace *ns)
155 return true;
158 static inline bool in_userns(const struct user_namespace *ancestor,
159 const struct user_namespace *child)
161 return true;
164 static inline bool current_in_userns(const struct user_namespace *target_ns)
166 return true;
169 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
171 return ERR_PTR(-EPERM);
173 #endif
175 #endif /* _LINUX_USER_H */