File: crypt2_load_ondisk_fuzz.cc

package info (click to toggle)
cryptsetup 2%3A2.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 19,624 kB
  • sloc: ansic: 62,955; sh: 16,774; cpp: 994; xml: 920; makefile: 488; perl: 486
file content (51 lines) | stat: -rw-r--r-- 1,098 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * cryptsetup LUKS1, FileVault, BitLocker fuzz target
 */

extern "C" {
#define FILESIZE (16777216)
#include "src/cryptsetup.h"
#include <err.h>
#include "luks1/luks.h"
#include "crypto_backend/crypto_backend.h"
#include "FuzzerInterface.h"

void empty_log(int level, const char *msg, void *usrptr) {}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
	int fd, r;
	struct crypt_device *cd = NULL;
	char name[] = "/tmp/test-script-fuzz.XXXXXX";

	fd = mkostemp(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC);
	if (fd == -1)
		err(EXIT_FAILURE, "mkostemp() failed");

	/* enlarge header */
	if (ftruncate(fd, FILESIZE) == -1)
		goto out;

	if (write_buffer(fd, data, size) != (ssize_t) size)
		goto out;

	crypt_set_log_callback(NULL, empty_log, NULL);

	if (crypt_init(&cd, name) == 0) {
		r = crypt_load(cd, CRYPT_LUKS1, NULL);
		if (r == 0)
			goto out;

		r = crypt_load(cd, CRYPT_FVAULT2, NULL);
		if (r == 0)
			goto out;

		(void) crypt_load(cd, CRYPT_BITLK, NULL);
	}
out:
	crypt_free(cd);
	close(fd);
	unlink(name);
	return 0;
}
}