1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * This fuzzer exercises the SNMP PDU parsing code, including ASN.1.
17  */
18 #include <net-snmp/net-snmp-config.h>
19 #include <net-snmp/net-snmp-includes.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 
LLVMFuzzerInitialize(int * argc,char *** argv)24 int LLVMFuzzerInitialize(int *argc, char ***argv) {
25     if (getenv("NETSNMP_DEBUGGING") != NULL) {
26         /*
27          * Turn on all debugging, to help understand what
28          * bits of the parser are running.
29          */
30         snmp_enable_stderrlog();
31         snmp_set_do_debugging(1);
32         debug_register_tokens("");
33     }
34     return 0;
35 }
36 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)37 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
38     size_t bytes_remaining = size;
39     netsnmp_pdu *pdu = SNMP_MALLOC_TYPEDEF(netsnmp_pdu);
40 
41     snmp_pdu_parse(pdu, (unsigned char *)data, &bytes_remaining);
42     snmp_free_pdu(pdu);
43     return 0;
44 }
45