Coverage Report

Created: 2026-12-01 08:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/tests/fuzz/ssh_sftp_attr_fuzzer.c
Line
Count
Source
1
/*
2
 * Copyright 2026 libssh authors
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 <assert.h>
19
#include <stdint.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#define LIBSSH_STATIC 1
25
#include "libssh/libssh.h"
26
#include "libssh/priv.h"
27
#include "libssh/sftp.h"
28
#include "libssh/sftp_priv.h"
29
30
#include "nallocinc.c"
31
32
/* SFTP protocol version constants */
33
2.20k
#define SFTP_PROTOCOL_VERSION_3 3
34
2.20k
#define SFTP_PROTOCOL_VERSION_4 4
35
36
/* Flags for sftp_parse_attr expectname parameter */
37
2.20k
#define SFTP_EXPECT_NAME 1
38
2.20k
#define SFTP_NO_NAME 0
39
40
/*
41
 * Helper to create a minimal sftp_session for fuzzing.
42
 * We don't use sftp_new() as it requires a real SSH connection.
43
 */
44
static sftp_session create_minimal_sftp_session(ssh_session session)
45
846
{
46
846
    sftp_session sftp;
47
48
846
    sftp = calloc(1, sizeof(struct sftp_session_struct));
49
846
    if (sftp == NULL) {
50
0
        return NULL;
51
0
    }
52
846
    sftp->session = session;
53
54
846
    return sftp;
55
846
}
56
57
static void _fuzz_finalize(void)
58
4
{
59
4
    ssh_finalize();
60
4
}
61
62
int LLVMFuzzerInitialize(int *argc, char ***argv)
63
41
{
64
41
    (void)argc;
65
66
41
    nalloc_init(*argv[0]);
67
68
41
    ssh_init();
69
70
41
    atexit(_fuzz_finalize);
71
72
41
    return 0;
73
41
}
74
75
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
76
1.10k
{
77
1.10k
    ssh_session session = NULL;
78
1.10k
    sftp_session sftp = NULL;
79
1.10k
    ssh_buffer buffer = NULL;
80
1.10k
    sftp_attributes attr = NULL;
81
1.10k
    int versions[] = {
82
1.10k
        SFTP_PROTOCOL_VERSION_3, SFTP_PROTOCOL_VERSION_3,
83
1.10k
        SFTP_PROTOCOL_VERSION_4, SFTP_PROTOCOL_VERSION_4
84
1.10k
    };
85
1.10k
    int expectnames[] = {SFTP_NO_NAME, SFTP_EXPECT_NAME, SFTP_NO_NAME, SFTP_EXPECT_NAME};
86
1.10k
    size_t i;
87
88
    /* Minimum bytes for a valid SFTP message */
89
1.10k
    if (size == 0) {
90
0
        return 0;
91
0
    }
92
93
1.10k
    assert(nalloc_start(data, size) > 0);
94
95
    /* Allocate shared resources once for all test iterations */
96
1.10k
    session = ssh_new();
97
1.10k
    if (session == NULL) {
98
254
        goto cleanup;
99
254
    }
100
101
846
    sftp = create_minimal_sftp_session(session);
102
846
    if (sftp == NULL) {
103
0
        goto cleanup;
104
0
    }
105
106
846
    buffer = ssh_buffer_new();
107
846
    if (buffer == NULL) {
108
0
        goto cleanup;
109
0
    }
110
111
    /* Main fuzzing target: sftp_parse_attr */
112
    /* Parses untrusted SFTP messages from client */
113
    /* Test all combinations (v3/v4, with/without name) */
114
4.23k
    for (i = 0; i < ARRAY_SIZE(versions); i++) {
115
3.38k
        sftp->version = versions[i];
116
117
        /* Reset and repopulate buffer for each iteration */
118
3.38k
        ssh_buffer_reinit(buffer);
119
3.38k
        if (ssh_buffer_add_data(buffer, data, size) == SSH_OK) {
120
3.38k
            attr = sftp_parse_attr(sftp, buffer, expectnames[i]);
121
3.38k
            sftp_attributes_free(attr);
122
3.38k
            attr = NULL;
123
3.38k
        }
124
3.38k
    }
125
126
1.10k
cleanup:
127
1.10k
    ssh_buffer_free(buffer);
128
1.10k
    free(sftp);
129
1.10k
    ssh_free(session);
130
1.10k
    nalloc_end();
131
132
1.10k
    return 0;
133
846
}