libcap.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 1997 Andrew G Morgan <morgan@kernel.org>
  3. *
  4. * This file contains internal definitions for the various functions in
  5. * this small capability library.
  6. */
  7. #ifndef LIBCAP_H
  8. #define LIBCAP_H
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <sys/capability.h>
  15. #ifndef __u8
  16. #define __u8 uint8_t
  17. #endif /* __8 */
  18. #ifndef __u32
  19. #define __u32 uint32_t
  20. #endif /* __u32 */
  21. /* include the names for the caps and a definition of __CAP_BITS */
  22. #include "cap_names.h"
  23. #ifndef _LINUX_CAPABILITY_U32S_1
  24. # define _LINUX_CAPABILITY_U32S_1 1
  25. #endif /* ndef _LINUX_CAPABILITY_U32S */
  26. /*
  27. * Do we match the local kernel?
  28. */
  29. #if !defined(_LINUX_CAPABILITY_VERSION)
  30. # error Kernel <linux/capability.h> does not support library
  31. # error file "libcap.h" --> fix and recompile libcap
  32. #elif !defined(_LINUX_CAPABILITY_VERSION_2)
  33. # warning Kernel <linux/capability.h> does not support 64-bit capabilities
  34. # warning and libcap is being built with no support for 64-bit capabilities
  35. # ifndef _LINUX_CAPABILITY_VERSION_1
  36. # define _LINUX_CAPABILITY_VERSION_1 0x19980330
  37. # endif
  38. # _LIBCAP_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_1
  39. # _LIBCAP_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_1
  40. #elif defined(_LINUX_CAPABILITY_VERSION_3)
  41. # if (_LINUX_CAPABILITY_VERSION_3 != 0x20080522)
  42. # error Kernel <linux/capability.h> v3 does not match library
  43. # error file "libcap.h" --> fix and recompile libcap
  44. # else
  45. # define _LIBCAP_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
  46. # define _LIBCAP_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
  47. # endif
  48. #elif (_LINUX_CAPABILITY_VERSION_2 != 0x20071026)
  49. # error Kernel <linux/capability.h> does not match library
  50. # error file "libcap.h" --> fix and recompile libcap
  51. #else
  52. # define _LIBCAP_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_2
  53. # define _LIBCAP_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_2
  54. #endif
  55. #undef _LINUX_CAPABILITY_VERSION
  56. #undef _LINUX_CAPABILITY_U32S
  57. /*
  58. * This is a pointer to a struct containing three consecutive
  59. * capability sets in the order of the cap_flag_t type: the are
  60. * effective,inheritable and permitted. This is the type that the
  61. * user-space routines think of as 'internal' capabilities - this is
  62. * the type that is passed to the kernel with the system calls related
  63. * to processes.
  64. */
  65. #if defined(VFS_CAP_REVISION_MASK) && !defined(VFS_CAP_U32)
  66. # define VFS_CAP_U32_1 1
  67. # define XATTR_CAPS_SZ_1 (sizeof(__le32)*(1 + 2*VFS_CAP_U32_1))
  68. # define VFS_CAP_U32 VFS_CAP_U32_1
  69. struct _cap_vfs_cap_data {
  70. __le32 magic_etc;
  71. struct {
  72. __le32 permitted;
  73. __le32 inheritable;
  74. } data[VFS_CAP_U32_1];
  75. };
  76. # define vfs_cap_data _cap_vfs_cap_data
  77. #endif
  78. #ifndef CAP_TO_INDEX
  79. # define CAP_TO_INDEX(x) ((x) >> 5) /* 1 << 5 == bits in __u32 */
  80. #endif /* ndef CAP_TO_INDEX */
  81. #ifndef CAP_TO_MASK
  82. # define CAP_TO_MASK(x) (1 << ((x) & 31))
  83. #endif /* ndef CAP_TO_MASK */
  84. #define NUMBER_OF_CAP_SETS 3 /* effective, inheritable, permitted */
  85. #define __CAP_BLKS (_LIBCAP_CAPABILITY_U32S)
  86. #define CAP_SET_SIZE (__CAP_BLKS * sizeof(__u32))
  87. #define CAP_T_MAGIC 0xCA90D0
  88. struct _cap_struct {
  89. struct __user_cap_header_struct head;
  90. union {
  91. struct __user_cap_data_struct set;
  92. __u32 flat[NUMBER_OF_CAP_SETS];
  93. } u[_LIBCAP_CAPABILITY_U32S];
  94. };
  95. /* the maximum bits supportable */
  96. #define __CAP_MAXBITS (__CAP_BLKS * 32)
  97. /* string magic for cap_free */
  98. #define CAP_S_MAGIC 0xCA95D0
  99. /*
  100. * kernel API cap set abstraction
  101. */
  102. #define raise_cap(x,set) u[(x)>>5].flat[set] |= (1<<((x)&31))
  103. #define lower_cap(x,set) u[(x)>>5].flat[set] &= ~(1<<((x)&31))
  104. #define isset_cap(y,x,set) ((y)->u[(x)>>5].flat[set] & (1<<((x)&31)))
  105. /*
  106. * Private definitions for internal use by the library.
  107. */
  108. #define __libcap_check_magic(c,magic) ((c) && *(-1+(__u32 *)(c)) == (magic))
  109. #define good_cap_t(c) __libcap_check_magic(c, CAP_T_MAGIC)
  110. #define good_cap_string(c) __libcap_check_magic(c, CAP_S_MAGIC)
  111. /*
  112. * These match CAP_DIFFERS() expectations
  113. */
  114. #define LIBCAP_EFF (1 << CAP_EFFECTIVE)
  115. #define LIBCAP_INH (1 << CAP_INHERITABLE)
  116. #define LIBCAP_PER (1 << CAP_PERMITTED)
  117. /*
  118. * library debugging
  119. */
  120. #ifdef DEBUG
  121. #include <stdio.h>
  122. # define _cap_debug(f, x...) do { \
  123. fprintf(stderr, "%s(%s:%d): ", __FUNCTION__, __FILE__, __LINE__); \
  124. fprintf(stderr, f, ## x); \
  125. fprintf(stderr, "\n"); \
  126. } while (0)
  127. # define _cap_debugcap(s, c, set) do { \
  128. unsigned _cap_index; \
  129. fprintf(stderr, "%s(%s:%d): %s", __FUNCTION__, __FILE__, __LINE__, s); \
  130. for (_cap_index=_LIBCAP_CAPABILITY_U32S; _cap_index-- > 0; ) { \
  131. fprintf(stderr, "%08x", (c).u[_cap_index].flat[set]); \
  132. } \
  133. fprintf(stderr, "\n"); \
  134. } while (0)
  135. #else /* !DEBUG */
  136. # define _cap_debug(f, x...)
  137. # define _cap_debugcap(s, c, set)
  138. #endif /* DEBUG */
  139. extern char *_libcap_strdup(const char *text);
  140. /*
  141. * These are semi-public prototypes, they will only be defined in
  142. * <sys/capability.h> if _POSIX_SOURCE is not #define'd, so we
  143. * place them here too.
  144. */
  145. extern int capget(cap_user_header_t header, cap_user_data_t data);
  146. extern int capset(cap_user_header_t header, const cap_user_data_t data);
  147. extern int capgetp(pid_t pid, cap_t cap_d);
  148. extern int capsetp(pid_t pid, cap_t cap_d);
  149. /* prctl based API for altering character of current process */
  150. #define PR_GET_KEEPCAPS 7
  151. #define PR_SET_KEEPCAPS 8
  152. #define PR_CAPBSET_READ 23
  153. #define PR_CAPBSET_DROP 24
  154. #define PR_GET_SECUREBITS 27
  155. #define PR_SET_SECUREBITS 28
  156. /*
  157. * The library compares sizeof() with integer return values. To avoid
  158. * signed/unsigned comparisons, leading to unfortunate
  159. * misinterpretations of -1, we provide a convenient cast-to-signed-integer
  160. * version of sizeof().
  161. */
  162. #define ssizeof(x) ((ssize_t) sizeof(x))
  163. #endif /* LIBCAP_H */