Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 95eb071

Browse files
ansasakicryptomilk
authored andcommitted
tests: Add a test for SCP with protocol message injection
Test if the file name is correctly escaped to avoid protocol message injection. Fixes T189 Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com> (cherry picked from commit cc9db5b)
1 parent d117de1 commit 95eb071

1 file changed

Lines changed: 72 additions & 1 deletion

File tree

tests/client/torture_scp.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,74 @@ static void torture_scp_download_recursive(void **state)
469469
ssh_scp_free(scp);
470470
}
471471

472+
static void torture_scp_upload_newline(void **state)
473+
{
474+
struct scp_st *ts = NULL;
475+
struct torture_state *s = NULL;
476+
477+
ssh_session session = NULL;
478+
ssh_scp scp = NULL;
479+
480+
FILE *file = NULL;
481+
482+
char buf[1024];
483+
484+
int rc;
485+
486+
assert_non_null(state);
487+
ts = *state;
488+
489+
assert_non_null(ts->s);
490+
s = ts->s;
491+
492+
session = s->ssh.session;
493+
assert_non_null(session);
494+
495+
assert_non_null(ts->tmp_dir_basename);
496+
assert_non_null(ts->tmp_dir);
497+
498+
/* Upload recursively trying to inject protocol messages */
499+
500+
/* When writing the file_name must be the directory name */
501+
scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE,
502+
ts->tmp_dir_basename);
503+
assert_non_null(scp);
504+
505+
rc = ssh_scp_init(scp);
506+
assert_ssh_return_code(session, rc);
507+
508+
/* Push directory where the new file will be copied */
509+
rc = ssh_scp_push_directory(scp, "test_inject", 0755);
510+
assert_ssh_return_code(session, rc);
511+
512+
/* Try to push file with injected protocol messages */
513+
rc = ssh_scp_push_file(scp, "original\nreplacedC0777 8 injected", 8, 0644);
514+
assert_ssh_return_code(session, rc);
515+
516+
rc = ssh_scp_write(scp, "original", 8);
517+
assert_ssh_return_code(session, rc);
518+
519+
/* Leave the directory */
520+
rc = ssh_scp_leave_directory(scp);
521+
assert_ssh_return_code(session, rc);
522+
523+
/* Cleanup */
524+
ssh_scp_close(scp);
525+
ssh_scp_free(scp);
526+
527+
/* Open the file and check content */
528+
snprintf(buf, BUF_SIZE, "%s/test_inject/"
529+
"original\\nreplacedC0777 8 injected",
530+
ts->tmp_dir);
531+
file = fopen(buf, "r");
532+
assert_non_null(file);
533+
534+
fgets(buf, 1024, file);
535+
assert_string_equal(buf, "original");
536+
537+
fclose(file);
538+
}
539+
472540
int torture_run_tests(void)
473541
{
474542
int rc;
@@ -484,7 +552,10 @@ int torture_run_tests(void)
484552
session_teardown),
485553
cmocka_unit_test_setup_teardown(torture_scp_download_recursive,
486554
session_setup,
487-
session_teardown)
555+
session_teardown),
556+
cmocka_unit_test_setup_teardown(torture_scp_upload_newline,
557+
session_setup,
558+
session_teardown),
488559
};
489560

490561
ssh_init();

0 commit comments

Comments
 (0)