/src/mosquitto/fuzzing/plugins/dynamic-security/dynsec_fuzz_load.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2023 Cedalo GmbH |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include <cstdio> |
20 | | #include <cstdint> |
21 | | #include <cstdlib> |
22 | | #include <cstring> |
23 | | #include <sys/stat.h> |
24 | | #include <unistd.h> |
25 | | |
26 | | #ifdef __cplusplus |
27 | | extern "C" { |
28 | | #endif |
29 | | |
30 | | #include <mosquitto.h> |
31 | | #include <mosquitto_broker_internal.h> |
32 | | |
33 | | #ifdef __cplusplus |
34 | | } |
35 | | #endif |
36 | | |
37 | | /* |
38 | | * Test loading a file |
39 | | */ |
40 | | |
41 | | extern struct mosquitto_db db; |
42 | | |
43 | | void run_dynsec(char *filename) |
44 | 5.29k | { |
45 | 5.29k | struct mosquitto_plugin_id_t identifier; |
46 | 5.29k | struct mosquitto_opt options[1]; |
47 | | |
48 | 5.29k | db.config = (struct mosquitto__config *)calloc(1, sizeof(struct mosquitto__config)); |
49 | 5.29k | log__init(db.config); |
50 | | |
51 | 5.29k | memset(&identifier, 0, sizeof(identifier)); |
52 | | |
53 | 5.29k | options[0].key = strdup("config_file"); |
54 | 5.29k | options[0].value = filename; |
55 | | |
56 | 5.29k | mosquitto_plugin_init(&identifier, NULL, options, 1); |
57 | 5.29k | mosquitto_plugin_cleanup(NULL, options, 1); |
58 | | |
59 | 5.29k | free(options[0].key); |
60 | 5.29k | free(db.config); |
61 | 5.29k | free(identifier.plugin_name); |
62 | 5.29k | free(identifier.plugin_version); |
63 | 5.29k | db.config = NULL; |
64 | 5.29k | } |
65 | | |
66 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
67 | 17.3k | { |
68 | 17.3k | char filename[100]; |
69 | 17.3k | FILE *fptr; |
70 | | |
71 | 17.3k | umask(0077); |
72 | | |
73 | 17.3k | snprintf(filename, sizeof(filename), "/tmp/dynsec%d.conf", getpid()); |
74 | 17.3k | fptr = fopen(filename, "wb"); |
75 | 17.3k | if(!fptr) return 1; |
76 | 17.3k | fwrite(data, 1, size, fptr); |
77 | 17.3k | fclose(fptr); |
78 | | |
79 | 17.3k | run_dynsec(filename); |
80 | | |
81 | 17.3k | unlink(filename); |
82 | | |
83 | 17.3k | return 0; |
84 | 17.3k | } |