The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
fuzzer_json.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: c5d09af656653f0d577e834eeba7a1684020434d $
19 *
20 * @file src/bin/fuzzer_json.c
21 * @brief Functions to fuzz json
22 * */
23RCSID("$Id: c5d09af656653f0d577e834eeba7a1684020434d $")
24
25#include <freeradius-devel/build.h>
26
27DIAG_OFF(documentation)
28DIAG_OFF(deprecated)
29
30#include <stdint.h>
31#include <stddef.h>
32#include <stdlib.h>
33#include <string.h>
34#include <stdbool.h>
35#include <sys/types.h>
36#include <talloc.h>
37#include <json-c/json.h>
38
39/* Forward declarations for FreeRADIUS types to avoid header complexity */
40typedef struct fr_jpath_node_s fr_jpath_node_t;
41
42/* External declarations for functions */
44 char const *in, size_t inlen);
45
46int LLVMFuzzerInitialize(int *argc, char ***argv);
47int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
48
49int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
50{
51 void *ctx = NULL;
52 size_t split_point;
53
54 /* Need at least 2 bytes */
55 if (size < 2) {
56 return 0;
57 }
58
59 /* Limit input size to prevent timeouts */
60 if (size > 8192) {
61 return 0;
62 }
63
64 /* Initialize talloc context */
65 ctx = talloc_init("fuzzer_json");
66 if (!ctx) {
67 return 0;
68 }
69
70 /*
71 * Use first byte to determine split between JSON and jpath
72 */
73 split_point = (data[0] * size) / 256;
74 if (split_point >= size - 1) {
75 split_point = size / 2;
76 }
77
78 /*
79 * JSON string to parse with json-c
80 */
81 if (split_point > 1) {
82 char *str = NULL;
83 json_object *json_obj = NULL;
84
85 str = talloc_strndup(ctx, (const char *)(data + 1), split_point - 1);
86 if (str) {
87 json_obj = json_tokener_parse(str);
88 if (json_obj) {
89 json_object_put(json_obj);
90 json_obj = NULL;
91 }
92 }
93 }
94
95 /*
96 * jpath expression string to parse with FreeRADIUS
97 */
98 if (split_point < size - 1) {
99 size_t len = size - split_point - 1;
100 char *str = NULL;
101 fr_jpath_node_t *jpath_head = NULL;
102
103 if (len > 0) {
104 str = talloc_strndup(ctx,
105 (const char *)(data + split_point + 1),
106 len);
107 }
108
109 if (str) {
110 (void) fr_jpath_parse(ctx, &jpath_head, str, len);
111 }
112 }
113
114 talloc_free(ctx);
115 return 0;
116}
#define RCSID(id)
Definition build.h:506
#define DIAG_OFF(_x)
Definition build.h:480
static fr_slen_t in
Definition dict.h:882
struct fr_jpath_node_s fr_jpath_node_t
Definition fuzzer_json.c:40
int LLVMFuzzerInitialize(int *argc, char ***argv)
Definition fuzzer.c:93
ssize_t fr_jpath_parse(void *ctx, fr_jpath_node_t **head, char const *in, size_t inlen)
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
Definition fuzzer_json.c:49
talloc_free(hp)
long int ssize_t
unsigned char uint8_t
Functions which we wish were included in the standard talloc distribution.
#define talloc_strndup(_ctx, _str, _len)
Definition talloc.h:143
static fr_slen_t head
Definition xlat.h:420
static fr_slen_t data
Definition value.h:1340
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030