user_namespace.h 2.9 KB

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