Debian Codegraph @

tests/unittests/torture_packet_filter.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Anderson Toshiyuki Sasaki <[email protected]>
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 /*
23 * This test checks if the messages accepted by the packet filter were intended
24 * to be accepted.
25 *
26 * The process consists in 2 steps:
27 * - Try the filter with a message type in an arbitrary state
28 * - If the message is accepted by the filter, check if the message is in the
29 * set of accepted states.
30 *
31 * Only the values selected by the flag (COMPARE_*) are considered.
32 * */
33
34 #include "config.h"
35
36 #define LIBSSH_STATIC
37
38 #include "torture.h"
39 #include "libssh/priv.h"
40 #include "libssh/libssh.h"
41 #include "libssh/session.h"
42 #include "libssh/auth.h"
43 #include "libssh/ssh2.h"
44 #include "libssh/packet.h"
45
46 #include "packet.c"
47
48 #define COMPARE_SESSION_STATE 1
49 #define COMPARE_ROLE (1 << 1)
50 #define COMPARE_DH_STATE (1 << 2)
51 #define COMPARE_AUTH_STATE (1 << 3)
52 #define COMPARE_GLOBAL_REQ_STATE (1 << 4)
53 #define COMPARE_CURRENT_METHOD (1 << 5)
54
55 #define SESSION_STATE_COUNT 11
56 #define DH_STATE_COUNT 4
57 #define AUTH_STATE_COUNT 15
58 #define GLOBAL_REQ_STATE_COUNT 5
59 #define MESSAGE_COUNT 100 // from 1 to 100
60
61 #define ROLE_CLIENT 0
62 #define ROLE_SERVER 1
63
64 /*
65 * This is the list of currently unfiltered message types.
66 * Only unrecognized types should be in this list.
67 * */
68 static uint8_t unfiltered[] = {
69 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
70 22, 23, 24, 25, 26, 27, 28, 29,
71 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
72 54, 55, 56, 57, 58, 59,
73 62,
74 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
75 83, 84, 85, 86, 87, 88, 89,
76 };
77
78 typedef struct global_state_st {
79 /* If the bit in this flag is zero, the corresponding state is not
80 * considered, working as a wildcard (meaning any value is accepted) */
81 uint32_t flags;
82 uint8_t role;
83 enum ssh_session_state_e session;
84 enum ssh_dh_state_e dh;
85 enum ssh_auth_state_e auth;
86 enum ssh_channel_request_state_e global_req;
87 } global_state;
88
89 static int cmp_state(const void *e1, const void *e2)
90 {
91 global_state *s1 = (global_state *) e1;
92 global_state *s2 = (global_state *) e2;
93
94 /* Compare role (client == 0 or server == 1)*/
95 if (s1->role < s2->role) {
96 return -1;
97 }
98 else if (s1->role > s2->role) {
99 return 1;
100 }
101
102 /* Compare session state */
103 if (s1->session < s2->session) {
104 return -1;
105 }
106 else if (s1->session > s2->session) {
107 return 1;
108 }
109
110 /* Compare DH state */
111 if (s1->dh < s2->dh) {
112 return -1;
113 }
114 else if (s1->dh > s2->dh) {
115 return 1;
116 }
117
118 /* Compare auth */
119 if (s1->auth < s2->auth) {
120 return -1;
121 }
122 else if (s1->auth > s2->auth) {
123 return 1;
124 }
125
126 /* Compare global_req */
127 if (s1->global_req < s2->global_req) {
128 return -1;
129 }
130 else if (s1->global_req > s2->global_req) {
131 return 1;
132 }
133
134 /* If all equal, they are equal */
135 return 0;
136 }
137
138 static int cmp_state_search(const void *key, const void *array_element)
139 {
140 global_state *s1 = (global_state *) key;
141 global_state *s2 = (global_state *) array_element;
142
143 int result = 0;
144
145 if (s2->flags & COMPARE_ROLE) {
146 /* Compare role (client == 0 or server == 1)*/
147 if (s1->role < s2->role) {
148 return -1;
149 }
150 else if (s1->role > s2->role) {
151 return 1;
152 }
153 }
154
155 if (s2->flags & COMPARE_SESSION_STATE) {
156 /* Compare session state */
157 if (s1->session < s2->session) {
158 result = -1;
159 goto end;
160 }
161 else if (s1->session > s2->session) {
162 result = 1;
163 goto end;
164 }
165 }
166
167 if (s2->flags & COMPARE_DH_STATE) {
168 /* Compare DH state */
169 if (s1->dh < s2->dh) {
170 result = -1;
171 goto end;
172 }
173 else if (s1->dh > s2->dh) {
174 result = 1;
175 goto end;
176 }
177 }
178
179 if (s2->flags & COMPARE_AUTH_STATE) {
180 /* Compare auth */
181 if (s1->auth < s2->auth) {
182 result = -1;
183 goto end;
184 }
185 else if (s1->auth > s2->auth) {
186 result = 1;
187 goto end;
188 }
189 }
190
191 if (s2->flags & COMPARE_GLOBAL_REQ_STATE) {
192 /* Compare global_req */
193 if (s1->global_req < s2->global_req) {
194 result = -1;
195 goto end;
196 }
197 else if (s1->global_req > s2->global_req) {
198 result = 1;
199 goto end;
200 }
201 }
202
203 end:
204 return result;
205 }
206
207 static int is_state_accepted(global_state *tested, global_state *accepted,
208 int accepted_len)
209 {
210 global_state *found = NULL;
211
212 found = bsearch(tested, accepted, accepted_len, sizeof(global_state),
213 cmp_state_search);
214
215 if (found != NULL) {
216 return 1;
217 }
218
219 return 0;
220 }
221
222 static int cmp_uint8(const void *i, const void *j)
223 {
224 uint8_t e1 = *((uint8_t *)i);
225 uint8_t e2 = *((uint8_t *)j);
226
227 if (e1 < e2) {
228 return -1;
229 }
230 else if (e1 > e2) {
231 return 1;
232 }
233
234 return 0;
235 }
236
237 static int check_unfiltered(uint8_t msg_type)
238 {
239 uint8_t *found;
240
241 found = bsearch(&msg_type, unfiltered, sizeof(unfiltered)/sizeof(uint8_t),
242 sizeof(uint8_t), cmp_uint8);
243
244 if (found != NULL) {
245 return 1;
246 }
247
248 return 0;
249 }
250
251 static void torture_packet_filter_check_unfiltered(void **state)
252 {
253 ssh_session session;
254
255 int role_c;
256 int auth_c;
257 int session_c;
258 int dh_c;
259 int global_req_c;
260
261 uint8_t msg_type;
262
263 enum ssh_packet_filter_result_e rc;
264 int in_unfiltered;
265
266 (void)state;
267
268 session = ssh_new();
269
270 for (msg_type = 1; msg_type <= MESSAGE_COUNT; msg_type++) {
271 session->in_packet.type = msg_type;
272 for (role_c = 0; role_c < 2; role_c++) {
273 session->server = role_c;
274 for (session_c = 0; session_c < SESSION_STATE_COUNT; session_c++) {
275 session->session_state = session_c;
276 for (dh_c = 0; dh_c < DH_STATE_COUNT; dh_c++) {
277 session->dh_handshake_state = dh_c;
278 for (auth_c = 0; auth_c < AUTH_STATE_COUNT; auth_c++) {
279 session->auth.state = auth_c;
280 for (global_req_c = 0;
281 global_req_c < GLOBAL_REQ_STATE_COUNT;
282 global_req_c++)
283 {
284 session->global_req_state = global_req_c;
285
286 rc = ssh_packet_incoming_filter(session);
287
288 if (rc == SSH_PACKET_UNKNOWN) {
289 in_unfiltered = check_unfiltered(msg_type);
290
291 if (!in_unfiltered) {
292 fprintf(stderr, "Message type %d UNFILTERED "
293 "in state: role %d, session %d, dh %d, auth %d\n",
294 msg_type, role_c, session_c, dh_c, auth_c);
295 }
296 assert_int_equal(in_unfiltered, 1);
297 }
298 else {
299 in_unfiltered = check_unfiltered(msg_type);
300
301 if (in_unfiltered) {
302 fprintf(stderr, "Message type %d NOT UNFILTERED "
303 "in state: role %d, session %d, dh %d, auth %d\n",
304 msg_type, role_c, session_c, dh_c, auth_c);
305 }
306 assert_int_equal(in_unfiltered, 0);
307 }
308 }
309 }
310 }
311 }
312 }
313 }
314 ssh_free(session);
315 }
316
317 static int check_message_in_all_states(global_state accepted[],
318 int accepted_count, uint8_t msg_type)
319 {
320 ssh_session session;
321
322 int role_c;
323 int auth_c;
324 int session_c;
325 int dh_c;
326 int global_req_c;
327
328 enum ssh_packet_filter_result_e rc;
329 int in_accepted;
330
331 global_state key;
332
333 session = ssh_new();
334
335 /* Sort the accepted array so that the elements can be searched using
336 * bsearch */
337 qsort(accepted, accepted_count, sizeof(global_state), cmp_state);
338
339 session->in_packet.type = msg_type;
340
341 for (role_c = 0; role_c < 2; role_c++) {
342 session->server = role_c;
343 key.role = role_c;
344 for (session_c = 0; session_c < SESSION_STATE_COUNT; session_c++) {
345 session->session_state = session_c;
346 key.session = session_c;
347 for (dh_c = 0; dh_c < DH_STATE_COUNT; dh_c++) {
348 session->dh_handshake_state = dh_c;
349 key.dh = dh_c;
350 for (auth_c = 0; auth_c < AUTH_STATE_COUNT; auth_c++) {
351 session->auth.state = auth_c;
352 key.auth = auth_c;
353 for (global_req_c = 0;
354 global_req_c < GLOBAL_REQ_STATE_COUNT;
355 global_req_c++)
356 {
357 session->global_req_state = global_req_c;
358 key.global_req = global_req_c;
359
360 rc = ssh_packet_incoming_filter(session);
361
362 if (rc == SSH_PACKET_ALLOWED) {
363 in_accepted = is_state_accepted(&key, accepted,
364 accepted_count);
365
366 if (!in_accepted) {
367 fprintf(stderr, "Message type %d ALLOWED "
368 "in state: role %d, session %d, dh %d, auth %d\n",
369 msg_type, role_c, session_c, dh_c, auth_c);
370 }
371 assert_int_equal(in_accepted, 1);
372 }
373 else if (rc == SSH_PACKET_DENIED) {
374 in_accepted = is_state_accepted(&key, accepted, accepted_count);
375
376 if (in_accepted) {
377 fprintf(stderr, "Message type %d DENIED "
378 "in state: role %d, session %d, dh %d, auth %d\n",
379 msg_type, role_c, session_c, dh_c, auth_c);
380 }
381 assert_int_equal(in_accepted, 0);
382 }
383 else {
384 fprintf(stderr, "Message type %d UNFILTERED "
385 "in state: role %d, session %d, dh %d, auth %d\n",
386 msg_type, role_c, session_c, dh_c, auth_c);
387 }
388 }
389 }
390 }
391 }
392 }
393
394 ssh_free(session);
395 return 0;
396 }
397
398 static void torture_packet_filter_check_auth_success(void **state)
399 {
400 int rc;
401
402 global_state accepted[] = {
403 {
404 .flags = (COMPARE_SESSION_STATE |
405 COMPARE_ROLE |
406 COMPARE_AUTH_STATE |
407 COMPARE_DH_STATE),
408 .role = ROLE_CLIENT,
409 .session = SSH_SESSION_STATE_AUTHENTICATING,
410 .dh = DH_STATE_FINISHED,
411 .auth = SSH_AUTH_STATE_PUBKEY_AUTH_SENT,
412 },
413 {
414 .flags = (COMPARE_SESSION_STATE |
415 COMPARE_ROLE |
416 COMPARE_AUTH_STATE |
417 COMPARE_DH_STATE),
418 .role = ROLE_CLIENT,
419 .session = SSH_SESSION_STATE_AUTHENTICATING,
420 .dh = DH_STATE_FINISHED,
421 .auth = SSH_AUTH_STATE_PASSWORD_AUTH_SENT,
422 },
423 {
424 .flags = (COMPARE_SESSION_STATE |
425 COMPARE_ROLE |
426 COMPARE_AUTH_STATE |
427 COMPARE_DH_STATE),
428 .role = ROLE_CLIENT,
429 .session = SSH_SESSION_STATE_AUTHENTICATING,
430 .dh = DH_STATE_FINISHED,
431 .auth = SSH_AUTH_STATE_GSSAPI_MIC_SENT,
432 },
433 {
434 .flags = (COMPARE_SESSION_STATE |
435 COMPARE_ROLE |
436 COMPARE_AUTH_STATE |
437 COMPARE_DH_STATE),
438 .role = ROLE_CLIENT,
439 .session = SSH_SESSION_STATE_AUTHENTICATING,
440 .dh = DH_STATE_FINISHED,
441 .auth = SSH_AUTH_STATE_KBDINT_SENT,
442 },
443 {
444 .flags = (COMPARE_SESSION_STATE |
445 COMPARE_ROLE |
446 COMPARE_AUTH_STATE |
447 COMPARE_DH_STATE |
448 COMPARE_CURRENT_METHOD),
449 .role = ROLE_CLIENT,
450 .session = SSH_SESSION_STATE_AUTHENTICATING,
451 .dh = DH_STATE_FINISHED,
452 .auth = SSH_AUTH_STATE_AUTH_NONE_SENT,
453 }
454 };
455
456 int accepted_count = 5;
457
458 /* Unused */
459 (void) state;
460
461 rc = check_message_in_all_states(accepted, accepted_count,
462 SSH2_MSG_USERAUTH_SUCCESS);
463
464 assert_int_equal(rc, 0);
465 }
466
467 static void torture_packet_filter_check_msg_ext_info(void **state)
468 {
469 int rc;
470
471 global_state accepted[] = {
472 {
473 .flags = (COMPARE_SESSION_STATE |
474 COMPARE_DH_STATE),
475 .session = SSH_SESSION_STATE_AUTHENTICATING,
476 .dh = DH_STATE_FINISHED,
477 },
478 {
479 .flags = (COMPARE_SESSION_STATE |
480 COMPARE_DH_STATE),
481 .session = SSH_SESSION_STATE_AUTHENTICATED,
482 .dh = DH_STATE_FINISHED,
483 },
484 };
485
486 int accepted_count = 2;
487
488 /* Unused */
489 (void) state;
490
491 rc = check_message_in_all_states(accepted, accepted_count,
492 SSH2_MSG_EXT_INFO);
493
494 assert_int_equal(rc, 0);
495 }
496
497 static void torture_packet_filter_check_channel_open(void **state)
498 {
499 int rc;
500
501 /* The only condition to accept a CHANNEL_OPEN is to be authenticated */
502 global_state accepted[] = {
503 {
504 .flags = COMPARE_SESSION_STATE,
505 .session = SSH_SESSION_STATE_AUTHENTICATED,
506 }
507 };
508
509 int accepted_count = 1;
510
511 /* Unused */
512 (void) state;
513
514 rc = check_message_in_all_states(accepted, accepted_count,
515 SSH2_MSG_CHANNEL_OPEN);
516
517 assert_int_equal(rc, 0);
518 }
519
520 static void torture_packet_filter_check_channel_success(void **state)
521 {
522 int rc;
523
524 /* The only condition to accept a CHANNEL_SUCCESS is to be authenticated */
525 global_state accepted[] = {
526 {
527 .flags = COMPARE_SESSION_STATE,
528 .session = SSH_SESSION_STATE_AUTHENTICATED,
529 }
530 };
531
532 int accepted_count = 1;
533
534 /* Unused */
535 (void) state;
536
537 rc = check_message_in_all_states(accepted, accepted_count,
538 SSH2_MSG_CHANNEL_SUCCESS);
539
540 assert_int_equal(rc, 0);
541 }
542
543 static void torture_packet_filter_check_channel_failure(void **state)
544 {
545 int rc;
546
547 /* The only condition to accept a CHANNEL_FAILURE is to be authenticated */
548 global_state accepted[] = {
549 {
550 .flags = COMPARE_SESSION_STATE,
551 .session = SSH_SESSION_STATE_AUTHENTICATED,
552 }
553 };
554
555 int accepted_count = 1;
556
557 /* Unused */
558 (void) state;
559
560 rc = check_message_in_all_states(accepted, accepted_count,
561 SSH2_MSG_CHANNEL_FAILURE);
562
563 assert_int_equal(rc, 0);
564 }
565
566 static void torture_packet_filter_check_request_success(void **state)
567 {
568 int rc;
569
570 /* The only condition to accept a REQUEST_SUCCESS is to be authenticated */
571 global_state accepted[] = {
572 {
573 .flags = COMPARE_SESSION_STATE,
574 .session = SSH_SESSION_STATE_AUTHENTICATED,
575 }
576 };
577
578 int accepted_count = 1;
579
580 /* Unused */
581 (void) state;
582
583 rc = check_message_in_all_states(accepted, accepted_count,
584 SSH2_MSG_REQUEST_SUCCESS);
585
586 assert_int_equal(rc, 0);
587 }
588
589 static void torture_packet_filter_check_request_failure(void **state)
590 {
591 int rc;
592
593 /* The only condition to accept a REQUEST_FAILURE is to be authenticated */
594 global_state accepted[] = {
595 {
596 .flags = COMPARE_SESSION_STATE,
597 .session = SSH_SESSION_STATE_AUTHENTICATED,
598 }
599 };
600
601 int accepted_count = 1;
602
603 /* Unused */
604 (void) state;
605
606 rc = check_message_in_all_states(accepted, accepted_count,
607 SSH2_MSG_REQUEST_FAILURE);
608
609 assert_int_equal(rc, 0);
610 }
611
612 int torture_run_tests(void)
613 {
614 int rc;
615 struct CMUnitTest tests[] = {
616 cmocka_unit_test(torture_packet_filter_check_auth_success),
617 cmocka_unit_test(torture_packet_filter_check_channel_open),
618 cmocka_unit_test(torture_packet_filter_check_channel_success),
619 cmocka_unit_test(torture_packet_filter_check_channel_failure),
620 cmocka_unit_test(torture_packet_filter_check_request_success),
621 cmocka_unit_test(torture_packet_filter_check_request_failure),
622 cmocka_unit_test(torture_packet_filter_check_unfiltered),
623 cmocka_unit_test(torture_packet_filter_check_msg_ext_info)
624 };
625
626 ssh_init();
627 torture_filter_tests(tests);
628 rc = cmocka_run_group_tests(tests, NULL, NULL);
629 ssh_finalize();
630 return rc;
631 }