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