Debian Codegraph @

tests/client/torture_proxycommand.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include "torture.h"
6 #include <libssh/libssh.h>
7 #include "libssh/priv.h"
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <pwd.h>
12 #include <errno.h>
13 #include <fcntl.h>
14
15 static int sshd_setup(void **state)
16 {
17 torture_setup_sshd_server(state, false);
18
19 return 0;
20 }
21
22 static int sshd_teardown(void **state) {
23 torture_teardown_sshd_server(state);
24
25 return 0;
26 }
27
28 static int session_setup(void **state)
29 {
30 struct torture_state *s = *state;
31 int verbosity = torture_libssh_verbosity();
32 struct passwd *pwd;
33 int rc;
34
35 pwd = getpwnam("bob");
36 assert_non_null(pwd);
37
38 rc = setuid(pwd->pw_uid);
39 assert_return_code(rc, errno);
40
41 s->ssh.session = ssh_new();
42 assert_non_null(s->ssh.session);
43
44 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
45 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
46
47 ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
48
49 return 0;
50 }
51
52 static int session_teardown(void **state)
53 {
54 struct torture_state *s = *state;
55
56 ssh_disconnect(s->ssh.session);
57 ssh_free(s->ssh.session);
58
59 return 0;
60 }
61
62 #ifdef NCAT_EXECUTABLE
63 static void torture_options_set_proxycommand(void **state)
64 {
65 struct torture_state *s = *state;
66 ssh_session session = s->ssh.session;
67 const char *address = torture_server_address(AF_INET);
68 int port = torture_server_port();
69 char command[255] = {0};
70 struct stat sb;
71 int rc;
72 #ifdef WITH_EXEC
73 socket_t fd;
74 #endif
75
76 rc = stat(NCAT_EXECUTABLE, &sb);
77 if (rc != 0 || (sb.st_mode & S_IXOTH) == 0) {
78 SSH_LOG(SSH_LOG_WARNING,
79 "Could not find " NCAT_EXECUTABLE ": Skipping the test");
80 skip();
81 }
82
83 rc = snprintf(command,
84 sizeof(command),
85 NCAT_EXECUTABLE " %s %d",
86 address,
87 port);
88 assert_true((size_t)rc < sizeof(command));
89
90 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, command);
91 assert_int_equal(rc, 0);
92 rc = ssh_connect(session);
93 #ifdef WITH_EXEC
94 assert_ssh_return_code(session, rc);
95 fd = ssh_get_fd(session);
96 assert_int_not_equal(fd, SSH_INVALID_SOCKET);
97 rc = fcntl(fd, F_GETFL);
98 assert_int_equal(rc & O_RDWR, O_RDWR);
99 #else
100 assert_int_equal(rc, SSH_ERROR);
101 #endif /* WITH_EXEC */
102 }
103
104 #else /* NCAT_EXECUTABLE */
105
106 static void torture_options_set_proxycommand(void **state)
107 {
108 (void) state;
109 skip();
110 }
111
112 #endif /* NCAT_EXECUTABLE */
113
114 static void torture_options_set_proxycommand_notexist(void **state) {
115 struct torture_state *s = *state;
116 ssh_session session = s->ssh.session;
117 int rc;
118
119 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, "this_command_does_not_exist");
120 assert_ssh_return_code(session, rc);
121
122 rc = ssh_connect(session);
123 assert_ssh_return_code_equal(session, rc, SSH_ERROR);
124 }
125
126 static void torture_options_set_proxycommand_ssh(void **state)
127 {
128 struct torture_state *s = *state;
129 ssh_session session = s->ssh.session;
130 const char *address = torture_server_address(AF_INET);
131 char command[255] = {0};
132 int rc;
133 #ifdef WITH_EXEC
134 socket_t fd;
135 #endif
136
137 rc = snprintf(command, sizeof(command),
138 "ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -W [%%h]:%%p alice@%s",
139 address);
140 assert_true((size_t)rc < sizeof(command));
141
142 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, command);
143 assert_int_equal(rc, 0);
144 rc = ssh_connect(session);
145 #ifdef WITH_EXEC
146 assert_ssh_return_code(session, rc);
147 fd = ssh_get_fd(session);
148 assert_int_not_equal(fd, SSH_INVALID_SOCKET);
149 rc = fcntl(fd, F_GETFL);
150 assert_int_equal(rc & O_RDWR, O_RDWR);
151 #else
152 assert_int_equal(rc, SSH_ERROR);
153 #endif /* WITH_EXEC */
154 }
155
156 static void torture_options_set_proxycommand_ssh_stderr(void **state)
157 {
158 struct torture_state *s = *state;
159 ssh_session session = s->ssh.session;
160 const char *address = torture_server_address(AF_INET);
161 char command[255] = {0};
162 int rc;
163 #ifdef WITH_EXEC
164 socket_t fd;
165 #endif
166
167 /* The -vvv switches produce the desired output on the standard error */
168 rc = snprintf(command, sizeof(command),
169 "ssh -vvv -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -W [%%h]:%%p alice@%s",
170 address);
171 assert_true((size_t)rc < sizeof(command));
172
173 rc = ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, command);
174 assert_int_equal(rc, 0);
175 rc = ssh_connect(session);
176 #ifdef WITH_EXEC
177 assert_ssh_return_code(session, rc);
178 fd = ssh_get_fd(session);
179 assert_int_not_equal(fd, SSH_INVALID_SOCKET);
180 rc = fcntl(fd, F_GETFL);
181 assert_int_equal(rc & O_RDWR, O_RDWR);
182 #else
183 assert_int_equal(rc, SSH_ERROR);
184 #endif /* WITH_EXEC */
185 }
186
187 static void torture_options_proxycommand_injection(void **state)
188 {
189 struct torture_state *s = *state;
190 struct passwd *pwd = NULL;
191 const char *malicious_host = "`echo foo > mfile`";
192 const char *command = "nc %h %p";
193 char *current_dir = NULL;
194 char *malicious_file_path = NULL;
195 int mfp_len;
196 int verbosity = torture_libssh_verbosity();
197 struct stat sb;
198 int rc;
199
200 pwd = getpwnam("bob");
201 assert_non_null(pwd);
202
203 rc = setuid(pwd->pw_uid);
204 assert_return_code(rc, errno);
205
206 s->ssh.session = ssh_new();
207 assert_non_null(s->ssh.session);
208
209 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
210 // if we would be checking the rc, this should fail
211 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, malicious_host);
212
213 ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
214
215 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROXYCOMMAND, command);
216 assert_int_equal(rc, 0);
217 rc = ssh_connect(s->ssh.session);
218 assert_ssh_return_code_equal(s->ssh.session, rc, SSH_ERROR);
219
220 current_dir = torture_get_current_working_dir();
221 assert_non_null(current_dir);
222 mfp_len = strlen(current_dir) + 6;
223 malicious_file_path = malloc(mfp_len);
224 assert_non_null(malicious_file_path);
225 rc = snprintf(malicious_file_path, mfp_len,
226 "%s/mfile", current_dir);
227 assert_int_equal(rc, mfp_len);
228 free(current_dir);
229 rc = stat(malicious_file_path, &sb);
230 assert_int_not_equal(rc, 0);
231
232 // cleanup
233 remove(malicious_file_path);
234 free(malicious_file_path);
235 }
236
237 int torture_run_tests(void) {
238 int rc;
239 struct CMUnitTest tests[] = {
240 cmocka_unit_test_setup_teardown(torture_options_set_proxycommand,
241 session_setup,
242 session_teardown),
243 cmocka_unit_test_setup_teardown(torture_options_set_proxycommand_notexist,
244 session_setup,
245 session_teardown),
246 cmocka_unit_test_setup_teardown(torture_options_set_proxycommand_ssh,
247 session_setup,
248 session_teardown),
249 cmocka_unit_test_setup_teardown(torture_options_set_proxycommand_ssh_stderr,
250 session_setup,
251 session_teardown),
252 cmocka_unit_test_setup_teardown(torture_options_proxycommand_injection,
253 NULL,
254 session_teardown),
255 };
256
257
258 ssh_init();
259
260 torture_filter_tests(tests);
261 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
262 ssh_finalize();
263
264 return rc;
265 }