user_namespace.h 6.3 KB

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