Coverage Report

Created: 2025-08-26 06:53

/src/libsrtp/fuzzer/fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/* By Guido Vranken <guidovranken@gmail.com> --
2
 * https://guidovranken.wordpress.com/ */
3
4
#include <stdio.h>
5
#include <string.h>
6
#include <stdlib.h>
7
#include <stdbool.h>
8
#include <limits.h>
9
#include "srtp.h"
10
#include "srtp_priv.h"
11
#include "fuzzer.h"
12
#include "mt19937.h"
13
#include "testmem.h"
14
15
/* Global variables */
16
static bool g_no_align = false; /* Can be enabled with --no_align */
17
static bool g_post_init =
18
    false; /* Set to true once past initialization phase */
19
static bool g_write_input = false;
20
21
#ifdef FUZZ_32BIT
22
#include <sys/mman.h>
23
static bool g_no_mmap = false; /* Can be enabled with --no_mmap */
24
static void *g_mmap_allocation =
25
    NULL; /* Keeps current mmap() allocation address */
26
static size_t g_mmap_allocation_size =
27
    0; /* Keeps current mmap() allocation size */
28
#endif
29
30
/* Custom allocator functions */
31
32
static void *fuzz_alloc(const size_t size, const bool do_zero)
33
3.09M
{
34
3.09M
    void *ret = NULL;
35
#ifdef FUZZ_32BIT
36
    bool do_malloc = true;
37
#endif
38
3.09M
    bool do_mmap, mmap_high = true;
39
40
3.09M
    if (size == 0) {
41
932k
        size_t ret;
42
        /* Allocations of size 0 are not illegal, but are a bad practice, since
43
         * writing just a single byte to this region constitutes undefined
44
         * behavior per the C spec. glibc will return a small, valid memory
45
         * region
46
         * whereas OpenBSD will crash upon writing to it.
47
         * Intentionally return a pointer to an invalid page to detect
48
         * unsound code efficiently.
49
         * fuzz_free is aware of this pointer range and will not attempt
50
         * to free()/munmap() it.
51
         */
52
932k
        ret = 0x01 + (fuzz_mt19937_get() % 1024);
53
932k
        return (void *)ret;
54
932k
    }
55
56
    /* Don't do mmap()-based allocations during initialization */
57
2.16M
    if (g_post_init == true) {
58
        /* Even extract these values if --no_mmap is specified.
59
         * This keeps the PRNG output stream consistent across
60
         * fuzzer configurations.
61
         */
62
2.16M
        do_mmap = (fuzz_mt19937_get() % 64) == 0 ? true : false;
63
2.16M
        if (do_mmap == true) {
64
34.7k
            mmap_high = (fuzz_mt19937_get() % 2) == 0 ? true : false;
65
34.7k
        }
66
2.16M
    } else {
67
0
        do_mmap = false;
68
0
    }
69
70
#ifdef FUZZ_32BIT
71
    /* g_mmap_allocation must be NULL because we only support a single
72
     * concurrent mmap allocation at a time
73
     */
74
    if (g_mmap_allocation == NULL && g_no_mmap == false && do_mmap == true) {
75
        void *mmap_address;
76
        if (mmap_high == true) {
77
            mmap_address = (void *)0xFFFF0000;
78
        } else {
79
            mmap_address = (void *)0x00010000;
80
        }
81
        g_mmap_allocation_size = size;
82
83
        ret = mmap(mmap_address, g_mmap_allocation_size, PROT_READ | PROT_WRITE,
84
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
85
86
        if (ret == MAP_FAILED) {
87
            /* That's okay -- just return NULL to the caller */
88
89
            ret = NULL;
90
91
            /* Reset this for the sake of cleanliness */
92
            g_mmap_allocation_size = 0;
93
        }
94
        /* ret not being MAP_FAILED does not mean that ret is the requested
95
         * address (mmap_address). That's okay. We're not going to perform
96
         * a munmap() on it and call malloc() instead. It won't gain us
97
         * anything.
98
         */
99
100
        g_mmap_allocation = ret;
101
        do_malloc = false;
102
    }
103
104
    if (do_malloc == true)
105
#endif
106
2.16M
    {
107
2.16M
        ret = malloc(size);
108
2.16M
    }
109
110
    /* Mimic calloc() if so requested */
111
2.16M
    if (ret != NULL && do_zero) {
112
37.3k
        memset(ret, 0, size);
113
37.3k
    }
114
115
2.16M
    return ret;
116
3.09M
}
117
118
/* Internal allocations by this fuzzer must on one hand (sometimes)
119
 * receive memory from mmap(), but on the other hand these requests for
120
 * memory may not fail. By calling this function, the allocation is
121
 * guaranteed to succeed; it first tries with fuzz_alloc(), which may
122
 * fail if it uses mmap(), and if that is the case, memory is allocated
123
 * via the libc allocator (malloc, calloc) which should always succeed */
124
static void *fuzz_alloc_succeed(const size_t size, const bool do_zero)
125
3.09M
{
126
3.09M
    void *ret = fuzz_alloc(size, do_zero);
127
3.09M
    if (ret == NULL) {
128
0
        if (do_zero == false) {
129
0
            ret = malloc(size);
130
0
        } else {
131
0
            ret = calloc(1, size);
132
0
        }
133
0
    }
134
135
3.09M
    return ret;
136
3.09M
}
137
138
void *fuzz_calloc(const size_t nmemb, const size_t size)
139
0
{
140
    /* We must be past srtp_init() to prevent that that function fails */
141
0
    if (g_post_init == true) {
142
        /* Fail 1 in 64 allocations on average to test whether the library
143
         * can deal with this properly.
144
         */
145
0
        if ((fuzz_mt19937_get() % 64) == 0) {
146
0
            return NULL;
147
0
        }
148
0
    }
149
150
0
    return fuzz_alloc(nmemb * size, true);
151
0
}
152
153
static bool fuzz_is_special_pointer(void *ptr)
154
3.16M
{
155
    /* Special, invalid pointers introduced when code attempted
156
     * to do size = 0 allocations.
157
     */
158
3.16M
    if ((size_t)ptr >= 0x01 && (size_t)ptr < (0x01 + 1024)) {
159
932k
        return true;
160
2.23M
    } else {
161
2.23M
        return false;
162
2.23M
    }
163
3.16M
}
164
165
void fuzz_free(void *ptr)
166
3.16M
{
167
3.16M
    if (fuzz_is_special_pointer(ptr) == true) {
168
932k
        return;
169
932k
    }
170
171
#ifdef FUZZ_32BIT
172
    if (g_post_init == true && ptr != NULL && ptr == g_mmap_allocation) {
173
        if (munmap(g_mmap_allocation, g_mmap_allocation_size) == -1) {
174
            /* Shouldn't happen */
175
            abort();
176
        }
177
        g_mmap_allocation = NULL;
178
    } else
179
#endif
180
2.23M
    {
181
2.23M
        free(ptr);
182
2.23M
    }
183
2.23M
}
184
185
static srtp_err_status_t fuzz_srtp_protect(srtp_t srtp_sender,
186
                                           void *hdr,
187
                                           size_t *len,
188
                                           size_t mki)
189
28.7k
{
190
28.7k
    size_t out_len = *len + SRTP_MAX_TRAILER_LEN;
191
28.7k
    srtp_err_status_t s =
192
28.7k
        srtp_protect(srtp_sender, hdr, *len, hdr, &out_len, mki);
193
28.7k
    *len = out_len;
194
28.7k
    return s;
195
28.7k
}
196
197
static srtp_err_status_t fuzz_srtp_unprotect(srtp_t srtp_sender,
198
                                             void *hdr,
199
                                             size_t *len,
200
                                             size_t mki)
201
223k
{
202
223k
    return srtp_unprotect(srtp_sender, hdr, *len, hdr, len);
203
223k
}
204
205
static srtp_err_status_t fuzz_srtp_protect_rtcp(srtp_t srtp_sender,
206
                                                void *hdr,
207
                                                size_t *len,
208
                                                size_t mki)
209
231k
{
210
231k
    size_t out_len = *len + SRTP_MAX_SRTCP_TRAILER_LEN;
211
231k
    srtp_err_status_t s =
212
231k
        srtp_protect_rtcp(srtp_sender, hdr, *len, hdr, &out_len, mki);
213
231k
    *len = out_len;
214
231k
    return s;
215
231k
}
216
217
static srtp_err_status_t fuzz_srtp_unprotect_rtcp(srtp_t srtp_sender,
218
                                                  void *hdr,
219
                                                  size_t *len,
220
                                                  size_t mki)
221
2.71k
{
222
2.71k
    return srtp_unprotect_rtcp(srtp_sender, hdr, *len, hdr, len);
223
2.71k
}
224
225
/* Get protect length functions */
226
227
static srtp_err_status_t fuzz_srtp_get_protect_length(const srtp_t srtp_ctx,
228
                                                      size_t mki,
229
                                                      size_t *length)
230
28.7k
{
231
28.7k
    return srtp_get_protect_trailer_length(srtp_ctx, mki, length);
232
28.7k
}
233
234
static srtp_err_status_t fuzz_srtp_get_protect_rtcp_length(
235
    const srtp_t srtp_ctx,
236
    size_t mki,
237
    size_t *length)
238
231k
{
239
231k
    return srtp_get_protect_rtcp_trailer_length(srtp_ctx, mki, length);
240
231k
}
241
242
static uint8_t *extract_key(const uint8_t **data,
243
                            size_t *size,
244
                            const size_t key_size)
245
17.1k
{
246
17.1k
    uint8_t *ret;
247
17.1k
    if (*size < key_size) {
248
40
        return NULL;
249
40
    }
250
251
17.1k
    ret = fuzz_alloc_succeed(key_size, false);
252
17.1k
    EXTRACT(ret, *data, *size, key_size);
253
254
17.1k
    return ret;
255
17.1k
}
256
257
static srtp_master_key_t *extract_master_key(const uint8_t **data,
258
                                             size_t *size,
259
                                             const size_t key_size,
260
                                             bool simulate,
261
                                             bool *success)
262
1.68M
{
263
1.68M
    srtp_master_key_t *ret = NULL;
264
1.68M
    uint16_t mki_id_size;
265
266
1.68M
    if (simulate == true) {
267
847k
        *success = false;
268
847k
    }
269
270
1.68M
    EXTRACT_IF(&mki_id_size, *data, *size, sizeof(mki_id_size));
271
272
1.68M
    if (*size < key_size + mki_id_size) {
273
7.52k
        goto end;
274
7.52k
    }
275
276
1.67M
    if (simulate == true) {
277
839k
        *data += key_size + mki_id_size;
278
839k
        *size -= key_size + mki_id_size;
279
839k
        *success = true;
280
839k
        goto end;
281
839k
    }
282
283
839k
    ret = fuzz_alloc_succeed(sizeof(srtp_master_key_t), false);
284
839k
    ret->key = fuzz_alloc_succeed(key_size, false);
285
286
839k
    ret->mki_id = fuzz_alloc_succeed(mki_id_size, false);
287
288
839k
    EXTRACT(ret->key, *data, *size, key_size);
289
839k
    EXTRACT(ret->mki_id, *data, *size, mki_id_size);
290
1.68M
end:
291
1.68M
    return ret;
292
839k
}
293
294
static srtp_master_key_t **extract_master_keys(const uint8_t **data,
295
                                               size_t *size,
296
                                               const size_t key_size,
297
                                               size_t *num_master_keys)
298
12.3k
{
299
12.3k
    const uint8_t *data_orig = *data;
300
12.3k
    size_t size_orig = *size;
301
12.3k
    size_t i = 0;
302
303
12.3k
    srtp_master_key_t **ret = NULL;
304
305
12.3k
    *num_master_keys = 0;
306
307
    /* First pass -- dry run, determine how many keys we want and can extract */
308
852k
    while (1) {
309
852k
        uint8_t do_extract_master_key;
310
852k
        bool success;
311
852k
        if (*size < sizeof(do_extract_master_key)) {
312
351
            goto next;
313
351
        }
314
851k
        EXTRACT(&do_extract_master_key, *data, *size,
315
851k
                sizeof(do_extract_master_key));
316
317
        /* Decide whether to extract another key */
318
851k
        if ((do_extract_master_key % 2) == 0) {
319
4.44k
            break;
320
4.44k
        }
321
322
847k
        extract_master_key(data, size, key_size, true, &success);
323
324
847k
        if (success == false) {
325
7.55k
            break;
326
7.55k
        }
327
328
839k
        (*num_master_keys)++;
329
839k
    }
330
331
12.3k
next:
332
12.3k
    *data = data_orig;
333
12.3k
    *size = size_orig;
334
335
    /* Allocate array of pointers */
336
12.3k
    ret = fuzz_alloc_succeed(*num_master_keys * sizeof(srtp_master_key_t *),
337
12.3k
                             false);
338
339
    /* Second pass -- perform the actual extractions */
340
852k
    for (i = 0; i < *num_master_keys; i++) {
341
839k
        uint8_t do_extract_master_key;
342
839k
        EXTRACT_IF(&do_extract_master_key, *data, *size,
343
839k
                   sizeof(do_extract_master_key));
344
345
839k
        if ((do_extract_master_key % 2) == 0) {
346
0
            break;
347
0
        }
348
349
839k
        ret[i] = extract_master_key(data, size, key_size, false, NULL);
350
351
839k
        if (ret[i] == NULL) {
352
            /* Shouldn't happen */
353
0
            abort();
354
0
        }
355
839k
    }
356
357
12.3k
end:
358
12.3k
    return ret;
359
12.3k
}
360
361
static srtp_policy_t *extract_policy(const uint8_t **data, size_t *size)
362
37.7k
{
363
37.7k
    srtp_policy_t *policy = NULL;
364
37.7k
    struct {
365
37.7k
        uint8_t srtp_crypto_policy_func;
366
37.7k
        size_t window_size;
367
37.7k
        uint8_t allow_repeat_tx;
368
37.7k
        uint8_t ssrc_type;
369
37.7k
        uint32_t ssrc_value;
370
37.7k
        uint8_t num_xtn_hdr;
371
37.7k
        uint8_t do_extract_key;
372
37.7k
        uint8_t do_extract_master_keys;
373
37.7k
    } params;
374
375
37.7k
    EXTRACT_IF(&params, *data, *size, sizeof(params));
376
377
37.3k
    params.srtp_crypto_policy_func %= sizeof(fuzz_srtp_crypto_policies) /
378
37.3k
                                      sizeof(fuzz_srtp_crypto_policies[0]);
379
37.3k
    params.allow_repeat_tx %= 2;
380
37.3k
    params.ssrc_type %=
381
37.3k
        sizeof(fuzz_ssrc_type_map) / sizeof(fuzz_ssrc_type_map[0]);
382
383
37.3k
    policy = fuzz_alloc_succeed(sizeof(*policy), true);
384
385
37.3k
    fuzz_srtp_crypto_policies[params.srtp_crypto_policy_func]
386
37.3k
        .crypto_policy_func(&policy->rtp);
387
37.3k
    fuzz_srtp_crypto_policies[params.srtp_crypto_policy_func]
388
37.3k
        .crypto_policy_func(&policy->rtcp);
389
390
37.3k
    if (policy->rtp.cipher_key_len > MAX_KEY_LEN) {
391
        /* Shouldn't happen */
392
0
        abort();
393
0
    }
394
395
37.3k
    policy->ssrc.type = fuzz_ssrc_type_map[params.ssrc_type].srtp_ssrc_type;
396
37.3k
    policy->ssrc.value = params.ssrc_value;
397
398
37.3k
    if ((params.do_extract_key % 2) == 0) {
399
17.1k
        policy->key = extract_key(data, size, policy->rtp.cipher_key_len);
400
401
17.1k
        if (policy->key == NULL) {
402
40
            fuzz_free(policy);
403
40
            return NULL;
404
40
        }
405
17.1k
    }
406
407
37.3k
    if (params.num_xtn_hdr != 0) {
408
19.2k
        const size_t xtn_hdr_size = params.num_xtn_hdr * sizeof(int);
409
19.2k
        if (*size < xtn_hdr_size) {
410
90
            fuzz_free(policy->key);
411
90
            fuzz_free(policy);
412
90
            return NULL;
413
90
        }
414
19.1k
        policy->enc_xtn_hdr = fuzz_alloc_succeed(xtn_hdr_size, false);
415
19.1k
        EXTRACT(policy->enc_xtn_hdr, *data, *size, xtn_hdr_size);
416
19.1k
        policy->enc_xtn_hdr_count = params.num_xtn_hdr;
417
19.1k
    }
418
419
37.2k
    if ((params.do_extract_master_keys % 2) == 0) {
420
12.3k
        policy->keys = extract_master_keys(
421
12.3k
            data, size, policy->rtp.cipher_key_len, &policy->num_master_keys);
422
12.3k
        if (policy->keys == NULL) {
423
0
            fuzz_free(policy->key);
424
0
            fuzz_free(policy->enc_xtn_hdr);
425
0
            fuzz_free(policy);
426
0
            return NULL;
427
0
        }
428
12.3k
    }
429
430
37.2k
    policy->window_size = params.window_size;
431
37.2k
    policy->allow_repeat_tx = params.allow_repeat_tx;
432
37.2k
    policy->next = NULL;
433
434
37.6k
end:
435
37.6k
    return policy;
436
37.2k
}
437
438
static srtp_policy_t *extract_policies(const uint8_t **data, size_t *size)
439
7.46k
{
440
7.46k
    srtp_policy_t *curpolicy = NULL, *policy_chain = NULL;
441
442
7.46k
    curpolicy = extract_policy(data, size);
443
7.46k
    if (curpolicy == NULL) {
444
287
        return NULL;
445
287
    }
446
447
7.17k
    policy_chain = curpolicy;
448
449
37.2k
    while (1) {
450
37.2k
        uint8_t do_extract_policy;
451
37.2k
        EXTRACT_IF(&do_extract_policy, *data, *size, sizeof(do_extract_policy));
452
453
        /* Decide whether to extract another policy */
454
36.7k
        if ((do_extract_policy % 2) == 0) {
455
6.46k
            break;
456
6.46k
        }
457
458
30.2k
        curpolicy->next = extract_policy(data, size);
459
30.2k
        if (curpolicy->next == NULL) {
460
214
            break;
461
214
        }
462
30.0k
        curpolicy = curpolicy->next;
463
30.0k
    }
464
465
7.17k
end:
466
7.17k
    return policy_chain;
467
7.17k
}
468
469
static uint32_t *extract_remove_stream_ssrc(const uint8_t **data,
470
                                            size_t *size,
471
                                            uint8_t *num_remove_stream)
472
3.12k
{
473
3.12k
    uint32_t *ret = NULL;
474
3.12k
    uint8_t _num_remove_stream;
475
3.12k
    size_t total_size;
476
477
3.12k
    *num_remove_stream = 0;
478
479
3.12k
    EXTRACT_IF(&_num_remove_stream, *data, *size, sizeof(_num_remove_stream));
480
481
2.94k
    if (_num_remove_stream == 0) {
482
1.80k
        goto end;
483
1.80k
    }
484
485
1.13k
    total_size = _num_remove_stream * sizeof(uint32_t);
486
487
1.13k
    if (*size < total_size) {
488
849
        goto end;
489
849
    }
490
491
290
    ret = fuzz_alloc_succeed(total_size, false);
492
290
    EXTRACT(ret, *data, *size, total_size);
493
494
290
    *num_remove_stream = _num_remove_stream;
495
496
3.12k
end:
497
3.12k
    return ret;
498
290
}
499
500
static uint32_t *extract_set_roc(const uint8_t **data,
501
                                 size_t *size,
502
                                 uint8_t *num_set_roc)
503
3.12k
{
504
3.12k
    uint32_t *ret = NULL;
505
3.12k
    uint8_t _num_set_roc;
506
3.12k
    size_t total_size;
507
508
3.12k
    *num_set_roc = 0;
509
3.12k
    EXTRACT_IF(&_num_set_roc, *data, *size, sizeof(_num_set_roc));
510
2.92k
    if (_num_set_roc == 0) {
511
1.60k
        goto end;
512
1.60k
    }
513
514
    /* Tuples of 2 uint32_t's */
515
1.31k
    total_size = _num_set_roc * sizeof(uint32_t) * 2;
516
517
1.31k
    if (*size < total_size) {
518
583
        goto end;
519
583
    }
520
521
731
    ret = fuzz_alloc_succeed(total_size, false);
522
731
    EXTRACT(ret, *data, *size, total_size);
523
524
731
    *num_set_roc = _num_set_roc;
525
526
3.12k
end:
527
3.12k
    return ret;
528
731
}
529
530
static void free_policies(srtp_policy_t *curpolicy)
531
9.55k
{
532
9.55k
    size_t i;
533
46.7k
    while (curpolicy) {
534
37.2k
        srtp_policy_t *next = curpolicy->next;
535
536
37.2k
        fuzz_free(curpolicy->key);
537
538
876k
        for (i = 0; i < curpolicy->num_master_keys; i++) {
539
839k
            fuzz_free(curpolicy->keys[i]->key);
540
839k
            fuzz_free(curpolicy->keys[i]->mki_id);
541
839k
            fuzz_free(curpolicy->keys[i]);
542
839k
        }
543
544
37.2k
        fuzz_free(curpolicy->keys);
545
37.2k
        fuzz_free(curpolicy->enc_xtn_hdr);
546
37.2k
        fuzz_free(curpolicy);
547
548
37.2k
        curpolicy = next;
549
37.2k
    }
550
9.55k
}
551
552
static uint8_t *run_srtp_func(const srtp_t srtp_ctx,
553
                              const uint8_t **data,
554
                              size_t *size)
555
246k
{
556
246k
    uint8_t *ret = NULL;
557
246k
    uint8_t *copy = NULL, *copy_2 = NULL;
558
559
246k
    struct {
560
246k
        uint16_t size;
561
246k
        uint8_t srtp_func;
562
246k
        uint32_t mki;
563
246k
        uint8_t stretch;
564
246k
    } params_1;
565
566
246k
    struct {
567
246k
        uint8_t srtp_func;
568
246k
        uint32_t mki;
569
246k
    } params_2;
570
246k
    size_t ret_size;
571
572
246k
    EXTRACT_IF(&params_1, *data, *size, sizeof(params_1));
573
244k
    params_1.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
574
575
244k
    if (*size < params_1.size) {
576
106
        goto end;
577
106
    }
578
579
    /* Enforce 4 byte alignment */
580
244k
    if (g_no_align == false) {
581
244k
        params_1.size -= params_1.size % 4;
582
244k
    }
583
584
244k
    if (params_1.size == 0) {
585
75
        goto end;
586
75
    }
587
588
244k
    ret_size = params_1.size;
589
244k
    if (srtp_funcs[params_1.srtp_func].protect == true) {
590
        /* Intentionally not initialized to trigger MemorySanitizer, if
591
         * applicable */
592
239k
        size_t alloc_size;
593
594
239k
        if (srtp_funcs[params_1.srtp_func].get_length(
595
239k
                srtp_ctx, params_1.mki, &alloc_size) != srtp_err_status_ok) {
596
1
            goto end;
597
1
        }
598
599
239k
        copy = fuzz_alloc_succeed(ret_size + alloc_size, false);
600
239k
    } else {
601
4.98k
        copy = fuzz_alloc_succeed(ret_size, false);
602
4.98k
    }
603
604
244k
    EXTRACT(copy, *data, *size, params_1.size);
605
606
244k
    if (srtp_funcs[params_1.srtp_func].srtp_func(
607
244k
            srtp_ctx, copy, &ret_size, params_1.mki) != srtp_err_status_ok) {
608
601
        fuzz_free(copy);
609
601
        goto end;
610
601
    }
611
    // fuzz_free(copy);
612
613
243k
    fuzz_testmem(copy, ret_size);
614
615
243k
    ret = copy;
616
617
243k
    EXTRACT_IF(&params_2, *data, *size, sizeof(params_2));
618
242k
    params_2.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
619
620
242k
    if (ret_size == 0) {
621
0
        goto end;
622
0
    }
623
624
242k
    if (srtp_funcs[params_2.srtp_func].protect == true) {
625
        /* Intentionally not initialized to trigger MemorySanitizer, if
626
         * applicable */
627
21.2k
        size_t alloc_size;
628
629
21.2k
        if (srtp_funcs[params_2.srtp_func].get_length(
630
21.2k
                srtp_ctx, params_2.mki, &alloc_size) != srtp_err_status_ok) {
631
0
            goto end;
632
0
        }
633
634
21.2k
        copy_2 = fuzz_alloc_succeed(ret_size + alloc_size, false);
635
221k
    } else {
636
221k
        copy_2 = fuzz_alloc_succeed(ret_size, false);
637
221k
    }
638
639
242k
    memcpy(copy_2, copy, ret_size);
640
242k
    fuzz_free(copy);
641
242k
    copy = copy_2;
642
643
242k
    if (srtp_funcs[params_2.srtp_func].srtp_func(
644
242k
            srtp_ctx, copy, &ret_size, params_2.mki) != srtp_err_status_ok) {
645
338
        fuzz_free(copy);
646
338
        ret = NULL;
647
338
        goto end;
648
338
    }
649
650
242k
    fuzz_testmem(copy, ret_size);
651
652
242k
    ret = copy;
653
654
246k
end:
655
246k
    return ret;
656
242k
}
657
658
void fuzz_srtp_event_handler(srtp_event_data_t *data)
659
229k
{
660
229k
    fuzz_testmem(data, sizeof(srtp_event_data_t));
661
229k
    if (data->session != NULL) {
662
229k
        fuzz_testmem(data->session, sizeof(*data->session));
663
229k
    }
664
229k
}
665
666
static void fuzz_write_input(const uint8_t *data, size_t size)
667
0
{
668
0
    FILE *fp = fopen("input.bin", "wb");
669
670
0
    if (fp == NULL) {
671
        /* Shouldn't happen */
672
0
        abort();
673
0
    }
674
675
0
    if (size != 0 && fwrite(data, size, 1, fp) != 1) {
676
0
        printf("Cannot write\n");
677
        /* Shouldn't happen */
678
0
        abort();
679
0
    }
680
681
0
    fclose(fp);
682
0
}
683
684
int LLVMFuzzerInitialize(int *argc, char ***argv)
685
2
{
686
2
    char **_argv = *argv;
687
2
    int i;
688
2
    bool no_custom_event_handler = false;
689
690
2
    if (srtp_init() != srtp_err_status_ok) {
691
        /* Shouldn't happen */
692
0
        abort();
693
0
    }
694
695
13
    for (i = 0; i < *argc; i++) {
696
11
        if (strcmp("--no_align", _argv[i]) == 0) {
697
0
            g_no_align = true;
698
11
        } else if (strcmp("--no_custom_event_handler", _argv[i]) == 0) {
699
0
            no_custom_event_handler = true;
700
11
        } else if (strcmp("--write_input", _argv[i]) == 0) {
701
0
            g_write_input = true;
702
0
        }
703
#ifdef FUZZ_32BIT
704
        else if (strcmp("--no_mmap", _argv[i]) == 0) {
705
            g_no_mmap = true;
706
        }
707
#endif
708
11
        else if (strncmp("--", _argv[i], 2) == 0) {
709
0
            printf("Invalid argument: %s\n", _argv[i]);
710
0
            exit(0);
711
0
        }
712
11
    }
713
714
2
    if (no_custom_event_handler == false) {
715
2
        if (srtp_install_event_handler(fuzz_srtp_event_handler) !=
716
2
            srtp_err_status_ok) {
717
            /* Shouldn't happen */
718
0
            abort();
719
0
        }
720
2
    }
721
722
    /* Fully initialized -- past this point, simulated allocation failures
723
     * are allowed to occur */
724
2
    g_post_init = true;
725
726
2
    return 0;
727
2
}
728
729
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
730
3.75k
{
731
3.75k
    uint8_t num_remove_stream;
732
3.75k
    uint32_t *remove_stream_ssrc = NULL;
733
3.75k
    uint8_t num_set_roc;
734
3.75k
    uint32_t *set_roc = NULL;
735
3.75k
    srtp_t srtp_ctx = NULL;
736
3.75k
    srtp_policy_t *policy_chain = NULL, *policy_chain_2 = NULL;
737
3.75k
    uint32_t randseed;
738
3.75k
    static bool firstrun = true;
739
740
3.75k
    if (firstrun == true) {
741
        /* TODO version check etc and send it to MSAN */
742
3.75k
    }
743
744
#ifdef FUZZ_32BIT
745
    /* Free the mmap allocation made during the previous iteration, if
746
     * applicable */
747
    fuzz_free(g_mmap_allocation);
748
#endif
749
750
3.75k
    if (g_write_input == true) {
751
0
        fuzz_write_input(data, size);
752
0
    }
753
754
3.75k
    EXTRACT_IF(&randseed, data, size, sizeof(randseed));
755
3.74k
    fuzz_mt19937_init(randseed);
756
3.74k
    srand(randseed);
757
758
    /* policy_chain is used to initialize the srtp context with */
759
3.74k
    if ((policy_chain = extract_policies(&data, &size)) == NULL) {
760
33
        goto end;
761
33
    }
762
    /* policy_chain_2 is used as an argument to srtp_update later on */
763
3.71k
    if ((policy_chain_2 = extract_policies(&data, &size)) == NULL) {
764
254
        goto end;
765
254
    }
766
767
    /* Create context */
768
3.46k
    if (srtp_create(&srtp_ctx, policy_chain) != srtp_err_status_ok) {
769
334
        goto end;
770
334
    }
771
772
    // free_policies(policy_chain);
773
    // policy_chain = NULL;
774
775
    /* Don't check for NULL result -- no extractions is fine */
776
3.12k
    remove_stream_ssrc =
777
3.12k
        extract_remove_stream_ssrc(&data, &size, &num_remove_stream);
778
779
    /* Don't check for NULL result -- no extractions is fine */
780
3.12k
    set_roc = extract_set_roc(&data, &size, &num_set_roc);
781
782
3.12k
    {
783
3.12k
        uint8_t *ret;
784
3.12k
        int i = 0, j = 0;
785
786
246k
        while ((ret = run_srtp_func(srtp_ctx, &data, &size)) != NULL) {
787
243k
            fuzz_free(ret);
788
789
            /* Keep removing streams until the set of SSRCs extracted from the
790
             * fuzzer input is exhausted */
791
243k
            if (i < num_remove_stream) {
792
1.35k
                if (srtp_stream_remove(srtp_ctx, remove_stream_ssrc[i]) !=
793
1.35k
                    srtp_err_status_ok) {
794
97
                    goto end;
795
97
                }
796
1.25k
                i++;
797
1.25k
            }
798
799
            /* Keep setting and getting ROCs until the set of SSRC/ROC tuples
800
             * extracted from the fuzzer input is exhausted */
801
243k
            if (j < num_set_roc * 2) {
802
1.53k
                uint32_t roc;
803
1.53k
                if (srtp_stream_set_roc(srtp_ctx, set_roc[j], set_roc[j + 1]) !=
804
1.53k
                    srtp_err_status_ok) {
805
83
                    goto end;
806
83
                }
807
1.44k
                if (srtp_stream_get_roc(srtp_ctx, set_roc[j + 1], &roc) !=
808
1.44k
                    srtp_err_status_ok) {
809
45
                    goto end;
810
45
                }
811
1.40k
                j += 2;
812
1.40k
            }
813
814
242k
            if (policy_chain_2 != NULL) {
815
                /* TODO srtp_update(srtp_ctx, policy_chain_2); */
816
817
                /* Discard after using once */
818
2.05k
                free_policies(policy_chain_2);
819
2.05k
                policy_chain_2 = NULL;
820
2.05k
            }
821
242k
        }
822
3.12k
    }
823
824
3.75k
end:
825
3.75k
    free_policies(policy_chain);
826
3.75k
    free_policies(policy_chain_2);
827
3.75k
    fuzz_free(remove_stream_ssrc);
828
3.75k
    fuzz_free(set_roc);
829
3.75k
    if (srtp_ctx != NULL) {
830
3.12k
        srtp_dealloc(srtp_ctx);
831
3.12k
    }
832
3.75k
    fuzz_mt19937_destroy();
833
834
3.75k
    return 0;
835
3.12k
}