| 1 |
/* |
| 2 |
* Copyright 2022 Jakub Jelen <[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 "knownhosts.c" |
| 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 |
char *hostname = NULL; |
| 49 |
const uint8_t *hostname_end = NULL; |
| 50 |
size_t hostname_len = 0; |
| 51 |
char filename[256]; |
| 52 |
struct ssh_list *entries = NULL; |
| 53 |
struct ssh_iterator *it = NULL; |
| 54 |
FILE *fp = NULL; |
| 55 |
|
| 56 |
/* Interpret the first part of the string (until the first NULL byte) |
| 57 |
* as a hostname we are searching for in the file */ |
| 58 |
hostname_end = memchr(data, '\0', size); |
| 59 |
if (hostname_end == NULL) { |
| 60 |
return 1; |
| 61 |
} |
| 62 |
hostname_len = hostname_end - data + 1; |
| 63 |
if (hostname_len > 253) { |
| 64 |
/* This is the maximum valid length of a hostname */ |
| 65 |
return 1; |
| 66 |
} |
| 67 |
hostname = malloc(hostname_len); |
| 68 |
if (hostname == NULL) { |
| 69 |
return 1; |
| 70 |
} |
| 71 |
memcpy(hostname, data, hostname_len); |
| 72 |
|
| 73 |
snprintf(filename, sizeof(filename), "/tmp/libfuzzer.%d", getpid()); |
| 74 |
fp = fopen(filename, "wb"); |
| 75 |
if (!fp) { |
| 76 |
free(hostname); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
fwrite(data + hostname_len, size - hostname_len, 1, fp); |
| 80 |
fclose(fp); |
| 81 |
|
| 82 |
assert(nalloc_start(data, size) > 0); |
| 83 |
|
| 84 |
ssh_known_hosts_read_entries(hostname, filename, &entries); |
| 85 |
for (it = ssh_list_get_iterator(entries); |
| 86 |
it != NULL; |
| 87 |
it = ssh_list_get_iterator(entries)) { |
| 88 |
struct ssh_knownhosts_entry *entry = NULL; |
| 89 |
|
| 90 |
entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it); |
| 91 |
ssh_knownhosts_entry_free(entry); |
| 92 |
ssh_list_remove(entries, it); |
| 93 |
} |
| 94 |
ssh_list_free(entries); |
| 95 |
|
| 96 |
ssh_finalize(); |
| 97 |
|
| 98 |
free(hostname); |
| 99 |
unlink(filename); |
| 100 |
|
| 101 |
nalloc_end(); |
| 102 |
return 0; |
| 103 |
} |