Coverage Report

Created: 2025-08-26 07:03

/src/libssh/tests/fuzz/ssh_pubkey_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2023 Jakub Jelen <jjelen@redhat.com>
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
#include "config.h"
17
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/misc.h"
25
26
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
27
2.26k
{
28
2.26k
    ssh_key pkey = NULL;
29
2.26k
    char *filename = NULL;
30
2.26k
    int fd;
31
2.26k
    int rc;
32
2.26k
    ssize_t sz;
33
34
2.26k
    ssh_init();
35
36
2.26k
    filename = strdup("/tmp/libssh_pubkey_XXXXXX");
37
2.26k
    if (filename == NULL) {
38
0
        return -1;
39
0
    }
40
2.26k
    fd = mkstemp(filename);
41
2.26k
    if (fd  == -1) {
42
0
        free(filename);
43
0
        close(fd);
44
0
        return -1;
45
0
    }
46
2.26k
    sz = ssh_writen(fd, data, size);
47
2.26k
    close(fd);
48
2.26k
    if (sz == SSH_ERROR) {
49
0
        unlink(filename);
50
0
        free(filename);
51
0
        return -1;
52
0
    }
53
54
2.26k
    rc = ssh_pki_import_pubkey_file(filename, &pkey);
55
2.26k
    if (rc != SSH_OK) {
56
2.11k
        unlink(filename);
57
2.11k
        free(filename);
58
2.11k
        return 1;
59
2.11k
    }
60
148
    ssh_key_free(pkey);
61
148
    unlink(filename);
62
148
    free(filename);
63
64
148
    ssh_finalize();
65
66
148
    return 0;
67
2.26k
}
68