user_namespace.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. };
  36. extern struct user_namespace init_user_ns;
  37. #ifdef CONFIG_USER_NS
  38. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  39. {
  40. if (ns)
  41. atomic_inc(&ns->count);
  42. return ns;
  43. }
  44. extern int create_user_ns(struct cred *new);
  45. extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
  46. extern void free_user_ns(struct user_namespace *ns);
  47. static inline void put_user_ns(struct user_namespace *ns)
  48. {
  49. if (ns && atomic_dec_and_test(&ns->count))
  50. free_user_ns(ns);
  51. }
  52. struct seq_operations;
  53. extern const struct seq_operations proc_uid_seq_operations;
  54. extern const struct seq_operations proc_gid_seq_operations;
  55. extern const struct seq_operations proc_projid_seq_operations;
  56. extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
  57. extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
  58. extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
  59. extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
  60. extern int proc_setgroups_show(struct seq_file *m, void *v);
  61. extern bool userns_may_setgroups(const struct user_namespace *ns);
  62. #else
  63. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  64. {
  65. return &init_user_ns;
  66. }
  67. static inline int create_user_ns(struct cred *new)
  68. {
  69. return -EINVAL;
  70. }
  71. static inline int unshare_userns(unsigned long unshare_flags,
  72. struct cred **new_cred)
  73. {
  74. if (unshare_flags & CLONE_NEWUSER)
  75. return -EINVAL;
  76. return 0;
  77. }
  78. static inline void put_user_ns(struct user_namespace *ns)
  79. {
  80. }
  81. static inline bool userns_may_setgroups(const struct user_namespace *ns)
  82. {
  83. return true;
  84. }
  85. #endif
  86. #endif /* _LINUX_USER_H */