| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2010 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 |
#include "torture_cmocka.h" |
| 24 |
|
| 25 |
#define LIBSSH_STATIC |
| 26 |
|
| 27 |
#include "torture.h" |
| 28 |
#include <libssh/libssh.h> |
| 29 |
#ifdef HAVE_SYS_TIME_H |
| 30 |
#include <sys/time.h> |
| 31 |
#endif /* HAVE_SYS_TIME_H */ |
| 32 |
#include <arpa/inet.h> |
| 33 |
#include <netinet/in.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/socket.h> |
| 36 |
#include <errno.h> |
| 37 |
#include <pwd.h> |
| 38 |
|
| 39 |
/* Should work until Apnic decides to assign it :) */ |
| 40 |
#define BLACKHOLE "1.1.1.1" |
| 41 |
|
| 42 |
static int sshd_setup(void **state) |
| 43 |
{ |
| 44 |
torture_setup_sshd_server(state, false); |
| 45 |
|
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
static int sshd_teardown(void **state) { |
| 50 |
torture_teardown_sshd_server(state); |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static int session_setup(void **state) |
| 56 |
{ |
| 57 |
struct torture_state *s = *state; |
| 58 |
int verbosity = torture_libssh_verbosity(); |
| 59 |
struct passwd *pwd; |
| 60 |
int rc; |
| 61 |
|
| 62 |
pwd = getpwnam("bob"); |
| 63 |
assert_non_null(pwd); |
| 64 |
|
| 65 |
rc = setuid(pwd->pw_uid); |
| 66 |
assert_return_code(rc, errno); |
| 67 |
|
| 68 |
s->ssh.session = ssh_new(); |
| 69 |
assert_non_null(s->ssh.session); |
| 70 |
|
| 71 |
ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 72 |
ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, BLACKHOLE); |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
static int session_teardown(void **state) |
| 78 |
{ |
| 79 |
struct torture_state *s = *state; |
| 80 |
|
| 81 |
ssh_disconnect(s->ssh.session); |
| 82 |
ssh_free(s->ssh.session); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static void torture_connect_peer_discon_msg(void **state) { |
| 88 |
struct torture_state *s = *state; |
| 89 |
ssh_session session = s->ssh.session; |
| 90 |
|
| 91 |
int rc; |
| 92 |
|
| 93 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 94 |
assert_ssh_return_code(session, rc); |
| 95 |
|
| 96 |
rc = ssh_connect(session); |
| 97 |
assert_ssh_return_code(session, rc); |
| 98 |
|
| 99 |
rc = ssh_service_request(session, "wrong-service"); |
| 100 |
assert_int_not_equal(rc, SSH_OK); |
| 101 |
|
| 102 |
ssh_disconnect(session); |
| 103 |
assert_non_null(session->peer_discon_msg); |
| 104 |
assert_non_null(ssh_get_disconnect_message(session)); |
| 105 |
} |
| 106 |
|
| 107 |
static void torture_connect_nonblocking(void **state) { |
| 108 |
struct torture_state *s = *state; |
| 109 |
ssh_session session = s->ssh.session; |
| 110 |
int rc; |
| 111 |
|
| 112 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 113 |
assert_ssh_return_code(session, rc); |
| 114 |
ssh_set_blocking(session,0); |
| 115 |
|
| 116 |
do { |
| 117 |
rc = ssh_connect(session); |
| 118 |
assert_ssh_return_code_not_equal(session, rc, SSH_ERROR); |
| 119 |
} while(rc == SSH_AGAIN); |
| 120 |
|
| 121 |
assert_ssh_return_code(session, rc); |
| 122 |
} |
| 123 |
|
| 124 |
static void torture_connect_ipv6(void **state) { |
| 125 |
struct torture_state *s = *state; |
| 126 |
ssh_session session = s->ssh.session; |
| 127 |
int rc; |
| 128 |
|
| 129 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, "testing"); |
| 130 |
assert_ssh_return_code(session, rc); |
| 131 |
/* set non-blocking mode */ |
| 132 |
ssh_set_blocking(session, 0); |
| 133 |
|
| 134 |
do { |
| 135 |
rc = ssh_connect(session); |
| 136 |
} while (rc == SSH_AGAIN); |
| 137 |
|
| 138 |
assert_ssh_return_code(session, rc); |
| 139 |
|
| 140 |
/* should work for blocking mode too */ |
| 141 |
ssh_disconnect(session); |
| 142 |
ssh_set_blocking(session, 1); |
| 143 |
rc = ssh_connect(session); |
| 144 |
assert_ssh_return_code(session, rc); |
| 145 |
} |
| 146 |
|
| 147 |
static void torture_connect_addrfamily(void **state) |
| 148 |
{ |
| 149 |
struct torture_state *s = *state; |
| 150 |
ssh_session session = s->ssh.session; |
| 151 |
int rc; |
| 152 |
|
| 153 |
struct aftest { |
| 154 |
enum ssh_address_family_options_e family; |
| 155 |
char const *host; |
| 156 |
int return_code; |
| 157 |
}; |
| 158 |
static struct aftest aftests[] = { |
| 159 |
{SSH_ADDRESS_FAMILY_ANY, "afboth", SSH_OK}, |
| 160 |
{SSH_ADDRESS_FAMILY_INET, "afboth", SSH_OK}, |
| 161 |
{SSH_ADDRESS_FAMILY_INET6, "afboth", SSH_OK}, |
| 162 |
{SSH_ADDRESS_FAMILY_ANY, "afinet", SSH_OK}, |
| 163 |
{SSH_ADDRESS_FAMILY_INET, "afinet", SSH_OK}, |
| 164 |
{SSH_ADDRESS_FAMILY_INET6, "afinet", SSH_ERROR}, |
| 165 |
{SSH_ADDRESS_FAMILY_ANY, "afinet6", SSH_OK}, |
| 166 |
{SSH_ADDRESS_FAMILY_INET, "afinet6", SSH_ERROR}, |
| 167 |
{SSH_ADDRESS_FAMILY_INET6, "afinet6", SSH_OK}, |
| 168 |
}; |
| 169 |
|
| 170 |
int aftest_count = sizeof(aftests) / sizeof(aftests[0]); |
| 171 |
for (int i = 0; i < aftest_count; ++i) { |
| 172 |
struct aftest const *t = &aftests[i]; |
| 173 |
|
| 174 |
rc = ssh_options_set(session, SSH_OPTIONS_ADDRESS_FAMILY, &t->family); |
| 175 |
assert_ssh_return_code(session, rc); |
| 176 |
|
| 177 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, t->host); |
| 178 |
assert_ssh_return_code(session, rc); |
| 179 |
|
| 180 |
do { |
| 181 |
rc = ssh_connect(session); |
| 182 |
} while (rc == SSH_AGAIN); |
| 183 |
|
| 184 |
assert_ssh_return_code_equal(session, rc, t->return_code); |
| 185 |
ssh_disconnect(session); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
#if 0 /* This does not work with socket_wrapper */ |
| 190 |
static void torture_connect_timeout(void **state) { |
| 191 |
struct torture_state *s = *state; |
| 192 |
ssh_session session = s->ssh.session; |
| 193 |
struct timeval before, after; |
| 194 |
int rc; |
| 195 |
long timeout = 2; |
| 196 |
time_t sec; |
| 197 |
suseconds_t usec; |
| 198 |
|
| 199 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, BLACKHOLE); |
| 200 |
assert_true(rc == SSH_OK); |
| 201 |
rc = ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout); |
| 202 |
assert_true(rc == SSH_OK); |
| 203 |
|
| 204 |
rc = gettimeofday(&before, NULL); |
| 205 |
assert_true(rc == 0); |
| 206 |
rc = ssh_connect(session); |
| 207 |
assert_true(rc == SSH_ERROR); |
| 208 |
rc = gettimeofday(&after, NULL); |
| 209 |
assert_true(rc == 0); |
| 210 |
sec = after.tv_sec - before.tv_sec; |
| 211 |
usec = after.tv_usec - before.tv_usec; |
| 212 |
/* Borrow a second for the missing usecs, but don't bother calculating */ |
| 213 |
if (usec < 0) |
| 214 |
sec--; |
| 215 |
assert_in_range(sec, 1, 3); |
| 216 |
} |
| 217 |
#endif |
| 218 |
|
| 219 |
static void torture_connect_double(void **state) { |
| 220 |
struct torture_state *s = *state; |
| 221 |
ssh_session session = s->ssh.session; |
| 222 |
|
| 223 |
int rc; |
| 224 |
|
| 225 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 226 |
assert_ssh_return_code(session, rc); |
| 227 |
|
| 228 |
rc = ssh_connect(session); |
| 229 |
assert_ssh_return_code(session, rc); |
| 230 |
ssh_disconnect(session); |
| 231 |
|
| 232 |
rc = ssh_connect(session); |
| 233 |
assert_ssh_return_code(session, rc); |
| 234 |
} |
| 235 |
|
| 236 |
static void torture_connect_failure(void **state) { |
| 237 |
/* |
| 238 |
* The intent of this test is to check that a fresh |
| 239 |
* ssh_new/ssh_disconnect/ssh_free sequence doesn't crash/leak |
| 240 |
* and the behavior of a double ssh_disconnect |
| 241 |
*/ |
| 242 |
struct torture_state *s = *state; |
| 243 |
ssh_session session = s->ssh.session; |
| 244 |
|
| 245 |
ssh_disconnect(session); |
| 246 |
} |
| 247 |
|
| 248 |
static void torture_connect_socket(void **state) { |
| 249 |
struct torture_state *s = *state; |
| 250 |
ssh_session session = s->ssh.session; |
| 251 |
|
| 252 |
int rc; |
| 253 |
int sock_fd = 0; |
| 254 |
struct sockaddr_in server_addr = { |
| 255 |
.sin_family = AF_INET, |
| 256 |
.sin_port = htons(22), |
| 257 |
.sin_addr.s_addr = inet_addr(TORTURE_SSH_SERVER), |
| 258 |
}; |
| 259 |
|
| 260 |
sock_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 261 |
assert_true(sock_fd > 2); |
| 262 |
|
| 263 |
rc = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); |
| 264 |
assert_return_code(rc, errno); |
| 265 |
|
| 266 |
ssh_options_set(session, SSH_OPTIONS_FD, &sock_fd); |
| 267 |
|
| 268 |
rc = ssh_connect(session); |
| 269 |
assert_ssh_return_code(session, rc); |
| 270 |
} |
| 271 |
|
| 272 |
static void torture_connect_uninitialized(UNUSED_PARAM(void **state)) |
| 273 |
{ |
| 274 |
int rc; |
| 275 |
ssh_session session; |
| 276 |
struct passwd *pwd; |
| 277 |
|
| 278 |
/* Make sure the library is uninitialized */ |
| 279 |
while (is_ssh_initialized()) { |
| 280 |
rc = ssh_finalize(); |
| 281 |
assert_return_code(rc, errno); |
| 282 |
} |
| 283 |
|
| 284 |
pwd = getpwnam("bob"); |
| 285 |
assert_non_null(pwd); |
| 286 |
|
| 287 |
rc = setuid(pwd->pw_uid); |
| 288 |
assert_return_code(rc, errno); |
| 289 |
|
| 290 |
session = ssh_new(); |
| 291 |
assert_non_null(session); |
| 292 |
|
| 293 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 294 |
assert_ssh_return_code(session, rc); |
| 295 |
|
| 296 |
/* Expect error from ssh_connect */ |
| 297 |
rc = ssh_connect(session); |
| 298 |
assert_false(rc == SSH_OK); |
| 299 |
assert_string_equal(ssh_get_error(session), "Library not initialized."); |
| 300 |
|
| 301 |
ssh_free(session); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
internal_log(ssh_session session, |
| 306 |
int priority, |
| 307 |
const char *message, |
| 308 |
void *userdata) |
| 309 |
{ |
| 310 |
(void)session; |
| 311 |
(void)priority; |
| 312 |
(void)message; |
| 313 |
(void)userdata; |
| 314 |
|
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
static void |
| 319 |
torture_legacy_callback(void **state) |
| 320 |
{ |
| 321 |
struct ssh_callbacks_struct cb[2] = {0}; |
| 322 |
int rc, verbosity = SSH_LOG_WARNING; |
| 323 |
ssh_session session = NULL; |
| 324 |
|
| 325 |
/* unused. */ |
| 326 |
(void)state; |
| 327 |
|
| 328 |
/* |
| 329 |
* Legacy code in 'ssh_set_callbacks' used to |
| 330 |
* create the conditions for a use-after-free |
| 331 |
* issue, in multi-session programs, by failing |
| 332 |
* to update a pointer with the new session. |
| 333 |
* |
| 334 |
* To verify it won't happen again, this test |
| 335 |
* creates two consecutive sessions and frees |
| 336 |
* them; if any fault occurs then the pointer |
| 337 |
* remained at the previous session, failing |
| 338 |
* to be updated. |
| 339 |
*/ |
| 340 |
for (int i = 0; i < 2; i++) { |
| 341 |
session = ssh_new(); |
| 342 |
assert_non_null(session); |
| 343 |
|
| 344 |
rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 345 |
assert_ssh_return_code(session, rc); |
| 346 |
|
| 347 |
rc = ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 348 |
assert_ssh_return_code(session, rc); |
| 349 |
|
| 350 |
cb[i].log_function = internal_log; |
| 351 |
|
| 352 |
ssh_callbacks_init(&cb[i]); |
| 353 |
ssh_set_callbacks(session, &cb[i]); |
| 354 |
|
| 355 |
rc = ssh_connect(session); |
| 356 |
assert_ssh_return_code(session, rc); |
| 357 |
ssh_disconnect(session); |
| 358 |
|
| 359 |
ssh_free(session); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
int torture_run_tests(void) { |
| 364 |
int rc; |
| 365 |
struct CMUnitTest tests[] = { |
| 366 |
cmocka_unit_test_setup_teardown(torture_connect_peer_discon_msg, |
| 367 |
session_setup, |
| 368 |
session_teardown), |
| 369 |
cmocka_unit_test_setup_teardown(torture_connect_nonblocking, |
| 370 |
session_setup, |
| 371 |
session_teardown), |
| 372 |
cmocka_unit_test_setup_teardown(torture_connect_ipv6, |
| 373 |
session_setup, |
| 374 |
session_teardown), |
| 375 |
cmocka_unit_test_setup_teardown(torture_connect_addrfamily, |
| 376 |
session_setup, |
| 377 |
session_teardown), |
| 378 |
cmocka_unit_test_setup_teardown(torture_connect_double, |
| 379 |
session_setup, |
| 380 |
session_teardown), |
| 381 |
cmocka_unit_test_setup_teardown(torture_connect_failure, |
| 382 |
session_setup, |
| 383 |
session_teardown), |
| 384 |
#if 0 |
| 385 |
cmocka_unit_test_setup_teardown(torture_connect_timeout, session_setup, session_teardown), |
| 386 |
#endif |
| 387 |
cmocka_unit_test_setup_teardown(torture_connect_socket, |
| 388 |
session_setup, |
| 389 |
session_teardown), |
| 390 |
cmocka_unit_test(torture_legacy_callback), |
| 391 |
cmocka_unit_test(torture_connect_uninitialized), |
| 392 |
}; |
| 393 |
|
| 394 |
ssh_init(); |
| 395 |
|
| 396 |
torture_filter_tests(tests); |
| 397 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 398 |
|
| 399 |
ssh_finalize(); |
| 400 |
return rc; |
| 401 |
} |