Coverage Report

Created: 2025-07-11 06:15

/src/pjsip/tests/fuzz/fuzz-stun.c
Line
Count
Source (jump to first uncovered line)
1
/* 
2
 * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
17
 */
18
#include <stdio.h>
19
#include <stdint.h>
20
#include <stdlib.h>
21
22
#include <pjlib.h>
23
#include <pjlib-util.h>
24
25
#include <pjnath.h>
26
27
27.9k
#define kMinInputLength 10
28
13.9k
#define kMaxInputLength 5120
29
30
pj_pool_factory *mem;
31
32
914
int stun_parse(uint8_t *data,size_t Size) {
33
34
914
    pj_status_t status;
35
914
    pj_pool_t *pool;
36
914
    pj_stun_msg *msg;
37
914
    pj_stun_auth_cred cred;
38
39
914
    const pj_str_t USERNAME = {"A", 1};
40
914
    const pj_str_t PASSWORD = {"A", 1};
41
42
914
    pool = pj_pool_create(mem, "decode_test", 1024, 1024, NULL);
43
44
914
    status = pj_stun_msg_decode(pool, data, Size, PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, &msg, NULL, NULL);
45
914
    if(status != PJ_SUCCESS){
46
340
        goto ret_error;
47
340
    }
48
49
574
    pj_bzero(&cred, sizeof(cred));
50
574
    cred.type = PJ_STUN_AUTH_CRED_STATIC;
51
574
    cred.data.static_cred.username = USERNAME;
52
574
    cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
53
574
    cred.data.static_cred.data = PASSWORD;
54
55
574
    pj_stun_authenticate_request(data, (unsigned)Size, msg, &cred, pool, NULL, NULL);
56
57
574
    pj_pool_release(pool);
58
574
    return status;
59
60
340
ret_error:
61
340
    pj_pool_release(pool);
62
340
    return status;
63
914
}
64
65
66
extern int
67
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
68
13.9k
{
69
70
13.9k
    if (Size < kMinInputLength || Size > kMaxInputLength) {
71
328
        return 1;
72
328
    }
73
74
13.6k
    int ret = 0;
75
13.6k
    uint8_t *data;
76
13.6k
    pj_caching_pool caching_pool;
77
78
    /* Add NULL byte */
79
13.6k
    data = (uint8_t *)calloc((Size+1), sizeof(uint8_t));
80
13.6k
    memcpy((void *)data, (void *)Data, Size);
81
82
    /* init Calls */
83
13.6k
    pj_init();
84
13.6k
    pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0);
85
13.6k
    pj_log_set_level(0);
86
87
13.6k
    mem = &caching_pool.factory;
88
89
    /* Call fuzzer */
90
13.6k
    ret = stun_parse(data, Size);
91
92
13.6k
    free(data);
93
13.6k
    pj_caching_pool_destroy(&caching_pool);
94
95
13.6k
    return ret;
96
13.9k
}