at v5.12 5.0 kB view raw
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 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; 30 }; 31 }; 32}; 33 34#define USERNS_SETGROUPS_ALLOWED 1UL 35 36#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED 37 38struct ucounts; 39 40enum 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 UCOUNT_TIME_NAMESPACES, 49#ifdef CONFIG_INOTIFY_USER 50 UCOUNT_INOTIFY_INSTANCES, 51 UCOUNT_INOTIFY_WATCHES, 52#endif 53 UCOUNT_COUNTS, 54}; 55 56struct user_namespace { 57 struct uid_gid_map uid_map; 58 struct uid_gid_map gid_map; 59 struct uid_gid_map projid_map; 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; 66 /* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP 67 * in its effective capability set at the child ns creation time. */ 68 bool parent_could_setfcap; 69 70#ifdef CONFIG_KEYS 71 /* List of joinable keyrings in this namespace. Modification access of 72 * these pointers is controlled by keyring_sem. Once 73 * user_keyring_register is set, it won't be changed, so it can be 74 * accessed directly with READ_ONCE(). 75 */ 76 struct list_head keyring_name_list; 77 struct key *user_keyring_register; 78 struct rw_semaphore keyring_sem; 79#endif 80 81 /* Register of per-UID persistent keyrings for this namespace */ 82#ifdef CONFIG_PERSISTENT_KEYRINGS 83 struct key *persistent_keyring_register; 84#endif 85 struct work_struct work; 86#ifdef CONFIG_SYSCTL 87 struct ctl_table_set set; 88 struct ctl_table_header *sysctls; 89#endif 90 struct ucounts *ucounts; 91 int ucount_max[UCOUNT_COUNTS]; 92} __randomize_layout; 93 94struct ucounts { 95 struct hlist_node node; 96 struct user_namespace *ns; 97 kuid_t uid; 98 int count; 99 atomic_t ucount[UCOUNT_COUNTS]; 100}; 101 102extern struct user_namespace init_user_ns; 103 104bool setup_userns_sysctls(struct user_namespace *ns); 105void retire_userns_sysctls(struct user_namespace *ns); 106struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); 107void dec_ucount(struct ucounts *ucounts, enum ucount_type type); 108 109#ifdef CONFIG_USER_NS 110 111static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 112{ 113 if (ns) 114 refcount_inc(&ns->ns.count); 115 return ns; 116} 117 118extern int create_user_ns(struct cred *new); 119extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); 120extern void __put_user_ns(struct user_namespace *ns); 121 122static inline void put_user_ns(struct user_namespace *ns) 123{ 124 if (ns && refcount_dec_and_test(&ns->ns.count)) 125 __put_user_ns(ns); 126} 127 128struct seq_operations; 129extern const struct seq_operations proc_uid_seq_operations; 130extern const struct seq_operations proc_gid_seq_operations; 131extern const struct seq_operations proc_projid_seq_operations; 132extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *); 133extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *); 134extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *); 135extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); 136extern int proc_setgroups_show(struct seq_file *m, void *v); 137extern bool userns_may_setgroups(const struct user_namespace *ns); 138extern bool in_userns(const struct user_namespace *ancestor, 139 const struct user_namespace *child); 140extern bool current_in_userns(const struct user_namespace *target_ns); 141struct ns_common *ns_get_owner(struct ns_common *ns); 142#else 143 144static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 145{ 146 return &init_user_ns; 147} 148 149static inline int create_user_ns(struct cred *new) 150{ 151 return -EINVAL; 152} 153 154static inline int unshare_userns(unsigned long unshare_flags, 155 struct cred **new_cred) 156{ 157 if (unshare_flags & CLONE_NEWUSER) 158 return -EINVAL; 159 return 0; 160} 161 162static inline void put_user_ns(struct user_namespace *ns) 163{ 164} 165 166static inline bool userns_may_setgroups(const struct user_namespace *ns) 167{ 168 return true; 169} 170 171static inline bool in_userns(const struct user_namespace *ancestor, 172 const struct user_namespace *child) 173{ 174 return true; 175} 176 177static inline bool current_in_userns(const struct user_namespace *target_ns) 178{ 179 return true; 180} 181 182static inline struct ns_common *ns_get_owner(struct ns_common *ns) 183{ 184 return ERR_PTR(-EPERM); 185} 186#endif 187 188#endif /* _LINUX_USER_H */