1 #ifndef _LINUX_USER_NAMESPACE_H 2 #define _LINUX_USER_NAMESPACE_H 3 4 #include <linux/kref.h> 5 #include <linux/nsproxy.h> 6 #include <linux/ns_common.h> 7 #include <linux/sched.h> 8 #include <linux/workqueue.h> 9 #include <linux/rwsem.h> 10 #include <linux/sysctl.h> 11 #include <linux/err.h> 12 13 #define UID_GID_MAP_MAX_BASE_EXTENTS 5 14 #define UID_GID_MAP_MAX_EXTENTS 340 15 16 struct uid_gid_extent { 17 u32 first; 18 u32 lower_first; 19 u32 count; 20 }; 21 22 struct uid_gid_map { /* 64 bytes -- 1 cache line */ 23 u32 nr_extents; 24 union { 25 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS]; 26 struct { 27 struct uid_gid_extent *forward; 28 struct uid_gid_extent *reverse; 29 }; 30 }; 31 }; 32 33 #define USERNS_SETGROUPS_ALLOWED 1UL 34 35 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED 36 37 struct ucounts; 38 39 enum ucount_type { 40 UCOUNT_USER_NAMESPACES, 41 UCOUNT_PID_NAMESPACES, 42 UCOUNT_UTS_NAMESPACES, 43 UCOUNT_IPC_NAMESPACES, 44 UCOUNT_NET_NAMESPACES, 45 UCOUNT_MNT_NAMESPACES, 46 UCOUNT_CGROUP_NAMESPACES, 47 #ifdef CONFIG_INOTIFY_USER 48 UCOUNT_INOTIFY_INSTANCES, 49 UCOUNT_INOTIFY_WATCHES, 50 #endif 51 UCOUNT_COUNTS, 52 }; 53 54 struct user_namespace { 55 struct uid_gid_map uid_map; 56 struct uid_gid_map gid_map; 57 struct uid_gid_map projid_map; 58 atomic_t count; 59 struct user_namespace *parent; 60 int level; 61 kuid_t owner; 62 kgid_t group; 63 struct ns_common ns; 64 unsigned long flags; 65 66 /* Register of per-UID persistent keyrings for this namespace */ 67 #ifdef CONFIG_PERSISTENT_KEYRINGS 68 struct key *persistent_keyring_register; 69 struct rw_semaphore persistent_keyring_register_sem; 70 #endif 71 struct work_struct work; 72 #ifdef CONFIG_SYSCTL 73 struct ctl_table_set set; 74 struct ctl_table_header *sysctls; 75 #endif 76 struct ucounts *ucounts; 77 int ucount_max[UCOUNT_COUNTS]; 78 } __randomize_layout; 79 80 struct ucounts { 81 struct hlist_node node; 82 struct user_namespace *ns; 83 kuid_t uid; 84 int count; 85 atomic_t ucount[UCOUNT_COUNTS]; 86 }; 87 88 extern struct user_namespace init_user_ns; 89 90 bool setup_userns_sysctls(struct user_namespace *ns); 91 void retire_userns_sysctls(struct user_namespace *ns); 92 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); 93 void dec_ucount(struct ucounts *ucounts, enum ucount_type type); 94 95 #ifdef CONFIG_USER_NS 96 97 static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 98 { 99 if (ns) 100 atomic_inc(&ns->count); 101 return ns; 102 } 103 104 extern int create_user_ns(struct cred *new); 105 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); 106 extern void __put_user_ns(struct user_namespace *ns); 107 108 static inline void put_user_ns(struct user_namespace *ns) 109 { 110 if (ns && atomic_dec_and_test(&ns->count)) 111 __put_user_ns(ns); 112 } 113 114 struct seq_operations; 115 extern const struct seq_operations proc_uid_seq_operations; 116 extern const struct seq_operations proc_gid_seq_operations; 117 extern const struct seq_operations proc_projid_seq_operations; 118 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *); 119 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *); 120 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *); 121 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); 122 extern int proc_setgroups_show(struct seq_file *m, void *v); 123 extern bool userns_may_setgroups(const struct user_namespace *ns); 124 extern bool in_userns(const struct user_namespace *ancestor, 125 const struct user_namespace *child); 126 extern bool current_in_userns(const struct user_namespace *target_ns); 127 struct ns_common *ns_get_owner(struct ns_common *ns); 128 #else 129 130 static inline struct user_namespace *get_user_ns(struct user_namespace *ns) 131 { 132 return &init_user_ns; 133 } 134 135 static inline int create_user_ns(struct cred *new) 136 { 137 return -EINVAL; 138 } 139 140 static inline int unshare_userns(unsigned long unshare_flags, 141 struct cred **new_cred) 142 { 143 if (unshare_flags & CLONE_NEWUSER) 144 return -EINVAL; 145 return 0; 146 } 147 148 static inline void put_user_ns(struct user_namespace *ns) 149 { 150 } 151 152 static inline bool userns_may_setgroups(const struct user_namespace *ns) 153 { 154 return true; 155 } 156 157 static inline bool in_userns(const struct user_namespace *ancestor, 158 const struct user_namespace *child) 159 { 160 return true; 161 } 162 163 static inline bool current_in_userns(const struct user_namespace *target_ns) 164 { 165 return true; 166 } 167 168 static inline struct ns_common *ns_get_owner(struct ns_common *ns) 169 { 170 return ERR_PTR(-EPERM); 171 } 172 #endif 173 174 #endif /* _LINUX_USER_H */ 175