Coverage Report

Created: 2024-07-27 06:39

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