xref: /OK3568_Linux_fs/kernel/include/linux/highuid.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_HIGHUID_H
3*4882a593Smuzhiyun #define _LINUX_HIGHUID_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun  * general notes:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * CONFIG_UID16 is defined if the given architecture needs to
11*4882a593Smuzhiyun  * support backwards compatibility for old system calls.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * kernel code should use uid_t and gid_t at all times when dealing with
14*4882a593Smuzhiyun  * kernel-private data.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * old_uid_t and old_gid_t should only be different if CONFIG_UID16 is
17*4882a593Smuzhiyun  * defined, else the platform should provide dummy typedefs for them
18*4882a593Smuzhiyun  * such that they are equivalent to __kernel_{u,g}id_t.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * uid16_t and gid16_t are used on all architectures. (when dealing
21*4882a593Smuzhiyun  * with structures hard coded to 16 bits, such as in filesystems)
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun  * This is the "overflow" UID and GID. They are used to signify uid/gid
27*4882a593Smuzhiyun  * overflow to old programs when they request uid/gid information but are
28*4882a593Smuzhiyun  * using the old 16 bit interfaces.
29*4882a593Smuzhiyun  * When you run a libc5 program, it will think that all highuid files or
30*4882a593Smuzhiyun  * processes are owned by this uid/gid.
31*4882a593Smuzhiyun  * The idea is that it's better to do so than possibly return 0 in lieu of
32*4882a593Smuzhiyun  * 65536, etc.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun extern int overflowuid;
36*4882a593Smuzhiyun extern int overflowgid;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun extern void __bad_uid(void);
39*4882a593Smuzhiyun extern void __bad_gid(void);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define DEFAULT_OVERFLOWUID	65534
42*4882a593Smuzhiyun #define DEFAULT_OVERFLOWGID	65534
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #ifdef CONFIG_UID16
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* prevent uid mod 65536 effect by returning a default value for high UIDs */
47*4882a593Smuzhiyun #define high2lowuid(uid) ((uid) & ~0xFFFF ? (old_uid_t)overflowuid : (old_uid_t)(uid))
48*4882a593Smuzhiyun #define high2lowgid(gid) ((gid) & ~0xFFFF ? (old_gid_t)overflowgid : (old_gid_t)(gid))
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * -1 is different in 16 bits than it is in 32 bits
51*4882a593Smuzhiyun  * these macros are used by chown(), setreuid(), ...,
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun #define low2highuid(uid) ((uid) == (old_uid_t)-1 ? (uid_t)-1 : (uid_t)(uid))
54*4882a593Smuzhiyun #define low2highgid(gid) ((gid) == (old_gid_t)-1 ? (gid_t)-1 : (gid_t)(gid))
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define __convert_uid(size, uid) \
57*4882a593Smuzhiyun 	(size >= sizeof(uid) ? (uid) : high2lowuid(uid))
58*4882a593Smuzhiyun #define __convert_gid(size, gid) \
59*4882a593Smuzhiyun 	(size >= sizeof(gid) ? (gid) : high2lowgid(gid))
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #else
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define __convert_uid(size, uid) (uid)
65*4882a593Smuzhiyun #define __convert_gid(size, gid) (gid)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #endif /* !CONFIG_UID16 */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* uid/gid input should be always 32bit uid_t */
70*4882a593Smuzhiyun #define SET_UID(var, uid) do { (var) = __convert_uid(sizeof(var), (uid)); } while (0)
71*4882a593Smuzhiyun #define SET_GID(var, gid) do { (var) = __convert_gid(sizeof(var), (gid)); } while (0)
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * Everything below this line is needed on all architectures, to deal with
75*4882a593Smuzhiyun  * filesystems that only store 16 bits of the UID/GID, etc.
76*4882a593Smuzhiyun  */
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * This is the UID and GID that will get written to disk if a filesystem
80*4882a593Smuzhiyun  * only supports 16-bit UIDs and the kernel has a high UID/GID to write
81*4882a593Smuzhiyun  */
82*4882a593Smuzhiyun extern int fs_overflowuid;
83*4882a593Smuzhiyun extern int fs_overflowgid;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define DEFAULT_FS_OVERFLOWUID	65534
86*4882a593Smuzhiyun #define DEFAULT_FS_OVERFLOWGID	65534
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * Since these macros are used in architectures that only need limited
90*4882a593Smuzhiyun  * 16-bit UID back compatibility, we won't use old_uid_t and old_gid_t
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun #define fs_high2lowuid(uid) ((uid) & ~0xFFFF ? (uid16_t)fs_overflowuid : (uid16_t)(uid))
93*4882a593Smuzhiyun #define fs_high2lowgid(gid) ((gid) & ~0xFFFF ? (gid16_t)fs_overflowgid : (gid16_t)(gid))
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #define low_16_bits(x)	((x) & 0xFFFF)
96*4882a593Smuzhiyun #define high_16_bits(x)	(((x) & 0xFFFF0000) >> 16)
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #endif /* _LINUX_HIGHUID_H */
99