Debian Codegraph @

tests/fuzz/ssh_server_fuzzer.c

raw
1 /*
2 # Copyright 2016 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 ################################################################################
17 */
18
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28
29 #define LIBSSH_STATIC 1
30 #include <libssh/libssh.h>
31 #include <libssh/callbacks.h>
32 #include <libssh/server.h>
33
34 #include "nallocinc.c"
35
36 static const char kRSAPrivateKeyPEM[] =
37 "-----BEGIN RSA PRIVATE KEY-----\n"
38 "MIIEowIBAAKCAQEArAOREUWlBXJAKZ5hABYyxnRayDZP1bJeLbPVK+npxemrhHyZ\n"
39 "gjdbY3ADot+JRyWjvll2w2GI+3blt0j+x/ZWwjMKu/QYcycYp5HL01goxOxuusZb\n"
40 "i+KiHRGB6z0EMdXM7U82U7lA/j//HyZppyDjUDniWabXQJge8ksGXGTiFeAJ/687\n"
41 "uV+JJcjGPxAGFQxzyjitf/FrL9S0WGKZbyqeGDzyeBZ1NLIuaiOORyLGSW4duHLD\n"
42 "N78EmsJnwqg2gJQmRSaD4BNZMjtbfiFcSL9Uw4XQFTsWugUDEY1AU4c5g11nhzHz\n"
43 "Bi9qMOt5DzrZQpD4j0gA2LOHpHhoOdg1ZuHrGQIDAQABAoIBAFJTaqy/jllq8vZ4\n"
44 "TKiD900wBvrns5HtSlHJTe80hqQoT+Sa1cWSxPR0eekL32Hjy9igbMzZ83uWzh7I\n"
45 "mtgNODy9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ORPV9MOX3v+VnsCgD4/56URKOAngiS70xEP\n"
61 "ONwNbTCWuuv43HGzJoVFiAMGnQP1BAJ7gkHkjSegOGKkiw12EPUWhFcMg+GkgPhc\n"
62 "pOqNt/VMBPjJ/ysHJqmLfQK9A35JV6Cmdphe+OIl28bcKhAOz8Dw\n"
63 "-----END RSA PRIVATE KEY-----\n";
64
65 /* A userdata struct for session. */
66 struct session_data_struct {
67 /* Pointer to the channel the session will allocate. */
68 ssh_channel channel;
69 size_t auth_attempts;
70 bool authenticated;
71 };
72
73 static void _fuzz_finalize(void)
74 {
75 ssh_finalize();
76 }
77
78 int LLVMFuzzerInitialize(int *argc, char ***argv)
79 {
80 (void)argc;
81
82 nalloc_init(*argv[0]);
83
84 ssh_init();
85
86 atexit(_fuzz_finalize);
87
88 return 0;
89 }
90
91 static int auth_none(ssh_session session, const char *user, void *userdata)
92 {
93 struct session_data_struct *sdata =
94 (struct session_data_struct *)userdata;
95
96 (void)session;
97 (void)user;
98
99 if (sdata->auth_attempts > 0) {
100 sdata->authenticated = true;
101 }
102 sdata->auth_attempts++;
103
104 if (!sdata->authenticated) {
105 return SSH_AUTH_PARTIAL;
106 }
107
108 return SSH_AUTH_SUCCESS;
109 }
110
111 static ssh_channel channel_open(ssh_session session, void *userdata)
112 {
113 struct session_data_struct *sdata =
114 (struct session_data_struct *)userdata;
115
116 sdata->channel = ssh_channel_new(session);
117
118 return sdata->channel;
119 }
120
121 static int write_rsa_hostkey(const char *rsakey_path)
122 {
123 FILE *fp = NULL;
124 size_t nwritten;
125
126 fp = fopen(rsakey_path, "wb");
127 if (fp == NULL) {
128 return -1;
129 }
130
131 nwritten = fwrite(kRSAPrivateKeyPEM, 1, strlen(kRSAPrivateKeyPEM), fp);
132 fclose(fp);
133
134 if (nwritten != strlen(kRSAPrivateKeyPEM)) {
135 return -1;
136 }
137
138 return 0;
139 }
140
141 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
142 {
143 int socket_fds[2] = {-1, -1};
144 ssize_t nwritten;
145 bool no = false;
146 const char *env = NULL;
147 int rc;
148 ssh_bind sshbind = NULL;
149 ssh_session session = NULL;
150 ssh_event event = NULL;
151
152 /* Our struct holding information about the session. */
153 struct session_data_struct sdata = {
154 .channel = NULL,
155 .auth_attempts = 0,
156 .authenticated = false,
157 };
158
159 struct ssh_server_callbacks_struct server_cb = {
160 .userdata = &sdata,
161 .auth_none_function = auth_none,
162 .channel_open_request_session_function = channel_open,
163 };
164
165 /* This is the maximum that can be handled by the socket buffer before the
166 * other side will read some data. Other option would be feeding the socket
167 * from different thread which would not mind if it would be blocked, but I
168 * believe all the important inputs should fit into this size */
169 if (size > 219264) {
170 return -1;
171 }
172
173 /* Write SSH RSA host key to disk */
174 rc = write_rsa_hostkey("/tmp/libssh_fuzzer_private_key");
175 assert(rc == 0);
176
177 /* Set up the socket to send data */
178 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds);
179 assert(rc == 0);
180
181 nwritten = send(socket_fds[1], data, size, 0);
182 assert((size_t)nwritten == size);
183
184 rc = shutdown(socket_fds[1], SHUT_WR);
185 assert(rc == 0);
186
187 assert(nalloc_start(data, size) > 0);
188
189 /* Set up the libssh server */
190 sshbind = ssh_bind_new();
191 if (sshbind == NULL) {
192 goto out;
193 }
194
195 session = ssh_new();
196 if (session == NULL) {
197 goto out;
198 }
199
200 env = getenv("LIBSSH_VERBOSITY");
201 if (env != NULL && strlen(env) > 0) {
202 rc = ssh_bind_options_set(sshbind,
203 SSH_BIND_OPTIONS_LOG_VERBOSITY_STR,
204 env);
205 if (rc != SSH_OK) {
206 goto out;
207 }
208 }
209 rc = ssh_bind_options_set(sshbind,
210 SSH_BIND_OPTIONS_HOSTKEY,
211 "/tmp/libssh_fuzzer_private_key");
212 if (rc != SSH_OK) {
213 goto out;
214 }
215 rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_CIPHERS_C_S, "none");
216 if (rc != SSH_OK) {
217 goto out;
218 }
219 rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_CIPHERS_S_C, "none");
220 if (rc != SSH_OK) {
221 goto out;
222 }
223 rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HMAC_C_S, "none");
224 if (rc != SSH_OK) {
225 goto out;
226 }
227 rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HMAC_S_C, "none");
228 if (rc != SSH_OK) {
229 goto out;
230 }
231 rc = ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_PROCESS_CONFIG, &no);
232 if (rc != SSH_OK) {
233 goto out;
234 }
235
236 ssh_set_auth_methods(session, SSH_AUTH_METHOD_NONE);
237
238 ssh_callbacks_init(&server_cb);
239 ssh_set_server_callbacks(session, &server_cb);
240
241 rc = ssh_bind_accept_fd(sshbind, session, socket_fds[0]);
242 if (rc != SSH_OK) {
243 goto out;
244 }
245
246 event = ssh_event_new();
247 if (event == NULL) {
248 goto out;
249 }
250
251 if (ssh_handle_key_exchange(session) == SSH_OK) {
252 ssh_event_add_session(event, session);
253
254 size_t n = 0;
255 while (sdata.authenticated == false || sdata.channel == NULL) {
256 if (sdata.auth_attempts >= 3 || n >= 100) {
257 break;
258 }
259
260 if (ssh_event_dopoll(event, 100) == SSH_ERROR) {
261 break;
262 }
263
264 n++;
265 }
266 }
267
268 out:
269 nalloc_end();
270
271 ssh_event_free(event);
272
273 close(socket_fds[0]);
274 close(socket_fds[1]);
275
276 ssh_disconnect(session);
277 ssh_free(session);
278 ssh_bind_free(sshbind);
279
280 return 0;
281 }