Coverage Report

Created: 2025-08-11 07:04

/src/openssl34/fuzz/quic-client.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2022 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
215M
{
26
215M
    return fake_now;
27
215M
}
28
29
int FuzzerInitialize(int *argc, char ***argv)
30
54
{
31
54
    STACK_OF(SSL_COMP) *comp_methods;
32
33
54
    FuzzerSetRand();
34
54
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
35
54
    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
36
54
    ERR_clear_error();
37
54
    CRYPTO_free_ex_index(0, -1);
38
54
    idx = SSL_get_ex_data_X509_STORE_CTX_idx();
39
54
    comp_methods = SSL_COMP_get_compression_methods();
40
54
    if (comp_methods != NULL)
41
54
        sk_SSL_COMP_sort(comp_methods);
42
43
54
    return 1;
44
54
}
45
46
36.5M
#define HANDSHAKING      0
47
32.5M
#define READING          1
48
117k
#define WRITING          2
49
6.41M
#define ACCEPTING_STREAM 3
50
3.57M
#define CREATING_STREAM  4
51
5.05k
#define SWAPPING_STREAM  5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
46.3k
{
55
46.3k
    SSL *client = NULL, *stream = NULL;
56
46.3k
    SSL *allstreams[] = {NULL, NULL, NULL, NULL};
57
46.3k
    size_t i, thisstream = 0, numstreams = 1;
58
46.3k
    BIO *in;
59
46.3k
    BIO *out;
60
46.3k
    SSL_CTX *ctx;
61
46.3k
    BIO_ADDR *peer_addr = NULL;
62
46.3k
    struct in_addr ina = {0};
63
46.3k
    struct timeval tv;
64
46.3k
    int state = HANDSHAKING;
65
46.3k
    uint8_t tmp[1024];
66
46.3k
    int writelen = 0;
67
68
46.3k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
46.3k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
46.3k
    if (ctx == NULL)
74
0
        goto end;
75
76
46.3k
    client = SSL_new(ctx);
77
46.3k
    if (client == NULL)
78
0
        goto end;
79
80
46.3k
    fake_now = ossl_ms2time(1);
81
46.3k
    if (!ossl_quic_conn_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
46.3k
    peer_addr = BIO_ADDR_new();
85
46.3k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
46.3k
    ina.s_addr = htonl(0x7f000001UL);
89
90
46.3k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
       goto end;
92
93
46.3k
    SSL_set_tlsext_host_name(client, "localhost");
94
46.3k
    in = BIO_new(BIO_s_dgram_mem());
95
46.3k
    if (in == NULL)
96
0
        goto end;
97
46.3k
    out = BIO_new(BIO_s_dgram_mem());
98
46.3k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
46.3k
    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
46.3k
    SSL_set_bio(client, in, out);
108
46.3k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
46.3k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
46.3k
    SSL_set_connect_state(client);
113
114
46.3k
    if (!SSL_set_incoming_stream_policy(client,
115
46.3k
                                        SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
46.3k
                                        0))
117
0
        goto end;
118
119
46.3k
    allstreams[0] = stream = client;
120
47.3M
    for (;;) {
121
47.3M
        size_t size;
122
47.3M
        uint64_t nxtpktms = 0;
123
47.3M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
47.3M
        int isinf, ret = 0;
125
126
47.3M
        if (len >= 2) {
127
47.3M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
5.04M
                switch (buf[2]) {
129
3.22M
                case 0x00:
130
3.22M
                    if (state == READING)
131
3.20M
                        state = ACCEPTING_STREAM;
132
3.22M
                    break;
133
1.80M
                case 0x01:
134
1.80M
                    if (state == READING)
135
1.78M
                        state = CREATING_STREAM;
136
1.80M
                    break;
137
3.19k
                case 0x02:
138
3.19k
                    if (state == READING)
139
2.52k
                        state = SWAPPING_STREAM;
140
3.19k
                    break;
141
8.10k
                default:
142
                    /*ignore*/
143
8.10k
                    break;
144
5.04M
                }
145
5.04M
                len -= 3;
146
5.04M
                buf += 3;
147
5.04M
            }
148
47.3M
            nxtpktms = buf[0] + (buf[1] << 8);
149
47.3M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
47.3M
            len -= 2;
151
47.3M
            buf += 2;
152
47.3M
        }
153
154
64.0M
        for (;;) {
155
64.0M
            switch (state) {
156
36.5M
            case HANDSHAKING:
157
36.5M
                ret = SSL_do_handshake(stream);
158
36.5M
                if (ret == 1)
159
11.4k
                    state = READING;
160
36.5M
                break;
161
162
22.4M
            case READING:
163
22.4M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
22.4M
                if (ret > 0) {
165
5.70k
                    state = WRITING;
166
5.70k
                    writelen = ret;
167
5.70k
                    assert(writelen <= (int)sizeof(tmp));
168
5.70k
                }
169
22.4M
                break;
170
171
22.4M
            case WRITING:
172
112k
                ret = SSL_write(stream, tmp, writelen);
173
112k
                if (ret > 0)
174
4.95k
                    state = READING;
175
112k
                break;
176
177
3.20M
            case ACCEPTING_STREAM:
178
3.20M
                state = READING;
179
3.20M
                ret = 1;
180
3.20M
                if (numstreams == OSSL_NELEM(allstreams)
181
3.20M
                        || SSL_get_accept_stream_queue_len(client) == 0)
182
3.20M
                    break;
183
368
                thisstream = numstreams;
184
368
                stream = allstreams[numstreams++]
185
368
                        = SSL_accept_stream(client, 0);
186
368
                if (stream == NULL)
187
0
                    goto end;
188
368
                break;
189
190
1.78M
            case CREATING_STREAM:
191
1.78M
                state = READING;
192
1.78M
                ret = 1;
193
1.78M
                if (numstreams == OSSL_NELEM(allstreams))
194
1.77M
                    break;
195
8.03k
                stream = SSL_new_stream(client, 0);
196
8.03k
                if (stream == NULL) {
197
                    /* Ignore, and go back to the previous stream */
198
3.08k
                    stream = allstreams[thisstream];
199
3.08k
                    break;
200
3.08k
                }
201
4.94k
                thisstream = numstreams;
202
4.94k
                allstreams[numstreams++] = stream;
203
4.94k
                break;
204
205
2.52k
            case SWAPPING_STREAM:
206
2.52k
                state = READING;
207
2.52k
                ret = 1;
208
2.52k
                if (numstreams == 1)
209
780
                    break;
210
1.74k
                if (++thisstream == numstreams)
211
703
                    thisstream = 0;
212
1.74k
                stream = allstreams[thisstream];
213
1.74k
                break;
214
64.0M
            }
215
64.0M
            assert(stream != NULL);
216
64.0M
            assert(thisstream < numstreams);
217
64.0M
            if (ret <= 0) {
218
59.0M
                switch (SSL_get_error(stream, ret)) {
219
58.9M
                case SSL_ERROR_WANT_READ:
220
59.0M
                case SSL_ERROR_WANT_WRITE:
221
59.0M
                    break;
222
36.1k
                default:
223
36.1k
                    goto end;
224
59.0M
                }
225
59.0M
            }
226
227
64.0M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
64.0M
            if (isinf) {
231
216k
                fake_now = nxtpkt;
232
216k
                break;
233
63.8M
            } else {
234
63.8M
                nxttimeout = ossl_time_add(fake_now,
235
63.8M
                                           ossl_time_from_timeval(tv));
236
63.8M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
47.1M
                    fake_now = nxtpkt;
238
47.1M
                    break;
239
47.1M
                }
240
16.6M
                fake_now = nxttimeout;
241
16.6M
            }
242
64.0M
        }
243
244
47.3M
        if (len <= 3)
245
1.67k
            break;
246
247
47.3M
        size = buf[0] + (buf[1] << 8);
248
47.3M
        if (size > len - 2)
249
8.45k
            break;
250
251
47.3M
        if (size > 0)
252
10.7M
            BIO_write(in, buf+2, size);
253
47.3M
        len -= size + 2;
254
47.3M
        buf += size + 2;
255
47.3M
    }
256
46.3k
 end:
257
97.9k
    for (i = 0; i < numstreams; i++)
258
51.6k
        SSL_free(allstreams[i]);
259
46.3k
    ERR_clear_error();
260
46.3k
    SSL_CTX_free(ctx);
261
46.3k
    BIO_ADDR_free(peer_addr);
262
263
46.3k
    return 0;
264
46.3k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}