| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "architecture.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "gpt.h" |
| 8 |
#include "log.h" |
| 9 |
#include "memfd-util.h" |
| 10 |
#include "memory-util.h" |
| 11 |
#include "pretty-print.h" |
| 12 |
#include "strv.h" |
| 13 |
#include "tests.h" |
| 14 |
|
| 15 |
TEST(gpt_types_against_architectures) { |
| 16 |
int r; |
| 17 |
|
| 18 |
/* Dumps a table indicating for which architectures we know we have matching GPT partition |
| 19 |
* types. Also validates whether we can properly categorize the entries. */ |
| 20 |
|
| 21 |
FOREACH_STRING(prefix, "root-", "usr-") |
| 22 |
for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) |
| 23 |
FOREACH_STRING(suffix, "", "-verity", "-verity-sig") { |
| 24 |
_cleanup_free_ char *joined = NULL; |
| 25 |
GptPartitionType type; |
| 26 |
|
| 27 |
joined = strjoin(prefix, architecture_to_string(a), suffix); |
| 28 |
if (!joined) |
| 29 |
return (void) log_oom(); |
| 30 |
|
| 31 |
r = gpt_partition_type_from_string(joined, &type); |
| 32 |
if (r < 0) { |
| 33 |
printf("%s %s\n", RED_CROSS_MARK(), joined); |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
printf("%s %s\n", GREEN_CHECK_MARK(), joined); |
| 38 |
|
| 39 |
if (streq(prefix, "root-") && streq(suffix, "")) |
| 40 |
ASSERT_EQ(type.designator, PARTITION_ROOT); |
| 41 |
if (streq(prefix, "root-") && streq(suffix, "-verity")) |
| 42 |
ASSERT_EQ(type.designator, PARTITION_ROOT_VERITY); |
| 43 |
if (streq(prefix, "usr-") && streq(suffix, "")) |
| 44 |
ASSERT_EQ(type.designator, PARTITION_USR); |
| 45 |
if (streq(prefix, "usr-") && streq(suffix, "-verity")) |
| 46 |
ASSERT_EQ(type.designator, PARTITION_USR_VERITY); |
| 47 |
|
| 48 |
ASSERT_EQ(type.arch, a); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
TEST(verity_mappings) { |
| 53 |
for (PartitionDesignator p = 0; p < _PARTITION_DESIGNATOR_MAX; p++) { |
| 54 |
PartitionDesignator q; |
| 55 |
|
| 56 |
q = partition_verity_hash_of(p); |
| 57 |
ASSERT_TRUE(q < 0 || partition_verity_hash_to_data(q) == p); |
| 58 |
|
| 59 |
q = partition_verity_sig_of(p); |
| 60 |
ASSERT_TRUE(q < 0 || partition_verity_sig_to_data(q) == p); |
| 61 |
|
| 62 |
q = partition_verity_hash_to_data(p); |
| 63 |
ASSERT_TRUE(q < 0 || partition_verity_hash_of(q) == p); |
| 64 |
|
| 65 |
q = partition_verity_sig_to_data(p); |
| 66 |
ASSERT_TRUE(q < 0 || partition_verity_sig_of(q) == p); |
| 67 |
|
| 68 |
q = partition_verity_to_data(p); |
| 69 |
ASSERT_TRUE(q < 0 || partition_verity_hash_of(q) == p || partition_verity_sig_of(q) == p); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
TEST(type_alias_same) { |
| 74 |
/* Check that the partition type table is consistent, i.e. all aliases of the same partition type |
| 75 |
* carry the same metadata */ |
| 76 |
|
| 77 |
for (const GptPartitionType *t = gpt_partition_type_table; t->name; t++) { |
| 78 |
GptPartitionType x, y; |
| 79 |
|
| 80 |
x = gpt_partition_type_from_uuid(t->uuid); /* search first by uuid */ |
| 81 |
ASSERT_GE(gpt_partition_type_from_string(t->name, &y), 0); /* search first by name */ |
| 82 |
|
| 83 |
ASSERT_EQ(t->arch, x.arch); |
| 84 |
ASSERT_EQ(t->arch, y.arch); |
| 85 |
ASSERT_EQ(t->designator, x.designator); |
| 86 |
ASSERT_EQ(t->designator, y.designator); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
TEST(override_architecture) { |
| 91 |
GptPartitionType x, y; |
| 92 |
|
| 93 |
ASSERT_GE(gpt_partition_type_from_string("root-x86-64", &x), 0); |
| 94 |
ASSERT_EQ(x.arch, ARCHITECTURE_X86_64); |
| 95 |
|
| 96 |
ASSERT_GE(gpt_partition_type_from_string("root-arm64", &y), 0); |
| 97 |
ASSERT_EQ(y.arch, ARCHITECTURE_ARM64); |
| 98 |
|
| 99 |
x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64); |
| 100 |
ASSERT_EQ(x.arch, y.arch); |
| 101 |
ASSERT_EQ(x.designator, y.designator); |
| 102 |
ASSERT_EQ_ID128(x.uuid, y.uuid); |
| 103 |
ASSERT_STREQ(x.name, y.name); |
| 104 |
|
| 105 |
/* If the partition type does not have an architecture, nothing should change. */ |
| 106 |
|
| 107 |
ASSERT_GE(gpt_partition_type_from_string("esp", &x), 0); |
| 108 |
y = x; |
| 109 |
|
| 110 |
x = gpt_partition_type_override_architecture(x, ARCHITECTURE_ARM64); |
| 111 |
ASSERT_EQ(x.arch, y.arch); |
| 112 |
ASSERT_EQ(x.designator, y.designator); |
| 113 |
ASSERT_EQ_ID128(x.uuid, y.uuid); |
| 114 |
ASSERT_STREQ(x.name, y.name); |
| 115 |
} |
| 116 |
|
| 117 |
static void make_gpt(int fd, uint32_t sector_size, const GptPartitionEntry *part_entries, size_t n_entries) { |
| 118 |
/* Zero-fill enough for header probing (gpt_probe reads 2*4096 = 8KB) */ |
| 119 |
static const uint8_t zeros[2 * 4096] = {}; |
| 120 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, zeros, sizeof(zeros), 0), (ssize_t) sizeof(zeros)); |
| 121 |
|
| 122 |
GptHeader h = { |
| 123 |
.signature = { 'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T' }, |
| 124 |
.revision = htole32(UINT32_C(0x00010000)), |
| 125 |
.header_size = htole32(sizeof(GptHeader)), |
| 126 |
.my_lba = htole64(1), |
| 127 |
.partition_entry_lba = htole64(2), |
| 128 |
.number_of_partition_entries = htole32(n_entries), |
| 129 |
.size_of_partition_entry = htole32(sizeof(GptPartitionEntry)), |
| 130 |
}; |
| 131 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, &h, sizeof(h), sector_size), (ssize_t) sizeof(h)); |
| 132 |
|
| 133 |
if (n_entries > 0) { |
| 134 |
size_t entries_size = n_entries * sizeof(GptPartitionEntry); |
| 135 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, part_entries, entries_size, 2 * sector_size), (ssize_t) entries_size); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
TEST(gpt_probe_empty) { |
| 140 |
_cleanup_close_ int fd = -EBADF; |
| 141 |
|
| 142 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 143 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 0); |
| 144 |
} |
| 145 |
|
| 146 |
TEST(gpt_probe_too_short) { |
| 147 |
_cleanup_close_ int fd = -EBADF; |
| 148 |
static const uint8_t buf[4096] = {}; |
| 149 |
|
| 150 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 151 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, buf, sizeof(buf), 0), (ssize_t) sizeof(buf)); |
| 152 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 0); |
| 153 |
} |
| 154 |
|
| 155 |
TEST(gpt_probe_no_signature) { |
| 156 |
_cleanup_close_ int fd = -EBADF; |
| 157 |
static const uint8_t buf[2 * 4096] = {}; |
| 158 |
|
| 159 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 160 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, buf, sizeof(buf), 0), (ssize_t) sizeof(buf)); |
| 161 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 0); |
| 162 |
} |
| 163 |
|
| 164 |
TEST(gpt_probe_sector_512) { |
| 165 |
_cleanup_close_ int fd = -EBADF; |
| 166 |
|
| 167 |
const GptPartitionEntry entries[2] = { |
| 168 |
{ |
| 169 |
.unique_partition_guid = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 170 |
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 }, |
| 171 |
.starting_lba = htole64(100), |
| 172 |
.ending_lba = htole64(200), |
| 173 |
}, |
| 174 |
{ |
| 175 |
.unique_partition_guid = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 176 |
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20 }, |
| 177 |
.starting_lba = htole64(300), |
| 178 |
.ending_lba = htole64(400), |
| 179 |
}, |
| 180 |
}; |
| 181 |
|
| 182 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 183 |
make_gpt(fd, 512, entries, 2); |
| 184 |
|
| 185 |
/* Sector size detection only */ |
| 186 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 512); |
| 187 |
|
| 188 |
/* Header return */ |
| 189 |
GptHeader h; |
| 190 |
ASSERT_OK_EQ(gpt_probe(fd, &h, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 512); |
| 191 |
ASSERT_EQ(le32toh(h.number_of_partition_entries), 2u); |
| 192 |
ASSERT_EQ(le32toh(h.size_of_partition_entry), (uint32_t) sizeof(GptPartitionEntry)); |
| 193 |
|
| 194 |
/* Full probe with entries */ |
| 195 |
_cleanup_free_ void *ret_entries = NULL; |
| 196 |
uint32_t n, sz; |
| 197 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, &ret_entries, &n, &sz), (ssize_t) 512); |
| 198 |
ASSERT_EQ(n, 2u); |
| 199 |
ASSERT_EQ(sz, (uint32_t) sizeof(GptPartitionEntry)); |
| 200 |
ASSERT_NOT_NULL(ret_entries); |
| 201 |
|
| 202 |
GptPartitionEntry *e = ret_entries; |
| 203 |
ASSERT_EQ(memcmp_nn(e[0].unique_partition_guid, sizeof(e[0].unique_partition_guid), entries[0].unique_partition_guid, sizeof(entries[0].unique_partition_guid)), 0); |
| 204 |
ASSERT_EQ(memcmp_nn(e[1].unique_partition_guid, sizeof(e[1].unique_partition_guid), entries[1].unique_partition_guid, sizeof(entries[1].unique_partition_guid)), 0); |
| 205 |
ASSERT_EQ(le64toh(e[0].starting_lba), UINT64_C(100)); |
| 206 |
ASSERT_EQ(le64toh(e[1].starting_lba), UINT64_C(300)); |
| 207 |
} |
| 208 |
|
| 209 |
TEST(gpt_probe_sector_4096) { |
| 210 |
_cleanup_close_ int fd = -EBADF; |
| 211 |
|
| 212 |
const GptPartitionEntry entry = { |
| 213 |
.unique_partition_guid = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, |
| 214 |
0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99 }, |
| 215 |
.starting_lba = htole64(50), |
| 216 |
.ending_lba = htole64(100), |
| 217 |
}; |
| 218 |
|
| 219 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 220 |
make_gpt(fd, 4096, &entry, 1); |
| 221 |
|
| 222 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), (ssize_t) 4096); |
| 223 |
|
| 224 |
_cleanup_free_ void *ret_entries = NULL; |
| 225 |
uint32_t n, sz; |
| 226 |
ASSERT_OK_EQ(gpt_probe(fd, /* ret_header= */ NULL, &ret_entries, &n, &sz), (ssize_t) 4096); |
| 227 |
ASSERT_EQ(n, 1u); |
| 228 |
|
| 229 |
GptPartitionEntry *e = ret_entries; |
| 230 |
ASSERT_EQ(memcmp_nn(e[0].unique_partition_guid, sizeof(e[0].unique_partition_guid), entry.unique_partition_guid, sizeof(entry.unique_partition_guid)), 0); |
| 231 |
ASSERT_EQ(le64toh(e[0].starting_lba), UINT64_C(50)); |
| 232 |
} |
| 233 |
|
| 234 |
TEST(gpt_probe_ambiguous) { |
| 235 |
_cleanup_close_ int fd = -EBADF; |
| 236 |
|
| 237 |
const GptPartitionEntry entry = {}; |
| 238 |
|
| 239 |
fd = ASSERT_OK(memfd_new("test-gpt-probe")); |
| 240 |
make_gpt(fd, 512, &entry, 1); |
| 241 |
|
| 242 |
/* Place a second valid header at offset 4096 */ |
| 243 |
GptHeader h2 = { |
| 244 |
.signature = { 'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T' }, |
| 245 |
.revision = htole32(UINT32_C(0x00010000)), |
| 246 |
.header_size = htole32(sizeof(GptHeader)), |
| 247 |
.my_lba = htole64(1), |
| 248 |
.partition_entry_lba = htole64(2), |
| 249 |
.number_of_partition_entries = htole32(1), |
| 250 |
.size_of_partition_entry = htole32(sizeof(GptPartitionEntry)), |
| 251 |
}; |
| 252 |
ASSERT_OK_EQ_ERRNO(pwrite(fd, &h2, sizeof(h2), 4096), (ssize_t) sizeof(h2)); |
| 253 |
|
| 254 |
ASSERT_ERROR(gpt_probe(fd, /* ret_header= */ NULL, /* ret_entries= */ NULL, /* ret_n_entries= */ NULL, /* ret_entry_size= */ NULL), ENOTUNIQ); |
| 255 |
} |
| 256 |
|
| 257 |
DEFINE_TEST_MAIN(LOG_INFO); |