Debian Codegraph @

tests/fuzz/ssh_client_fuzzer.c

raw
1 /*
2 * Copyright 2019 Andreas Schneider <[email protected]>
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 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <signal.h>
21 #include <stdbool.h>
22
23 #define LIBSSH_STATIC 1
24 #include <libssh/libssh.h>
25 #include <libssh/callbacks.h>
26
27 #include "nallocinc.c"
28
29 static void _fuzz_finalize(void)
30 {
31 ssh_finalize();
32 }
33
34 int LLVMFuzzerInitialize(int *argc, char ***argv)
35 {
36 (void)argc;
37
38 nalloc_init(*argv[0]);
39
40 ssh_init();
41
42 atexit(_fuzz_finalize);
43
44 return 0;
45 }
46
47 static int auth_callback(const char *prompt,
48 char *buf,
49 size_t len,
50 int echo,
51 int verify,
52 void *userdata)
53 {
54 (void)prompt; /* unused */
55 (void)echo; /* unused */
56 (void)verify; /* unused */
57 (void)userdata; /* unused */
58
59 snprintf(buf, len, "secret");
60
61 return 0;
62 }
63
64 struct ssh_callbacks_struct cb = {
65 .userdata = NULL,
66 .auth_function = auth_callback,
67 };
68
69 static void select_loop(ssh_session session, ssh_channel channel)
70 {
71 ssh_connector connector_in, connector_out, connector_err;
72
73 ssh_event event = ssh_event_new();
74
75 /* stdin */
76 connector_in = ssh_connector_new(session);
77 ssh_connector_set_out_channel(connector_in, channel, SSH_CONNECTOR_STDINOUT);
78 ssh_connector_set_in_fd(connector_in, 0);
79 ssh_event_add_connector(event, connector_in);
80
81 /* stdout */
82 connector_out = ssh_connector_new(session);
83 ssh_connector_set_out_fd(connector_out, 1);
84 ssh_connector_set_in_channel(connector_out, channel, SSH_CONNECTOR_STDINOUT);
85 ssh_event_add_connector(event, connector_out);
86
87 /* stderr */
88 connector_err = ssh_connector_new(session);
89 ssh_connector_set_out_fd(connector_err, 2);
90 ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR);
91 ssh_event_add_connector(event, connector_err);
92
93 while (ssh_channel_is_open(channel)) {
94 ssh_event_dopoll(event, 60000);
95 }
96 ssh_event_remove_connector(event, connector_in);
97 ssh_event_remove_connector(event, connector_out);
98 ssh_event_remove_connector(event, connector_err);
99
100 ssh_connector_free(connector_in);
101 ssh_connector_free(connector_out);
102 ssh_connector_free(connector_err);
103
104 ssh_event_free(event);
105 }
106
107 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
108 {
109 ssh_session session = NULL;
110 ssh_channel channel = NULL;
111 const char *env = NULL;
112 int socket_fds[2] = {-1, -1};
113 ssize_t nwritten;
114 bool no = false;
115 int rc;
116 long timeout = 1; /* use short timeout to avoid timeouts during fuzzing */
117
118 /* This is the maximum that can be handled by the socket buffer before the
119 * other side will read some data. Other option would be feeding the socket
120 * from different thread which would not mind if it would be blocked, but I
121 * believe all the important inputs should fit into this size */
122 if (size > 219264) {
123 return -1;
124 }
125
126 /* Set up the socket to send data */
127 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds);
128 assert(rc == 0);
129
130 nwritten = send(socket_fds[1], data, size, 0);
131 assert((size_t)nwritten == size);
132
133 rc = shutdown(socket_fds[1], SHUT_WR);
134 assert(rc == 0);
135
136 assert(nalloc_start(data, size) > 0);
137
138 session = ssh_new();
139 if (session == NULL) {
140 goto out;
141 }
142
143 env = getenv("LIBSSH_VERBOSITY");
144 if (env != NULL && strlen(env) > 0) {
145 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY_STR, env);
146 }
147 rc = ssh_options_set(session, SSH_OPTIONS_FD, &socket_fds[0]);
148 if (rc != SSH_OK) {
149 goto out;
150 }
151 rc = ssh_options_set(session, SSH_OPTIONS_HOST, "127.0.0.1");
152 if (rc != SSH_OK) {
153 goto out;
154 }
155 rc = ssh_options_set(session, SSH_OPTIONS_USER, "alice");
156 if (rc != SSH_OK) {
157 goto out;
158 }
159 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, "none");
160 if (rc != SSH_OK) {
161 goto out;
162 }
163 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, "none");
164 if (rc != SSH_OK) {
165 goto out;
166 }
167 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_C_S, "none");
168 if (rc != SSH_OK) {
169 goto out;
170 }
171 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "none");
172 if (rc != SSH_OK) {
173 goto out;
174 }
175 rc = ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &no);
176 if (rc != SSH_OK) {
177 goto out;
178 }
179 rc = ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
180 if (rc != SSH_OK) {
181 goto out;
182 }
183
184 ssh_callbacks_init(&cb);
185 ssh_set_callbacks(session, &cb);
186
187 rc = ssh_connect(session);
188 if (rc != SSH_OK) {
189 goto out;
190 }
191
192 rc = ssh_userauth_none(session, NULL);
193 if (rc != SSH_OK) {
194 goto out;
195 }
196
197 channel = ssh_channel_new(session);
198 if (channel == NULL) {
199 goto out;
200 }
201
202 rc = ssh_channel_open_session(channel);
203 if (rc != SSH_OK) {
204 goto out;
205 }
206
207 rc = ssh_channel_request_exec(channel, "ls");
208 if (rc != SSH_OK) {
209 goto out;
210 }
211
212 select_loop(session, channel);
213
214 out:
215 ssh_channel_free(channel);
216 ssh_disconnect(session);
217 ssh_free(session);
218
219 close(socket_fds[0]);
220 close(socket_fds[1]);
221
222 nalloc_end();
223 return 0;
224 }