user_namespace.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #define UID_GID_MAP_MAX_BASE_EXTENTS 5
  13. #define UID_GID_MAP_MAX_EXTENTS 340
  14. struct uid_gid_extent {
  15. u32 first;
  16. u32 lower_first;
  17. u32 count;
  18. };
  19. struct uid_gid_map { /* 64 bytes -- 1 cache line */
  20. u32 nr_extents;
  21. union {
  22. struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
  23. struct {
  24. struct uid_gid_extent *forward;
  25. struct uid_gid_extent *reverse;
  26. };
  27. };
  28. };
  29. #define USERNS_SETGROUPS_ALLOWED 1UL
  30. #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
  31. struct ucounts;
  32. enum ucount_type {
  33. UCOUNT_USER_NAMESPACES,
  34. UCOUNT_PID_NAMESPACES,
  35. UCOUNT_UTS_NAMESPACES,
  36. UCOUNT_IPC_NAMESPACES,
  37. UCOUNT_NET_NAMESPACES,
  38. UCOUNT_MNT_NAMESPACES,
  39. UCOUNT_CGROUP_NAMESPACES,
  40. #ifdef CONFIG_INOTIFY_USER
  41. UCOUNT_INOTIFY_INSTANCES,
  42. UCOUNT_INOTIFY_WATCHES,
  43. #endif
  44. UCOUNT_COUNTS,
  45. };
  46. struct user_namespace {
  47. struct uid_gid_map uid_map;
  48. struct uid_gid_map gid_map;
  49. struct uid_gid_map projid_map;
  50. atomic_t count;
  51. struct user_namespace *parent;
  52. int level;
  53. kuid_t owner;
  54. kgid_t group;
  55. struct ns_common ns;
  56. unsigned long flags;
  57. #ifdef CONFIG_KEYS
  58. /* List of joinable keyrings in this namespace. Modification access of
  59. * these pointers is controlled by keyring_sem. Once
  60. * user_keyring_register is set, it won't be changed, so it can be
  61. * accessed directly with READ_ONCE().
  62. */
  63. struct list_head keyring_name_list;
  64. struct key *user_keyring_register;
  65. struct rw_semaphore keyring_sem;
  66. #endif
  67. /* Register of per-UID persistent keyrings for this namespace */
  68. #ifdef CONFIG_PERSISTENT_KEYRINGS
  69. struct key *persistent_keyring_register;
  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. struct ucounts {
  80. struct hlist_node node;
  81. struct user_namespace *ns;
  82. kuid_t uid;
  83. int count;
  84. atomic_t ucount[UCOUNT_COUNTS];
  85. };
  86. extern struct user_namespace init_user_ns;
  87. bool setup_userns_sysctls(struct user_namespace *ns);
  88. void retire_userns_sysctls(struct user_namespace *ns);
  89. struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
  90. void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
  91. #ifdef CONFIG_USER_NS
  92. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  93. {
  94. if (ns)
  95. atomic_inc(&ns->count);
  96. return ns;
  97. }
  98. extern int create_user_ns(struct cred *new);
  99. extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
  100. extern void __put_user_ns(struct user_namespace *ns);
  101. static inline void put_user_ns(struct user_namespace *ns)
  102. {
  103. if (ns && atomic_dec_and_test(&ns->count))
  104. __put_user_ns(ns);
  105. }
  106. struct seq_operations;
  107. extern const struct seq_operations proc_uid_seq_operations;
  108. extern const struct seq_operations proc_gid_seq_operations;
  109. extern const struct seq_operations proc_projid_seq_operations;
  110. extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
  111. extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
  112. extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
  113. extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
  114. extern int proc_setgroups_show(struct seq_file *m, void *v);
  115. extern bool userns_may_setgroups(const struct user_namespace *ns);
  116. extern bool in_userns(const struct user_namespace *ancestor,
  117. const struct user_namespace *child);
  118. extern bool current_in_userns(const struct user_namespace *target_ns);
  119. struct ns_common *ns_get_owner(struct ns_common *ns);
  120. #else
  121. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  122. {
  123. return &init_user_ns;
  124. }
  125. static inline int create_user_ns(struct cred *new)
  126. {
  127. return -EINVAL;
  128. }
  129. static inline int unshare_userns(unsigned long unshare_flags,
  130. struct cred **new_cred)
  131. {
  132. if (unshare_flags & CLONE_NEWUSER)
  133. return -EINVAL;
  134. return 0;
  135. }
  136. static inline void put_user_ns(struct user_namespace *ns)
  137. {
  138. }
  139. static inline bool userns_may_setgroups(const struct user_namespace *ns)
  140. {
  141. return true;
  142. }
  143. static inline bool in_userns(const struct user_namespace *ancestor,
  144. const struct user_namespace *child)
  145. {
  146. return true;
  147. }
  148. static inline bool current_in_userns(const struct user_namespace *target_ns)
  149. {
  150. return true;
  151. }
  152. static inline struct ns_common *ns_get_owner(struct ns_common *ns)
  153. {
  154. return ERR_PTR(-EPERM);
  155. }
  156. #endif
  157. #endif /* _LINUX_USER_H */