/src/libtasn1/fuzz/libtasn1_parser2tree_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright(c) 2019 Free Software Foundation, Inc. |
3 | | * |
4 | | * This file is part of libtasn1. |
5 | | * |
6 | | * Libtasn1 is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * Libtasn1 is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with libtasn1. If not, see <https://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This fuzzer is testing asn1_parser2tree()'s robustness with arbitrary ASN.1 |
20 | | * input data. |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | |
25 | | #include <stdlib.h> /* malloc, free */ |
26 | | #include <string.h> /* strcmp, memcpy */ |
27 | | |
28 | | #include "libtasn1.h" |
29 | | #include "fuzzer.h" |
30 | | |
31 | | static const uint8_t *g_data; |
32 | | static size_t g_size; |
33 | | |
34 | | #if defined HAVE_DLFCN_H && defined HAVE_FMEMOPEN |
35 | | # include <dlfcn.h> |
36 | | # ifdef RTLD_NEXT /* Not defined e.g. on CygWin */ |
37 | | |
38 | | FILE * |
39 | | fopen (const char *pathname, const char *mode) |
40 | 4.82k | { |
41 | 4.82k | FILE *(*libc_fopen) (const char *, const char *) = |
42 | 4.82k | (FILE * (*)(const char *, const char *)) dlsym (RTLD_NEXT, "fopen"); |
43 | | |
44 | 4.82k | if (!strcmp (pathname, "pkix.asn")) |
45 | 2.40k | return fmemopen ((void *) g_data, g_size, mode); |
46 | | |
47 | 2.42k | return libc_fopen (pathname, mode); |
48 | 4.82k | } |
49 | | # endif |
50 | | #endif |
51 | | |
52 | | int |
53 | | LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
54 | 2.41k | { |
55 | 2.41k | char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; |
56 | 2.41k | asn1_node definitions = NULL; |
57 | 2.41k | int rc; |
58 | | |
59 | 2.41k | if (size > 10000) /* same as max_len = 10000 in .options file */ |
60 | 7 | return 0; |
61 | | |
62 | 2.40k | g_data = data; |
63 | 2.40k | g_size = size; |
64 | | |
65 | 2.40k | rc = asn1_parser2tree ("pkix.asn", &definitions, errorDescription); |
66 | 2.40k | if (rc == ASN1_SUCCESS) |
67 | 432 | { |
68 | 432 | asn1_delete_structure (&definitions); |
69 | 432 | } |
70 | | |
71 | 2.40k | return 0; |
72 | 2.41k | } |