1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_UIDGID_H 3 #define _LINUX_UIDGID_H 4 5 /* 6 * A set of types for the internal kernel types representing uids and gids. 7 * 8 * The types defined in this header allow distinguishing which uids and gids in 9 * the kernel are values used by userspace and which uid and gid values are 10 * the internal kernel values. With the addition of user namespaces the values 11 * can be different. Using the type system makes it possible for the compiler 12 * to detect when we overlook these differences. 13 * 14 */ 15 #include <linux/uidgid_types.h> 16 #include <linux/highuid.h> 17 18 struct user_namespace; 19 extern struct user_namespace init_user_ns; 20 struct uid_gid_map; 21 22 #define KUIDT_INIT(value) (kuid_t){ value } 23 #define KGIDT_INIT(value) (kgid_t){ value } 24 25 #ifdef CONFIG_MULTIUSER 26 static inline uid_t __kuid_val(kuid_t uid) 27 { 28 return uid.val; 29 } 30 31 static inline gid_t __kgid_val(kgid_t gid) 32 { 33 return gid.val; 34 } 35 #else 36 static inline uid_t __kuid_val(kuid_t uid) 37 { 38 return 0; 39 } 40 41 static inline gid_t __kgid_val(kgid_t gid) 42 { 43 return 0; 44 } 45 #endif 46 47 #define GLOBAL_ROOT_UID KUIDT_INIT(0) 48 #define GLOBAL_ROOT_GID KGIDT_INIT(0) 49 50 #define INVALID_UID KUIDT_INIT(-1) 51 #define INVALID_GID KGIDT_INIT(-1) 52 53 static inline bool uid_eq(kuid_t left, kuid_t right) 54 { 55 return __kuid_val(left) == __kuid_val(right); 56 } 57 58 static inline bool gid_eq(kgid_t left, kgid_t right) 59 { 60 return __kgid_val(left) == __kgid_val(right); 61 } 62 63 static inline bool uid_gt(kuid_t left, kuid_t right) 64 { 65 return __kuid_val(left) > __kuid_val(right); 66 } 67 68 static inline bool gid_gt(kgid_t left, kgid_t right) 69 { 70 return __kgid_val(left) > __kgid_val(right); 71 } 72 73 static inline bool uid_gte(kuid_t left, kuid_t right) 74 { 75 return __kuid_val(left) >= __kuid_val(right); 76 } 77 78 static inline bool gid_gte(kgid_t left, kgid_t right) 79 { 80 return __kgid_val(left) >= __kgid_val(right); 81 } 82 83 static inline bool uid_lt(kuid_t left, kuid_t right) 84 { 85 return __kuid_val(left) < __kuid_val(right); 86 } 87 88 static inline bool gid_lt(kgid_t left, kgid_t right) 89 { 90 return __kgid_val(left) < __kgid_val(right); 91 } 92 93 static inline bool uid_lte(kuid_t left, kuid_t right) 94 { 95 return __kuid_val(left) <= __kuid_val(right); 96 } 97 98 static inline bool gid_lte(kgid_t left, kgid_t right) 99 { 100 return __kgid_val(left) <= __kgid_val(right); 101 } 102 103 static inline bool uid_valid(kuid_t uid) 104 { 105 return __kuid_val(uid) != (uid_t) -1; 106 } 107 108 static inline bool gid_valid(kgid_t gid) 109 { 110 return __kgid_val(gid) != (gid_t) -1; 111 } 112 113 #ifdef CONFIG_USER_NS 114 115 extern kuid_t make_kuid(struct user_namespace *from, uid_t uid); 116 extern kgid_t make_kgid(struct user_namespace *from, gid_t gid); 117 118 extern uid_t from_kuid(struct user_namespace *to, kuid_t uid); 119 extern gid_t from_kgid(struct user_namespace *to, kgid_t gid); 120 extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid); 121 extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid); 122 123 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid) 124 { 125 return from_kuid(ns, uid) != (uid_t) -1; 126 } 127 128 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid) 129 { 130 return from_kgid(ns, gid) != (gid_t) -1; 131 } 132 133 u32 map_id_down(struct uid_gid_map *map, u32 id); 134 u32 map_id_up(struct uid_gid_map *map, u32 id); 135 136 #else 137 138 static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid) 139 { 140 return KUIDT_INIT(uid); 141 } 142 143 static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid) 144 { 145 return KGIDT_INIT(gid); 146 } 147 148 static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid) 149 { 150 return __kuid_val(kuid); 151 } 152 153 static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid) 154 { 155 return __kgid_val(kgid); 156 } 157 158 static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid) 159 { 160 uid_t uid = from_kuid(to, kuid); 161 if (uid == (uid_t)-1) 162 uid = overflowuid; 163 return uid; 164 } 165 166 static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid) 167 { 168 gid_t gid = from_kgid(to, kgid); 169 if (gid == (gid_t)-1) 170 gid = overflowgid; 171 return gid; 172 } 173 174 static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid) 175 { 176 return uid_valid(uid); 177 } 178 179 static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid) 180 { 181 return gid_valid(gid); 182 } 183 184 static inline u32 map_id_down(struct uid_gid_map *map, u32 id) 185 { 186 return id; 187 } 188 189 static inline u32 map_id_up(struct uid_gid_map *map, u32 id) 190 { 191 return id; 192 } 193 #endif /* CONFIG_USER_NS */ 194 195 #endif /* _LINUX_UIDGID_H */ 196
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.