user_namespace.h 3.7 KB

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