user_namespace.h 3.8 KB

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