1 /* Private definitions for libsepol. */
2
3 /* Endian conversion for reading and writing binary policies */
4
5 #include <sepol/policydb/policydb.h>
6
7
8 #ifdef __APPLE__
9 #include <sys/types.h>
10 #include <machine/endian.h>
11 #else
12 #include <byteswap.h>
13 #include <endian.h>
14 #endif
15
16 #include <errno.h>
17
18 #ifdef __APPLE__
19 #define __BYTE_ORDER BYTE_ORDER
20 #define __LITTLE_ENDIAN LITTLE_ENDIAN
21 #endif
22
23 #if __BYTE_ORDER == __LITTLE_ENDIAN
24 #define cpu_to_le16(x) (x)
25 #define le16_to_cpu(x) (x)
26 #define cpu_to_le32(x) (x)
27 #define le32_to_cpu(x) (x)
28 #define cpu_to_le64(x) (x)
29 #define le64_to_cpu(x) (x)
30 #else
31 #define cpu_to_le16(x) bswap_16(x)
32 #define le16_to_cpu(x) bswap_16(x)
33 #define cpu_to_le32(x) bswap_32(x)
34 #define le32_to_cpu(x) bswap_32(x)
35 #define cpu_to_le64(x) bswap_64(x)
36 #define le64_to_cpu(x) bswap_64(x)
37 #endif
38
39 #undef min
40 #define min(a,b) (((a) < (b)) ? (a) : (b))
41
42 #undef max
43 #define max(a,b) ((a) >= (b) ? (a) : (b))
44
45 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
46
exceeds_available_bytes(const struct policy_file * fp,size_t x,size_t req_elem_size)47 static inline int exceeds_available_bytes(const struct policy_file *fp, size_t x, size_t req_elem_size)
48 {
49 size_t req_size;
50
51 /* Remaining input size is only available for mmap'ed memory */
52 if (fp->type != PF_USE_MEMORY)
53 return 0;
54
55 if (__builtin_mul_overflow(x, req_elem_size, &req_size))
56 return 1;
57
58 return req_size > fp->len;
59 }
60
61 #define is_saturated(x) ((x) == (typeof(x))-1)
62
63 #define zero_or_saturated(x) (((x) == 0) || is_saturated(x))
64
65 #define spaceship_cmp(a, b) (((a) > (b)) - ((a) < (b)))
66
67 /* Use to ignore intentional unsigned under- and overflows while running under UBSAN. */
68 #if defined(__clang__) && defined(__clang_major__) && (__clang_major__ >= 4)
69 #if (__clang_major__ >= 12)
70 #define ignore_unsigned_overflow_ __attribute__((no_sanitize("unsigned-integer-overflow", "unsigned-shift-base")))
71 #else
72 #define ignore_unsigned_overflow_ __attribute__((no_sanitize("unsigned-integer-overflow")))
73 #endif
74 #else
75 #define ignore_unsigned_overflow_
76 #endif
77
78 /* Policy compatibility information. */
79 struct policydb_compat_info {
80 unsigned int type;
81 unsigned int version;
82 unsigned int sym_num;
83 unsigned int ocon_num;
84 unsigned int target_platform;
85 };
86
87 extern const struct policydb_compat_info *policydb_lookup_compat(unsigned int version,
88 unsigned int type,
89 unsigned int target_platform);
90
91 /* Reading from a policy "file". */
92 extern int next_entry(void *buf, struct policy_file *fp, size_t bytes);
93 extern size_t put_entry(const void *ptr, size_t size, size_t n,
94 struct policy_file *fp);
95 extern int str_read(char **strp, struct policy_file *fp, size_t len);
96
97 #ifndef HAVE_REALLOCARRAY
reallocarray(void * ptr,size_t nmemb,size_t size)98 static inline void* reallocarray(void *ptr, size_t nmemb, size_t size) {
99 if (size && nmemb > (size_t)-1 / size) {
100 errno = ENOMEM;
101 return NULL;
102 }
103
104 return realloc(ptr, nmemb * size);
105 }
106 #endif
107