Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/ssl/quic/quic_lcidm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/quic_lcidm.h"
11
#include "internal/quic_types.h"
12
#include "internal/quic_vlint.h"
13
#include "internal/common.h"
14
#include <openssl/lhash.h>
15
#include <openssl/rand.h>
16
#include <openssl/err.h>
17
18
/*
19
 * QUIC Local Connection ID Manager
20
 * ================================
21
 */
22
23
typedef struct quic_lcidm_conn_st QUIC_LCIDM_CONN;
24
25
enum {
26
    LCID_TYPE_ODCID,        /* This LCID is the ODCID from the peer */
27
    LCID_TYPE_INITIAL,      /* This is our Initial SCID */
28
    LCID_TYPE_NCID          /* This LCID was issued via a NCID frame */
29
};
30
31
typedef struct quic_lcid_st {
32
    QUIC_CONN_ID                cid;
33
    uint64_t                    seq_num;
34
35
    /* Back-pointer to the owning QUIC_LCIDM_CONN structure. */
36
    QUIC_LCIDM_CONN             *conn;
37
38
    /* LCID_TYPE_* */
39
    unsigned int                type                : 2;
40
} QUIC_LCID;
41
42
DEFINE_LHASH_OF_EX(QUIC_LCID);
43
DEFINE_LHASH_OF_EX(QUIC_LCIDM_CONN);
44
45
struct quic_lcidm_conn_st {
46
    size_t              num_active_lcid;
47
    LHASH_OF(QUIC_LCID) *lcids;
48
    void                *opaque;
49
    QUIC_LCID           *odcid_lcid_obj;
50
    uint64_t            next_seq_num;
51
52
    /* Have we enrolled an ODCID? */
53
    unsigned int        done_odcid          : 1;
54
};
55
56
struct quic_lcidm_st {
57
    OSSL_LIB_CTX                *libctx;
58
    LHASH_OF(QUIC_LCID)         *lcids; /* (QUIC_CONN_ID) -> (QUIC_LCID *)  */
59
    LHASH_OF(QUIC_LCIDM_CONN)   *conns; /* (void *opaque) -> (QUIC_LCIDM_CONN *) */
60
    size_t                      lcid_len; /* Length in bytes for all LCIDs */
61
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
62
    QUIC_CONN_ID                next_lcid;
63
#endif
64
};
65
66
static unsigned long bin_hash(const unsigned char *buf, size_t buf_len)
67
10.1M
{
68
10.1M
    unsigned long hash = 0;
69
10.1M
    size_t i;
70
71
82.5M
    for (i = 0; i < buf_len; ++i)
72
72.3M
        hash ^= ((unsigned long)buf[i]) << (8 * (i % sizeof(unsigned long)));
73
74
10.1M
    return hash;
75
10.1M
}
76
77
static unsigned long lcid_hash(const QUIC_LCID *lcid_obj)
78
10.1M
{
79
10.1M
    return bin_hash(lcid_obj->cid.id, lcid_obj->cid.id_len);
80
10.1M
}
81
82
static int lcid_comp(const QUIC_LCID *a, const QUIC_LCID *b)
83
8.61M
{
84
8.61M
    return !ossl_quic_conn_id_eq(&a->cid, &b->cid);
85
8.61M
}
86
87
static unsigned long lcidm_conn_hash(const QUIC_LCIDM_CONN *conn)
88
3.41M
{
89
3.41M
    return (unsigned long)(uintptr_t)conn->opaque;
90
3.41M
}
91
92
static int lcidm_conn_comp(const QUIC_LCIDM_CONN *a, const QUIC_LCIDM_CONN *b)
93
3.11M
{
94
3.11M
    return a->opaque != b->opaque;
95
3.11M
}
96
97
QUIC_LCIDM *ossl_quic_lcidm_new(OSSL_LIB_CTX *libctx, size_t lcid_len)
98
21.0k
{
99
21.0k
    QUIC_LCIDM *lcidm = NULL;
100
101
21.0k
    if (lcid_len > QUIC_MAX_CONN_ID_LEN)
102
0
        goto err;
103
104
21.0k
    if ((lcidm = OPENSSL_zalloc(sizeof(*lcidm))) == NULL)
105
0
        goto err;
106
107
21.0k
    if ((lcidm->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL)
108
0
        goto err;
109
110
21.0k
    if ((lcidm->conns = lh_QUIC_LCIDM_CONN_new(lcidm_conn_hash,
111
21.0k
                                               lcidm_conn_comp)) == NULL)
112
0
        goto err;
113
114
21.0k
    lcidm->libctx   = libctx;
115
21.0k
    lcidm->lcid_len = lcid_len;
116
21.0k
    return lcidm;
117
118
0
err:
119
0
    if (lcidm != NULL) {
120
0
        lh_QUIC_LCID_free(lcidm->lcids);
121
0
        lh_QUIC_LCIDM_CONN_free(lcidm->conns);
122
0
        OPENSSL_free(lcidm);
123
0
    }
124
0
    return NULL;
125
21.0k
}
126
127
static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn);
128
129
static void lcidm_delete_conn_(QUIC_LCIDM_CONN *conn, void *arg)
130
62.6k
{
131
62.6k
    lcidm_delete_conn((QUIC_LCIDM *)arg, conn);
132
62.6k
}
133
134
void ossl_quic_lcidm_free(QUIC_LCIDM *lcidm)
135
42.4k
{
136
42.4k
    if (lcidm == NULL)
137
18
        return;
138
139
    /*
140
     * Calling OPENSSL_lh_delete during a doall call is unsafe with our
141
     * current LHASH implementation for several reasons:
142
     *
143
     * - firstly, because deletes can cause the hashtable to be contracted,
144
     *   resulting in rehashing which might cause items in later buckets to
145
     *   move to earlier buckets, which might cause doall to skip an item,
146
     *   resulting in a memory leak;
147
     *
148
     * - secondly, because doall in general is not safe across hashtable
149
     *   size changes, as it caches hashtable size and pointer values
150
     *   while operating.
151
     *
152
     * The fix for this is to disable hashtable contraction using the following
153
     * call, which guarantees that no rehashing will occur so long as we only
154
     * call delete and not insert.
155
     */
156
42.4k
    lh_QUIC_LCIDM_CONN_set_down_load(lcidm->conns, 0);
157
158
42.4k
    lh_QUIC_LCIDM_CONN_doall_arg(lcidm->conns, lcidm_delete_conn_, lcidm);
159
160
42.4k
    lh_QUIC_LCID_free(lcidm->lcids);
161
42.4k
    lh_QUIC_LCIDM_CONN_free(lcidm->conns);
162
42.4k
    OPENSSL_free(lcidm);
163
42.4k
}
164
165
static QUIC_LCID *lcidm_get0_lcid(const QUIC_LCIDM *lcidm, const QUIC_CONN_ID *lcid)
166
2.69M
{
167
2.69M
    QUIC_LCID key;
168
169
2.69M
    key.cid = *lcid;
170
171
2.69M
    if (key.cid.id_len > QUIC_MAX_CONN_ID_LEN)
172
0
        return NULL;
173
174
2.69M
    return lh_QUIC_LCID_retrieve(lcidm->lcids, &key);
175
2.69M
}
176
177
static QUIC_LCIDM_CONN *lcidm_get0_conn(const QUIC_LCIDM *lcidm, void *opaque)
178
2.93M
{
179
2.93M
    QUIC_LCIDM_CONN key;
180
181
2.93M
    key.opaque = opaque;
182
183
2.93M
    return lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key);
184
2.93M
}
185
186
static QUIC_LCIDM_CONN *lcidm_upsert_conn(const QUIC_LCIDM *lcidm, void *opaque)
187
2.93M
{
188
2.93M
    QUIC_LCIDM_CONN *conn = lcidm_get0_conn(lcidm, opaque);
189
190
2.93M
    if (conn != NULL)
191
2.81M
        return conn;
192
193
112k
    if ((conn = OPENSSL_zalloc(sizeof(*conn))) == NULL)
194
0
        goto err;
195
196
112k
    if ((conn->lcids = lh_QUIC_LCID_new(lcid_hash, lcid_comp)) == NULL)
197
0
        goto err;
198
199
112k
    conn->opaque = opaque;
200
201
112k
    lh_QUIC_LCIDM_CONN_insert(lcidm->conns, conn);
202
112k
    if (lh_QUIC_LCIDM_CONN_error(lcidm->conns))
203
0
        goto err;
204
205
112k
    return conn;
206
207
0
err:
208
0
    if (conn != NULL) {
209
0
        lh_QUIC_LCID_free(conn->lcids);
210
0
        OPENSSL_free(conn);
211
0
    }
212
0
    return NULL;
213
112k
}
214
215
static void lcidm_delete_conn_lcid(QUIC_LCIDM *lcidm, QUIC_LCID *lcid_obj)
216
2.53M
{
217
2.53M
    lh_QUIC_LCID_delete(lcidm->lcids, lcid_obj);
218
2.53M
    lh_QUIC_LCID_delete(lcid_obj->conn->lcids, lcid_obj);
219
2.53M
    assert(lcid_obj->conn->num_active_lcid > 0);
220
2.53M
    --lcid_obj->conn->num_active_lcid;
221
2.53M
    OPENSSL_free(lcid_obj);
222
2.53M
}
223
224
/* doall_arg wrapper */
225
static void lcidm_delete_conn_lcid_(QUIC_LCID *lcid_obj, void *arg)
226
2.49M
{
227
2.49M
    lcidm_delete_conn_lcid((QUIC_LCIDM *)arg, lcid_obj);
228
2.49M
}
229
230
static void lcidm_delete_conn(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn)
231
112k
{
232
    /* See comment in ossl_quic_lcidm_free */
233
112k
    lh_QUIC_LCID_set_down_load(conn->lcids, 0);
234
235
112k
    lh_QUIC_LCID_doall_arg(conn->lcids, lcidm_delete_conn_lcid_, lcidm);
236
112k
    lh_QUIC_LCIDM_CONN_delete(lcidm->conns, conn);
237
112k
    lh_QUIC_LCID_free(conn->lcids);
238
112k
    OPENSSL_free(conn);
239
112k
}
240
241
static QUIC_LCID *lcidm_conn_new_lcid(QUIC_LCIDM *lcidm, QUIC_LCIDM_CONN *conn,
242
                                      const QUIC_CONN_ID *lcid)
243
2.53M
{
244
2.53M
    QUIC_LCID *lcid_obj = NULL;
245
246
2.53M
    if (lcid->id_len > QUIC_MAX_CONN_ID_LEN)
247
0
        return NULL;
248
249
2.53M
    if ((lcid_obj = OPENSSL_zalloc(sizeof(*lcid_obj))) == NULL)
250
0
        goto err;
251
252
2.53M
    lcid_obj->cid = *lcid;
253
2.53M
    lcid_obj->conn = conn;
254
255
2.53M
    lh_QUIC_LCID_insert(conn->lcids, lcid_obj);
256
2.53M
    if (lh_QUIC_LCID_error(conn->lcids))
257
0
        goto err;
258
259
2.53M
    lh_QUIC_LCID_insert(lcidm->lcids, lcid_obj);
260
2.53M
    if (lh_QUIC_LCID_error(lcidm->lcids)) {
261
0
        lh_QUIC_LCID_delete(conn->lcids, lcid_obj);
262
0
        goto err;
263
0
    }
264
265
2.53M
    ++conn->num_active_lcid;
266
2.53M
    return lcid_obj;
267
268
0
err:
269
0
    OPENSSL_free(lcid_obj);
270
0
    return NULL;
271
2.53M
}
272
273
size_t ossl_quic_lcidm_get_lcid_len(const QUIC_LCIDM *lcidm)
274
0
{
275
0
    return lcidm->lcid_len;
276
0
}
277
278
size_t ossl_quic_lcidm_get_num_active_lcid(const QUIC_LCIDM *lcidm,
279
                                           void *opaque)
280
0
{
281
0
    QUIC_LCIDM_CONN *conn;
282
283
0
    conn = lcidm_get0_conn(lcidm, opaque);
284
0
    if (conn == NULL)
285
0
        return 0;
286
287
0
    return conn->num_active_lcid;
288
0
}
289
290
static int lcidm_generate_cid(QUIC_LCIDM *lcidm,
291
                              QUIC_CONN_ID *cid)
292
3.70M
{
293
3.70M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
294
3.70M
    int i;
295
296
3.70M
    lcidm->next_lcid.id_len = (unsigned char)lcidm->lcid_len;
297
3.70M
    *cid = lcidm->next_lcid;
298
299
3.71M
    for (i = lcidm->lcid_len - 1; i >= 0; --i)
300
3.02M
        if (++lcidm->next_lcid.id[i] != 0)
301
3.00M
            break;
302
303
3.70M
    return 1;
304
#else
305
    return ossl_quic_gen_rand_conn_id(lcidm->libctx, lcidm->lcid_len, cid);
306
#endif
307
3.70M
}
308
309
static int lcidm_generate(QUIC_LCIDM *lcidm,
310
                          void *opaque,
311
                          unsigned int type,
312
                          QUIC_CONN_ID *lcid_out,
313
                          uint64_t *seq_num)
314
2.86M
{
315
2.86M
    QUIC_LCIDM_CONN *conn;
316
2.86M
    QUIC_LCID key, *lcid_obj;
317
2.86M
    size_t i;
318
3.84M
#define MAX_RETRIES 8
319
320
2.86M
    if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL)
321
0
        return 0;
322
323
2.86M
    if ((type == LCID_TYPE_INITIAL && conn->next_seq_num > 0)
324
2.86M
        || conn->next_seq_num > OSSL_QUIC_VLINT_MAX)
325
194k
        return 0;
326
327
2.67M
    i = 0;
328
3.84M
    do {
329
3.84M
        if (i++ >= MAX_RETRIES)
330
            /*
331
             * Too many retries; should not happen but if it does, don't loop
332
             * endlessly.
333
             */
334
145k
            return 0;
335
336
3.70M
        if (!lcidm_generate_cid(lcidm, lcid_out))
337
0
            return 0;
338
339
3.70M
        key.cid = *lcid_out;
340
        /* If a collision occurs, retry. */
341
3.70M
    } while (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL);
342
343
2.52M
    if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid_out)) == NULL)
344
0
        return 0;
345
346
2.52M
    lcid_obj->seq_num   = conn->next_seq_num;
347
2.52M
    lcid_obj->type      = type;
348
349
2.52M
    if (seq_num != NULL)
350
2.46M
        *seq_num = lcid_obj->seq_num;
351
352
2.52M
    ++conn->next_seq_num;
353
2.52M
    return 1;
354
2.52M
}
355
356
int ossl_quic_lcidm_enrol_odcid(QUIC_LCIDM *lcidm,
357
                                void *opaque,
358
                                const QUIC_CONN_ID *initial_odcid)
359
764k
{
360
764k
    QUIC_LCIDM_CONN *conn;
361
764k
    QUIC_LCID key, *lcid_obj;
362
363
764k
    if (initial_odcid == NULL || initial_odcid->id_len < QUIC_MIN_ODCID_LEN
364
764k
        || initial_odcid->id_len > QUIC_MAX_CONN_ID_LEN)
365
742k
        return 0;
366
367
21.6k
    if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL)
368
0
        return 0;
369
370
21.6k
    if (conn->done_odcid)
371
9.24k
        return 0;
372
373
12.4k
    key.cid = *initial_odcid;
374
12.4k
    if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL)
375
3.52k
        return 0;
376
377
8.88k
    if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, initial_odcid)) == NULL)
378
0
        return 0;
379
380
8.88k
    lcid_obj->seq_num       = LCIDM_ODCID_SEQ_NUM;
381
8.88k
    lcid_obj->type          = LCID_TYPE_ODCID;
382
383
8.88k
    conn->odcid_lcid_obj    = lcid_obj;
384
8.88k
    conn->done_odcid        = 1;
385
8.88k
    return 1;
386
8.88k
}
387
388
int ossl_quic_lcidm_generate_initial(QUIC_LCIDM *lcidm,
389
                                     void *opaque,
390
                                     QUIC_CONN_ID *initial_lcid)
391
274k
{
392
274k
    return lcidm_generate(lcidm, opaque, LCID_TYPE_INITIAL,
393
274k
                          initial_lcid, NULL);
394
274k
}
395
396
int ossl_quic_lcidm_generate(QUIC_LCIDM *lcidm,
397
                             void *opaque,
398
                             OSSL_QUIC_FRAME_NEW_CONN_ID *ncid_frame)
399
2.59M
{
400
2.59M
    ncid_frame->seq_num         = 0;
401
2.59M
    ncid_frame->retire_prior_to = 0;
402
403
2.59M
    return lcidm_generate(lcidm, opaque, LCID_TYPE_NCID,
404
2.59M
                          &ncid_frame->conn_id,
405
2.59M
                          &ncid_frame->seq_num);
406
2.59M
}
407
408
int ossl_quic_lcidm_retire_odcid(QUIC_LCIDM *lcidm, void *opaque)
409
41.4k
{
410
41.4k
    QUIC_LCIDM_CONN *conn;
411
412
41.4k
    if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL)
413
0
        return 0;
414
415
41.4k
    if (conn->odcid_lcid_obj == NULL)
416
36.9k
        return 0;
417
418
4.50k
    lcidm_delete_conn_lcid(lcidm, conn->odcid_lcid_obj);
419
4.50k
    conn->odcid_lcid_obj = NULL;
420
4.50k
    return 1;
421
41.4k
}
422
423
struct retire_args {
424
    QUIC_LCID           *earliest_seq_num_lcid_obj;
425
    uint64_t            earliest_seq_num, retire_prior_to;
426
};
427
428
static void retire_for_conn(QUIC_LCID *lcid_obj, void *arg)
429
22.0M
{
430
22.0M
    struct retire_args *args = arg;
431
432
    /* ODCID LCID cannot be retired via this API */
433
22.0M
    if (lcid_obj->type == LCID_TYPE_ODCID
434
22.0M
        || lcid_obj->seq_num >= args->retire_prior_to)
435
9.49M
        return;
436
437
12.5M
    if (lcid_obj->seq_num < args->earliest_seq_num) {
438
471k
        args->earliest_seq_num          = lcid_obj->seq_num;
439
471k
        args->earliest_seq_num_lcid_obj = lcid_obj;
440
471k
    }
441
12.5M
}
442
443
int ossl_quic_lcidm_retire(QUIC_LCIDM *lcidm,
444
                           void *opaque,
445
                           uint64_t retire_prior_to,
446
                           const QUIC_CONN_ID *containing_pkt_dcid,
447
                           QUIC_CONN_ID *retired_lcid,
448
                           uint64_t *retired_seq_num,
449
                           int *did_retire)
450
165k
{
451
165k
    QUIC_LCIDM_CONN key, *conn;
452
165k
    struct retire_args args = {0};
453
454
165k
    key.opaque = opaque;
455
456
165k
    if (did_retire == NULL)
457
0
        return 0;
458
459
165k
    *did_retire = 0;
460
165k
    if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL)
461
29.2k
        return 1;
462
463
135k
    args.retire_prior_to    = retire_prior_to;
464
135k
    args.earliest_seq_num   = UINT64_MAX;
465
466
135k
    lh_QUIC_LCID_doall_arg(conn->lcids, retire_for_conn, &args);
467
135k
    if (args.earliest_seq_num_lcid_obj == NULL)
468
102k
        return 1;
469
470
33.5k
    if (containing_pkt_dcid != NULL
471
33.5k
        && ossl_quic_conn_id_eq(&args.earliest_seq_num_lcid_obj->cid,
472
0
                                containing_pkt_dcid))
473
0
        return 0;
474
475
33.5k
    *did_retire = 1;
476
33.5k
    if (retired_lcid != NULL)
477
33.5k
        *retired_lcid = args.earliest_seq_num_lcid_obj->cid;
478
33.5k
    if (retired_seq_num != NULL)
479
33.5k
        *retired_seq_num = args.earliest_seq_num_lcid_obj->seq_num;
480
481
33.5k
    lcidm_delete_conn_lcid(lcidm, args.earliest_seq_num_lcid_obj);
482
33.5k
    return 1;
483
33.5k
}
484
485
int ossl_quic_lcidm_cull(QUIC_LCIDM *lcidm, void *opaque)
486
90.2k
{
487
90.2k
    QUIC_LCIDM_CONN key, *conn;
488
489
90.2k
    key.opaque = opaque;
490
491
90.2k
    if ((conn = lh_QUIC_LCIDM_CONN_retrieve(lcidm->conns, &key)) == NULL)
492
40.3k
        return 0;
493
494
49.8k
    lcidm_delete_conn(lcidm, conn);
495
49.8k
    return 1;
496
90.2k
}
497
498
int ossl_quic_lcidm_lookup(QUIC_LCIDM *lcidm,
499
                           const QUIC_CONN_ID *lcid,
500
                           uint64_t *seq_num,
501
                           void **opaque)
502
2.69M
{
503
2.69M
    QUIC_LCID *lcid_obj;
504
505
2.69M
    if (lcid == NULL)
506
0
        return 0;
507
508
2.69M
    if ((lcid_obj = lcidm_get0_lcid(lcidm, lcid)) == NULL)
509
423k
        return 0;
510
511
2.27M
    if (seq_num != NULL)
512
73.6k
        *seq_num        = lcid_obj->seq_num;
513
514
2.27M
    if (opaque != NULL)
515
2.27M
        *opaque         = lcid_obj->conn->opaque;
516
517
2.27M
    return 1;
518
2.69M
}
519
520
int ossl_quic_lcidm_debug_remove(QUIC_LCIDM *lcidm,
521
                                 const QUIC_CONN_ID *lcid)
522
0
{
523
0
    QUIC_LCID key, *lcid_obj;
524
525
0
    key.cid = *lcid;
526
0
    if ((lcid_obj = lh_QUIC_LCID_retrieve(lcidm->lcids, &key)) == NULL)
527
0
        return 0;
528
529
0
    lcidm_delete_conn_lcid(lcidm, lcid_obj);
530
0
    return 1;
531
0
}
532
533
int ossl_quic_lcidm_debug_add(QUIC_LCIDM *lcidm, void *opaque,
534
                              const QUIC_CONN_ID *lcid,
535
                              uint64_t seq_num)
536
0
{
537
0
    QUIC_LCIDM_CONN *conn;
538
0
    QUIC_LCID key, *lcid_obj;
539
540
0
    if (lcid == NULL || lcid->id_len > QUIC_MAX_CONN_ID_LEN)
541
0
        return 0;
542
543
0
    if ((conn = lcidm_upsert_conn(lcidm, opaque)) == NULL)
544
0
        return 0;
545
546
0
    key.cid = *lcid;
547
0
    if (lh_QUIC_LCID_retrieve(lcidm->lcids, &key) != NULL)
548
0
        return 0;
549
550
0
    if ((lcid_obj = lcidm_conn_new_lcid(lcidm, conn, lcid)) == NULL)
551
0
        return 0;
552
553
0
    lcid_obj->seq_num   = seq_num;
554
0
    lcid_obj->type      = LCID_TYPE_NCID;
555
0
    return 1;
556
0
}