| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <elf.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "coredump-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "format-util.h" |
| 10 |
#include "tests.h" |
| 11 |
|
| 12 |
TEST(coredump_filter_to_from_string) { |
| 13 |
for (CoredumpFilter i = 0; i < _COREDUMP_FILTER_MAX; i++) { |
| 14 |
const char *n; |
| 15 |
|
| 16 |
assert_se(n = coredump_filter_to_string(i)); |
| 17 |
log_info("0x%x\t%s", 1u << i, n); |
| 18 |
assert_se(coredump_filter_from_string(n) == i); |
| 19 |
|
| 20 |
uint64_t f; |
| 21 |
assert_se(coredump_filter_mask_from_string(n, &f) == 0); |
| 22 |
assert_se(f == 1u << i); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
TEST(coredump_filter_mask_from_string) { |
| 27 |
uint64_t f; |
| 28 |
assert_se(coredump_filter_mask_from_string("default", &f) == 0); |
| 29 |
assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); |
| 30 |
assert_se(coredump_filter_mask_from_string("all", &f) == 0); |
| 31 |
assert_se(f == COREDUMP_FILTER_MASK_ALL); |
| 32 |
|
| 33 |
assert_se(coredump_filter_mask_from_string(" default\tdefault\tdefault ", &f) == 0); |
| 34 |
assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); |
| 35 |
|
| 36 |
assert_se(coredump_filter_mask_from_string("defaulta", &f) < 0); |
| 37 |
assert_se(coredump_filter_mask_from_string("default defaulta default", &f) < 0); |
| 38 |
assert_se(coredump_filter_mask_from_string("default default defaulta", &f) < 0); |
| 39 |
|
| 40 |
assert_se(coredump_filter_mask_from_string("private-anonymous default", &f) == 0); |
| 41 |
assert_se(f == COREDUMP_FILTER_MASK_DEFAULT); |
| 42 |
|
| 43 |
assert_se(coredump_filter_mask_from_string("shared-file-backed shared-dax", &f) == 0); |
| 44 |
assert_se(f == (1 << COREDUMP_FILTER_SHARED_FILE_BACKED | |
| 45 |
1 << COREDUMP_FILTER_SHARED_DAX)); |
| 46 |
|
| 47 |
assert_se(coredump_filter_mask_from_string("private-file-backed private-dax 0xF", &f) == 0); |
| 48 |
assert_se(f == (1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED | |
| 49 |
1 << COREDUMP_FILTER_PRIVATE_DAX | |
| 50 |
0xF)); |
| 51 |
|
| 52 |
assert_se(coredump_filter_mask_from_string("11", &f) == 0); |
| 53 |
assert_se(f == 0x11); |
| 54 |
|
| 55 |
assert_se(coredump_filter_mask_from_string("0x1101", &f) == 0); |
| 56 |
assert_se(f == 0x1101); |
| 57 |
|
| 58 |
assert_se(coredump_filter_mask_from_string("0", &f) == 0); |
| 59 |
assert_se(f == 0); |
| 60 |
|
| 61 |
assert_se(coredump_filter_mask_from_string("all", &f) == 0); |
| 62 |
assert_se(FLAGS_SET(f, (1 << COREDUMP_FILTER_PRIVATE_ANONYMOUS | |
| 63 |
1 << COREDUMP_FILTER_SHARED_ANONYMOUS | |
| 64 |
1 << COREDUMP_FILTER_PRIVATE_FILE_BACKED | |
| 65 |
1 << COREDUMP_FILTER_SHARED_FILE_BACKED | |
| 66 |
1 << COREDUMP_FILTER_ELF_HEADERS | |
| 67 |
1 << COREDUMP_FILTER_PRIVATE_HUGE | |
| 68 |
1 << COREDUMP_FILTER_SHARED_HUGE | |
| 69 |
1 << COREDUMP_FILTER_PRIVATE_DAX | |
| 70 |
1 << COREDUMP_FILTER_SHARED_DAX))); |
| 71 |
} |
| 72 |
|
| 73 |
static void test_parse_auxv_two( |
| 74 |
uint8_t elf_class, |
| 75 |
size_t offset, |
| 76 |
const char *data, |
| 77 |
size_t data_size, |
| 78 |
int expect_at_secure, |
| 79 |
uid_t expect_uid, |
| 80 |
uid_t expect_euid, |
| 81 |
gid_t expect_gid, |
| 82 |
gid_t expect_egid) { |
| 83 |
|
| 84 |
int at_secure; |
| 85 |
uid_t uid, euid; |
| 86 |
gid_t gid, egid; |
| 87 |
assert_se(parse_auxv(LOG_ERR, elf_class, data, data_size, |
| 88 |
&at_secure, &uid, &euid, &gid, &egid) == 0); |
| 89 |
|
| 90 |
log_debug("[offset=%zu] at_secure=%d, uid="UID_FMT", euid="UID_FMT", gid="GID_FMT", egid="GID_FMT, |
| 91 |
offset, |
| 92 |
at_secure, uid, euid, gid, egid); |
| 93 |
|
| 94 |
assert_se(uid == expect_uid); |
| 95 |
assert_se(euid == expect_euid); |
| 96 |
assert_se(gid == expect_gid); |
| 97 |
assert_se(egid == expect_egid); |
| 98 |
} |
| 99 |
|
| 100 |
static void test_parse_auxv_one( |
| 101 |
uint8_t elf_class, |
| 102 |
int dir_fd, |
| 103 |
const char *filename, |
| 104 |
int expect_at_secure, |
| 105 |
uid_t expect_uid, |
| 106 |
uid_t expect_euid, |
| 107 |
gid_t expect_gid, |
| 108 |
gid_t expect_egid) { |
| 109 |
|
| 110 |
_cleanup_free_ char *buf; |
| 111 |
const char *data; |
| 112 |
size_t data_size; |
| 113 |
log_info("Parsing %s…", filename); |
| 114 |
assert_se(read_full_file_at(dir_fd, filename, &buf, &data_size) >= 0); |
| 115 |
|
| 116 |
for (size_t offset = 0; offset < 8; offset++) { |
| 117 |
_cleanup_free_ char *buf2 = NULL; |
| 118 |
|
| 119 |
if (offset == 0) |
| 120 |
data = buf; |
| 121 |
else { |
| 122 |
assert_se(buf2 = malloc(offset + data_size)); |
| 123 |
memcpy(buf2 + offset, buf, data_size); |
| 124 |
data = buf2 + offset; |
| 125 |
} |
| 126 |
|
| 127 |
test_parse_auxv_two(elf_class, offset, data, data_size, |
| 128 |
expect_at_secure, expect_uid, expect_euid, expect_gid, expect_egid); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
TEST(parse_auxv) { |
| 133 |
_cleanup_free_ char *dir = NULL; |
| 134 |
_cleanup_close_ int dir_fd = -EBADF; |
| 135 |
|
| 136 |
assert_se(get_testdata_dir("auxv", &dir) >= 0); |
| 137 |
dir_fd = open(dir, O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 138 |
assert_se(dir_fd >= 0); |
| 139 |
|
| 140 |
if (__BYTE_ORDER == __LITTLE_ENDIAN) { |
| 141 |
test_parse_auxv_one(ELFCLASS32, dir_fd, "resolved.arm32", 0, 193, 193, 193, 193); |
| 142 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "bash.riscv64", 0, 1001, 1001, 1001, 1001); |
| 143 |
test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep.i686", 0, 1000, 1000, 1000, 1000); |
| 144 |
/* after chgrp and chmod g+s */ |
| 145 |
test_parse_auxv_one(ELFCLASS32, dir_fd, "sleep32.i686", 1, 1000, 1000, 1000, 10); |
| 146 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "sleep64.amd64", 1, 1000, 1000, 1000, 10); |
| 147 |
|
| 148 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.aarch64", 1, 1494200408, 0, 1494200408, 1494200408); |
| 149 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "sudo.amd64", 1, 1000, 0, 1000, 1000); |
| 150 |
|
| 151 |
/* Those run unprivileged, but start as root. */ |
| 152 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.amd64", 0, 0, 0, 0, 0); |
| 153 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "dbus-broker-launch.aarch64", 0, 0, 0, 0, 0); |
| 154 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "polkitd.aarch64", 0, 0, 0, 0, 0); |
| 155 |
} else { |
| 156 |
test_parse_auxv_one(ELFCLASS64, dir_fd, "cat.s390x", 0, 3481, 3481, 3481, 3481); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
DEFINE_TEST_MAIN(LOG_INFO); |