Debian Codegraph @

tests/client/torture_session.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2012 by Aris Adamantiadis
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include "libssh/libssh.h"
28 #include "libssh/priv.h"
29 #include "libssh/session.h"
30
31 #include <sys/types.h>
32 #include <pwd.h>
33 #include <errno.h>
34
35 #define BUFLEN 4096
36 static char buffer[BUFLEN];
37
38 static int sshd_setup(void **state)
39 {
40 torture_setup_sshd_server(state, false);
41
42 return 0;
43 }
44
45 static int sshd_teardown(void **state) {
46 torture_teardown_sshd_server(state);
47
48 return 0;
49 }
50
51 static int session_setup(void **state)
52 {
53 struct torture_state *s = *state;
54 struct passwd *pwd;
55 int rc;
56
57 pwd = getpwnam("bob");
58 assert_non_null(pwd);
59
60 rc = setuid(pwd->pw_uid);
61 assert_return_code(rc, errno);
62
63 s->ssh.session = torture_ssh_session(s,
64 TORTURE_SSH_SERVER,
65 NULL,
66 TORTURE_SSH_USER_ALICE,
67 NULL);
68 assert_non_null(s->ssh.session);
69
70 return 0;
71 }
72
73 static int session_teardown(void **state)
74 {
75 struct torture_state *s = *state;
76
77 ssh_disconnect(s->ssh.session);
78 ssh_free(s->ssh.session);
79
80 return 0;
81 }
82
83 static void torture_channel_read_error(void **state) {
84 struct torture_state *s = *state;
85 ssh_session session = s->ssh.session;
86 ssh_channel channel;
87 int rc;
88 int fd;
89 int i;
90
91 channel = ssh_channel_new(session);
92 assert_non_null(channel);
93
94 rc = ssh_channel_open_session(channel);
95 assert_ssh_return_code(session, rc);
96
97 rc = ssh_channel_request_exec(channel, "hexdump -C /dev/urandom");
98 assert_ssh_return_code(session, rc);
99
100 /* send crap and wait for server to send us a disconnect */
101 fd = ssh_get_fd(session);
102 assert_true(fd > 2);
103 rc = write(fd, "AAAA", 4);
104 assert_int_equal(rc, 4);
105
106 for (i=0;i<20;++i){
107 rc = ssh_channel_read(channel,buffer,sizeof(buffer),0);
108 if (rc == SSH_ERROR)
109 break;
110 }
111 #if OPENSSH_VERSION_MAJOR == 6 && OPENSSH_VERSION_MINOR >= 7
112 /* With openssh 6.7 this doesn't produce and error anymore */
113 assert_ssh_return_code(session, rc);
114 #else
115 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
116 #endif
117
118 ssh_channel_free(channel);
119 }
120
121 static void torture_channel_poll_timeout_valid(void **state) {
122 struct torture_state *s = *state;
123 ssh_session session = s->ssh.session;
124 ssh_channel channel;
125 int rc;
126
127 channel = ssh_channel_new(session);
128 assert_non_null(channel);
129
130 rc = ssh_channel_open_session(channel);
131 assert_ssh_return_code(session, rc);
132
133 rc = ssh_channel_request_exec(channel, "echo -n ABCD");
134 assert_ssh_return_code(session, rc);
135
136 rc = ssh_channel_poll_timeout(channel, 500, 0);
137 assert_int_equal(rc, strlen("ABCD"));
138 }
139
140 static void torture_channel_poll_timeout(void **state) {
141 struct torture_state *s = *state;
142 ssh_session session = s->ssh.session;
143 ssh_channel channel;
144 int rc;
145 int fd;
146
147 channel = ssh_channel_new(session);
148 assert_non_null(channel);
149
150 rc = ssh_channel_open_session(channel);
151 assert_ssh_return_code(session, rc);
152
153 fd = ssh_get_fd(session);
154 assert_true(fd > 2);
155
156 rc = ssh_channel_poll_timeout(channel, 500, 0);
157 assert_int_equal(rc, SSH_OK);
158
159 /* send crap and for server to send us a disconnect */
160 rc = write(fd, "AAAA", 4);
161 assert_int_equal(rc, 4);
162
163 rc = ssh_channel_poll_timeout(channel, 500, 0);
164 assert_int_equal(rc, SSH_ERROR);
165
166 ssh_channel_free(channel);
167 }
168
169 /*
170 * Check that the client can properly handle the error returned from the server
171 * when the maximum number of sessions is exceeded.
172 *
173 * Related: T75, T239
174 *
175 */
176 static void torture_max_sessions(void **state)
177 {
178 struct torture_state *s = *state;
179 ssh_session session = s->ssh.session;
180 char max_session_config[32] = {0};
181 #define MAX_CHANNELS 10
182 ssh_channel channels[MAX_CHANNELS + 1];
183 size_t i;
184 int rc;
185
186 snprintf(max_session_config,
187 sizeof(max_session_config),
188 "MaxSessions %u",
189 MAX_CHANNELS);
190
191 /* Update server configuration to limit number of sessions */
192 torture_update_sshd_config(state, max_session_config);
193
194 /* Open the maximum number of channel sessions */
195 for (i = 0; i < MAX_CHANNELS; i++) {
196 channels[i] = ssh_channel_new(session);
197 assert_non_null(channels[i]);
198
199 rc = ssh_channel_open_session(channels[i]);
200 assert_ssh_return_code(session, rc);
201 }
202
203 /* Try to open an extra session and expect failure */
204 channels[i] = ssh_channel_new(session);
205 assert_non_null(channels[i]);
206
207 rc = ssh_channel_open_session(channels[i]);
208 assert_int_equal(rc, SSH_ERROR);
209
210 /* Free the unused channel */
211 ssh_channel_free(channels[i]);
212
213 /* Close and free channels */
214 for (i = 0; i < MAX_CHANNELS; i++) {
215 ssh_channel_close(channels[i]);
216 ssh_channel_free(channels[i]);
217 }
218 #undef MAX_CHANNELS
219 }
220
221 static void torture_no_more_sessions(void **state)
222 {
223 struct torture_state *s = *state;
224 ssh_session session = s->ssh.session;
225 ssh_channel channels[2];
226 int rc;
227
228 /* Open a channel session */
229 channels[0] = ssh_channel_new(session);
230 assert_non_null(channels[0]);
231
232 rc = ssh_channel_open_session(channels[0]);
233 assert_ssh_return_code(session, rc);
234
235 /* Send [email protected] global request */
236 rc = ssh_request_no_more_sessions(session);
237 assert_ssh_return_code(session, rc);
238
239 /* Try to open an extra session and expect failure */
240 channels[1] = ssh_channel_new(session);
241 assert_non_null(channels[1]);
242
243 rc = ssh_channel_open_session(channels[1]);
244 assert_int_equal(rc, SSH_ERROR);
245
246 /* Free the unused channel */
247 ssh_channel_free(channels[1]);
248
249 /* Close and free open channel */
250 ssh_channel_close(channels[0]);
251 ssh_channel_free(channels[0]);
252 }
253
254 static void torture_channel_delayed_close(void **state)
255 {
256 struct torture_state *s = *state;
257 ssh_session session = s->ssh.session;
258 ssh_channel channel;
259
260 char request[256];
261 char buff[256] = {0};
262
263 int rc;
264 int fd;
265
266 snprintf(request, 256,
267 "dd if=/dev/urandom of=/tmp/file bs=64000 count=2; hexdump -C /tmp/file");
268
269 channel = ssh_channel_new(session);
270 assert_non_null(channel);
271
272 rc = ssh_channel_open_session(channel);
273 assert_ssh_return_code(session, rc);
274
275 fd = ssh_get_fd(session);
276 assert_true(fd > 2);
277
278 /* Make the request, read parts with close */
279 rc = ssh_channel_request_exec(channel, request);
280 assert_ssh_return_code(session, rc);
281
282 do {
283 rc = ssh_channel_read(channel, buff, 256, 0);
284 } while(rc > 0);
285 assert_ssh_return_code(session, rc);
286
287 rc = ssh_channel_poll_timeout(channel, 500, 0);
288 assert_int_equal(rc, SSH_EOF);
289
290 ssh_channel_free(channel);
291
292 }
293
294 /* Ensure that calling 'ssh_channel_poll' on a freed channel does not lead to
295 * segmentation faults. */
296 static void torture_freed_channel_poll(void **state)
297 {
298 struct torture_state *s = *state;
299 ssh_session session = s->ssh.session;
300 ssh_channel channel;
301
302 char request[256];
303 int rc;
304
305 snprintf(request, 256,
306 "dd if=/dev/urandom of=/tmp/file bs=64000 count=2; hexdump -C /tmp/file");
307
308 channel = ssh_channel_new(session);
309 assert_non_null(channel);
310
311 rc = ssh_channel_open_session(channel);
312 assert_ssh_return_code(session, rc);
313
314 /* Make the request, read parts with close */
315 rc = ssh_channel_request_exec(channel, request);
316 assert_ssh_return_code(session, rc);
317
318 ssh_channel_free(channel);
319
320 rc = ssh_channel_poll(channel, 0);
321 assert_int_equal(rc, SSH_ERROR);
322 }
323
324 /* Ensure that calling 'ssh_channel_read_nonblocking' on a freed channel does
325 * not lead to segmentation faults. */
326 static void torture_freed_channel_read_nonblocking(void **state)
327 {
328 struct torture_state *s = *state;
329 ssh_session session = s->ssh.session;
330 ssh_channel channel;
331
332 char request[256];
333 char buff[256] = {0};
334 int rc;
335
336 snprintf(request, 256,
337 "dd if=/dev/urandom of=/tmp/file bs=64000 count=2; hexdump -C /tmp/file");
338
339 channel = ssh_channel_new(session);
340 assert_non_null(channel);
341
342 rc = ssh_channel_open_session(channel);
343 assert_ssh_return_code(session, rc);
344
345 /* Make the request, read parts with close */
346 rc = ssh_channel_request_exec(channel, request);
347 assert_ssh_return_code(session, rc);
348
349 ssh_channel_free(channel);
350
351 rc = ssh_channel_read_nonblocking(channel, buff, 256, 0);
352 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
353 }
354
355 static void torture_channel_exit_status(void **state)
356 {
357 struct torture_state *s = *state;
358 ssh_session session = s->ssh.session;
359 ssh_channel channel = NULL;
360 char request[256];
361 uint32_t exit_status = (uint32_t)-1;
362 int rc;
363
364 rc = snprintf(request, sizeof(request), "true");
365 assert_return_code(rc, errno);
366
367 channel = ssh_channel_new(session);
368 assert_non_null(channel);
369
370 rc = ssh_channel_open_session(channel);
371 assert_ssh_return_code(session, rc);
372
373 /* Make the request, read parts with close */
374 rc = ssh_channel_request_exec(channel, request);
375 assert_ssh_return_code(session, rc);
376
377 exit_status = ssh_channel_get_exit_state(channel, &exit_status, NULL, NULL);
378 assert_ssh_return_code(session, rc);
379 assert_int_equal(exit_status, 0);
380 }
381
382 static void torture_channel_exit_signal(void **state)
383 {
384 struct torture_state *s = *state;
385 ssh_session session = s->ssh.session;
386 ssh_channel channel = NULL;
387 char request[256];
388 uint32_t exit_status = (uint32_t)-1;
389 char *exit_signal = NULL;
390 int core_dumped = false;
391 int rc;
392
393 rc = snprintf(request, sizeof(request), "cat");
394 assert_return_code(rc, errno);
395
396 channel = ssh_channel_new(session);
397 assert_non_null(channel);
398
399 rc = ssh_channel_open_session(channel);
400 assert_ssh_return_code(session, rc);
401
402 /* Make the request, read parts with close */
403 rc = ssh_channel_request_exec(channel, request);
404 assert_ssh_return_code(session, rc);
405 rc = ssh_channel_request_send_signal(channel, "TERM");
406 assert_ssh_return_code(session, rc);
407
408 rc = ssh_channel_get_exit_state(channel,
409 &exit_status,
410 &exit_signal,
411 &core_dumped);
412
413 assert_ssh_return_code(session, rc);
414 assert_int_equal(exit_status, (uint32_t)-1);
415 assert_string_equal(exit_signal, "TERM");
416 SAFE_FREE(exit_signal);
417 }
418
419 static void
420 torture_channel_read_stderr(void **state)
421 {
422 struct torture_state *s = *state;
423 ssh_session session = s->ssh.session;
424 ssh_channel channel;
425 int rc;
426
427 channel = ssh_channel_new(session);
428 assert_non_null(channel);
429
430 rc = ssh_channel_open_session(channel);
431 assert_ssh_return_code(session, rc);
432
433 /* This writes to standard error "pipe" */
434 rc = ssh_channel_request_exec(channel, "echo -n ABCD >&2");
435 assert_ssh_return_code(session, rc);
436
437 /* No data in stdout */
438 rc = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
439 assert_int_equal(rc, 0);
440
441 /* poll should say how much we can read */
442 rc = ssh_channel_poll(channel, 1);
443 assert_int_equal(rc, strlen("ABCD"));
444
445 /* Everything in stderr */
446 rc = ssh_channel_read(channel, buffer, sizeof(buffer), 1);
447 assert_int_equal(rc, strlen("ABCD"));
448
449 buffer[rc] = '\0';
450 assert_string_equal("ABCD", buffer);
451
452 ssh_channel_free(channel);
453 }
454
455 static void torture_pubkey_hash(void **state)
456 {
457 struct torture_state *s = *state;
458 ssh_session session = s->ssh.session;
459 char *hash = NULL;
460 char *hexa = NULL;
461 int rc = 0;
462
463 /* bad arguments */
464 rc = ssh_get_pubkey_hash(session, NULL);
465 assert_int_equal(rc, SSH_ERROR);
466
467 rc = ssh_get_pubkey_hash(NULL, (unsigned char **)&hash);
468 assert_int_equal(rc, SSH_ERROR);
469
470 /* deprecated, but should be covered by tests! */
471 rc = ssh_get_pubkey_hash(session, (unsigned char **)&hash);
472 if (ssh_fips_mode()) {
473 /* When in FIPS mode, expect the call to fail */
474 assert_int_equal(rc, SSH_ERROR);
475 } else {
476 assert_int_equal(rc, MD5_DIGEST_LEN);
477
478 hexa = ssh_get_hexa((unsigned char *)hash, rc);
479 SSH_STRING_FREE_CHAR(hash);
480 assert_string_equal(hexa,
481 "ee:80:7f:61:f9:d5:be:f1:96:86:cc:96:7a:db:7a:7b");
482
483 SSH_STRING_FREE_CHAR(hexa);
484 }
485 }
486
487 static void torture_openssh_banner_version(void **state)
488 {
489 struct torture_state *s = *state;
490 ssh_session session = s->ssh.session;
491
492 int openssh_version = ssh_get_openssh_version(session);
493 int cmake_openssh_version = SSH_VERSION_INT(OPENSSH_VERSION_MAJOR, OPENSSH_VERSION_MINOR, 0);
494
495 assert_int_equal(openssh_version, cmake_openssh_version);
496 }
497
498
499 int torture_run_tests(void) {
500 int rc;
501 struct CMUnitTest tests[] = {
502 cmocka_unit_test_setup_teardown(torture_channel_read_error,
503 session_setup,
504 session_teardown),
505 cmocka_unit_test_setup_teardown(torture_channel_poll_timeout_valid,
506 session_setup,
507 session_teardown),
508 cmocka_unit_test_setup_teardown(torture_channel_poll_timeout,
509 session_setup,
510 session_teardown),
511 cmocka_unit_test_setup_teardown(torture_max_sessions,
512 session_setup,
513 session_teardown),
514 cmocka_unit_test_setup_teardown(torture_no_more_sessions,
515 session_setup,
516 session_teardown),
517 cmocka_unit_test_setup_teardown(torture_channel_delayed_close,
518 session_setup,
519 session_teardown),
520 cmocka_unit_test_setup_teardown(torture_freed_channel_poll,
521 session_setup,
522 session_teardown),
523 cmocka_unit_test_setup_teardown(torture_freed_channel_read_nonblocking,
524 session_setup,
525 session_teardown),
526 cmocka_unit_test_setup_teardown(torture_channel_exit_status,
527 session_setup,
528 session_teardown),
529 cmocka_unit_test_setup_teardown(torture_channel_exit_signal,
530 session_setup,
531 session_teardown),
532 cmocka_unit_test_setup_teardown(torture_channel_read_stderr,
533 session_setup,
534 session_teardown),
535 cmocka_unit_test_setup_teardown(torture_pubkey_hash,
536 session_setup,
537 session_teardown),
538 cmocka_unit_test_setup_teardown(torture_openssh_banner_version,
539 session_setup,
540 session_teardown),
541 };
542
543 ssh_init();
544
545 torture_filter_tests(tests);
546 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
547
548 ssh_finalize();
549
550 return rc;
551 }