blob: fb9f4f799554e13a375a9ad52ed7facb69c7d143 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Cedric Le Goateracce2922007-07-15 23:40:59 -07002#ifndef _LINUX_USER_NAMESPACE_H
3#define _LINUX_USER_NAMESPACE_H
4
5#include <linux/kref.h>
6#include <linux/nsproxy.h>
Al Viro435d5f42014-10-31 22:56:04 -04007#include <linux/ns_common.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -07008#include <linux/sched.h>
Ingo Molnarb2d5bfe2017-02-06 09:56:40 +01009#include <linux/workqueue.h>
Ingo Molnarcd9c5132017-02-08 18:51:58 +010010#include <linux/rwsem.h>
Ingo Molnarcc5efc22017-02-03 10:06:45 +010011#include <linux/sysctl.h>
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070012#include <linux/err.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070013
Christian Brauner6397fac2017-10-25 00:04:41 +020014#define UID_GID_MAP_MAX_BASE_EXTENTS 5
15#define UID_GID_MAP_MAX_EXTENTS 340
Eric W. Biederman22d917d2011-11-17 00:11:58 -080016
Christian Brauneraa4bf442017-10-25 00:04:40 +020017struct uid_gid_extent {
18 u32 first;
19 u32 lower_first;
20 u32 count;
21};
22
Christian Brauner6397fac2017-10-25 00:04:41 +020023struct uid_gid_map { /* 64 bytes -- 1 cache line */
Eric W. Biederman22d917d2011-11-17 00:11:58 -080024 u32 nr_extents;
Christian Brauneraa4bf442017-10-25 00:04:40 +020025 union {
Christian Brauner6397fac2017-10-25 00:04:41 +020026 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
Christian Brauneraa4bf442017-10-25 00:04:40 +020027 struct {
28 struct uid_gid_extent *forward;
29 struct uid_gid_extent *reverse;
30 };
31 };
Eric W. Biederman22d917d2011-11-17 00:11:58 -080032};
33
Eric W. Biederman9cc46512014-12-02 12:27:26 -060034#define USERNS_SETGROUPS_ALLOWED 1UL
35
36#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
37
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050038struct ucounts;
Eric W. Biederman25f9c082016-08-08 14:41:52 -050039
40enum ucount_type {
41 UCOUNT_USER_NAMESPACES,
Eric W. Biedermanf333c702016-08-08 14:08:36 -050042 UCOUNT_PID_NAMESPACES,
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050043 UCOUNT_UTS_NAMESPACES,
Eric W. Biedermanaba35662016-08-08 14:20:23 -050044 UCOUNT_IPC_NAMESPACES,
Eric W. Biederman70328662016-08-08 14:33:23 -050045 UCOUNT_NET_NAMESPACES,
Eric W. Biederman537f7cc2016-08-08 14:37:37 -050046 UCOUNT_MNT_NAMESPACES,
Eric W. Biedermand08311d2016-08-08 14:25:30 -050047 UCOUNT_CGROUP_NAMESPACES,
Nikolay Borisov1cce1ee2016-12-14 15:56:33 +020048#ifdef CONFIG_INOTIFY_USER
49 UCOUNT_INOTIFY_INSTANCES,
50 UCOUNT_INOTIFY_WATCHES,
51#endif
Eric W. Biederman25f9c082016-08-08 14:41:52 -050052 UCOUNT_COUNTS,
53};
54
Cedric Le Goateracce2922007-07-15 23:40:59 -070055struct user_namespace {
Eric W. Biederman22d917d2011-11-17 00:11:58 -080056 struct uid_gid_map uid_map;
57 struct uid_gid_map gid_map;
Eric W. Biedermanf76d2072012-08-30 01:24:05 -070058 struct uid_gid_map projid_map;
Eric W. Biedermanc61a2812012-12-28 18:58:39 -080059 atomic_t count;
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080060 struct user_namespace *parent;
Oleg Nesterov8742f222013-08-08 18:55:32 +020061 int level;
Eric W. Biederman783291e2011-11-17 01:32:59 -080062 kuid_t owner;
63 kgid_t group;
Al Viro435d5f42014-10-31 22:56:04 -040064 struct ns_common ns;
Eric W. Biederman9cc46512014-12-02 12:27:26 -060065 unsigned long flags;
David Howellsf36f8c72013-09-24 10:35:19 +010066
David Howellsb206f282019-06-26 21:02:32 +010067#ifdef CONFIG_KEYS
David Howells0f44e4d2019-06-26 21:02:32 +010068 /* List of joinable keyrings in this namespace. Modification access of
69 * these pointers is controlled by keyring_sem. Once
70 * user_keyring_register is set, it won't be changed, so it can be
71 * accessed directly with READ_ONCE().
72 */
David Howellsb206f282019-06-26 21:02:32 +010073 struct list_head keyring_name_list;
David Howells0f44e4d2019-06-26 21:02:32 +010074 struct key *user_keyring_register;
75 struct rw_semaphore keyring_sem;
David Howellsb206f282019-06-26 21:02:32 +010076#endif
77
David Howellsf36f8c72013-09-24 10:35:19 +010078 /* Register of per-UID persistent keyrings for this namespace */
79#ifdef CONFIG_PERSISTENT_KEYRINGS
80 struct key *persistent_keyring_register;
David Howellsf36f8c72013-09-24 10:35:19 +010081#endif
Eric W. Biedermanb0321322016-07-30 13:53:37 -050082 struct work_struct work;
Eric W. Biedermandbec2842016-07-30 13:58:49 -050083#ifdef CONFIG_SYSCTL
84 struct ctl_table_set set;
85 struct ctl_table_header *sysctls;
86#endif
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050087 struct ucounts *ucounts;
Eric W. Biederman25f9c082016-08-08 14:41:52 -050088 int ucount_max[UCOUNT_COUNTS];
Kees Cook3859a272016-10-28 01:22:25 -070089} __randomize_layout;
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050090
91struct ucounts {
92 struct hlist_node node;
93 struct user_namespace *ns;
94 kuid_t uid;
Eric W. Biederman040757f2017-03-05 15:03:22 -060095 int count;
Eric W. Biederman25f9c082016-08-08 14:41:52 -050096 atomic_t ucount[UCOUNT_COUNTS];
Cedric Le Goateracce2922007-07-15 23:40:59 -070097};
98
99extern struct user_namespace init_user_ns;
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500100
101bool setup_userns_sysctls(struct user_namespace *ns);
102void retire_userns_sysctls(struct user_namespace *ns);
Eric W. Biederman25f9c082016-08-08 14:41:52 -0500103struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
104void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700105
106#ifdef CONFIG_USER_NS
107
108static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
109{
110 if (ns)
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800111 atomic_inc(&ns->count);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700112 return ns;
113}
114
Serge Hallyn18b6e042008-10-15 16:38:45 -0500115extern int create_user_ns(struct cred *new);
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700116extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500117extern void __put_user_ns(struct user_namespace *ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700118
119static inline void put_user_ns(struct user_namespace *ns)
120{
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800121 if (ns && atomic_dec_and_test(&ns->count))
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500122 __put_user_ns(ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700123}
124
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800125struct seq_operations;
Fabian Frederickccf94f12014-08-08 14:21:22 -0700126extern const struct seq_operations proc_uid_seq_operations;
127extern const struct seq_operations proc_gid_seq_operations;
128extern const struct seq_operations proc_projid_seq_operations;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800129extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
130extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700131extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600132extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
133extern int proc_setgroups_show(struct seq_file *m, void *v);
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600134extern bool userns_may_setgroups(const struct user_namespace *ns);
Eric W. Biedermana2b42622017-04-29 14:12:15 -0500135extern bool in_userns(const struct user_namespace *ancestor,
136 const struct user_namespace *child);
Seth Forsheed07b8462015-09-23 15:16:04 -0500137extern bool current_in_userns(const struct user_namespace *target_ns);
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700138struct ns_common *ns_get_owner(struct ns_common *ns);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700139#else
140
141static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
142{
143 return &init_user_ns;
144}
145
Serge Hallyn18b6e042008-10-15 16:38:45 -0500146static inline int create_user_ns(struct cred *new)
Cedric Le Goateracce2922007-07-15 23:40:59 -0700147{
Serge Hallyn18b6e042008-10-15 16:38:45 -0500148 return -EINVAL;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700149}
150
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700151static inline int unshare_userns(unsigned long unshare_flags,
152 struct cred **new_cred)
153{
154 if (unshare_flags & CLONE_NEWUSER)
155 return -EINVAL;
156 return 0;
157}
158
Cedric Le Goateracce2922007-07-15 23:40:59 -0700159static inline void put_user_ns(struct user_namespace *ns)
160{
161}
162
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600163static inline bool userns_may_setgroups(const struct user_namespace *ns)
164{
165 return true;
166}
Seth Forsheed07b8462015-09-23 15:16:04 -0500167
Eric W. Biedermana2b42622017-04-29 14:12:15 -0500168static inline bool in_userns(const struct user_namespace *ancestor,
169 const struct user_namespace *child)
170{
171 return true;
172}
173
Seth Forsheed07b8462015-09-23 15:16:04 -0500174static inline bool current_in_userns(const struct user_namespace *target_ns)
175{
176 return true;
177}
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700178
179static inline struct ns_common *ns_get_owner(struct ns_common *ns)
180{
181 return ERR_PTR(-EPERM);
182}
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800183#endif
184
Cedric Le Goateracce2922007-07-15 23:40:59 -0700185#endif /* _LINUX_USER_H */