Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/logsrvd/regress/fuzz/fuzz_logsrvd_conf.c
1532 views
1
/*
2
* Copyright (c) 2021-2022 Todd C. Miller <[email protected]>
3
*
4
* Permission to use, copy, modify, and distribute this software for any
5
* purpose with or without fee is hereby granted, provided that the above
6
* copyright notice and this permission notice appear in all copies.
7
*
8
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
*/
16
17
#include <config.h>
18
19
#include <sys/types.h>
20
#include <sys/socket.h>
21
#include <netinet/in.h>
22
#include <arpa/inet.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <fcntl.h>
28
#include <limits.h>
29
#include <netdb.h>
30
#include <regex.h>
31
#include <time.h>
32
#include <unistd.h>
33
#if defined(HAVE_STDINT_H)
34
# include <stdint.h>
35
#elif defined(HAVE_INTTYPES_H)
36
# include <inttypes.h>
37
#endif
38
39
#include <sudo_compat.h>
40
#include <sudo_conf.h>
41
#include <sudo_debug.h>
42
#include <sudo_eventlog.h>
43
#include <sudo_fatal.h>
44
#include <sudo_iolog.h>
45
#include <sudo_plugin.h>
46
#include <sudo_util.h>
47
48
#include <logsrvd.h>
49
50
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
51
52
/*
53
* Stub version that always succeeds for small inputs and fails for large.
54
* We want to fuzz our parser, not libc's regular expression code.
55
*/
56
bool
57
sudo_regex_compile_v1(void *v, const char *pattern, const char **errstr)
58
{
59
regex_t *preg = v;
60
61
if (strlen(pattern) > 32) {
62
*errstr = "invalid regular expression";
63
return false;
64
}
65
66
/* hopefully avoid regfree() crashes */
67
memset(preg, 0, sizeof(*preg));
68
return true;
69
}
70
71
/*
72
* The fuzzing environment may not have DNS available, this may result
73
* in long delays that cause a timeout when fuzzing.
74
* This getaddrinfo() resolves every name as "localhost" (127.0.0.1).
75
*/
76
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
77
/* Avoid compilation errors if getaddrinfo() or freeaddrinfo() are macros. */
78
# undef getaddrinfo
79
# undef freeaddrinfo
80
81
int
82
# ifdef HAVE_GETADDRINFO
83
getaddrinfo(
84
# else
85
sudo_getaddrinfo(
86
# endif
87
const char *nodename, const char *servname,
88
const struct addrinfo *hints, struct addrinfo **res)
89
{
90
struct addrinfo *ai;
91
struct in_addr addr;
92
unsigned short port = 0;
93
94
/* Stub getaddrinfo(3) to avoid a DNS timeout in CIfuzz. */
95
if (servname == NULL) {
96
/* Must have either nodename or servname. */
97
if (nodename == NULL)
98
return EAI_NONAME;
99
} else {
100
struct servent *servent;
101
const char *errstr;
102
103
/* Parse servname as a port number or IPv4 TCP service name. */
104
port = sudo_strtonum(servname, 0, USHRT_MAX, &errstr);
105
if (errstr != NULL && errno == ERANGE)
106
return EAI_SERVICE;
107
if (hints != NULL && ISSET(hints->ai_flags, AI_NUMERICSERV))
108
return EAI_NONAME;
109
servent = getservbyname(servname, "tcp");
110
if (servent == NULL)
111
return EAI_NONAME;
112
port = htons(servent->s_port);
113
}
114
115
/* Hard-code IPv4 localhost for fuzzing. */
116
ai = calloc(1, sizeof(*ai) + sizeof(struct sockaddr_in));
117
if (ai == NULL)
118
return EAI_MEMORY;
119
ai->ai_canonname = strdup("localhost");
120
if (ai == NULL) {
121
free(ai);
122
return EAI_MEMORY;
123
}
124
ai->ai_family = AF_INET;
125
ai->ai_protocol = IPPROTO_TCP;
126
ai->ai_addrlen = sizeof(struct sockaddr_in);
127
ai->ai_addr = (struct sockaddr *)(ai + 1);
128
inet_pton(AF_INET, "127.0.0.1", &addr);
129
((struct sockaddr_in *)ai->ai_addr)->sin_family = AF_INET;
130
((struct sockaddr_in *)ai->ai_addr)->sin_addr = addr;
131
((struct sockaddr_in *)ai->ai_addr)->sin_port = htons(port);
132
*res = ai;
133
return 0;
134
}
135
136
void
137
# ifdef HAVE_GETADDRINFO
138
freeaddrinfo(struct addrinfo *ai)
139
# else
140
sudo_freeaddrinfo(struct addrinfo *ai)
141
# endif
142
{
143
struct addrinfo *next;
144
145
while (ai != NULL) {
146
next = ai->ai_next;
147
free(ai->ai_canonname);
148
free(ai);
149
ai = next;
150
}
151
}
152
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
153
154
static int
155
fuzz_conversation(int num_msgs, const struct sudo_conv_message msgs[],
156
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
157
{
158
int n;
159
160
for (n = 0; n < num_msgs; n++) {
161
const struct sudo_conv_message *msg = &msgs[n];
162
163
switch (msg->msg_type & 0xff) {
164
case SUDO_CONV_PROMPT_ECHO_ON:
165
case SUDO_CONV_PROMPT_MASK:
166
case SUDO_CONV_PROMPT_ECHO_OFF:
167
/* input not supported */
168
return -1;
169
case SUDO_CONV_ERROR_MSG:
170
case SUDO_CONV_INFO_MSG:
171
/* no output for fuzzers */
172
break;
173
default:
174
return -1;
175
}
176
}
177
return 0;
178
}
179
180
int
181
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
182
{
183
char tempfile[] = "/tmp/logsrvd_conf.XXXXXX";
184
ssize_t nwritten;
185
int fd;
186
187
initprogname("fuzz_logsrvd_conf");
188
if (getenv("SUDO_FUZZ_VERBOSE") == NULL)
189
sudo_warn_set_conversation(fuzz_conversation);
190
191
/* logsrvd_conf_read() uses a conf file path, not an open file. */
192
fd = mkstemp(tempfile);
193
if (fd == -1)
194
return 0;
195
nwritten = write(fd, data, size);
196
if ((size_t)nwritten != size) {
197
close(fd);
198
return 0;
199
}
200
close(fd);
201
202
if (logsrvd_conf_read(tempfile)) {
203
/* public config getters */
204
logsrvd_conf_iolog_dir();
205
logsrvd_conf_iolog_file();
206
logsrvd_conf_iolog_mode();
207
logsrvd_conf_pid_file();
208
logsrvd_conf_relay_address();
209
logsrvd_conf_relay_connect_timeout();
210
logsrvd_conf_relay_tcp_keepalive();
211
logsrvd_conf_relay_timeout();
212
logsrvd_conf_server_listen_address();
213
logsrvd_conf_server_tcp_keepalive();
214
logsrvd_conf_server_timeout();
215
216
/* free config */
217
logsrvd_conf_cleanup();
218
}
219
220
unlink(tempfile);
221
222
fflush(stdout);
223
224
return 0;
225
}
226
227