Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/fuzz/quic-client.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 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
 * https://www.openssl.org/source/license.html
8
 * or in the file LICENSE in the source distribution.
9
 */
10
11
#include <openssl/ssl.h>
12
#include <openssl/err.h>
13
#include <openssl/bio.h>
14
#include "fuzzer.h"
15
#include "internal/sockets.h"
16
#include "internal/time.h"
17
#include "internal/quic_ssl.h"
18
19
/* unused, to avoid warning. */
20
static int idx;
21
22
static OSSL_TIME fake_now;
23
24
static OSSL_TIME fake_now_cb(void *arg)
25
272M
{
26
272M
    return fake_now;
27
272M
}
28
29
int FuzzerInitialize(int *argc, char ***argv)
30
66
{
31
66
    STACK_OF(SSL_COMP) *comp_methods;
32
33
66
    FuzzerSetRand();
34
66
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
35
66
    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
36
66
    ERR_clear_error();
37
66
    CRYPTO_free_ex_index(0, -1);
38
66
    idx = SSL_get_ex_data_X509_STORE_CTX_idx();
39
66
    comp_methods = SSL_COMP_get_compression_methods();
40
66
    if (comp_methods != NULL)
41
66
        sk_SSL_COMP_sort(comp_methods);
42
43
66
    return 1;
44
66
}
45
46
49.9M
#define HANDSHAKING      0
47
36.0M
#define READING          1
48
493k
#define WRITING          2
49
7.93M
#define ACCEPTING_STREAM 3
50
4.34M
#define CREATING_STREAM  4
51
5.59k
#define SWAPPING_STREAM  5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
54.8k
{
55
54.8k
    SSL *client = NULL, *stream = NULL;
56
54.8k
    SSL *allstreams[] = {NULL, NULL, NULL, NULL};
57
54.8k
    size_t i, thisstream = 0, numstreams = 1;
58
54.8k
    BIO *in;
59
54.8k
    BIO *out;
60
54.8k
    SSL_CTX *ctx;
61
54.8k
    BIO_ADDR *peer_addr = NULL;
62
54.8k
    struct in_addr ina = {0};
63
54.8k
    struct timeval tv;
64
54.8k
    int state = HANDSHAKING;
65
54.8k
    uint8_t tmp[1024];
66
54.8k
    int writelen = 0;
67
68
54.8k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
54.8k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
54.8k
    if (ctx == NULL)
74
0
        goto end;
75
76
54.8k
    client = SSL_new(ctx);
77
54.8k
    if (client == NULL)
78
0
        goto end;
79
80
54.8k
    fake_now = ossl_ms2time(1);
81
54.8k
    if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
54.8k
    peer_addr = BIO_ADDR_new();
85
54.8k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
54.8k
    ina.s_addr = htonl(0x7f000001UL);
89
90
54.8k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
       goto end;
92
93
54.8k
    SSL_set_tlsext_host_name(client, "localhost");
94
54.8k
    in = BIO_new(BIO_s_dgram_mem());
95
54.8k
    if (in == NULL)
96
0
        goto end;
97
54.8k
    out = BIO_new(BIO_s_dgram_mem());
98
54.8k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
54.8k
    if (!BIO_dgram_set_caps(out, BIO_DGRAM_CAP_HANDLES_DST_ADDR)) {
103
0
        BIO_free(in);
104
0
        BIO_free(out);
105
0
        goto end;
106
0
    }
107
54.8k
    SSL_set_bio(client, in, out);
108
54.8k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
54.8k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
54.8k
    SSL_set_connect_state(client);
113
114
54.8k
    if (!SSL_set_incoming_stream_policy(client,
115
54.8k
                                        SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
54.8k
                                        0))
117
0
        goto end;
118
119
54.8k
    allstreams[0] = stream = client;
120
57.7M
    for (;;) {
121
57.7M
        size_t size;
122
57.7M
        uint64_t nxtpktms = 0;
123
57.7M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
57.7M
        int isinf, ret = 0;
125
126
57.7M
        if (len >= 2) {
127
57.7M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
6.38M
                switch (buf[2]) {
129
4.10M
                case 0x00:
130
4.10M
                    if (state == READING)
131
3.96M
                        state = ACCEPTING_STREAM;
132
4.10M
                    break;
133
2.25M
                case 0x01:
134
2.25M
                    if (state == READING)
135
2.17M
                        state = CREATING_STREAM;
136
2.25M
                    break;
137
3.70k
                case 0x02:
138
3.70k
                    if (state == READING)
139
2.79k
                        state = SWAPPING_STREAM;
140
3.70k
                    break;
141
15.5k
                default:
142
                    /*ignore*/
143
15.5k
                    break;
144
6.38M
                }
145
6.38M
                len -= 3;
146
6.38M
                buf += 3;
147
6.38M
            }
148
57.7M
            nxtpktms = buf[0] + (buf[1] << 8);
149
57.7M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
57.7M
            len -= 2;
151
57.7M
            buf += 2;
152
57.7M
        }
153
154
80.0M
        for (;;) {
155
80.0M
            switch (state) {
156
49.8M
            case HANDSHAKING:
157
49.8M
                ret = SSL_do_handshake(stream);
158
49.8M
                if (ret == 1)
159
13.2k
                    state = READING;
160
49.8M
                break;
161
162
23.5M
            case READING:
163
23.5M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
23.5M
                if (ret > 0) {
165
7.12k
                    state = WRITING;
166
7.12k
                    writelen = ret;
167
7.12k
                    assert(writelen <= (int)sizeof(tmp));
168
7.12k
                }
169
23.5M
                break;
170
171
23.5M
            case WRITING:
172
485k
                ret = SSL_write(stream, tmp, writelen);
173
485k
                if (ret > 0)
174
6.05k
                    state = READING;
175
485k
                break;
176
177
3.96M
            case ACCEPTING_STREAM:
178
3.96M
                state = READING;
179
3.96M
                ret = 1;
180
3.96M
                if (numstreams == OSSL_NELEM(allstreams)
181
10.1k
                        || SSL_get_accept_stream_queue_len(client) == 0)
182
3.96M
                    break;
183
339
                thisstream = numstreams;
184
339
                stream = allstreams[numstreams++]
185
339
                        = SSL_accept_stream(client, 0);
186
339
                if (stream == NULL)
187
0
                    goto end;
188
339
                break;
189
190
2.17M
            case CREATING_STREAM:
191
2.17M
                state = READING;
192
2.17M
                ret = 1;
193
2.17M
                if (numstreams == OSSL_NELEM(allstreams))
194
2.15M
                    break;
195
12.7k
                stream = SSL_new_stream(client, 0);
196
12.7k
                if (stream == NULL) {
197
                    /* Ignore, and go back to the previous stream */
198
6.94k
                    stream = allstreams[thisstream];
199
6.94k
                    break;
200
6.94k
                }
201
5.85k
                thisstream = numstreams;
202
5.85k
                allstreams[numstreams++] = stream;
203
5.85k
                break;
204
205
2.79k
            case SWAPPING_STREAM:
206
2.79k
                state = READING;
207
2.79k
                ret = 1;
208
2.79k
                if (numstreams == 1)
209
1.03k
                    break;
210
1.76k
                if (++thisstream == numstreams)
211
898
                    thisstream = 0;
212
1.76k
                stream = allstreams[thisstream];
213
1.76k
                break;
214
80.0M
            }
215
80.0M
            assert(stream != NULL);
216
80.0M
            assert(thisstream < numstreams);
217
80.0M
            if (ret <= 0) {
218
73.8M
                switch (SSL_get_error(stream, ret)) {
219
73.3M
                case SSL_ERROR_WANT_READ:
220
73.8M
                case SSL_ERROR_WANT_WRITE:
221
73.8M
                    break;
222
43.2k
                default:
223
43.2k
                    goto end;
224
73.8M
                }
225
73.8M
            }
226
227
79.9M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
79.9M
            if (isinf) {
231
135k
                fake_now = nxtpkt;
232
135k
                break;
233
79.8M
            } else {
234
79.8M
                nxttimeout = ossl_time_add(fake_now,
235
79.8M
                                           ossl_time_from_timeval(tv));
236
79.8M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
57.5M
                    fake_now = nxtpkt;
238
57.5M
                    break;
239
57.5M
                }
240
22.2M
                fake_now = nxttimeout;
241
22.2M
            }
242
79.9M
        }
243
244
57.7M
        if (len <= 3)
245
2.09k
            break;
246
247
57.7M
        size = buf[0] + (buf[1] << 8);
248
57.7M
        if (size > len - 2)
249
9.53k
            break;
250
251
57.7M
        if (size > 0)
252
13.0M
            BIO_write(in, buf+2, (int)size);
253
57.7M
        len -= size + 2;
254
57.7M
        buf += size + 2;
255
57.7M
    }
256
54.8k
 end:
257
115k
    for (i = 0; i < numstreams; i++)
258
61.0k
        SSL_free(allstreams[i]);
259
54.8k
    ERR_clear_error();
260
54.8k
    SSL_CTX_free(ctx);
261
54.8k
    BIO_ADDR_free(peer_addr);
262
263
54.8k
    return 0;
264
54.8k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}