1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_USER_NAMESPACE_H 3#define _LINUX_USER_NAMESPACE_H 4 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> 13 14#define UID_GID_MAP_MAX_BASE_EXTENTS 5 15#define UID_GID_MAP_MAX_EXTENTS 340 16 17struct uid_gid_extent { 18 u32 first; 19 u32 lower_first; 20 u32 count; 21}; 22 23struct uid_gid_map { /* 64 bytes -- 1 cache line */ 24 union { 25 struct { 26 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS]; 27 u32 nr_extents; 28 }; 29 struct { 30 struct uid_gid_extent *forward; 31 struct uid_gid_extent *reverse; 32 }; 33 }; 34}; 35 36#define USERNS_SETGROUPS_ALLOWED 1UL 37 38#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED 39 40struct ucounts; 41 42enum ucount_type { 43 UCOUNT_USER_NAMESPACES, 44 UCOUNT_PID_NAMESPACES, 45 UCOUNT_UTS_NAMESPACES, 46 UCOUNT_IPC_NAMESPACES, 47 UCOUNT_NET_NAMESPACES, 48 UCOUNT_MNT_NAMESPACES, 49 UCOUNT_CGROUP_NAMESPACES, 50 UCOUNT_TIME_NAMESPACES, 51#ifdef CONFIG_INOTIFY_USER 52 UCOUNT_INOTIFY_INSTANCES, 53 UCOUNT_INOTIFY_WATCHES, 54#endif 55#ifdef CONFIG_FANOTIFY 56 UCOUNT_FANOTIFY_GROUPS, 57 UCOUNT_FANOTIFY_MARKS, 58#endif 59 UCOUNT_COUNTS, 60}; 61 62enum rlimit_type { 63 UCOUNT_RLIMIT_NPROC, 64 UCOUNT_RLIMIT_MSGQUEUE, 65 UCOUNT_RLIMIT_SIGPENDING, 66 UCOUNT_RLIMIT_MEMLOCK, 67 UCOUNT_RLIMIT_COUNTS, 68}; 69 70#if IS_ENABLED(CONFIG_BINFMT_MISC) 71struct binfmt_misc; 72#endif 73 74struct user_namespace { 75 struct uid_gid_map uid_map; 76 struct uid_gid_map gid_map; 77 struct uid_gid_map projid_map; 78 struct user_namespace *parent; 79 int level; 80 kuid_t owner; 81 kgid_t group; 82 struct ns_common ns; 83 unsigned long flags; 84 /* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP 85 * in its effective capability set at the child ns creation time. */ 86 bool parent_could_setfcap; 87 88#ifdef CONFIG_KEYS 89 /* List of joinable keyrings in this namespace. Modification access of 90 * these pointers is controlled by keyring_sem. Once 91 * user_keyring_register is set, it won't be changed, so it can be 92 * accessed directly with READ_ONCE(). 93 */ 94 struct list_head keyring_name_list; 95 struct key *user_keyring_register; 96 struct rw_semaphore keyring_sem; 97#endif 98 99 /* Register of per-UID persistent keyrings for this namespace */ 100#ifdef CONFIG_PERSISTENT_KEYRINGS 101 struct key *persistent_keyring_register; 102#endif 103 struct work_struct work; 104#ifdef CONFIG_SYSCTL 105 struct ctl_table_set set; 106 struct ctl_table_header *sysctls; 107#endif 108 struct ucounts *ucounts; 109 long ucount_max[UCOUNT_COUNTS]; 110 long rlimit_max[UCOUNT_RLIMIT_COUNTS]; 111 112#if IS_ENABLED(CONFIG_BINFMT_MISC) 113 struct binfmt_misc *binfmt_misc; 114#endif 115} __randomize_layout; 116 117struct ucounts { 118 struct hlist_node node; 119 struct user_namespace *ns; 120 kuid_t uid; 121 atomic_t count; 122 atomic_long_t ucount[UCOUNT_COUNTS]; 123 atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS]; 124}; 125 126extern struct user_namespace init_user_ns; 127extern struct ucounts init_ucounts; 128 129bool setup_userns_sysctls(struct user_namespace *ns); 130void retire_userns_sysctls(struct user_namespace *ns); 131struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); 132void dec_ucount(struct ucounts *ucounts, enum ucount_type type); 133struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid); 134struct ucounts * __must_check get_ucounts(struct ucounts *ucounts); 135void put_ucounts(struct ucounts *ucounts); 136 137static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type type) 138{ 139 return atomic_long_read(&ucounts->rlimit[type]); 140} 141 142long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v); 143bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v); 144long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type); 145void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type); 146bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long max); 147 148static inline long get_userns_rlimit_max(struct user_namespace *ns, enum rlimit_type type) 149{ 150 return READ_ONCE(ns->rlimit_max[type]); 151} 152 153static inline void set_userns_rlimit_max(struct user_namespace *ns, 154 enum rlimit_type type, unsigned long max) 155{ 156 ns->rlimit_max[type] = max <= LONG_MAX ? max : LONG_MAX; 157} 158 159#ifdef CONFIG_USER_NS 160 161static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 162{ 163 if (ns) 164 refcount_inc(&ns->ns.count); 165 return ns; 166} 167 168extern int create_user_ns(struct cred *new); 169extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); 170extern void __put_user_ns(struct user_namespace *ns); 171 172static inline void put_user_ns(struct user_namespace *ns) 173{ 174 if (ns && refcount_dec_and_test(&ns->ns.count)) 175 __put_user_ns(ns); 176} 177 178struct seq_operations; 179extern const struct seq_operations proc_uid_seq_operations; 180extern const struct seq_operations proc_gid_seq_operations; 181extern const struct seq_operations proc_projid_seq_operations; 182extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *); 183extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *); 184extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *); 185extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); 186extern int proc_setgroups_show(struct seq_file *m, void *v); 187extern bool userns_may_setgroups(const struct user_namespace *ns); 188extern bool in_userns(const struct user_namespace *ancestor, 189 const struct user_namespace *child); 190extern bool current_in_userns(const struct user_namespace *target_ns); 191struct ns_common *ns_get_owner(struct ns_common *ns); 192#else 193 194static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 195{ 196 return &init_user_ns; 197} 198 199static inline int create_user_ns(struct cred *new) 200{ 201 return -EINVAL; 202} 203 204static inline int unshare_userns(unsigned long unshare_flags, 205 struct cred **new_cred) 206{ 207 if (unshare_flags & CLONE_NEWUSER) 208 return -EINVAL; 209 return 0; 210} 211 212static inline void put_user_ns(struct user_namespace *ns) 213{ 214} 215 216static inline bool userns_may_setgroups(const struct user_namespace *ns) 217{ 218 return true; 219} 220 221static inline bool in_userns(const struct user_namespace *ancestor, 222 const struct user_namespace *child) 223{ 224 return true; 225} 226 227static inline bool current_in_userns(const struct user_namespace *target_ns) 228{ 229 return true; 230} 231 232static inline struct ns_common *ns_get_owner(struct ns_common *ns) 233{ 234 return ERR_PTR(-EPERM); 235} 236#endif 237 238#endif /* _LINUX_USER_H */