| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-hwdb.h" |
| 7 |
|
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "hwdb-internal.h" |
| 11 |
#include "nulstr-util.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "rm-rf.h" |
| 14 |
#include "tests.h" |
| 15 |
#include "tmpfile-util.h" |
| 16 |
|
| 17 |
TEST(failed_enumerate) { |
| 18 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 19 |
const char *key, *value; |
| 20 |
|
| 21 |
assert_se(sd_hwdb_new(&hwdb) == 0); |
| 22 |
|
| 23 |
assert_se(sd_hwdb_seek(hwdb, "no-such-modalias-should-exist") == 0); |
| 24 |
|
| 25 |
assert_se(sd_hwdb_enumerate(hwdb, &key, &value) == 0); |
| 26 |
ASSERT_RETURN_EXPECTED_SE(sd_hwdb_enumerate(hwdb, &key, NULL) == -EINVAL); |
| 27 |
ASSERT_RETURN_EXPECTED_SE(sd_hwdb_enumerate(hwdb, NULL, &value) == -EINVAL); |
| 28 |
} |
| 29 |
|
| 30 |
#define DELL_MODALIAS \ |
| 31 |
"evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY:" |
| 32 |
|
| 33 |
TEST(basic_enumerate) { |
| 34 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 35 |
const char *key, *value; |
| 36 |
size_t len1 = 0, len2 = 0; |
| 37 |
int r; |
| 38 |
|
| 39 |
assert_se(sd_hwdb_new(&hwdb) == 0); |
| 40 |
|
| 41 |
assert_se(sd_hwdb_seek(hwdb, DELL_MODALIAS) == 0); |
| 42 |
|
| 43 |
for (;;) { |
| 44 |
r = sd_hwdb_enumerate(hwdb, &key, &value); |
| 45 |
assert_se(IN_SET(r, 0, 1)); |
| 46 |
if (r == 0) |
| 47 |
break; |
| 48 |
assert_se(key); |
| 49 |
assert_se(value); |
| 50 |
log_debug("A: \"%s\" → \"%s\"", key, value); |
| 51 |
len1 += strlen(key) + strlen(value); |
| 52 |
} |
| 53 |
|
| 54 |
SD_HWDB_FOREACH_PROPERTY(hwdb, DELL_MODALIAS, key, value) { |
| 55 |
log_debug("B: \"%s\" → \"%s\"", key, value); |
| 56 |
len2 += strlen(key) + strlen(value); |
| 57 |
} |
| 58 |
|
| 59 |
assert_se(len1 == len2); |
| 60 |
} |
| 61 |
|
| 62 |
TEST(sd_hwdb_new_from_path) { |
| 63 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 64 |
int r; |
| 65 |
|
| 66 |
ASSERT_RETURN_EXPECTED_SE(sd_hwdb_new_from_path(NULL, &hwdb) == -EINVAL); |
| 67 |
ASSERT_RETURN_EXPECTED_SE(sd_hwdb_new_from_path("", &hwdb) == -EINVAL); |
| 68 |
assert_se(sd_hwdb_new_from_path("/path/that/should/not/exist", &hwdb) < 0); |
| 69 |
|
| 70 |
NULSTR_FOREACH(hwdb_bin_path, HWDB_BIN_PATHS) { |
| 71 |
r = sd_hwdb_new_from_path(hwdb_bin_path, &hwdb); |
| 72 |
if (r >= 0) |
| 73 |
break; |
| 74 |
} |
| 75 |
|
| 76 |
assert_se(r >= 0); |
| 77 |
} |
| 78 |
|
| 79 |
static sd_hwdb* hwdb_new_from_blob(const void *data, size_t size) { |
| 80 |
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
| 81 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 82 |
_cleanup_close_ int fd = -EBADF; |
| 83 |
|
| 84 |
ASSERT_OK(mkdtemp_malloc(/* template= */ NULL, &tmp)); |
| 85 |
_cleanup_free_ char *path = ASSERT_NOT_NULL(path_join(tmp, "hwdb.bin")); |
| 86 |
|
| 87 |
fd = ASSERT_OK_ERRNO(open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC, 0644)); |
| 88 |
ASSERT_OK_EQ_ERRNO(write(fd, data, size), (ssize_t) size); |
| 89 |
fd = safe_close(fd); |
| 90 |
|
| 91 |
ASSERT_OK(sd_hwdb_new_from_path(path, &hwdb)); |
| 92 |
|
| 93 |
return TAKE_PTR(hwdb); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(sd_hwdb_seek_rejects_invalid_fnmatch_child_node) { |
| 97 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 98 |
|
| 99 |
struct invalid_hwdb { |
| 100 |
struct trie_header_f header; |
| 101 |
struct trie_node_f root_node; |
| 102 |
struct trie_child_entry_f child1; |
| 103 |
struct trie_node_f mid_node; |
| 104 |
struct trie_child_entry_f child2; |
| 105 |
struct trie_node_f wildcard_node; |
| 106 |
struct trie_child_entry_f child3; |
| 107 |
char strings[STRLEN("usb:") + 1]; |
| 108 |
} _packed_ data = { |
| 109 |
.header = { |
| 110 |
.signature = HWDB_SIG, |
| 111 |
.tool_version = htole64(PROJECT_VERSION), |
| 112 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 113 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 114 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 115 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 116 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 117 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 118 |
.nodes_len = htole64(offsetof(struct invalid_hwdb, strings) - offsetof(struct invalid_hwdb, root_node)), |
| 119 |
.strings_len = htole64(STRLEN("usb:") + 1), |
| 120 |
}, |
| 121 |
.root_node = { |
| 122 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings)), |
| 123 |
.children_count = 1, |
| 124 |
}, |
| 125 |
.child1 = { |
| 126 |
.c = 'v', |
| 127 |
.child_off = htole64(offsetof(struct invalid_hwdb, mid_node)), |
| 128 |
}, |
| 129 |
.mid_node = { |
| 130 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb:")), |
| 131 |
.children_count = 1, |
| 132 |
}, |
| 133 |
.child2 = { |
| 134 |
.c = '*', |
| 135 |
.child_off = htole64(offsetof(struct invalid_hwdb, wildcard_node)), |
| 136 |
}, |
| 137 |
.wildcard_node = { |
| 138 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb:")), |
| 139 |
.children_count = 1, |
| 140 |
}, |
| 141 |
.child3 = { |
| 142 |
.c = 'x', |
| 143 |
.child_off = htole64(0xDEAD0000), |
| 144 |
}, |
| 145 |
.strings = "usb:", |
| 146 |
}; |
| 147 |
|
| 148 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 149 |
|
| 150 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "usb:vx"), EBADMSG); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(sd_hwdb_seek_rejects_invalid_fnmatch_prefix) { |
| 154 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 155 |
|
| 156 |
struct invalid_hwdb { |
| 157 |
struct trie_header_f header; |
| 158 |
struct trie_node_f root_node; |
| 159 |
struct trie_child_entry_f child1; |
| 160 |
struct trie_node_f mid_node; |
| 161 |
struct trie_child_entry_f child2; |
| 162 |
struct trie_node_f crash_node; |
| 163 |
char strings[STRLEN("usb:") + 1]; |
| 164 |
} _packed_ data = { |
| 165 |
.header = { |
| 166 |
.signature = HWDB_SIG, |
| 167 |
.tool_version = htole64(PROJECT_VERSION), |
| 168 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 169 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 170 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 171 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 172 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 173 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 174 |
.nodes_len = htole64(offsetof(struct invalid_hwdb, strings) - offsetof(struct invalid_hwdb, root_node)), |
| 175 |
.strings_len = htole64(STRLEN("usb:") + 1), |
| 176 |
}, |
| 177 |
.root_node = { |
| 178 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings)), |
| 179 |
.children_count = 1, |
| 180 |
}, |
| 181 |
.child1 = { |
| 182 |
.c = 'v', |
| 183 |
.child_off = htole64(offsetof(struct invalid_hwdb, mid_node)), |
| 184 |
}, |
| 185 |
.mid_node = { |
| 186 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb:")), |
| 187 |
.children_count = 1, |
| 188 |
}, |
| 189 |
.child2 = { |
| 190 |
.c = '*', |
| 191 |
.child_off = htole64(offsetof(struct invalid_hwdb, crash_node)), |
| 192 |
}, |
| 193 |
.crash_node = { |
| 194 |
.prefix_off = htole64(0xDEAD0000), |
| 195 |
}, |
| 196 |
.strings = "usb:", |
| 197 |
}; |
| 198 |
|
| 199 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 200 |
|
| 201 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "usb:v*"), EBADMSG); |
| 202 |
} |
| 203 |
|
| 204 |
TEST(sd_hwdb_seek_rejects_invalid_search_prefix) { |
| 205 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 206 |
|
| 207 |
struct invalid_hwdb { |
| 208 |
struct trie_header_f header; |
| 209 |
struct trie_node_f root_node; |
| 210 |
char strings[1]; |
| 211 |
} _packed_ data = { |
| 212 |
.header = { |
| 213 |
.signature = HWDB_SIG, |
| 214 |
.tool_version = htole64(PROJECT_VERSION), |
| 215 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 216 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 217 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 218 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 219 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 220 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 221 |
.nodes_len = htole64(sizeof(struct trie_node_f)), |
| 222 |
.strings_len = htole64(1), |
| 223 |
}, |
| 224 |
.root_node = { |
| 225 |
.prefix_off = htole64(0xDEAD0000), |
| 226 |
}, |
| 227 |
}; |
| 228 |
|
| 229 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 230 |
|
| 231 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "usb:v1234"), EBADMSG); |
| 232 |
} |
| 233 |
|
| 234 |
TEST(sd_hwdb_seek_rejects_truncated_children_array) { |
| 235 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 236 |
|
| 237 |
struct invalid_hwdb { |
| 238 |
struct trie_header_f header; |
| 239 |
struct trie_node_f root_node; |
| 240 |
} _packed_ data = { |
| 241 |
.header = { |
| 242 |
.signature = HWDB_SIG, |
| 243 |
.tool_version = htole64(PROJECT_VERSION), |
| 244 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 245 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 246 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 247 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 248 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 249 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 250 |
.nodes_len = htole64(sizeof(struct trie_node_f)), |
| 251 |
}, |
| 252 |
.root_node = { |
| 253 |
.children_count = 1, |
| 254 |
}, |
| 255 |
}; |
| 256 |
|
| 257 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 258 |
|
| 259 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "x"), EBADMSG); |
| 260 |
} |
| 261 |
|
| 262 |
TEST(sd_hwdb_seek_rejects_invalid_property_key) { |
| 263 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 264 |
|
| 265 |
struct invalid_hwdb { |
| 266 |
struct trie_header_f header; |
| 267 |
struct trie_node_f root_node; |
| 268 |
struct trie_value_entry_f value; |
| 269 |
char strings[STRLEN("usb") + 1 + STRLEN("value") + 1]; |
| 270 |
} _packed_ data = { |
| 271 |
.header = { |
| 272 |
.signature = HWDB_SIG, |
| 273 |
.tool_version = htole64(PROJECT_VERSION), |
| 274 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 275 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 276 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 277 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 278 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 279 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 280 |
.nodes_len = htole64(offsetof(struct invalid_hwdb, strings) - offsetof(struct invalid_hwdb, root_node)), |
| 281 |
.strings_len = htole64(STRLEN("usb") + 1 + STRLEN("value") + 1), |
| 282 |
}, |
| 283 |
.root_node = { |
| 284 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings)), |
| 285 |
.values_count = htole64(1), |
| 286 |
}, |
| 287 |
.value = { |
| 288 |
.key_off = htole64(0xDEAD0000), |
| 289 |
.value_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb") + 1), |
| 290 |
}, |
| 291 |
.strings = "usb\0value", |
| 292 |
}; |
| 293 |
|
| 294 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 295 |
|
| 296 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "usb"), EBADMSG); |
| 297 |
} |
| 298 |
|
| 299 |
TEST(sd_hwdb_seek_rejects_cyclic_trie) { |
| 300 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 301 |
|
| 302 |
/* Cyclic trie: the wildcard_node's only child points back to |
| 303 |
* itself, which would drive trie_fnmatch_f() into infinite |
| 304 |
* recursion (stack-overflow CVE-class bug found by CIFuzz). |
| 305 |
* The recursion-depth cap must turn this into a clean -EBADMSG. */ |
| 306 |
struct invalid_hwdb { |
| 307 |
struct trie_header_f header; |
| 308 |
struct trie_node_f root_node; |
| 309 |
struct trie_child_entry_f child1; |
| 310 |
struct trie_node_f mid_node; |
| 311 |
struct trie_child_entry_f child2; |
| 312 |
struct trie_node_f wildcard_node; |
| 313 |
struct trie_child_entry_f wildcard_self_child; |
| 314 |
char strings[STRLEN("usb:") + 1]; |
| 315 |
} _packed_ data = { |
| 316 |
.header = { |
| 317 |
.signature = HWDB_SIG, |
| 318 |
.tool_version = htole64(PROJECT_VERSION), |
| 319 |
.file_size = htole64(sizeof(struct invalid_hwdb)), |
| 320 |
.header_size = htole64(sizeof(struct trie_header_f)), |
| 321 |
.node_size = htole64(sizeof(struct trie_node_f)), |
| 322 |
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)), |
| 323 |
.value_entry_size = htole64(sizeof(struct trie_value_entry_f)), |
| 324 |
.nodes_root_off = htole64(offsetof(struct invalid_hwdb, root_node)), |
| 325 |
.nodes_len = htole64(offsetof(struct invalid_hwdb, strings) - offsetof(struct invalid_hwdb, root_node)), |
| 326 |
.strings_len = htole64(STRLEN("usb:") + 1), |
| 327 |
}, |
| 328 |
.root_node = { |
| 329 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings)), |
| 330 |
.children_count = 1, |
| 331 |
}, |
| 332 |
.child1 = { |
| 333 |
.c = 'v', |
| 334 |
.child_off = htole64(offsetof(struct invalid_hwdb, mid_node)), |
| 335 |
}, |
| 336 |
.mid_node = { |
| 337 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb:")), |
| 338 |
.children_count = 1, |
| 339 |
}, |
| 340 |
.child2 = { |
| 341 |
.c = '*', |
| 342 |
.child_off = htole64(offsetof(struct invalid_hwdb, wildcard_node)), |
| 343 |
}, |
| 344 |
.wildcard_node = { |
| 345 |
.prefix_off = htole64(offsetof(struct invalid_hwdb, strings) + STRLEN("usb:")), |
| 346 |
.children_count = 1, |
| 347 |
}, |
| 348 |
.wildcard_self_child = { |
| 349 |
.c = 'a', |
| 350 |
.child_off = htole64(offsetof(struct invalid_hwdb, wildcard_node)), |
| 351 |
}, |
| 352 |
.strings = "usb:", |
| 353 |
}; |
| 354 |
|
| 355 |
hwdb = ASSERT_NOT_NULL(hwdb_new_from_blob(&data, sizeof(data))); |
| 356 |
|
| 357 |
ASSERT_ERROR(sd_hwdb_seek(hwdb, "usb:v*"), EBADMSG); |
| 358 |
} |
| 359 |
|
| 360 |
static int intro(void) { |
| 361 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 362 |
int r; |
| 363 |
|
| 364 |
r = sd_hwdb_new(&hwdb); |
| 365 |
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) |
| 366 |
return log_tests_skipped_errno(r, "cannot open hwdb"); |
| 367 |
|
| 368 |
return EXIT_SUCCESS; |
| 369 |
} |
| 370 |
|
| 371 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |