user_namespace.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /* Register of per-UID persistent keyrings for this namespace */
  58. #ifdef CONFIG_PERSISTENT_KEYRINGS
  59. struct key *persistent_keyring_register;
  60. struct rw_semaphore persistent_keyring_register_sem;
  61. #endif
  62. struct work_struct work;
  63. #ifdef CONFIG_SYSCTL
  64. struct ctl_table_set set;
  65. struct ctl_table_header *sysctls;
  66. #endif
  67. struct ucounts *ucounts;
  68. int ucount_max[UCOUNT_COUNTS];
  69. } __randomize_layout;
  70. struct ucounts {
  71. struct hlist_node node;
  72. struct user_namespace *ns;
  73. kuid_t uid;
  74. int count;
  75. atomic_t ucount[UCOUNT_COUNTS];
  76. };
  77. extern struct user_namespace init_user_ns;
  78. bool setup_userns_sysctls(struct user_namespace *ns);
  79. void retire_userns_sysctls(struct user_namespace *ns);
  80. struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
  81. void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
  82. #ifdef CONFIG_USER_NS
  83. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  84. {
  85. if (ns)
  86. atomic_inc(&ns->count);
  87. return ns;
  88. }
  89. extern int create_user_ns(struct cred *new);
  90. extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
  91. extern void __put_user_ns(struct user_namespace *ns);
  92. static inline void put_user_ns(struct user_namespace *ns)
  93. {
  94. if (ns && atomic_dec_and_test(&ns->count))
  95. __put_user_ns(ns);
  96. }
  97. struct seq_operations;
  98. extern const struct seq_operations proc_uid_seq_operations;
  99. extern const struct seq_operations proc_gid_seq_operations;
  100. extern const struct seq_operations proc_projid_seq_operations;
  101. extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
  102. extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
  103. extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
  104. extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
  105. extern int proc_setgroups_show(struct seq_file *m, void *v);
  106. extern bool userns_may_setgroups(const struct user_namespace *ns);
  107. extern bool in_userns(const struct user_namespace *ancestor,
  108. const struct user_namespace *child);
  109. extern bool current_in_userns(const struct user_namespace *target_ns);
  110. struct ns_common *ns_get_owner(struct ns_common *ns);
  111. #else
  112. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  113. {
  114. return &init_user_ns;
  115. }
  116. static inline int create_user_ns(struct cred *new)
  117. {
  118. return -EINVAL;
  119. }
  120. static inline int unshare_userns(unsigned long unshare_flags,
  121. struct cred **new_cred)
  122. {
  123. if (unshare_flags & CLONE_NEWUSER)
  124. return -EINVAL;
  125. return 0;
  126. }
  127. static inline void put_user_ns(struct user_namespace *ns)
  128. {
  129. }
  130. static inline bool userns_may_setgroups(const struct user_namespace *ns)
  131. {
  132. return true;
  133. }
  134. static inline bool in_userns(const struct user_namespace *ancestor,
  135. const struct user_namespace *child)
  136. {
  137. return true;
  138. }
  139. static inline bool current_in_userns(const struct user_namespace *target_ns)
  140. {
  141. return true;
  142. }
  143. static inline struct ns_common *ns_get_owner(struct ns_common *ns)
  144. {
  145. return ERR_PTR(-EPERM);
  146. }
  147. #endif
  148. #endif /* _LINUX_USER_H */