user_namespace.h 4.3 KB

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