| 1 |
/* |
| 2 |
* Test miscellaneous helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
#undef NDEBUG |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/stat.h> |
| 9 |
#include "util/misc.h" |
| 10 |
|
| 11 |
/* |
| 12 |
* Check for which memfd-seals are supported by the running kernel and return |
| 13 |
* them as mask to the caller. This allows running tests on older kernels, |
| 14 |
* that possibly do not support the newer seals. |
| 15 |
*/ |
| 16 |
static unsigned int memfd_seals(void) { |
| 17 |
unsigned int seal_mask; |
| 18 |
int fd; |
| 19 |
|
| 20 |
seal_mask = MISC_F_SEAL_SEAL | MISC_F_SEAL_SHRINK | |
| 21 |
MISC_F_SEAL_GROW | MISC_F_SEAL_WRITE; |
| 22 |
|
| 23 |
fd = misc_memfd( |
| 24 |
"test", |
| 25 |
MISC_MFD_ALLOW_SEALING | MISC_MFD_NOEXEC_SEAL, |
| 26 |
MISC_F_SEAL_FUTURE_WRITE |
| 27 |
); |
| 28 |
if (fd >= 0) { |
| 29 |
seal_mask |= MISC_F_SEAL_FUTURE_WRITE; |
| 30 |
c_close(fd); |
| 31 |
} |
| 32 |
|
| 33 |
fd = misc_memfd( |
| 34 |
"test", |
| 35 |
MISC_MFD_ALLOW_SEALING | MISC_MFD_NOEXEC_SEAL, |
| 36 |
MISC_F_SEAL_EXEC |
| 37 |
); |
| 38 |
if (fd >= 0) { |
| 39 |
seal_mask |= MISC_F_SEAL_EXEC; |
| 40 |
c_close(fd); |
| 41 |
} |
| 42 |
|
| 43 |
return seal_mask; |
| 44 |
} |
| 45 |
|
| 46 |
static void test_memfd(void) { |
| 47 |
static const struct { |
| 48 |
unsigned int in_flags; |
| 49 |
unsigned int in_seals; |
| 50 |
int out_error; |
| 51 |
unsigned int out_seals; |
| 52 |
unsigned int out_fmode; |
| 53 |
} v[] = { |
| 54 |
{ |
| 55 |
.out_error = -ENOTRECOVERABLE, |
| 56 |
}, |
| 57 |
{ |
| 58 |
.in_flags = MISC_MFD_EXEC, |
| 59 |
.in_seals = MISC_F_SEAL_WRITE, |
| 60 |
.out_error = -ENOTRECOVERABLE, |
| 61 |
}, |
| 62 |
{ |
| 63 |
.in_flags = MISC_MFD_EXEC, |
| 64 |
.out_seals = MISC_F_SEAL_SEAL, |
| 65 |
.out_fmode = 0777, |
| 66 |
}, |
| 67 |
{ |
| 68 |
.in_flags = MISC_MFD_EXEC, |
| 69 |
.in_seals = MISC_F_SEAL_SEAL, |
| 70 |
.out_seals = MISC_F_SEAL_SEAL, |
| 71 |
.out_fmode = 0777, |
| 72 |
}, |
| 73 |
{ |
| 74 |
.in_flags = MISC_MFD_NOEXEC_SEAL, |
| 75 |
.out_seals = MISC_F_SEAL_SEAL | MISC_F_SEAL_EXEC, |
| 76 |
.out_fmode = 0666, |
| 77 |
}, |
| 78 |
{ |
| 79 |
.in_flags = MISC_MFD_ALLOW_SEALING | MISC_MFD_NOEXEC_SEAL, |
| 80 |
.out_seals = MISC_F_SEAL_EXEC, |
| 81 |
.out_fmode = 0666, |
| 82 |
}, |
| 83 |
{ |
| 84 |
.in_flags = MISC_MFD_ALLOW_SEALING | MISC_MFD_NOEXEC_SEAL, |
| 85 |
.in_seals = MISC_F_SEAL_WRITE, |
| 86 |
.out_seals = MISC_F_SEAL_WRITE | MISC_F_SEAL_EXEC, |
| 87 |
.out_fmode = 0666, |
| 88 |
}, |
| 89 |
}; |
| 90 |
unsigned int seals, seal_mask; |
| 91 |
struct stat st; |
| 92 |
size_t i; |
| 93 |
int r, fd; |
| 94 |
|
| 95 |
seal_mask = memfd_seals(); |
| 96 |
|
| 97 |
for (i = 0; i < C_ARRAY_SIZE(v); ++i) { |
| 98 |
c_assert(!(v[i].in_seals & ~seal_mask)); |
| 99 |
|
| 100 |
fd = misc_memfd("test", v[i].in_flags, v[i].in_seals); |
| 101 |
if (fd < 0) { |
| 102 |
c_assert(fd == v[i].out_error); |
| 103 |
continue; |
| 104 |
} |
| 105 |
c_assert(!v[i].out_error); |
| 106 |
|
| 107 |
r = misc_memfd_get_seals(fd, &seals); |
| 108 |
c_assert(r >= 0); |
| 109 |
c_assert((seals & seal_mask) == (v[i].out_seals & seal_mask)); |
| 110 |
|
| 111 |
r = fstat(fd, &st); |
| 112 |
c_assert(r >= 0); |
| 113 |
c_assert((st.st_mode & 07777) == v[i].out_fmode); |
| 114 |
|
| 115 |
c_close(fd); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static void test_umul_saturating(void) { |
| 120 |
static const struct { |
| 121 |
uint64_t input_a; |
| 122 |
uint64_t input_b; |
| 123 |
uint64_t output; |
| 124 |
} values[] = { |
| 125 |
{ 0, 0, 0 }, |
| 126 |
{ 0, 1, 0 }, |
| 127 |
{ 1, 0, 0 }, |
| 128 |
{ 1, 1, 1 }, |
| 129 |
|
| 130 |
{ UINT32_MAX, UINT32_MAX, UINT64_MAX - UINT64_C(2) * UINT32_MAX }, |
| 131 |
{ UINT32_MAX, UINT32_MAX + UINT64_C(1), UINT64_MAX - UINT32_MAX }, |
| 132 |
{ UINT32_MAX + UINT64_C(1), UINT32_MAX + UINT64_C(1), UINT64_MAX }, |
| 133 |
|
| 134 |
{ 1, UINT64_MAX - 1, UINT64_MAX - 1 }, |
| 135 |
{ UINT64_MAX - 1, 1, UINT64_MAX - 1 }, |
| 136 |
{ UINT64_MAX - 1, 2, UINT64_MAX }, |
| 137 |
}; |
| 138 |
uint64_t output; |
| 139 |
size_t i; |
| 140 |
|
| 141 |
for (i = 0; i < C_ARRAY_SIZE(values); ++i) { |
| 142 |
output = util_umul64_saturating(values[i].input_a, values[i].input_b); |
| 143 |
c_assert(output == values[i].output); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static void test_casts(void) { |
| 148 |
static const struct { |
| 149 |
size_t from; |
| 150 |
unsigned int to; |
| 151 |
} casts_z2u_sat[] = { |
| 152 |
{ 0, 0 }, |
| 153 |
{ 32, 32 }, |
| 154 |
{ 256, 256 }, |
| 155 |
{ SIZE_MAX, UINT_MAX }, |
| 156 |
#if SIZE_MAX > UINT_MAX |
| 157 |
{ (size_t)UINT_MAX, UINT_MAX }, |
| 158 |
{ (size_t)UINT_MAX + 1, UINT_MAX }, |
| 159 |
{ (size_t)UINT_MAX * 2, UINT_MAX }, |
| 160 |
#endif |
| 161 |
}; |
| 162 |
static const struct { |
| 163 |
uint64_t from; |
| 164 |
unsigned int to; |
| 165 |
} casts_t2u_sat[] = { |
| 166 |
{ 0, 0 }, |
| 167 |
{ 32, 32 }, |
| 168 |
{ 256, 256 }, |
| 169 |
{ (uint64_t)UINT_MAX, UINT_MAX }, |
| 170 |
{ (uint64_t)UINT_MAX + 1, UINT_MAX }, |
| 171 |
{ (uint64_t)UINT_MAX * 2, UINT_MAX }, |
| 172 |
{ UINT64_MAX, UINT_MAX }, |
| 173 |
}; |
| 174 |
size_t i; |
| 175 |
|
| 176 |
for (i = 0; i < C_ARRAY_SIZE(casts_z2u_sat); ++i) { |
| 177 |
unsigned int r; |
| 178 |
|
| 179 |
r = util_z2u_saturating(casts_z2u_sat[i].from); |
| 180 |
c_assert(r == casts_z2u_sat[i].to); |
| 181 |
} |
| 182 |
|
| 183 |
for (i = 0; i < C_ARRAY_SIZE(casts_t2u_sat); ++i) { |
| 184 |
unsigned int r; |
| 185 |
|
| 186 |
r = util_t2u_saturating(casts_t2u_sat[i].from); |
| 187 |
c_assert(r == casts_t2u_sat[i].to); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
int main(int argc, char **argv) { |
| 192 |
test_memfd(); |
| 193 |
test_umul_saturating(); |
| 194 |
test_casts(); |
| 195 |
return 0; |
| 196 |
} |