| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdint.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <pjlib.h> |
| 24 |
#include <pjlib-util.h> |
| 25 |
#include <pjsip.h> |
| 26 |
|
| 27 |
pjsip_endpoint *endpt; |
| 28 |
pj_caching_pool caching_pool; |
| 29 |
pjsip_transport *loop_transport; |
| 30 |
|
| 31 |
#define POOL_SIZE 8000 |
| 32 |
#define PJSIP_TEST_MEM_SIZE (2*1024*1024) |
| 33 |
|
| 34 |
static pjsip_module dialog_test_mod; |
| 35 |
|
| 36 |
static char* alloc_str_from_data(pj_pool_t *pool, const uint8_t *data, |
| 37 |
size_t offset, size_t len) |
| 38 |
{ |
| 39 |
char *buf = (char*)pj_pool_alloc(pool, len + 1); |
| 40 |
pj_memcpy(buf, data + offset, len); |
| 41 |
buf[len] = '\0'; |
| 42 |
return buf; |
| 43 |
} |
| 44 |
|
| 45 |
static void test_uac_dialog(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 46 |
{ |
| 47 |
pj_ssize_t len1, len2, len3; |
| 48 |
pj_str_t local_uri, remote_uri, target; |
| 49 |
pjsip_dialog *dlg; |
| 50 |
pj_status_t status; |
| 51 |
pjsip_tx_data *tdata; |
| 52 |
char *route_data; |
| 53 |
pj_str_t route_name; |
| 54 |
pjsip_hdr *hdr; |
| 55 |
pjsip_route_hdr route_set; |
| 56 |
pjsip_route_hdr *route_hdr; |
| 57 |
|
| 58 |
if (size < 3) |
| 59 |
return; |
| 60 |
|
| 61 |
|
| 62 |
len1 = (size / 3 > 50) ? 50 : size / 3; |
| 63 |
len2 = (size / 3 > 50) ? 50 : size / 3; |
| 64 |
len3 = (size - len1 - len2 > 50) ? 50 : size - len1 - len2; |
| 65 |
|
| 66 |
local_uri.ptr = alloc_str_from_data(pool, data, 0, len1); |
| 67 |
local_uri.slen = len1; |
| 68 |
remote_uri.ptr = alloc_str_from_data(pool, data, len1, len2); |
| 69 |
remote_uri.slen = len2; |
| 70 |
target.ptr = alloc_str_from_data(pool, data, len1 + len2, len3); |
| 71 |
target.slen = len3; |
| 72 |
|
| 73 |
|
| 74 |
dlg = NULL; |
| 75 |
status = pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, |
| 76 |
NULL, &remote_uri, &target, &dlg); |
| 77 |
if (status != PJ_SUCCESS || !dlg) |
| 78 |
return; |
| 79 |
|
| 80 |
pjsip_dlg_inc_lock(dlg); |
| 81 |
|
| 82 |
|
| 83 |
if (size > 150) { |
| 84 |
route_data = alloc_str_from_data(pool, data, 150, size - 150); |
| 85 |
route_name = pj_str("Route"); |
| 86 |
hdr = (pjsip_hdr*)pjsip_parse_hdr(pool, &route_name, |
| 87 |
route_data, size - 150, NULL); |
| 88 |
if (hdr && hdr->type == PJSIP_H_ROUTE) { |
| 89 |
route_hdr = (pjsip_route_hdr*)hdr; |
| 90 |
pj_list_init(&route_set); |
| 91 |
pj_list_push_back(&route_set, route_hdr); |
| 92 |
pjsip_dlg_set_route_set(dlg, &route_set); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
tdata = NULL; |
| 98 |
status = pjsip_dlg_create_request(dlg, &pjsip_options_method, -1, &tdata); |
| 99 |
if (status == PJ_SUCCESS && tdata) |
| 100 |
pjsip_tx_data_dec_ref(tdata); |
| 101 |
|
| 102 |
|
| 103 |
pjsip_dlg_inc_session(dlg, &dialog_test_mod); |
| 104 |
pjsip_dlg_dec_session(dlg, &dialog_test_mod); |
| 105 |
|
| 106 |
pjsip_dlg_dec_lock(dlg); |
| 107 |
} |
| 108 |
|
| 109 |
static void test_uas_dialog(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 110 |
{ |
| 111 |
char *data_copy; |
| 112 |
pjsip_parser_err_report err_list; |
| 113 |
pjsip_msg *msg; |
| 114 |
pjsip_rx_data rdata; |
| 115 |
pjsip_dialog *dlg; |
| 116 |
pj_status_t status; |
| 117 |
pjsip_tx_data *tdata; |
| 118 |
pjsip_transaction *tsx; |
| 119 |
pj_time_val timeout; |
| 120 |
|
| 121 |
if (size < 1) |
| 122 |
return; |
| 123 |
|
| 124 |
|
| 125 |
data_copy = alloc_str_from_data(pool, data, 0, size); |
| 126 |
pj_list_init(&err_list); |
| 127 |
|
| 128 |
msg = pjsip_parse_msg(pool, data_copy, size, &err_list); |
| 129 |
if (!msg || msg->type != PJSIP_REQUEST_MSG) |
| 130 |
return; |
| 131 |
|
| 132 |
|
| 133 |
pj_bzero(&rdata, sizeof(rdata)); |
| 134 |
rdata.msg_info.msg = msg; |
| 135 |
rdata.tp_info.pool = pool; |
| 136 |
rdata.tp_info.transport = loop_transport; |
| 137 |
rdata.msg_info.via = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL); |
| 138 |
rdata.msg_info.from = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL); |
| 139 |
rdata.msg_info.to = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL); |
| 140 |
rdata.msg_info.cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL); |
| 141 |
rdata.msg_info.cid = (pjsip_cid_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL); |
| 142 |
|
| 143 |
|
| 144 |
if (!rdata.msg_info.via || !rdata.msg_info.from || !rdata.msg_info.to || |
| 145 |
!rdata.msg_info.cseq || !rdata.msg_info.cid) |
| 146 |
return; |
| 147 |
|
| 148 |
|
| 149 |
if (rdata.msg_info.via->recvd_param.slen == 0) |
| 150 |
rdata.msg_info.via->recvd_param = pj_str("127.0.0.1"); |
| 151 |
|
| 152 |
|
| 153 |
if (!pjsip_method_creates_dialog(&msg->line.req.method)) |
| 154 |
return; |
| 155 |
|
| 156 |
|
| 157 |
if (rdata.msg_info.to->tag.slen != 0) |
| 158 |
return; |
| 159 |
|
| 160 |
|
| 161 |
dlg = NULL; |
| 162 |
status = pjsip_dlg_create_uas_and_inc_lock(pjsip_ua_instance(), |
| 163 |
&rdata, NULL, &dlg); |
| 164 |
if (status != PJ_SUCCESS || !dlg) |
| 165 |
return; |
| 166 |
|
| 167 |
|
| 168 |
tdata = NULL; |
| 169 |
status = pjsip_dlg_create_response(dlg, &rdata, 200, NULL, &tdata); |
| 170 |
if (status == PJ_SUCCESS && tdata) |
| 171 |
pjsip_tx_data_dec_ref(tdata); |
| 172 |
|
| 173 |
|
| 174 |
pjsip_dlg_inc_session(dlg, &dialog_test_mod); |
| 175 |
pjsip_dlg_dec_session(dlg, &dialog_test_mod); |
| 176 |
|
| 177 |
|
| 178 |
tsx = pjsip_rdata_get_tsx(&rdata); |
| 179 |
if (tsx && tsx->state != PJSIP_TSX_STATE_TERMINATED) |
| 180 |
pjsip_tsx_terminate(tsx, 500); |
| 181 |
|
| 182 |
pjsip_dlg_dec_lock(dlg); |
| 183 |
|
| 184 |
|
| 185 |
timeout.sec = 0; |
| 186 |
timeout.msec = 0; |
| 187 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 188 |
} |
| 189 |
|
| 190 |
int LLVMFuzzerInitialize(int *argc, char ***argv) |
| 191 |
{ |
| 192 |
pj_status_t status; |
| 193 |
|
| 194 |
PJ_UNUSED_ARG(argc); |
| 195 |
PJ_UNUSED_ARG(argv); |
| 196 |
|
| 197 |
|
| 198 |
status = pj_init(); |
| 199 |
if (status != PJ_SUCCESS) |
| 200 |
return 1; |
| 201 |
|
| 202 |
|
| 203 |
pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, |
| 204 |
PJSIP_TEST_MEM_SIZE); |
| 205 |
|
| 206 |
|
| 207 |
status = pjsip_endpt_create(&caching_pool.factory, "dialog-fuzzer", &endpt); |
| 208 |
if (status != PJ_SUCCESS) |
| 209 |
return 1; |
| 210 |
|
| 211 |
|
| 212 |
status = pjsip_tsx_layer_init_module(endpt); |
| 213 |
if (status != PJ_SUCCESS) |
| 214 |
return 1; |
| 215 |
|
| 216 |
|
| 217 |
status = pjsip_ua_init_module(endpt, NULL); |
| 218 |
if (status != PJ_SUCCESS) |
| 219 |
return 1; |
| 220 |
|
| 221 |
|
| 222 |
status = pjsip_loop_start(endpt, &loop_transport); |
| 223 |
if (status != PJ_SUCCESS) |
| 224 |
return 1; |
| 225 |
|
| 226 |
|
| 227 |
pj_bzero(&dialog_test_mod, sizeof(dialog_test_mod)); |
| 228 |
dialog_test_mod.name = pj_str("dialog-test-mod"); |
| 229 |
dialog_test_mod.id = -1; |
| 230 |
dialog_test_mod.priority = PJSIP_MOD_PRIORITY_APPLICATION; |
| 231 |
|
| 232 |
status = pjsip_endpt_register_module(endpt, &dialog_test_mod); |
| 233 |
if (status != PJ_SUCCESS) |
| 234 |
return 1; |
| 235 |
|
| 236 |
|
| 237 |
pj_log_set_level(0); |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 243 |
{ |
| 244 |
pj_pool_t *pool; |
| 245 |
pj_time_val timeout; |
| 246 |
|
| 247 |
|
| 248 |
pool = pjsip_endpt_create_pool(endpt, "dialog-fuzz", POOL_SIZE, POOL_SIZE); |
| 249 |
if (!pool) |
| 250 |
return 0; |
| 251 |
|
| 252 |
|
| 253 |
test_uac_dialog(pool, Data, Size); |
| 254 |
test_uas_dialog(pool, Data, Size); |
| 255 |
|
| 256 |
|
| 257 |
pjsip_endpt_release_pool(endpt, pool); |
| 258 |
|
| 259 |
|
| 260 |
timeout.sec = 0; |
| 261 |
timeout.msec = 0; |
| 262 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |