Debian Codegraph @

tests/client/torture_scp.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2019 by Red Hat, Inc.
5 *
6 * Author: Anderson Toshiyuki Sasaki <[email protected]>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #define LIBSSH_STATIC
25
26 #include "config.h"
27
28 #include "torture.h"
29 #include "libssh/scp.h"
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <pwd.h>
35 #include <errno.h>
36
37 #define BUF_SIZE 1024
38
39 #define TEMPLATE BINARYDIR "/tests/home/alice/temp_dir_XXXXXX"
40 #define ALICE_HOME BINARYDIR "/tests/home/alice"
41
42 /* store the original umask */
43 mode_t old;
44
45 struct scp_st {
46 struct torture_state *s;
47 char *tmp_dir;
48 char *tmp_dir_basename;
49 };
50
51 static int sshd_setup(void **state)
52 {
53 struct scp_st *ts = NULL;
54 struct torture_state *s = NULL;
55
56 ts = (struct scp_st *)calloc(1, sizeof(struct scp_st));
57 assert_non_null(ts);
58
59 torture_setup_sshd_server((void **)&s, false);
60 assert_non_null(s);
61
62 ts->s = s;
63
64 *state = ts;
65
66 return 0;
67 }
68
69 static int sshd_teardown(void **state)
70 {
71 struct scp_st *ts = NULL;
72
73 ts = *((struct scp_st **)state);
74 assert_non_null(ts);
75 assert_non_null(ts->s);
76
77 torture_teardown_sshd_server((void **)&(ts->s));
78
79 SAFE_FREE(ts);
80
81 return 0;
82 }
83
84 static int session_setup(void **state)
85 {
86 struct scp_st *ts = NULL;
87 struct torture_state *s = NULL;
88
89 char *tmp_dir = NULL;
90 char *tmp_dir_basename = NULL;
91
92 struct passwd *pwd;
93
94 int rc;
95
96 assert_non_null(state);
97
98 ts = *state;
99
100 assert_non_null(ts);
101 assert_non_null(ts->s);
102
103 s = ts->s;
104
105 /* store the original umask and set a new one */
106 old = umask(0022);
107
108 /* Create temporary directory for alice */
109 tmp_dir = torture_make_temp_dir(TEMPLATE);
110 assert_non_null(tmp_dir);
111 ts->tmp_dir = tmp_dir;
112
113 tmp_dir_basename = ssh_basename(tmp_dir);
114 assert_non_null(tmp_dir_basename);
115 ts->tmp_dir_basename = tmp_dir_basename;
116
117 pwd = getpwnam("bob");
118 assert_non_null(pwd);
119
120 rc = setuid(pwd->pw_uid);
121 assert_return_code(rc, errno);
122
123 s->ssh.session = torture_ssh_session(s,
124 TORTURE_SSH_SERVER,
125 NULL,
126 TORTURE_SSH_USER_ALICE,
127 NULL);
128 assert_non_null(s->ssh.session);
129
130 return 0;
131 }
132
133 static int session_teardown(void **state)
134 {
135 struct scp_st *ts = NULL;
136 struct torture_state *s = NULL;
137
138 assert_non_null(state);
139 ts = *((struct scp_st **)state);
140
141 assert_non_null(ts->s);
142 s = ts->s;
143
144 /* restore the umask */
145 umask(old);
146
147 ssh_disconnect(s->ssh.session);
148 ssh_free(s->ssh.session);
149
150 assert_non_null(ts->tmp_dir);
151 torture_rmdirs(ts->tmp_dir);
152
153 SAFE_FREE(ts->tmp_dir);
154 SAFE_FREE(ts->tmp_dir_basename);
155
156 return 0;
157 }
158
159 static void torture_scp_upload(void **state)
160 {
161 struct scp_st *ts = NULL;
162 struct torture_state *s = NULL;
163
164 ssh_session session = NULL;
165 ssh_scp scp = NULL;
166
167 char expected_a[BUF_SIZE];
168 char buf[BUF_SIZE];
169 FILE *file = NULL;
170 size_t len = 0;
171 int rc;
172
173 assert_non_null(state);
174 ts = *state;
175
176 assert_non_null(ts->s);
177 s = ts->s;
178
179 session = s->ssh.session;
180 assert_non_null(session);
181
182 assert_non_null(ts->tmp_dir_basename);
183 assert_non_null(ts->tmp_dir);
184
185 /* Upload file "a" to alice's temp dir */
186
187 /* When writing the file_name must be the directory name */
188 scp = ssh_scp_new(session, SSH_SCP_WRITE, ts->tmp_dir_basename);
189 assert_non_null(scp);
190
191 rc = ssh_scp_init(scp);
192 assert_ssh_return_code(session, rc);
193
194 /* Init buffer content to be written */
195 memset(expected_a, 'A', BUF_SIZE);
196
197 /* For ssh_scp_push_file(), the file_name is the name of the file without
198 * path */
199 rc = ssh_scp_push_file(scp, "a", BUF_SIZE, 0644);
200 assert_ssh_return_code(session, rc);
201
202 rc = ssh_scp_write(scp, expected_a, BUF_SIZE);
203 assert_ssh_return_code(session, rc);
204
205 /* Cleanup */
206 ssh_scp_close(scp);
207 ssh_scp_free(scp);
208
209 /* Open file and check content */
210 snprintf(buf, BUF_SIZE, "%s/a", ts->tmp_dir);
211
212 file = fopen(buf, "r");
213 assert_non_null(file);
214
215 len = fread(buf, BUF_SIZE, 1, file);
216 assert_int_equal(len, 1);
217 assert_memory_equal(buf, expected_a, BUF_SIZE);
218
219 fclose(file);
220 }
221
222 static void torture_scp_upload_recursive(void **state)
223 {
224 struct scp_st *ts = NULL;
225 struct torture_state *s = NULL;
226
227 ssh_session session = NULL;
228 ssh_scp scp = NULL;
229
230 char expected_b[BUF_SIZE];
231 char buf[BUF_SIZE];
232 FILE *file = NULL;
233 size_t len = 0;
234
235 int rc;
236
237 assert_non_null(state);
238 ts = *state;
239
240 assert_non_null(ts->s);
241 s = ts->s;
242
243 session = s->ssh.session;
244 assert_non_null(session);
245
246 assert_non_null(ts->tmp_dir_basename);
247 assert_non_null(ts->tmp_dir);
248
249 /* Upload directory "test_dir" containing file "b" to alice's temp dir */
250
251 /* When writing the file_name must be the directory name */
252 scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE,
253 ts->tmp_dir_basename);
254 assert_non_null(scp);
255
256 rc = ssh_scp_init(scp);
257 assert_ssh_return_code(session, rc);
258
259 /* Push directory where the new file will be copied */
260 rc = ssh_scp_push_directory(scp, "test_dir", 0755);
261 assert_ssh_return_code(session, rc);
262
263 memset(expected_b, 'B', BUF_SIZE);
264
265 /* For ssh_scp_push_file(), the file_name is the name of the file without
266 * path */
267 rc = ssh_scp_push_file(scp, "b", BUF_SIZE, 0644);
268 assert_ssh_return_code(session, rc);
269
270 rc = ssh_scp_write(scp, expected_b, BUF_SIZE);
271 assert_ssh_return_code(session, rc);
272
273 /* Leave the directory */
274 rc = ssh_scp_leave_directory(scp);
275 assert_ssh_return_code(session, rc);
276
277 /* Cleanup */
278 ssh_scp_close(scp);
279 ssh_scp_free(scp);
280
281 /* Open file and check content */
282 snprintf(buf, BUF_SIZE, "%s/test_dir/b", ts->tmp_dir);
283
284 file = fopen(buf, "r");
285 assert_non_null(file);
286
287 len = fread(buf, BUF_SIZE, 1, file);
288 assert_int_equal(len, 1);
289 assert_memory_equal(buf, expected_b, BUF_SIZE);
290
291 fclose(file);
292 }
293
294 static void torture_scp_download(void **state)
295 {
296 struct scp_st *ts = NULL;
297 struct torture_state *s = NULL;
298
299 ssh_session session = NULL;
300 ssh_scp scp = NULL;
301
302 char expected_a[BUF_SIZE];
303 char buf[BUF_SIZE];
304 const char *remote_file = NULL;
305
306 FILE *file = NULL;
307 int fd = 0;
308
309 size_t size;
310
311 int mode;
312 int rc;
313
314 assert_non_null(state);
315
316 ts = *state;
317
318 assert_non_null(ts);
319 assert_non_null(ts->s);
320
321 s = ts->s;
322
323 session = s->ssh.session;
324 assert_non_null(session);
325
326 assert_non_null(ts->tmp_dir_basename);
327 assert_non_null(ts->tmp_dir);
328
329 /* Create file "a" for alice */
330 memset(expected_a, 'A', BUF_SIZE);
331
332 snprintf(buf, BUF_SIZE, "%s/a", ts->tmp_dir);
333
334 fd = open(buf, O_WRONLY | O_CREAT, 0644);
335 assert_true(fd > 0);
336
337 file = fdopen(fd, "w");
338 assert_non_null(file);
339
340 size = fwrite(expected_a, 1, BUF_SIZE, file);
341 assert_int_equal(size, BUF_SIZE);
342 fclose(file);
343
344 /* Construct the file path */
345 snprintf(buf, BUF_SIZE, "%s/a", ts->tmp_dir_basename);
346
347 /* When reading, the location is the file path */
348 scp = ssh_scp_new(session, SSH_SCP_READ, buf);
349 assert_non_null(scp);
350
351 rc = ssh_scp_init(scp);
352 assert_ssh_return_code(session, rc);
353
354 rc = ssh_scp_pull_request(scp);
355 assert_int_equal(rc, SSH_SCP_REQUEST_NEWFILE);
356
357 size = ssh_scp_request_get_size(scp);
358 assert_int_equal(size, BUF_SIZE);
359
360 mode = ssh_scp_request_get_permissions(scp);
361 assert_int_equal(mode, 0644);
362
363 remote_file = ssh_scp_request_get_filename(scp);
364 assert_non_null(remote_file);
365 assert_string_equal(remote_file, "a");
366
367 rc = ssh_scp_accept_request(scp);
368 assert_ssh_return_code(session, rc);
369
370 rc = ssh_scp_read(scp, buf, BUF_SIZE);
371 assert_int_equal(rc, size);
372
373 assert_memory_equal(expected_a, buf, BUF_SIZE);
374
375 /* Cleanup */
376 ssh_scp_close(scp);
377 ssh_scp_free(scp);
378 }
379
380 static void torture_scp_download_recursive(void **state)
381 {
382 struct scp_st *ts = NULL;
383 struct torture_state *s = NULL;
384
385 ssh_session session = NULL;
386 ssh_scp scp = NULL;
387
388 char expected_b[BUF_SIZE];
389 char buf[BUF_SIZE];
390 const char *remote_file = NULL;
391 FILE *file = NULL;
392 int fd = 0;
393
394 size_t size;
395
396 int mode;
397 int rc;
398
399 assert_non_null(state);
400 ts = *state;
401
402 assert_non_null(ts->s);
403 s = ts->s;
404
405 session = s->ssh.session;
406 assert_non_null(session);
407
408 assert_non_null(ts->tmp_dir_basename);
409 assert_non_null(ts->tmp_dir);
410
411 /* Create file "b" for alice */
412 memset(expected_b, 'B', BUF_SIZE);
413
414 snprintf(buf, BUF_SIZE, "%s/b", ts->tmp_dir);
415
416 fd = open(buf, O_WRONLY | O_CREAT, 0644);
417 assert_true(fd > 0);
418
419 file = fdopen(fd, "w");
420 assert_non_null(file);
421
422 size = fwrite(expected_b, 1, BUF_SIZE, file);
423 assert_int_equal(size, BUF_SIZE);
424 fclose(file);
425
426 /* Copy the directory containing the file "b" */
427 scp = ssh_scp_new(session, SSH_SCP_READ | SSH_SCP_RECURSIVE,
428 ts->tmp_dir_basename);
429 assert_non_null(scp);
430
431 rc = ssh_scp_init(scp);
432 assert_ssh_return_code(session, rc);
433
434 /* Receive the directory */
435 rc = ssh_scp_pull_request(scp);
436 assert_int_equal(rc, SSH_SCP_REQUEST_NEWDIR);
437
438 mode = ssh_scp_request_get_permissions(scp);
439 assert_int_equal(mode, 0700);
440
441 remote_file = ssh_scp_request_get_filename(scp);
442 assert_non_null(remote_file);
443 assert_string_equal(remote_file, ts->tmp_dir_basename);
444
445 rc = ssh_scp_accept_request(scp);
446 assert_ssh_return_code(session, rc);
447
448 /* Receive the file "b" */
449 rc = ssh_scp_pull_request(scp);
450 assert_int_equal(rc, SSH_SCP_REQUEST_NEWFILE);
451
452 size = ssh_scp_request_get_size(scp);
453 assert_int_equal(size, BUF_SIZE);
454
455 mode = ssh_scp_request_get_permissions(scp);
456 assert_int_equal(mode, 0644);
457
458 remote_file = ssh_scp_request_get_filename(scp);
459 assert_non_null(remote_file);
460 assert_string_equal(remote_file, "b");
461
462 rc = ssh_scp_accept_request(scp);
463 assert_ssh_return_code(session, rc);
464
465 rc = ssh_scp_read(scp, buf, BUF_SIZE);
466 assert_int_equal(rc, size);
467
468 /* Check if the content was the expected */
469 assert_memory_equal(expected_b, buf, BUF_SIZE);
470
471 /* Receive end of directory */
472 rc = ssh_scp_pull_request(scp);
473 assert_int_equal(rc, SSH_SCP_REQUEST_ENDDIR);
474
475 /* Receive end of communication */
476 rc = ssh_scp_pull_request(scp);
477 assert_int_equal(rc, SSH_SCP_REQUEST_EOF);
478
479 /* Cleanup */
480 ssh_scp_close(scp);
481 ssh_scp_free(scp);
482 }
483
484 static void torture_scp_upload_newline(void **state)
485 {
486 struct scp_st *ts = NULL;
487 struct torture_state *s = NULL;
488
489 ssh_session session = NULL;
490 ssh_scp scp = NULL;
491
492 FILE *file = NULL;
493
494 char buf[1024];
495 char *rs = NULL;
496 int rc;
497
498 assert_non_null(state);
499 ts = *state;
500
501 assert_non_null(ts->s);
502 s = ts->s;
503
504 session = s->ssh.session;
505 assert_non_null(session);
506
507 assert_non_null(ts->tmp_dir_basename);
508 assert_non_null(ts->tmp_dir);
509
510 /* Upload recursively trying to inject protocol messages */
511
512 /* When writing the file_name must be the directory name */
513 scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE,
514 ts->tmp_dir_basename);
515 assert_non_null(scp);
516
517 rc = ssh_scp_init(scp);
518 assert_ssh_return_code(session, rc);
519
520 /* Push directory where the new file will be copied */
521 rc = ssh_scp_push_directory(scp, "test_inject", 0755);
522 assert_ssh_return_code(session, rc);
523
524 /* Try to push file with injected protocol messages */
525 rc = ssh_scp_push_file(scp, "original\nreplacedC0777 8 injected", 8, 0644);
526 assert_ssh_return_code(session, rc);
527
528 rc = ssh_scp_write(scp, "original", 8);
529 assert_ssh_return_code(session, rc);
530
531 /* Leave the directory */
532 rc = ssh_scp_leave_directory(scp);
533 assert_ssh_return_code(session, rc);
534
535 /* Cleanup */
536 ssh_scp_close(scp);
537 ssh_scp_free(scp);
538
539 /* Open the file and check content */
540 snprintf(buf, BUF_SIZE, "%s/test_inject/"
541 "original\\nreplacedC0777 8 injected",
542 ts->tmp_dir);
543 file = fopen(buf, "r");
544 assert_non_null(file);
545
546 rs = fgets(buf, 1024, file);
547 assert_non_null(rs);
548 assert_string_equal(buf, "original");
549
550 fclose(file);
551 }
552
553 static void torture_scp_upload_appended_command(void **state)
554 {
555 struct scp_st *ts = NULL;
556 struct torture_state *s = NULL;
557
558 ssh_session session = NULL;
559 ssh_scp scp = NULL;
560
561 FILE *file = NULL;
562
563 char buf[1024];
564 char *rs = NULL;
565 int rc;
566
567 assert_non_null(state);
568 ts = *state;
569
570 assert_non_null(ts->s);
571 s = ts->s;
572
573 session = s->ssh.session;
574 assert_non_null(session);
575
576 assert_non_null(ts->tmp_dir_basename);
577 assert_non_null(ts->tmp_dir);
578
579 /* Upload a file path with a command appended */
580
581 /* Append a command to the file path */
582 snprintf(buf, BUF_SIZE, "%s"
583 "/;touch hack",
584 ts->tmp_dir);
585
586 /* When writing the file_name must be the directory name */
587 scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE,
588 buf);
589 assert_non_null(scp);
590
591 rc = ssh_scp_init(scp);
592 assert_ssh_return_code(session, rc);
593
594 /* Push directory where the new file will be copied */
595 rc = ssh_scp_push_directory(scp, ";touch hack", 0755);
596 assert_ssh_return_code(session, rc);
597
598 /* Try to push file */
599 rc = ssh_scp_push_file(scp, "original", 8, 0644);
600 assert_ssh_return_code(session, rc);
601
602 rc = ssh_scp_write(scp, "original", 8);
603 assert_ssh_return_code(session, rc);
604
605 /* Leave the directory */
606 rc = ssh_scp_leave_directory(scp);
607 assert_ssh_return_code(session, rc);
608
609 /* Cleanup */
610 ssh_scp_close(scp);
611 ssh_scp_free(scp);
612
613 /* Make sure the command was not executed */
614 snprintf(buf, BUF_SIZE, ALICE_HOME "/hack");
615 file = fopen(buf, "r");
616 assert_null(file);
617
618 /* Open the file and check content */
619 snprintf(buf, BUF_SIZE, "%s"
620 "/;touch hack/original",
621 ts->tmp_dir);
622
623 file = fopen(buf, "r");
624 assert_non_null(file);
625
626 rs = fgets(buf, 1024, file);
627 assert_non_null(rs);
628 assert_string_equal(buf, "original");
629
630 fclose(file);
631 }
632
633 int torture_run_tests(void)
634 {
635 int rc;
636 struct CMUnitTest tests[] = {
637 cmocka_unit_test_setup_teardown(torture_scp_upload,
638 session_setup,
639 session_teardown),
640 cmocka_unit_test_setup_teardown(torture_scp_upload_recursive,
641 session_setup,
642 session_teardown),
643 cmocka_unit_test_setup_teardown(torture_scp_download,
644 session_setup,
645 session_teardown),
646 cmocka_unit_test_setup_teardown(torture_scp_download_recursive,
647 session_setup,
648 session_teardown),
649 cmocka_unit_test_setup_teardown(torture_scp_upload_newline,
650 session_setup,
651 session_teardown),
652 cmocka_unit_test_setup_teardown(torture_scp_upload_appended_command,
653 session_setup,
654 session_teardown),
655 };
656
657 ssh_init();
658
659 torture_filter_tests(tests);
660 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
661 ssh_finalize();
662
663 return rc;
664 }