projid.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef _LINUX_PROJID_H
  2. #define _LINUX_PROJID_H
  3. /*
  4. * A set of types for the internal kernel types representing project ids.
  5. *
  6. * The types defined in this header allow distinguishing which project ids in
  7. * the kernel are values used by userspace and which project id values are
  8. * the internal kernel values. With the addition of user namespaces the values
  9. * can be different. Using the type system makes it possible for the compiler
  10. * to detect when we overlook these differences.
  11. *
  12. */
  13. #include <linux/types.h>
  14. struct user_namespace;
  15. extern struct user_namespace init_user_ns;
  16. typedef __kernel_uid32_t projid_t;
  17. typedef struct {
  18. projid_t val;
  19. } kprojid_t;
  20. static inline projid_t __kprojid_val(kprojid_t projid)
  21. {
  22. return projid.val;
  23. }
  24. #define KPROJIDT_INIT(value) (kprojid_t){ value }
  25. #define INVALID_PROJID KPROJIDT_INIT(-1)
  26. #define OVERFLOW_PROJID 65534
  27. static inline bool projid_eq(kprojid_t left, kprojid_t right)
  28. {
  29. return __kprojid_val(left) == __kprojid_val(right);
  30. }
  31. static inline bool projid_lt(kprojid_t left, kprojid_t right)
  32. {
  33. return __kprojid_val(left) < __kprojid_val(right);
  34. }
  35. static inline bool projid_valid(kprojid_t projid)
  36. {
  37. return !projid_eq(projid, INVALID_PROJID);
  38. }
  39. #ifdef CONFIG_USER_NS
  40. extern kprojid_t make_kprojid(struct user_namespace *from, projid_t projid);
  41. extern projid_t from_kprojid(struct user_namespace *to, kprojid_t projid);
  42. extern projid_t from_kprojid_munged(struct user_namespace *to, kprojid_t projid);
  43. static inline bool kprojid_has_mapping(struct user_namespace *ns, kprojid_t projid)
  44. {
  45. return from_kprojid(ns, projid) != (projid_t)-1;
  46. }
  47. #else
  48. static inline kprojid_t make_kprojid(struct user_namespace *from, projid_t projid)
  49. {
  50. return KPROJIDT_INIT(projid);
  51. }
  52. static inline projid_t from_kprojid(struct user_namespace *to, kprojid_t kprojid)
  53. {
  54. return __kprojid_val(kprojid);
  55. }
  56. static inline projid_t from_kprojid_munged(struct user_namespace *to, kprojid_t kprojid)
  57. {
  58. projid_t projid = from_kprojid(to, kprojid);
  59. if (projid == (projid_t)-1)
  60. projid = OVERFLOW_PROJID;
  61. return projid;
  62. }
  63. static inline bool kprojid_has_mapping(struct user_namespace *ns, kprojid_t projid)
  64. {
  65. return true;
  66. }
  67. #endif /* CONFIG_USER_NS */
  68. #endif /* _LINUX_PROJID_H */