user_namespace.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/sched.h>
  6. #include <linux/err.h>
  7. #define UID_GID_MAP_MAX_EXTENTS 5
  8. struct uid_gid_map { /* 64 bytes -- 1 cache line */
  9. u32 nr_extents;
  10. struct uid_gid_extent {
  11. u32 first;
  12. u32 lower_first;
  13. u32 count;
  14. } extent[UID_GID_MAP_MAX_EXTENTS];
  15. };
  16. #define USERNS_SETGROUPS_ALLOWED 1UL
  17. #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
  18. struct user_namespace {
  19. struct uid_gid_map uid_map;
  20. struct uid_gid_map gid_map;
  21. struct uid_gid_map projid_map;
  22. atomic_t count;
  23. struct user_namespace *parent;
  24. int level;
  25. kuid_t owner;
  26. kgid_t group;
  27. unsigned int proc_inum;
  28. unsigned long flags;
  29. /* Register of per-UID persistent keyrings for this namespace */
  30. #ifdef CONFIG_PERSISTENT_KEYRINGS
  31. struct key *persistent_keyring_register;
  32. struct rw_semaphore persistent_keyring_register_sem;
  33. #endif
  34. };
  35. extern struct user_namespace init_user_ns;
  36. #ifdef CONFIG_USER_NS
  37. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  38. {
  39. if (ns)
  40. atomic_inc(&ns->count);
  41. return ns;
  42. }
  43. extern int create_user_ns(struct cred *new);
  44. extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
  45. extern void free_user_ns(struct user_namespace *ns);
  46. static inline void put_user_ns(struct user_namespace *ns)
  47. {
  48. if (ns && atomic_dec_and_test(&ns->count))
  49. free_user_ns(ns);
  50. }
  51. struct seq_operations;
  52. extern const struct seq_operations proc_uid_seq_operations;
  53. extern const struct seq_operations proc_gid_seq_operations;
  54. extern const struct seq_operations proc_projid_seq_operations;
  55. extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
  56. extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
  57. extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
  58. extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
  59. extern int proc_setgroups_show(struct seq_file *m, void *v);
  60. extern bool userns_may_setgroups(const struct user_namespace *ns);
  61. #else
  62. static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
  63. {
  64. return &init_user_ns;
  65. }
  66. static inline int create_user_ns(struct cred *new)
  67. {
  68. return -EINVAL;
  69. }
  70. static inline int unshare_userns(unsigned long unshare_flags,
  71. struct cred **new_cred)
  72. {
  73. if (unshare_flags & CLONE_NEWUSER)
  74. return -EINVAL;
  75. return 0;
  76. }
  77. static inline void put_user_ns(struct user_namespace *ns)
  78. {
  79. }
  80. static inline bool userns_may_setgroups(const struct user_namespace *ns)
  81. {
  82. return true;
  83. }
  84. #endif
  85. #endif /* _LINUX_USER_H */