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