0001
0002 #ifndef _LINUX_USER_NAMESPACE_H
0003 #define _LINUX_USER_NAMESPACE_H
0004
0005 #include <linux/kref.h>
0006 #include <linux/nsproxy.h>
0007 #include <linux/ns_common.h>
0008 #include <linux/sched.h>
0009 #include <linux/workqueue.h>
0010 #include <linux/rwsem.h>
0011 #include <linux/sysctl.h>
0012 #include <linux/err.h>
0013
0014 #define UID_GID_MAP_MAX_BASE_EXTENTS 5
0015 #define UID_GID_MAP_MAX_EXTENTS 340
0016
0017 struct uid_gid_extent {
0018 u32 first;
0019 u32 lower_first;
0020 u32 count;
0021 };
0022
0023 struct uid_gid_map {
0024 u32 nr_extents;
0025 union {
0026 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
0027 struct {
0028 struct uid_gid_extent *forward;
0029 struct uid_gid_extent *reverse;
0030 };
0031 };
0032 };
0033
0034 #define USERNS_SETGROUPS_ALLOWED 1UL
0035
0036 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
0037
0038 struct ucounts;
0039
0040 enum ucount_type {
0041 UCOUNT_USER_NAMESPACES,
0042 UCOUNT_PID_NAMESPACES,
0043 UCOUNT_UTS_NAMESPACES,
0044 UCOUNT_IPC_NAMESPACES,
0045 UCOUNT_NET_NAMESPACES,
0046 UCOUNT_MNT_NAMESPACES,
0047 UCOUNT_CGROUP_NAMESPACES,
0048 UCOUNT_TIME_NAMESPACES,
0049 #ifdef CONFIG_INOTIFY_USER
0050 UCOUNT_INOTIFY_INSTANCES,
0051 UCOUNT_INOTIFY_WATCHES,
0052 #endif
0053 #ifdef CONFIG_FANOTIFY
0054 UCOUNT_FANOTIFY_GROUPS,
0055 UCOUNT_FANOTIFY_MARKS,
0056 #endif
0057 UCOUNT_RLIMIT_NPROC,
0058 UCOUNT_RLIMIT_MSGQUEUE,
0059 UCOUNT_RLIMIT_SIGPENDING,
0060 UCOUNT_RLIMIT_MEMLOCK,
0061 UCOUNT_COUNTS,
0062 };
0063
0064 #define MAX_PER_NAMESPACE_UCOUNTS UCOUNT_RLIMIT_NPROC
0065
0066 struct user_namespace {
0067 struct uid_gid_map uid_map;
0068 struct uid_gid_map gid_map;
0069 struct uid_gid_map projid_map;
0070 struct user_namespace *parent;
0071 int level;
0072 kuid_t owner;
0073 kgid_t group;
0074 struct ns_common ns;
0075 unsigned long flags;
0076
0077
0078 bool parent_could_setfcap;
0079
0080 #ifdef CONFIG_KEYS
0081
0082
0083
0084
0085
0086 struct list_head keyring_name_list;
0087 struct key *user_keyring_register;
0088 struct rw_semaphore keyring_sem;
0089 #endif
0090
0091
0092 #ifdef CONFIG_PERSISTENT_KEYRINGS
0093 struct key *persistent_keyring_register;
0094 #endif
0095 struct work_struct work;
0096 #ifdef CONFIG_SYSCTL
0097 struct ctl_table_set set;
0098 struct ctl_table_header *sysctls;
0099 #endif
0100 struct ucounts *ucounts;
0101 long ucount_max[UCOUNT_COUNTS];
0102 } __randomize_layout;
0103
0104 struct ucounts {
0105 struct hlist_node node;
0106 struct user_namespace *ns;
0107 kuid_t uid;
0108 atomic_t count;
0109 atomic_long_t ucount[UCOUNT_COUNTS];
0110 };
0111
0112 extern struct user_namespace init_user_ns;
0113 extern struct ucounts init_ucounts;
0114
0115 bool setup_userns_sysctls(struct user_namespace *ns);
0116 void retire_userns_sysctls(struct user_namespace *ns);
0117 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
0118 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
0119 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid);
0120 struct ucounts * __must_check get_ucounts(struct ucounts *ucounts);
0121 void put_ucounts(struct ucounts *ucounts);
0122
0123 static inline long get_ucounts_value(struct ucounts *ucounts, enum ucount_type type)
0124 {
0125 return atomic_long_read(&ucounts->ucount[type]);
0126 }
0127
0128 long inc_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v);
0129 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v);
0130 long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum ucount_type type);
0131 void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum ucount_type type);
0132 bool is_ucounts_overlimit(struct ucounts *ucounts, enum ucount_type type, unsigned long max);
0133
0134 static inline void set_rlimit_ucount_max(struct user_namespace *ns,
0135 enum ucount_type type, unsigned long max)
0136 {
0137 ns->ucount_max[type] = max <= LONG_MAX ? max : LONG_MAX;
0138 }
0139
0140 #ifdef CONFIG_USER_NS
0141
0142 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
0143 {
0144 if (ns)
0145 refcount_inc(&ns->ns.count);
0146 return ns;
0147 }
0148
0149 extern int create_user_ns(struct cred *new);
0150 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
0151 extern void __put_user_ns(struct user_namespace *ns);
0152
0153 static inline void put_user_ns(struct user_namespace *ns)
0154 {
0155 if (ns && refcount_dec_and_test(&ns->ns.count))
0156 __put_user_ns(ns);
0157 }
0158
0159 struct seq_operations;
0160 extern const struct seq_operations proc_uid_seq_operations;
0161 extern const struct seq_operations proc_gid_seq_operations;
0162 extern const struct seq_operations proc_projid_seq_operations;
0163 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
0164 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
0165 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
0166 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
0167 extern int proc_setgroups_show(struct seq_file *m, void *v);
0168 extern bool userns_may_setgroups(const struct user_namespace *ns);
0169 extern bool in_userns(const struct user_namespace *ancestor,
0170 const struct user_namespace *child);
0171 extern bool current_in_userns(const struct user_namespace *target_ns);
0172 struct ns_common *ns_get_owner(struct ns_common *ns);
0173 #else
0174
0175 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
0176 {
0177 return &init_user_ns;
0178 }
0179
0180 static inline int create_user_ns(struct cred *new)
0181 {
0182 return -EINVAL;
0183 }
0184
0185 static inline int unshare_userns(unsigned long unshare_flags,
0186 struct cred **new_cred)
0187 {
0188 if (unshare_flags & CLONE_NEWUSER)
0189 return -EINVAL;
0190 return 0;
0191 }
0192
0193 static inline void put_user_ns(struct user_namespace *ns)
0194 {
0195 }
0196
0197 static inline bool userns_may_setgroups(const struct user_namespace *ns)
0198 {
0199 return true;
0200 }
0201
0202 static inline bool in_userns(const struct user_namespace *ancestor,
0203 const struct user_namespace *child)
0204 {
0205 return true;
0206 }
0207
0208 static inline bool current_in_userns(const struct user_namespace *target_ns)
0209 {
0210 return true;
0211 }
0212
0213 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
0214 {
0215 return ERR_PTR(-EPERM);
0216 }
0217 #endif
0218
0219 #endif