| 1 |
/* |
| 2 |
* Copyright 2021 Stanislav Zidek <[email protected]> |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#define LIBSSH_STATIC 1 |
| 23 |
#include "libssh/libssh.h" |
| 24 |
#include "libssh/options.h" |
| 25 |
|
| 26 |
#include "nallocinc.c" |
| 27 |
|
| 28 |
static void _fuzz_finalize(void) |
| 29 |
{ |
| 30 |
ssh_finalize(); |
| 31 |
} |
| 32 |
|
| 33 |
int LLVMFuzzerInitialize(int *argc, char ***argv) |
| 34 |
{ |
| 35 |
(void)argc; |
| 36 |
|
| 37 |
nalloc_init(*argv[0]); |
| 38 |
|
| 39 |
ssh_init(); |
| 40 |
|
| 41 |
atexit(_fuzz_finalize); |
| 42 |
|
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
| 47 |
{ |
| 48 |
ssh_session session = NULL; |
| 49 |
char *input = NULL; |
| 50 |
|
| 51 |
input = (char *)malloc(size+1); |
| 52 |
if (!input) { |
| 53 |
return 1; |
| 54 |
} |
| 55 |
strncpy(input, (const char *)data, size); |
| 56 |
input[size] = '\0'; |
| 57 |
|
| 58 |
assert(nalloc_start(data, size) > 0); |
| 59 |
|
| 60 |
session = ssh_new(); |
| 61 |
if (session == NULL) { |
| 62 |
goto out; |
| 63 |
} |
| 64 |
|
| 65 |
/* Make sure we have default options set */ |
| 66 |
ssh_options_set(session, SSH_OPTIONS_SSH_DIR, NULL); |
| 67 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example.com"); |
| 68 |
|
| 69 |
ssh_config_parse_string(session, input); |
| 70 |
|
| 71 |
ssh_free(session); |
| 72 |
|
| 73 |
out: |
| 74 |
free(input); |
| 75 |
|
| 76 |
nalloc_end(); |
| 77 |
return 0; |
| 78 |
} |