/src/cryptsetup/tests/fuzz/crypt_load_misc_fuzz.cc
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * cryptsetup VERITY, INTEGRITY fuzz target |
4 | | */ |
5 | | |
6 | | extern "C" { |
7 | 586 | #define FILESIZE (16777216) |
8 | | #include "src/cryptsetup.h" |
9 | | #include <err.h> |
10 | | #include "verity/verity.h" |
11 | | #include "integrity/integrity.h" |
12 | | #include "crypto_backend/crypto_backend.h" |
13 | | #include "FuzzerInterface.h" |
14 | | |
15 | 455 | static void empty_log(int level, const char *msg, void *usrptr) {} |
16 | | |
17 | 586 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
18 | 586 | int fd, r; |
19 | 586 | struct crypt_device *cd = NULL; |
20 | 586 | char name[] = "/tmp/test-script-fuzz.XXXXXX"; |
21 | | |
22 | 586 | fd = mkostemp(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC); |
23 | 586 | if (fd == -1) |
24 | 0 | err(EXIT_FAILURE, "mkostemp() failed"); |
25 | | |
26 | | /* enlarge header */ |
27 | 586 | if (ftruncate(fd, FILESIZE) == -1) |
28 | 0 | goto out; |
29 | | |
30 | 586 | if (write_buffer(fd, data, size) != (ssize_t) size) |
31 | 0 | goto out; |
32 | | |
33 | 586 | crypt_set_log_callback(NULL, empty_log, NULL); |
34 | | |
35 | 586 | if (crypt_init(&cd, name) == 0) { |
36 | 586 | r = crypt_load(cd, CRYPT_VERITY, NULL); |
37 | 586 | if (r == 0) |
38 | 24 | goto out; |
39 | | |
40 | 562 | (void) crypt_load(cd, CRYPT_INTEGRITY, NULL); |
41 | 562 | } |
42 | 586 | out: |
43 | 586 | crypt_free(cd); |
44 | 586 | close(fd); |
45 | 586 | unlink(name); |
46 | 586 | return 0; |
47 | 586 | } |
48 | | } |