user_namespace.h 3.5 KB

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