xref: /freebsd/crypto/krb5/src/tests/fuzzing/fuzz_profile.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/fuzzing/fuzz_profile.c */
3 /*
4  * Copyright (C) 2024 by Arjun. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Fuzzing harness implementation for profile_parse_file.
34  */
35 
36 #include "autoconf.h"
37 #include <prof_int.h>
38 
39 void dump_profile(struct profile_node *root, int level);
40 
41 #define kMinInputLength 2
42 #define kMaxInputLength 1024
43 
44 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
45 
46 int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)47 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48 {
49     errcode_t ret;
50     FILE *fp_w, *fp_r;
51     char file_name[256], *output;
52     struct profile_node *root;
53 
54     if (size < kMinInputLength || size > kMaxInputLength)
55         return 0;
56 
57     snprintf(file_name, sizeof(file_name), "/tmp/libfuzzer.%d", getpid());
58 
59     /* Write data into the file. */
60     fp_w = fopen(file_name, "w");
61     if (!fp_w)
62         return 1;
63     fwrite(data, 1, size, fp_w);
64     fclose(fp_w);
65 
66     /* Provide the file pointer to the parser. */
67     fp_r = fopen(file_name, "r");
68     if (!fp_r)
69         return 1;
70 
71     initialize_prof_error_table();
72 
73     ret = profile_parse_file(fp_r, &root, NULL);
74     if (!ret) {
75         ret = profile_write_tree_to_buffer(root, &output);
76         if (!ret)
77             free(output);
78 
79         profile_verify_node(root);
80         profile_free_node(root);
81     }
82 
83     fclose(fp_r);
84     unlink(file_name);
85 
86     return 0;
87 }
88