1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
27a4e7408SEric W. Biederman #ifndef _LINUX_UIDGID_H
37a4e7408SEric W. Biederman #define _LINUX_UIDGID_H
47a4e7408SEric W. Biederman
57a4e7408SEric W. Biederman /*
67a4e7408SEric W. Biederman * A set of types for the internal kernel types representing uids and gids.
77a4e7408SEric W. Biederman *
87a4e7408SEric W. Biederman * The types defined in this header allow distinguishing which uids and gids in
97a4e7408SEric W. Biederman * the kernel are values used by userspace and which uid and gid values are
107a4e7408SEric W. Biederman * the internal kernel values. With the addition of user namespaces the values
117a4e7408SEric W. Biederman * can be different. Using the type system makes it possible for the compiler
127a4e7408SEric W. Biederman * to detect when we overlook these differences.
137a4e7408SEric W. Biederman *
147a4e7408SEric W. Biederman */
15af6da56aSKent Overstreet #include <linux/uidgid_types.h>
167a4e7408SEric W. Biederman #include <linux/highuid.h>
177a4e7408SEric W. Biederman
187a4e7408SEric W. Biederman struct user_namespace;
197a4e7408SEric W. Biederman extern struct user_namespace init_user_ns;
20783822e4SChristian Brauner struct uid_gid_map;
217a4e7408SEric W. Biederman
227a4e7408SEric W. Biederman #define KUIDT_INIT(value) (kuid_t){ value }
237a4e7408SEric W. Biederman #define KGIDT_INIT(value) (kgid_t){ value }
247a4e7408SEric W. Biederman
252813893fSIulia Manda #ifdef CONFIG_MULTIUSER
__kuid_val(kuid_t uid)267a4e7408SEric W. Biederman static inline uid_t __kuid_val(kuid_t uid)
277a4e7408SEric W. Biederman {
287a4e7408SEric W. Biederman return uid.val;
297a4e7408SEric W. Biederman }
307a4e7408SEric W. Biederman
__kgid_val(kgid_t gid)317a4e7408SEric W. Biederman static inline gid_t __kgid_val(kgid_t gid)
327a4e7408SEric W. Biederman {
337a4e7408SEric W. Biederman return gid.val;
347a4e7408SEric W. Biederman }
352813893fSIulia Manda #else
__kuid_val(kuid_t uid)362813893fSIulia Manda static inline uid_t __kuid_val(kuid_t uid)
372813893fSIulia Manda {
382813893fSIulia Manda return 0;
392813893fSIulia Manda }
402813893fSIulia Manda
__kgid_val(kgid_t gid)412813893fSIulia Manda static inline gid_t __kgid_val(kgid_t gid)
422813893fSIulia Manda {
432813893fSIulia Manda return 0;
442813893fSIulia Manda }
452813893fSIulia Manda #endif
467a4e7408SEric W. Biederman
477a4e7408SEric W. Biederman #define GLOBAL_ROOT_UID KUIDT_INIT(0)
487a4e7408SEric W. Biederman #define GLOBAL_ROOT_GID KGIDT_INIT(0)
497a4e7408SEric W. Biederman
507a4e7408SEric W. Biederman #define INVALID_UID KUIDT_INIT(-1)
517a4e7408SEric W. Biederman #define INVALID_GID KGIDT_INIT(-1)
527a4e7408SEric W. Biederman
uid_eq(kuid_t left,kuid_t right)537a4e7408SEric W. Biederman static inline bool uid_eq(kuid_t left, kuid_t right)
547a4e7408SEric W. Biederman {
557a4e7408SEric W. Biederman return __kuid_val(left) == __kuid_val(right);
567a4e7408SEric W. Biederman }
577a4e7408SEric W. Biederman
gid_eq(kgid_t left,kgid_t right)587a4e7408SEric W. Biederman static inline bool gid_eq(kgid_t left, kgid_t right)
597a4e7408SEric W. Biederman {
607a4e7408SEric W. Biederman return __kgid_val(left) == __kgid_val(right);
617a4e7408SEric W. Biederman }
627a4e7408SEric W. Biederman
uid_gt(kuid_t left,kuid_t right)637a4e7408SEric W. Biederman static inline bool uid_gt(kuid_t left, kuid_t right)
647a4e7408SEric W. Biederman {
657a4e7408SEric W. Biederman return __kuid_val(left) > __kuid_val(right);
667a4e7408SEric W. Biederman }
677a4e7408SEric W. Biederman
gid_gt(kgid_t left,kgid_t right)687a4e7408SEric W. Biederman static inline bool gid_gt(kgid_t left, kgid_t right)
697a4e7408SEric W. Biederman {
707a4e7408SEric W. Biederman return __kgid_val(left) > __kgid_val(right);
717a4e7408SEric W. Biederman }
727a4e7408SEric W. Biederman
uid_gte(kuid_t left,kuid_t right)737a4e7408SEric W. Biederman static inline bool uid_gte(kuid_t left, kuid_t right)
747a4e7408SEric W. Biederman {
757a4e7408SEric W. Biederman return __kuid_val(left) >= __kuid_val(right);
767a4e7408SEric W. Biederman }
777a4e7408SEric W. Biederman
gid_gte(kgid_t left,kgid_t right)787a4e7408SEric W. Biederman static inline bool gid_gte(kgid_t left, kgid_t right)
797a4e7408SEric W. Biederman {
807a4e7408SEric W. Biederman return __kgid_val(left) >= __kgid_val(right);
817a4e7408SEric W. Biederman }
827a4e7408SEric W. Biederman
uid_lt(kuid_t left,kuid_t right)837a4e7408SEric W. Biederman static inline bool uid_lt(kuid_t left, kuid_t right)
847a4e7408SEric W. Biederman {
857a4e7408SEric W. Biederman return __kuid_val(left) < __kuid_val(right);
867a4e7408SEric W. Biederman }
877a4e7408SEric W. Biederman
gid_lt(kgid_t left,kgid_t right)887a4e7408SEric W. Biederman static inline bool gid_lt(kgid_t left, kgid_t right)
897a4e7408SEric W. Biederman {
907a4e7408SEric W. Biederman return __kgid_val(left) < __kgid_val(right);
917a4e7408SEric W. Biederman }
927a4e7408SEric W. Biederman
uid_lte(kuid_t left,kuid_t right)937a4e7408SEric W. Biederman static inline bool uid_lte(kuid_t left, kuid_t right)
947a4e7408SEric W. Biederman {
957a4e7408SEric W. Biederman return __kuid_val(left) <= __kuid_val(right);
967a4e7408SEric W. Biederman }
977a4e7408SEric W. Biederman
gid_lte(kgid_t left,kgid_t right)987a4e7408SEric W. Biederman static inline bool gid_lte(kgid_t left, kgid_t right)
997a4e7408SEric W. Biederman {
1007a4e7408SEric W. Biederman return __kgid_val(left) <= __kgid_val(right);
1017a4e7408SEric W. Biederman }
1027a4e7408SEric W. Biederman
uid_valid(kuid_t uid)1037a4e7408SEric W. Biederman static inline bool uid_valid(kuid_t uid)
1047a4e7408SEric W. Biederman {
105929aa5b2SJosh Triplett return __kuid_val(uid) != (uid_t) -1;
1067a4e7408SEric W. Biederman }
1077a4e7408SEric W. Biederman
gid_valid(kgid_t gid)1087a4e7408SEric W. Biederman static inline bool gid_valid(kgid_t gid)
1097a4e7408SEric W. Biederman {
110929aa5b2SJosh Triplett return __kgid_val(gid) != (gid_t) -1;
1117a4e7408SEric W. Biederman }
1127a4e7408SEric W. Biederman
11322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
11422d917d8SEric W. Biederman
11522d917d8SEric W. Biederman extern kuid_t make_kuid(struct user_namespace *from, uid_t uid);
11622d917d8SEric W. Biederman extern kgid_t make_kgid(struct user_namespace *from, gid_t gid);
11722d917d8SEric W. Biederman
11822d917d8SEric W. Biederman extern uid_t from_kuid(struct user_namespace *to, kuid_t uid);
11922d917d8SEric W. Biederman extern gid_t from_kgid(struct user_namespace *to, kgid_t gid);
12022d917d8SEric W. Biederman extern uid_t from_kuid_munged(struct user_namespace *to, kuid_t uid);
12122d917d8SEric W. Biederman extern gid_t from_kgid_munged(struct user_namespace *to, kgid_t gid);
12222d917d8SEric W. Biederman
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)12322d917d8SEric W. Biederman static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
12422d917d8SEric W. Biederman {
12522d917d8SEric W. Biederman return from_kuid(ns, uid) != (uid_t) -1;
12622d917d8SEric W. Biederman }
12722d917d8SEric W. Biederman
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)12822d917d8SEric W. Biederman static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
12922d917d8SEric W. Biederman {
13022d917d8SEric W. Biederman return from_kgid(ns, gid) != (gid_t) -1;
13122d917d8SEric W. Biederman }
13222d917d8SEric W. Biederman
133783822e4SChristian Brauner u32 map_id_down(struct uid_gid_map *map, u32 id);
134783822e4SChristian Brauner u32 map_id_up(struct uid_gid_map *map, u32 id);
135*784ed435SChristian Brauner u32 map_id_range_up(struct uid_gid_map *map, u32 id, u32 count);
136783822e4SChristian Brauner
13722d917d8SEric W. Biederman #else
13822d917d8SEric W. Biederman
make_kuid(struct user_namespace * from,uid_t uid)1397a4e7408SEric W. Biederman static inline kuid_t make_kuid(struct user_namespace *from, uid_t uid)
1407a4e7408SEric W. Biederman {
1417a4e7408SEric W. Biederman return KUIDT_INIT(uid);
1427a4e7408SEric W. Biederman }
1437a4e7408SEric W. Biederman
make_kgid(struct user_namespace * from,gid_t gid)1447a4e7408SEric W. Biederman static inline kgid_t make_kgid(struct user_namespace *from, gid_t gid)
1457a4e7408SEric W. Biederman {
1467a4e7408SEric W. Biederman return KGIDT_INIT(gid);
1477a4e7408SEric W. Biederman }
1487a4e7408SEric W. Biederman
from_kuid(struct user_namespace * to,kuid_t kuid)1497a4e7408SEric W. Biederman static inline uid_t from_kuid(struct user_namespace *to, kuid_t kuid)
1507a4e7408SEric W. Biederman {
1517a4e7408SEric W. Biederman return __kuid_val(kuid);
1527a4e7408SEric W. Biederman }
1537a4e7408SEric W. Biederman
from_kgid(struct user_namespace * to,kgid_t kgid)1547a4e7408SEric W. Biederman static inline gid_t from_kgid(struct user_namespace *to, kgid_t kgid)
1557a4e7408SEric W. Biederman {
1567a4e7408SEric W. Biederman return __kgid_val(kgid);
1577a4e7408SEric W. Biederman }
1587a4e7408SEric W. Biederman
from_kuid_munged(struct user_namespace * to,kuid_t kuid)1597a4e7408SEric W. Biederman static inline uid_t from_kuid_munged(struct user_namespace *to, kuid_t kuid)
1607a4e7408SEric W. Biederman {
1617a4e7408SEric W. Biederman uid_t uid = from_kuid(to, kuid);
1627a4e7408SEric W. Biederman if (uid == (uid_t)-1)
1637a4e7408SEric W. Biederman uid = overflowuid;
1647a4e7408SEric W. Biederman return uid;
1657a4e7408SEric W. Biederman }
1667a4e7408SEric W. Biederman
from_kgid_munged(struct user_namespace * to,kgid_t kgid)1677a4e7408SEric W. Biederman static inline gid_t from_kgid_munged(struct user_namespace *to, kgid_t kgid)
1687a4e7408SEric W. Biederman {
1697a4e7408SEric W. Biederman gid_t gid = from_kgid(to, kgid);
1707a4e7408SEric W. Biederman if (gid == (gid_t)-1)
1717a4e7408SEric W. Biederman gid = overflowgid;
1727a4e7408SEric W. Biederman return gid;
1737a4e7408SEric W. Biederman }
1747a4e7408SEric W. Biederman
kuid_has_mapping(struct user_namespace * ns,kuid_t uid)1757a4e7408SEric W. Biederman static inline bool kuid_has_mapping(struct user_namespace *ns, kuid_t uid)
1767a4e7408SEric W. Biederman {
17737b11804SEric W. Biederman return uid_valid(uid);
1787a4e7408SEric W. Biederman }
1797a4e7408SEric W. Biederman
kgid_has_mapping(struct user_namespace * ns,kgid_t gid)1807a4e7408SEric W. Biederman static inline bool kgid_has_mapping(struct user_namespace *ns, kgid_t gid)
1817a4e7408SEric W. Biederman {
18237b11804SEric W. Biederman return gid_valid(gid);
1837a4e7408SEric W. Biederman }
1847a4e7408SEric W. Biederman
map_id_down(struct uid_gid_map * map,u32 id)185783822e4SChristian Brauner static inline u32 map_id_down(struct uid_gid_map *map, u32 id)
186783822e4SChristian Brauner {
187783822e4SChristian Brauner return id;
188783822e4SChristian Brauner }
189783822e4SChristian Brauner
map_id_range_up(struct uid_gid_map * map,u32 id,u32 count)190*784ed435SChristian Brauner static inline u32 map_id_range_up(struct uid_gid_map *map, u32 id, u32 count)
191*784ed435SChristian Brauner {
192*784ed435SChristian Brauner return id;
193*784ed435SChristian Brauner }
194*784ed435SChristian Brauner
map_id_up(struct uid_gid_map * map,u32 id)195783822e4SChristian Brauner static inline u32 map_id_up(struct uid_gid_map *map, u32 id)
196783822e4SChristian Brauner {
197783822e4SChristian Brauner return id;
198783822e4SChristian Brauner }
19922d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
20022d917d8SEric W. Biederman
2017a4e7408SEric W. Biederman #endif /* _LINUX_UIDGID_H */
202