Coverage Report

Created: 2026-12-01 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/fuzzer/fuzzer.c
Line
Count
Source
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
352k
{
34
352k
    void *ret = NULL;
35
#ifdef FUZZ_32BIT
36
    bool do_malloc = true;
37
#endif
38
352k
    bool do_mmap, mmap_high = true;
39
40
352k
    if (size == 0) {
41
0
        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
0
        ret = 0x01 + (fuzz_mt19937_get() % 1024);
53
0
        return (void *)ret;
54
0
    }
55
56
    /* Don't do mmap()-based allocations during initialization */
57
352k
    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
352k
        do_mmap = (fuzz_mt19937_get() % 64) == 0 ? true : false;
63
352k
        if (do_mmap == true) {
64
6.39k
            mmap_high = (fuzz_mt19937_get() % 2) == 0 ? true : false;
65
6.39k
        }
66
352k
    } 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
352k
    {
107
352k
        ret = malloc(size);
108
352k
    }
109
110
    /* Mimic calloc() if so requested */
111
352k
    if (ret != NULL && do_zero) {
112
0
        memset(ret, 0, size);
113
0
    }
114
115
352k
    return ret;
116
352k
}
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
352k
{
126
352k
    void *ret = fuzz_alloc(size, do_zero);
127
352k
    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
352k
    return ret;
136
352k
}
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
360k
{
155
    /* Special, invalid pointers introduced when code attempted
156
     * to do size = 0 allocations.
157
     */
158
360k
    if ((size_t)ptr >= 0x01 && (size_t)ptr < (0x01 + 1024)) {
159
0
        return true;
160
360k
    } else {
161
360k
        return false;
162
360k
    }
163
360k
}
164
165
void fuzz_free(void *ptr)
166
360k
{
167
360k
    if (fuzz_is_special_pointer(ptr) == true) {
168
0
        return;
169
0
    }
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
360k
    {
181
360k
        free(ptr);
182
360k
    }
183
360k
}
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
152k
{
190
152k
    size_t out_len = *len + SRTP_MAX_TRAILER_LEN;
191
152k
    srtp_err_status_t s =
192
152k
        srtp_protect(srtp_sender, hdr, *len, hdr, &out_len, mki);
193
152k
    *len = out_len;
194
152k
    return s;
195
152k
}
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
1.01k
{
202
1.01k
    return srtp_unprotect(srtp_sender, hdr, *len, hdr, len);
203
1.01k
}
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
104k
{
210
104k
    size_t out_len = *len + SRTP_MAX_SRTCP_TRAILER_LEN;
211
104k
    srtp_err_status_t s =
212
104k
        srtp_protect_rtcp(srtp_sender, hdr, *len, hdr, &out_len, mki);
213
104k
    *len = out_len;
214
104k
    return s;
215
104k
}
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
1.54k
{
222
1.54k
    return srtp_unprotect_rtcp(srtp_sender, hdr, *len, hdr, len);
223
1.54k
}
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
152k
{
231
152k
    return srtp_get_protect_trailer_length(srtp_ctx, mki, length);
232
152k
}
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
105k
{
239
105k
    return srtp_get_protect_rtcp_trailer_length(srtp_ctx, mki, length);
240
105k
}
241
242
static uint8_t *extract_key(const uint8_t **data,
243
                            size_t *size,
244
                            const size_t key_size)
245
92.4k
{
246
92.4k
    uint8_t *ret;
247
92.4k
    if (*size < key_size) {
248
62
        return NULL;
249
62
    }
250
251
92.4k
    ret = fuzz_alloc_succeed(key_size, false);
252
92.4k
    EXTRACT(ret, *data, *size, key_size);
253
254
92.4k
    return ret;
255
92.4k
}
256
257
static bool extract_and_add_key(srtp_policy_t policy,
258
                                const uint8_t **data,
259
                                size_t *size,
260
                                size_t key_size,
261
                                size_t salt_size)
262
19.8k
{
263
19.8k
    bool added = false;
264
19.8k
    uint8_t *salt = NULL;
265
19.8k
    uint8_t *key = extract_key(data, size, key_size);
266
19.8k
    if (key == NULL) {
267
37
        return false;
268
37
    }
269
270
19.7k
    salt = extract_key(data, size, salt_size);
271
19.7k
    if (salt == NULL) {
272
25
        goto end;
273
25
    }
274
275
19.7k
    if (srtp_policy_add_key(policy, key, key_size, salt, salt_size, NULL, 0) ==
276
19.7k
        srtp_err_status_ok) {
277
19.7k
        added = true;
278
19.7k
    }
279
280
19.7k
end:
281
19.7k
    fuzz_free(salt);
282
19.7k
    fuzz_free(key);
283
19.7k
    return added;
284
19.7k
}
285
286
static bool extract_and_add_master_key(srtp_policy_t policy,
287
                                       const uint8_t **data,
288
                                       size_t *size,
289
                                       size_t key_size,
290
                                       size_t salt_size)
291
27.6k
{
292
27.6k
    bool added = false;
293
27.6k
    uint8_t *key = NULL;
294
27.6k
    uint8_t *salt = NULL;
295
27.6k
    uint16_t mki_id_size = 0;
296
27.6k
    const uint8_t *mki = NULL;
297
27.6k
    srtp_err_status_t status;
298
299
27.6k
    EXTRACT_IF(&mki_id_size, *data, *size, sizeof(mki_id_size));
300
301
27.5k
    if (*size < key_size + salt_size + mki_id_size) {
302
923
        return false;
303
923
    }
304
305
26.6k
    if (mki_id_size > SRTP_MAX_MKI_LEN) {
306
221
        *data += key_size + salt_size + mki_id_size;
307
221
        *size -= key_size + salt_size + mki_id_size;
308
221
        return false;
309
221
    }
310
311
26.4k
    key = extract_key(data, size, key_size);
312
26.4k
    if (key == NULL) {
313
0
        return false;
314
0
    }
315
26.4k
    salt = extract_key(data, size, salt_size);
316
26.4k
    if (salt == NULL) {
317
0
        goto end;
318
0
    }
319
26.4k
    mki = *data;
320
26.4k
    *data += mki_id_size;
321
26.4k
    *size -= mki_id_size;
322
323
26.4k
    if (mki_id_size > 0) {
324
19.9k
        status = srtp_policy_use_mki(policy, mki_id_size);
325
19.9k
        if (status != srtp_err_status_ok) {
326
0
            goto end;
327
0
        }
328
19.9k
    }
329
330
26.4k
    status = srtp_policy_add_key(policy, key, key_size, salt, salt_size, mki,
331
26.4k
                                 mki_id_size);
332
26.4k
    if (status != srtp_err_status_ok) {
333
6.18k
        goto end;
334
6.18k
    }
335
336
20.2k
    added = true;
337
338
26.4k
end:
339
26.4k
    fuzz_free(salt);
340
26.4k
    fuzz_free(key);
341
26.4k
    return added;
342
20.2k
}
343
344
static srtp_policy_t extract_policy(const uint8_t **data, size_t *size)
345
42.7k
{
346
42.7k
    srtp_policy_t policy = NULL;
347
42.7k
    srtp_profile_t profile;
348
42.7k
    size_t key_size;
349
42.7k
    size_t salt_size;
350
42.7k
    srtp_err_status_t status;
351
42.7k
    struct {
352
42.7k
        uint8_t srtp_profile;
353
42.7k
        size_t window_size;
354
42.7k
        uint8_t allow_repeat_tx;
355
42.7k
        uint8_t ssrc_type;
356
42.7k
        uint32_t ssrc_value;
357
42.7k
        uint8_t num_xtn_hdr;
358
42.7k
        uint8_t do_extract_key;
359
42.7k
        uint8_t do_extract_master_keys;
360
42.7k
        uint32_t initial_roc;
361
42.7k
    } params;
362
363
42.7k
    EXTRACT_IF(&params, *data, *size, sizeof(params));
364
365
42.1k
    params.srtp_profile %=
366
42.1k
        sizeof(fuzz_srtp_profiles) / sizeof(fuzz_srtp_profiles[0]);
367
42.1k
    params.allow_repeat_tx %= 2;
368
42.1k
    params.ssrc_type %=
369
42.1k
        sizeof(fuzz_ssrc_type_map) / sizeof(fuzz_ssrc_type_map[0]);
370
42.1k
    profile = fuzz_srtp_profiles[params.srtp_profile].profile;
371
372
42.1k
    status = srtp_policy_create(&policy);
373
42.1k
    if (status != srtp_err_status_ok || policy == NULL) {
374
0
        return NULL;
375
0
    }
376
377
42.1k
    status = srtp_policy_set_profile(policy, profile);
378
42.1k
    if (status != srtp_err_status_ok) {
379
0
        srtp_policy_destroy(policy);
380
0
        return NULL;
381
0
    }
382
383
42.1k
    key_size = srtp_profile_get_master_key_length(profile);
384
42.1k
    salt_size = srtp_profile_get_master_salt_length(profile);
385
42.1k
    if (key_size + salt_size > SRTP_MAX_KEY_LEN) {
386
        /* Shouldn't happen for a public profile. */
387
0
        abort();
388
0
    }
389
390
42.1k
    status = srtp_policy_set_ssrc(
391
42.1k
        policy, (srtp_ssrc_t){
392
42.1k
                    .type = fuzz_ssrc_type_map[params.ssrc_type].srtp_ssrc_type,
393
42.1k
                    .value = params.ssrc_value,
394
42.1k
                });
395
42.1k
    if (status != srtp_err_status_ok) {
396
133
        srtp_policy_destroy(policy);
397
133
        return NULL;
398
133
    }
399
400
42.0k
    if (profile != srtp_profile_null_null && (params.do_extract_key % 2) == 0) {
401
19.8k
        if (!extract_and_add_key(policy, data, size, key_size, salt_size)) {
402
62
            srtp_policy_destroy(policy);
403
62
            return NULL;
404
62
        }
405
19.8k
    }
406
407
41.9k
    if (params.num_xtn_hdr != 0) {
408
3.37k
        const size_t xtn_hdr_size = params.num_xtn_hdr;
409
3.37k
        size_t copy_size = xtn_hdr_size;
410
3.37k
        if (*size < xtn_hdr_size) {
411
89
            srtp_policy_destroy(policy);
412
89
            return NULL;
413
89
        }
414
3.28k
        if (copy_size > SRTP_MAX_NUM_ENC_HDR_XTND_IDS) {
415
715
            copy_size = SRTP_MAX_NUM_ENC_HDR_XTND_IDS;
416
715
        }
417
23.1k
        for (size_t i = 0; i < copy_size; i++) {
418
19.8k
            (void)srtp_policy_add_enc_hdr_xtnd_id(policy, (*data)[i]);
419
19.8k
        }
420
3.28k
        *data += xtn_hdr_size;
421
3.28k
        *size -= xtn_hdr_size;
422
3.28k
    }
423
424
41.8k
    if (profile != srtp_profile_null_null &&
425
32.0k
        (params.do_extract_master_keys % 2) == 0) {
426
48.3k
        while (1) {
427
48.3k
            uint8_t do_extract_master_key;
428
48.3k
            EXTRACT_IF(&do_extract_master_key, *data, *size,
429
48.3k
                       sizeof(do_extract_master_key));
430
431
48.1k
            if ((do_extract_master_key % 2) == 0) {
432
20.5k
                break;
433
20.5k
            }
434
435
27.6k
            if (!extract_and_add_master_key(policy, data, size, key_size,
436
27.6k
                                            salt_size)) {
437
7.34k
                break;
438
7.34k
            }
439
27.6k
        }
440
28.1k
    }
441
442
41.6k
    status = srtp_policy_set_window_size(policy, params.window_size);
443
41.6k
    if (status != srtp_err_status_ok) {
444
437
        srtp_policy_destroy(policy);
445
437
        return NULL;
446
437
    }
447
41.2k
    status = srtp_policy_set_allow_repeat_tx(policy, params.allow_repeat_tx);
448
41.2k
    if (status != srtp_err_status_ok) {
449
0
        srtp_policy_destroy(policy);
450
0
        return NULL;
451
0
    }
452
41.2k
    if (fuzz_ssrc_type_map[params.ssrc_type].srtp_ssrc_type == ssrc_specific) {
453
13.9k
        status = srtp_policy_set_roc(policy, params.initial_roc);
454
13.9k
        if (status != srtp_err_status_ok) {
455
0
            srtp_policy_destroy(policy);
456
0
            return NULL;
457
0
        }
458
13.9k
    }
459
460
42.0k
end:
461
42.0k
    return policy;
462
41.2k
}
463
464
static void extract_more_policies_to_session(const uint8_t **data,
465
                                             size_t *size,
466
                                             srtp_t srtp_ctx)
467
6.73k
{
468
41.3k
    while (1) {
469
41.3k
        uint8_t do_extract_policy;
470
41.3k
        srtp_policy_t policy = NULL;
471
41.3k
        EXTRACT_IF(&do_extract_policy, *data, *size, sizeof(do_extract_policy));
472
473
        /* Decide whether to extract another policy */
474
40.8k
        if ((do_extract_policy % 2) == 0) {
475
5.86k
            break;
476
5.86k
        }
477
478
34.9k
        policy = extract_policy(data, size);
479
34.9k
        if (policy == NULL) {
480
392
            break;
481
392
        }
482
483
34.5k
        if (srtp_ctx != NULL) {
484
24.6k
            (void)srtp_stream_add(srtp_ctx, policy);
485
24.6k
        }
486
34.5k
        srtp_policy_destroy(policy);
487
34.5k
    }
488
489
6.73k
end:
490
6.73k
    return;
491
6.73k
}
492
493
static uint32_t *extract_remove_stream_ssrc(const uint8_t **data,
494
                                            size_t *size,
495
                                            uint8_t *num_remove_stream)
496
3.06k
{
497
3.06k
    uint32_t *ret = NULL;
498
3.06k
    uint8_t _num_remove_stream;
499
3.06k
    size_t total_size;
500
501
3.06k
    *num_remove_stream = 0;
502
503
3.06k
    EXTRACT_IF(&_num_remove_stream, *data, *size, sizeof(_num_remove_stream));
504
505
2.96k
    if (_num_remove_stream == 0) {
506
2.22k
        goto end;
507
2.22k
    }
508
509
732
    total_size = _num_remove_stream * sizeof(uint32_t);
510
511
732
    if (*size < total_size) {
512
486
        goto end;
513
486
    }
514
515
246
    ret = fuzz_alloc_succeed(total_size, false);
516
246
    EXTRACT(ret, *data, *size, total_size);
517
518
246
    *num_remove_stream = _num_remove_stream;
519
520
3.06k
end:
521
3.06k
    return ret;
522
246
}
523
524
static uint32_t *extract_set_roc(const uint8_t **data,
525
                                 size_t *size,
526
                                 uint8_t *num_set_roc)
527
3.06k
{
528
3.06k
    uint32_t *ret = NULL;
529
3.06k
    uint8_t _num_set_roc;
530
3.06k
    size_t total_size;
531
532
3.06k
    *num_set_roc = 0;
533
3.06k
    EXTRACT_IF(&_num_set_roc, *data, *size, sizeof(_num_set_roc));
534
2.94k
    if (_num_set_roc == 0) {
535
2.06k
        goto end;
536
2.06k
    }
537
538
    /* Tuples of 2 uint32_t's */
539
882
    total_size = _num_set_roc * sizeof(uint32_t) * 2;
540
541
882
    if (*size < total_size) {
542
709
        goto end;
543
709
    }
544
545
173
    ret = fuzz_alloc_succeed(total_size, false);
546
173
    EXTRACT(ret, *data, *size, total_size);
547
548
173
    *num_set_roc = _num_set_roc;
549
550
3.06k
end:
551
3.06k
    return ret;
552
173
}
553
554
static uint8_t *run_srtp_func(const srtp_t srtp_ctx,
555
                              const uint8_t **data,
556
                              size_t *size)
557
132k
{
558
132k
    uint8_t *ret = NULL;
559
132k
    uint8_t *copy = NULL, *copy_2 = NULL;
560
561
132k
    struct {
562
132k
        uint16_t size;
563
132k
        uint8_t srtp_func;
564
132k
        uint32_t mki;
565
132k
        uint8_t stretch;
566
132k
    } params_1;
567
568
132k
    struct {
569
132k
        uint8_t srtp_func;
570
132k
        uint32_t mki;
571
132k
    } params_2;
572
132k
    size_t ret_size;
573
574
132k
    EXTRACT_IF(&params_1, *data, *size, sizeof(params_1));
575
131k
    params_1.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
576
577
131k
    if (*size < params_1.size) {
578
59
        goto end;
579
59
    }
580
581
    /* Enforce 4 byte alignment */
582
131k
    if (g_no_align == false) {
583
131k
        params_1.size -= params_1.size % 4;
584
131k
    }
585
586
131k
    if (params_1.size == 0) {
587
62
        goto end;
588
62
    }
589
590
131k
    ret_size = params_1.size;
591
131k
    if (srtp_funcs[params_1.srtp_func].protect == true) {
592
        /* Intentionally not initialized to trigger MemorySanitizer, if
593
         * applicable */
594
129k
        size_t alloc_size;
595
596
129k
        if (srtp_funcs[params_1.srtp_func].get_length(
597
129k
                srtp_ctx, params_1.mki, &alloc_size) != srtp_err_status_ok) {
598
116
            goto end;
599
116
        }
600
601
129k
        copy = fuzz_alloc_succeed(ret_size + alloc_size, false);
602
129k
    } else {
603
1.61k
        copy = fuzz_alloc_succeed(ret_size, false);
604
1.61k
    }
605
606
130k
    EXTRACT(copy, *data, *size, params_1.size);
607
608
130k
    if (srtp_funcs[params_1.srtp_func].srtp_func(
609
130k
            srtp_ctx, copy, &ret_size, params_1.mki) != srtp_err_status_ok) {
610
878
        fuzz_free(copy);
611
878
        goto end;
612
878
    }
613
    // fuzz_free(copy);
614
615
130k
    fuzz_testmem(copy, ret_size);
616
617
130k
    ret = copy;
618
619
130k
    EXTRACT_IF(&params_2, *data, *size, sizeof(params_2));
620
129k
    params_2.srtp_func %= sizeof(srtp_funcs) / sizeof(srtp_funcs[0]);
621
622
129k
    if (ret_size == 0) {
623
0
        goto end;
624
0
    }
625
626
129k
    if (srtp_funcs[params_2.srtp_func].protect == true) {
627
        /* Intentionally not initialized to trigger MemorySanitizer, if
628
         * applicable */
629
128k
        size_t alloc_size;
630
631
128k
        if (srtp_funcs[params_2.srtp_func].get_length(
632
128k
                srtp_ctx, params_2.mki, &alloc_size) != srtp_err_status_ok) {
633
568
            goto end;
634
568
        }
635
636
127k
        copy_2 = fuzz_alloc_succeed(ret_size + alloc_size, false);
637
127k
    } else {
638
956
        copy_2 = fuzz_alloc_succeed(ret_size, false);
639
956
    }
640
641
128k
    memcpy(copy_2, copy, ret_size);
642
128k
    fuzz_free(copy);
643
128k
    copy = copy_2;
644
645
128k
    if (srtp_funcs[params_2.srtp_func].srtp_func(
646
128k
            srtp_ctx, copy, &ret_size, params_2.mki) != srtp_err_status_ok) {
647
280
        fuzz_free(copy);
648
280
        ret = NULL;
649
280
        goto end;
650
280
    }
651
652
128k
    fuzz_testmem(copy, ret_size);
653
654
128k
    ret = copy;
655
656
132k
end:
657
132k
    return ret;
658
128k
}
659
660
void fuzz_srtp_event_handler(srtp_event_data_t *data)
661
71
{
662
71
    fuzz_testmem(data, sizeof(srtp_event_data_t));
663
71
    if (data->session != NULL) {
664
71
        fuzz_testmem(data->session, sizeof(*data->session));
665
71
    }
666
71
}
667
668
static void fuzz_write_input(const uint8_t *data, size_t size)
669
0
{
670
0
    FILE *fp = fopen("input.bin", "wb");
671
672
0
    if (fp == NULL) {
673
        /* Shouldn't happen */
674
0
        abort();
675
0
    }
676
677
0
    if (size != 0 && fwrite(data, size, 1, fp) != 1) {
678
0
        printf("Cannot write\n");
679
        /* Shouldn't happen */
680
0
        abort();
681
0
    }
682
683
0
    fclose(fp);
684
0
}
685
686
int LLVMFuzzerInitialize(int *argc, char ***argv)
687
2
{
688
2
    char **_argv = *argv;
689
2
    int i;
690
2
    bool no_custom_event_handler = false;
691
692
2
    if (srtp_init() != srtp_err_status_ok) {
693
        /* Shouldn't happen */
694
0
        abort();
695
0
    }
696
697
13
    for (i = 0; i < *argc; i++) {
698
11
        if (strcmp("--no_align", _argv[i]) == 0) {
699
0
            g_no_align = true;
700
11
        } else if (strcmp("--no_custom_event_handler", _argv[i]) == 0) {
701
0
            no_custom_event_handler = true;
702
11
        } else if (strcmp("--write_input", _argv[i]) == 0) {
703
0
            g_write_input = true;
704
0
        }
705
#ifdef FUZZ_32BIT
706
        else if (strcmp("--no_mmap", _argv[i]) == 0) {
707
            g_no_mmap = true;
708
        }
709
#endif
710
11
    }
711
712
2
    if (no_custom_event_handler == false) {
713
2
        if (srtp_install_event_handler(fuzz_srtp_event_handler) !=
714
2
            srtp_err_status_ok) {
715
            /* Shouldn't happen */
716
0
            abort();
717
0
        }
718
2
    }
719
720
    /* Fully initialized -- past this point, simulated allocation failures
721
     * are allowed to occur */
722
2
    g_post_init = true;
723
724
2
    return 0;
725
2
}
726
727
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
728
4.11k
{
729
4.11k
    uint8_t num_remove_stream;
730
4.11k
    uint32_t *remove_stream_ssrc = NULL;
731
4.11k
    uint8_t num_set_roc;
732
4.11k
    uint32_t *set_roc = NULL;
733
4.11k
    srtp_t srtp_ctx = NULL;
734
4.11k
    srtp_policy_t policy = NULL;
735
4.11k
    srtp_policy_t policy_2 = NULL;
736
4.11k
    uint32_t randseed;
737
4.11k
    static bool firstrun = true;
738
739
4.11k
    if (firstrun == true) {
740
        /* TODO version check etc and send it to MSAN */
741
4.11k
    }
742
743
#ifdef FUZZ_32BIT
744
    /* Free the mmap allocation made during the previous iteration, if
745
     * applicable */
746
    fuzz_free(g_mmap_allocation);
747
#endif
748
749
4.11k
    if (g_write_input == true) {
750
0
        fuzz_write_input(data, size);
751
0
    }
752
753
4.11k
    EXTRACT_IF(&randseed, data, size, sizeof(randseed));
754
4.11k
    fuzz_mt19937_init(randseed);
755
4.11k
    srand(randseed);
756
757
    /* policy is used to initialize the srtp context with */
758
4.11k
    if ((policy = extract_policy(&data, &size)) == NULL) {
759
327
        goto end;
760
327
    }
761
762
    /* Create context */
763
3.78k
    if (srtp_create(&srtp_ctx, policy) != srtp_err_status_ok) {
764
111
        goto end;
765
111
    }
766
767
    /* Add additional policies extracted for initial stream setup */
768
3.67k
    extract_more_policies_to_session(&data, &size, srtp_ctx);
769
770
    /* policy_2 is used as an argument for post-create stream additions */
771
3.67k
    if ((policy_2 = extract_policy(&data, &size)) == NULL) {
772
608
        goto end;
773
608
    }
774
775
    /* Consume any additional policies from this second extraction phase */
776
3.06k
    extract_more_policies_to_session(&data, &size, NULL);
777
778
    /* Don't check for NULL result -- no extractions is fine */
779
3.06k
    remove_stream_ssrc =
780
3.06k
        extract_remove_stream_ssrc(&data, &size, &num_remove_stream);
781
782
    /* Don't check for NULL result -- no extractions is fine */
783
3.06k
    set_roc = extract_set_roc(&data, &size, &num_set_roc);
784
785
3.06k
    {
786
3.06k
        uint8_t *ret;
787
3.06k
        int i = 0, j = 0;
788
789
132k
        while ((ret = run_srtp_func(srtp_ctx, &data, &size)) != NULL) {
790
129k
            fuzz_free(ret);
791
792
            /* Keep removing streams until the set of SSRCs extracted from the
793
             * fuzzer input is exhausted */
794
129k
            if (i < num_remove_stream) {
795
913
                if (srtp_stream_remove(srtp_ctx, remove_stream_ssrc[i]) !=
796
913
                    srtp_err_status_ok) {
797
80
                    goto end;
798
80
                }
799
833
                i++;
800
833
            }
801
802
            /* Keep reading ROCs until the set of SSRC/ROC tuples extracted
803
             * from the fuzzer input is exhausted. The ROC itself is applied
804
             * from the policy before the stream exists. */
805
129k
            if (j < num_set_roc * 2) {
806
649
                uint32_t roc;
807
649
                if (srtp_stream_get_roc(srtp_ctx, set_roc[j], &roc) !=
808
649
                    srtp_err_status_ok) {
809
30
                    goto end;
810
30
                }
811
619
                j += 2;
812
619
            }
813
814
129k
            if (policy_2 != NULL) {
815
1.81k
                (void)srtp_stream_add(srtp_ctx, policy_2);
816
817
                /* Discard after using once */
818
1.81k
                srtp_policy_destroy(policy_2);
819
1.81k
                policy_2 = NULL;
820
1.81k
            }
821
129k
        }
822
3.06k
    }
823
824
4.11k
end:
825
4.11k
    srtp_policy_destroy(policy);
826
4.11k
    srtp_policy_destroy(policy_2);
827
4.11k
    fuzz_free(remove_stream_ssrc);
828
4.11k
    fuzz_free(set_roc);
829
4.11k
    if (srtp_ctx != NULL) {
830
3.67k
        srtp_dealloc(srtp_ctx);
831
3.67k
    }
832
4.11k
    fuzz_mt19937_destroy();
833
834
4.11k
    return 0;
835
3.06k
}