highuid.h 3.1 KB

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