]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/user_namespace.h
ACPI: fix acpi_find_child_device() invocation in acpi_preset_companion()
[mirror_ubuntu-bionic-kernel.git] / include / linux / user_namespace.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
acce292c
CLG
2#ifndef _LINUX_USER_NAMESPACE_H
3#define _LINUX_USER_NAMESPACE_H
4
5#include <linux/kref.h>
6#include <linux/nsproxy.h>
435d5f4b 7#include <linux/ns_common.h>
acce292c 8#include <linux/sched.h>
b2d5bfea 9#include <linux/workqueue.h>
cd9c513b 10#include <linux/rwsem.h>
cc5efc23 11#include <linux/sysctl.h>
77ec739d 12#include <linux/err.h>
acce292c 13
6397fac4
CB
14#define UID_GID_MAP_MAX_BASE_EXTENTS 5
15#define UID_GID_MAP_MAX_EXTENTS 340
22d917d8 16
aa4bf44d
CB
17struct uid_gid_extent {
18 u32 first;
19 u32 lower_first;
20 u32 count;
21};
22
6397fac4 23struct uid_gid_map { /* 64 bytes -- 1 cache line */
22d917d8 24 u32 nr_extents;
aa4bf44d 25 union {
6397fac4 26 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
aa4bf44d
CB
27 struct {
28 struct uid_gid_extent *forward;
29 struct uid_gid_extent *reverse;
30 };
31 };
22d917d8
EB
32};
33
9cc46516
EB
34#define USERNS_SETGROUPS_ALLOWED 1UL
35
36#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
37
f6b2db1a 38struct ucounts;
25f9c081
EB
39
40enum ucount_type {
41 UCOUNT_USER_NAMESPACES,
f333c700 42 UCOUNT_PID_NAMESPACES,
f7af3d1c 43 UCOUNT_UTS_NAMESPACES,
aba35661 44 UCOUNT_IPC_NAMESPACES,
70328660 45 UCOUNT_NET_NAMESPACES,
537f7ccb 46 UCOUNT_MNT_NAMESPACES,
d08311dd 47 UCOUNT_CGROUP_NAMESPACES,
1cce1eea
NB
48#ifdef CONFIG_INOTIFY_USER
49 UCOUNT_INOTIFY_INSTANCES,
50 UCOUNT_INOTIFY_WATCHES,
51#endif
25f9c081
EB
52 UCOUNT_COUNTS,
53};
54
acce292c 55struct user_namespace {
22d917d8
EB
56 struct uid_gid_map uid_map;
57 struct uid_gid_map gid_map;
f76d207a 58 struct uid_gid_map projid_map;
c61a2810 59 atomic_t count;
aeb3ae9d 60 struct user_namespace *parent;
8742f229 61 int level;
783291e6
EB
62 kuid_t owner;
63 kgid_t group;
435d5f4b 64 struct ns_common ns;
9cc46516 65 unsigned long flags;
f36f8c75
DH
66
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
b032132c 72 struct work_struct work;
dbec2846
EB
73#ifdef CONFIG_SYSCTL
74 struct ctl_table_set set;
75 struct ctl_table_header *sysctls;
76#endif
f6b2db1a 77 struct ucounts *ucounts;
25f9c081 78 int ucount_max[UCOUNT_COUNTS];
3859a271 79} __randomize_layout;
f6b2db1a
EB
80
81struct ucounts {
82 struct hlist_node node;
83 struct user_namespace *ns;
84 kuid_t uid;
040757f7 85 int count;
25f9c081 86 atomic_t ucount[UCOUNT_COUNTS];
acce292c
CLG
87};
88
89extern struct user_namespace init_user_ns;
f6b2db1a
EB
90
91bool setup_userns_sysctls(struct user_namespace *ns);
92void retire_userns_sysctls(struct user_namespace *ns);
25f9c081
EB
93struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
94void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
acce292c
CLG
95
96#ifdef CONFIG_USER_NS
97
98static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
99{
100 if (ns)
c61a2810 101 atomic_inc(&ns->count);
acce292c
CLG
102 return ns;
103}
104
18b6e041 105extern int create_user_ns(struct cred *new);
b2e0d987 106extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
b032132c 107extern void __put_user_ns(struct user_namespace *ns);
acce292c
CLG
108
109static inline void put_user_ns(struct user_namespace *ns)
110{
c61a2810 111 if (ns && atomic_dec_and_test(&ns->count))
b032132c 112 __put_user_ns(ns);
acce292c
CLG
113}
114
22d917d8 115struct seq_operations;
ccf94f1b
FF
116extern const struct seq_operations proc_uid_seq_operations;
117extern const struct seq_operations proc_gid_seq_operations;
118extern const struct seq_operations proc_projid_seq_operations;
22d917d8
EB
119extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
120extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
f76d207a 121extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
9cc46516
EB
122extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
123extern int proc_setgroups_show(struct seq_file *m, void *v);
273d2c67 124extern bool userns_may_setgroups(const struct user_namespace *ns);
a2b42626
EB
125extern bool in_userns(const struct user_namespace *ancestor,
126 const struct user_namespace *child);
d07b846f 127extern bool current_in_userns(const struct user_namespace *target_ns);
bcac25a5 128struct ns_common *ns_get_owner(struct ns_common *ns);
acce292c
CLG
129#else
130
131static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
132{
133 return &init_user_ns;
134}
135
18b6e041 136static inline int create_user_ns(struct cred *new)
acce292c 137{
18b6e041 138 return -EINVAL;
acce292c
CLG
139}
140
b2e0d987
EB
141static inline int unshare_userns(unsigned long unshare_flags,
142 struct cred **new_cred)
143{
144 if (unshare_flags & CLONE_NEWUSER)
145 return -EINVAL;
146 return 0;
147}
148
acce292c
CLG
149static inline void put_user_ns(struct user_namespace *ns)
150{
151}
152
273d2c67
EB
153static inline bool userns_may_setgroups(const struct user_namespace *ns)
154{
155 return true;
156}
d07b846f 157
a2b42626
EB
158static inline bool in_userns(const struct user_namespace *ancestor,
159 const struct user_namespace *child)
160{
161 return true;
162}
163
d07b846f
SF
164static inline bool current_in_userns(const struct user_namespace *target_ns)
165{
166 return true;
167}
bcac25a5
AV
168
169static inline struct ns_common *ns_get_owner(struct ns_common *ns)
170{
171 return ERR_PTR(-EPERM);
172}
22d917d8
EB
173#endif
174
acce292c 175#endif /* _LINUX_USER_H */