Skip to content

Commit a13bead

Browse files
arthurscchanmbroz
authored andcommitted
OSS-Fuzz: Add new fuzzer targets general crypto loading
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
1 parent 68cb74f commit a13bead

4 files changed

Lines changed: 73 additions & 2 deletions

File tree

tests/fuzz/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ distclean-local:
44
-rm -rf out build
55

66
if ENABLE_FUZZ_TARGETS
7-
EXTRA_PROGRAMS = crypt2_load_fuzz crypt2_load_ondisk_fuzz
7+
EXTRA_PROGRAMS = crypt2_load_fuzz crypt2_load_ondisk_fuzz crypt_load_misc_fuzz
88
CLEANFILES = $(EXTRA_PROGRAMS)
99

1010
LIB_FUZZING_ENGINE := $(if $(LIB_FUZZING_ENGINE),$(LIB_FUZZING_ENGINE),"-fsanitize=fuzzer")
@@ -22,6 +22,11 @@ crypt2_load_ondisk_fuzz_LDADD = ../../libcryptsetup.la -L$(DEPS_PATH)/lib
2222
crypt2_load_ondisk_fuzz_LDFLAGS = $(AM_LDFLAGS) $(LIB_FUZZING_ENGINE) $(SANITIZER)
2323
crypt2_load_ondisk_fuzz_CXXFLAGS = $(AM_CXXFLAGS) -I$(top_srcdir)/lib -I$(top_srcdir)/tests/fuzz
2424

25+
crypt_load_misc_fuzz_SOURCES = FuzzerInterface.h crypt_load_misc_fuzz.cc
26+
crypt_load_misc_fuzz_LDADD = ../../libcryptsetup.la -L$(DEPS_PATH)/lib
27+
crypt_load_misc_fuzz_LDFLAGS = $(AM_LDFLAGS) $(LIB_FUZZING_ENGINE) $(SANITIZER)
28+
crypt_load_misc_fuzz_CXXFLAGS = $(AM_CXXFLAGS) -I$(top_srcdir)/lib -I$(top_srcdir)/tests/fuzz
29+
2530
test-environment-m:
2631
@ if test ! -d $(DEPS_PATH); then \
2732
echo "You need to build static libraries first; use oss-fuzz-build.sh script."; \

tests/fuzz/crypt_load_misc_fuzz.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* cryptsetup VERITY, INTEGRITY fuzz target
4+
*/
5+
6+
extern "C" {
7+
#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+
static void empty_log(int level, const char *msg, void *usrptr) {}
16+
17+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
18+
int fd, r;
19+
struct crypt_device *cd = NULL;
20+
char name[] = "/tmp/test-script-fuzz.XXXXXX";
21+
22+
fd = mkostemp(name, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC);
23+
if (fd == -1)
24+
err(EXIT_FAILURE, "mkostemp() failed");
25+
26+
/* enlarge header */
27+
if (ftruncate(fd, FILESIZE) == -1)
28+
goto out;
29+
30+
if (write_buffer(fd, data, size) != (ssize_t) size)
31+
goto out;
32+
33+
crypt_set_log_callback(NULL, empty_log, NULL);
34+
35+
if (crypt_init(&cd, name) == 0) {
36+
r = crypt_load(cd, CRYPT_VERITY, NULL);
37+
if (r == 0)
38+
goto out;
39+
40+
(void) crypt_load(cd, CRYPT_INTEGRITY, NULL);
41+
}
42+
out:
43+
crypt_free(cd);
44+
close(fd);
45+
unlink(name);
46+
return 0;
47+
}
48+
}

tests/fuzz/meson.build

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ if get_option('fuzz-targets')
3434
link_args,
3535
],
3636
include_directories: includes_tools)
37+
38+
crypt_load_misc_fuzz = executable('crypt_load_misc_fuzz',
39+
[
40+
'crypt_load_misc_fuzz.cc',
41+
],
42+
dependencies: [
43+
devmapper,
44+
fuzzing_engine,
45+
],
46+
link_with: [
47+
libcryptsetup,
48+
libcrypto_backend,
49+
libutils_io,
50+
],
51+
link_args: [
52+
link_args,
53+
],
54+
include_directories: includes_tools)
3755
endif

tests/fuzz/oss-fuzz-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export CFLAGS="${CFLAGS:-$flags} -I$DEPS_PATH/include"
3434
export CXXFLAGS="${CXXFLAGS:-$flags} -I$DEPS_PATH/include"
3535
export LDFLAGS="${LDFLAGS-} -L$DEPS_PATH/lib"
3636

37-
ENABLED_FUZZERS=${ENABLED_FUZZERS:-crypt2_load_fuzz crypt2_load_ondisk_fuzz}
37+
ENABLED_FUZZERS=${ENABLED_FUZZERS:-crypt2_load_fuzz crypt2_load_ondisk_fuzz crypt_load_misc_fuzz}
3838

3939
mkdir -p $SRC
4040
mkdir -p $OUT

0 commit comments

Comments
 (0)