user_namespace.h 3.9 KB

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