Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_UIDGID_H |
| 2 | #define _LINUX_UIDGID_H |
| 3 | |
| 4 | /* |
| 5 | * A set of types for the internal kernel types representing uids and gids. |
| 6 | * |
| 7 | * The types defined in this header allow distinguishing which uids and gids in |
| 8 | * the kernel are values used by userspace and which uid and gid 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 | #include <linux/highuid.h> |
| 16 | |
| 17 | struct user_namespace; |
| 18 | extern struct user_namespace init_user_ns; |
| 19 | |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 20 | typedef struct { |
| 21 | uid_t val; |
| 22 | } kuid_t; |
| 23 | |
| 24 | |
| 25 | typedef struct { |
| 26 | gid_t val; |
| 27 | } kgid_t; |
| 28 | |
| 29 | #define KUIDT_INIT(value) (kuid_t){ value } |
| 30 | #define KGIDT_INIT(value) (kgid_t){ value } |
| 31 | |
Iulia Manda | 2813893 | 2015-04-15 16:16:41 -0700 | [diff] [blame] | 32 | #ifdef CONFIG_MULTIUSER |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 33 | static inline uid_t __kuid_val(kuid_t uid) |
| 34 | { |
| 35 | return uid.val; |
| 36 | } |
| 37 | |
| 38 | static inline gid_t __kgid_val(kgid_t gid) |
| 39 | { |
| 40 | return gid.val; |
| 41 | } |
Iulia Manda | 2813893 | 2015-04-15 16:16:41 -0700 | [diff] [blame] | 42 | #else |
| 43 | static inline uid_t __kuid_val(kuid_t uid) |
| 44 | { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static inline gid_t __kgid_val(kgid_t gid) |
| 49 | { |
| 50 | return 0; |
| 51 | } |
| 52 | #endif |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 53 | |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 54 | #define GLOBAL_ROOT_UID KUIDT_INIT(0) |
| 55 | #define GLOBAL_ROOT_GID KGIDT_INIT(0) |
| 56 | |
| 57 | #define INVALID_UID KUIDT_INIT(-1) |
| 58 | #define INVALID_GID KGIDT_INIT(-1) |
| 59 | |
| 60 | static inline bool uid_eq(kuid_t left, kuid_t right) |
| 61 | { |
| 62 | return __kuid_val(left) == __kuid_val(right); |
| 63 | } |
| 64 | |
| 65 | static inline bool gid_eq(kgid_t left, kgid_t right) |
| 66 | { |
| 67 | return __kgid_val(left) == __kgid_val(right); |
| 68 | } |
| 69 | |
| 70 | static inline bool uid_gt(kuid_t left, kuid_t right) |
| 71 | { |
| 72 | return __kuid_val(left) > __kuid_val(right); |
| 73 | } |
| 74 | |
| 75 | static inline bool gid_gt(kgid_t left, kgid_t right) |
| 76 | { |
| 77 | return __kgid_val(left) > __kgid_val(right); |
| 78 | } |
| 79 | |
| 80 | static inline bool uid_gte(kuid_t left, kuid_t right) |
| 81 | { |
| 82 | return __kuid_val(left) >= __kuid_val(right); |
| 83 | } |
| 84 | |
| 85 | static inline bool gid_gte(kgid_t left, kgid_t right) |
| 86 | { |
| 87 | return __kgid_val(left) >= __kgid_val(right); |
| 88 | } |
| 89 | |
| 90 | static inline bool uid_lt(kuid_t left, kuid_t right) |
| 91 | { |
| 92 | return __kuid_val(left) < __kuid_val(right); |
| 93 | } |
| 94 | |
| 95 | static inline bool gid_lt(kgid_t left, kgid_t right) |
| 96 | { |
| 97 | return __kgid_val(left) < __kgid_val(right); |
| 98 | } |
| 99 | |
| 100 | static inline bool uid_lte(kuid_t left, kuid_t right) |
| 101 | { |
| 102 | return __kuid_val(left) <= __kuid_val(right); |
| 103 | } |
| 104 | |
| 105 | static inline bool gid_lte(kgid_t left, kgid_t right) |
| 106 | { |
| 107 | return __kgid_val(left) <= __kgid_val(right); |
| 108 | } |
| 109 | |
| 110 | static inline bool uid_valid(kuid_t uid) |
| 111 | { |
Josh Triplett | 929aa5b | 2015-05-14 15:17:01 -0700 | [diff] [blame] | 112 | return __kuid_val(uid) != (uid_t) -1; |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline bool gid_valid(kgid_t gid) |
| 116 | { |
Josh Triplett | 929aa5b | 2015-05-14 15:17:01 -0700 | [diff] [blame] | 117 | return __kgid_val(gid) != (gid_t) -1; |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 120 | #ifdef CONFIG_USER_NS |
| 121 | |
| 122 | extern kuid_t make_kuid(struct user_namespace *from, uid_t uid); |
| 123 | extern kgid_t make_kgid(struct user_namespace *from, gid_t gid); |
| 124 | |
| 125 | extern uid_t from_kuid(struct user_namespace *to, kuid_t uid); |
| 126 | extern gid_t from_kgid(struct user_namespace *to, kgid_t gid); |
| 127 | extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid); |
| 128 | extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid); |
| 129 | |
| 130 | static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid) |
| 131 | { |
| 132 | return from_kuid(ns, uid) != (uid_t) -1; |
| 133 | } |
| 134 | |
| 135 | static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid) |
| 136 | { |
| 137 | return from_kgid(ns, gid) != (gid_t) -1; |
| 138 | } |
| 139 | |
| 140 | #else |
| 141 | |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 142 | static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid) |
| 143 | { |
| 144 | return KUIDT_INIT(uid); |
| 145 | } |
| 146 | |
| 147 | static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid) |
| 148 | { |
| 149 | return KGIDT_INIT(gid); |
| 150 | } |
| 151 | |
| 152 | static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid) |
| 153 | { |
| 154 | return __kuid_val(kuid); |
| 155 | } |
| 156 | |
| 157 | static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid) |
| 158 | { |
| 159 | return __kgid_val(kgid); |
| 160 | } |
| 161 | |
| 162 | static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid) |
| 163 | { |
| 164 | uid_t uid = from_kuid(to, kuid); |
| 165 | if (uid == (uid_t)-1) |
| 166 | uid = overflowuid; |
| 167 | return uid; |
| 168 | } |
| 169 | |
| 170 | static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid) |
| 171 | { |
| 172 | gid_t gid = from_kgid(to, kgid); |
| 173 | if (gid == (gid_t)-1) |
| 174 | gid = overflowgid; |
| 175 | return gid; |
| 176 | } |
| 177 | |
| 178 | static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid) |
| 179 | { |
Eric W. Biederman | 37b1180 | 2016-06-27 15:52:48 -0500 | [diff] [blame] | 180 | return uid_valid(uid); |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid) |
| 184 | { |
Eric W. Biederman | 37b1180 | 2016-06-27 15:52:48 -0500 | [diff] [blame] | 185 | return gid_valid(gid); |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 188 | #endif /* CONFIG_USER_NS */ |
| 189 | |
Eric W. Biederman | 7a4e740 | 2011-11-14 14:29:51 -0800 | [diff] [blame] | 190 | #endif /* _LINUX_UIDGID_H */ |