| 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 |
|
| 28 |
pjsip_endpoint *endpt; |
| 29 |
pj_caching_pool caching_pool; |
| 30 |
pjsip_transport *loop_transport; |
| 31 |
|
| 32 |
#define POOL_SIZE 8000 |
| 33 |
#define PJSIP_TEST_MEM_SIZE (2*1024*1024) |
| 34 |
|
| 35 |
|
| 36 |
static pjsip_module tsx_user; |
| 37 |
|
| 38 |
|
| 39 |
static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *e) |
| 40 |
{ |
| 41 |
PJ_UNUSED_ARG(tsx); |
| 42 |
PJ_UNUSED_ARG(e); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
static void test_uas_transaction(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 47 |
{ |
| 48 |
|
| 49 |
char *data_copy = (char*)pj_pool_alloc(pool, size + 1); |
| 50 |
pj_memcpy(data_copy, data, size); |
| 51 |
data_copy[size] = '\0'; |
| 52 |
|
| 53 |
pjsip_parser_err_report err_list; |
| 54 |
pj_list_init(&err_list); |
| 55 |
|
| 56 |
pjsip_msg *msg = pjsip_parse_msg(pool, data_copy, size, &err_list); |
| 57 |
if (!msg || msg->type != PJSIP_REQUEST_MSG) |
| 58 |
return; |
| 59 |
|
| 60 |
|
| 61 |
pjsip_rx_data rdata; |
| 62 |
pj_bzero(&rdata, sizeof(rdata)); |
| 63 |
rdata.msg_info.msg = msg; |
| 64 |
rdata.tp_info.pool = pool; |
| 65 |
rdata.tp_info.transport = loop_transport; |
| 66 |
|
| 67 |
|
| 68 |
rdata.msg_info.via = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL); |
| 69 |
rdata.msg_info.from = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL); |
| 70 |
rdata.msg_info.to = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL); |
| 71 |
rdata.msg_info.cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL); |
| 72 |
|
| 73 |
|
| 74 |
if (!rdata.msg_info.via || !rdata.msg_info.from || !rdata.msg_info.to || !rdata.msg_info.cseq) |
| 75 |
return; |
| 76 |
|
| 77 |
|
| 78 |
pjsip_transaction *tsx = NULL; |
| 79 |
if (pjsip_tsx_create_uas(&tsx_user, &rdata, &tsx) != PJ_SUCCESS || !tsx) |
| 80 |
return; |
| 81 |
|
| 82 |
|
| 83 |
pjsip_tsx_recv_msg(tsx, &rdata); |
| 84 |
|
| 85 |
|
| 86 |
pjsip_tx_data *tdata = NULL; |
| 87 |
if (pjsip_endpt_create_response(endpt, &rdata, 200, NULL, &tdata) == PJ_SUCCESS && tdata) |
| 88 |
pjsip_tx_data_dec_ref(tdata); |
| 89 |
|
| 90 |
|
| 91 |
if (tsx->state != PJSIP_TSX_STATE_TERMINATED) |
| 92 |
pjsip_tsx_terminate_async(tsx, 500); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
static void test_uac_transaction(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 97 |
{ |
| 98 |
if (size < 25) |
| 99 |
return; |
| 100 |
|
| 101 |
|
| 102 |
pj_str_t target = {(char*)data, size > 100 ? 100 : (pj_ssize_t)size}; |
| 103 |
pj_ssize_t from_len = (size > 110) ? 50 : ((size > 10) ? (pj_ssize_t)(size - 10) : 0); |
| 104 |
pj_ssize_t to_len = (size > 115) ? 50 : ((size > 15) ? (pj_ssize_t)(size - 15) : 0); |
| 105 |
pj_str_t from = {(char*)data + 10, from_len}; |
| 106 |
pj_str_t to = {(char*)data + 15, to_len}; |
| 107 |
|
| 108 |
|
| 109 |
pjsip_tx_data *tdata = NULL; |
| 110 |
if (pjsip_endpt_create_request(endpt, &pjsip_options_method, |
| 111 |
&target, &from, &to, |
| 112 |
NULL, NULL, -1, NULL, &tdata) != PJ_SUCCESS || !tdata) |
| 113 |
return; |
| 114 |
|
| 115 |
|
| 116 |
pjsip_transaction *tsx = NULL; |
| 117 |
pj_status_t status = pjsip_tsx_create_uac(&tsx_user, tdata, &tsx); |
| 118 |
|
| 119 |
if (status == PJ_SUCCESS && tsx) { |
| 120 |
if (tsx->state != PJSIP_TSX_STATE_TERMINATED) |
| 121 |
pjsip_tsx_terminate_async(tsx, 408); |
| 122 |
} else { |
| 123 |
pjsip_tx_data_dec_ref(tdata); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static void test_create_request(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 129 |
{ |
| 130 |
if (size < 30) |
| 131 |
return; |
| 132 |
|
| 133 |
|
| 134 |
pj_str_t uri = {(char*)data, size > 100 ? 100 : (pj_ssize_t)size}; |
| 135 |
pj_ssize_t from_len = (size > 110) ? 50 : ((size > 10) ? (pj_ssize_t)(size - 10) : 0); |
| 136 |
pj_ssize_t to_len = (size > 120) ? 50 : ((size > 20) ? (pj_ssize_t)(size - 20) : 0); |
| 137 |
pj_str_t from = {(char*)data + 10, from_len}; |
| 138 |
pj_str_t to = {(char*)data + 20, to_len}; |
| 139 |
|
| 140 |
pjsip_tx_data *tdata = NULL; |
| 141 |
pjsip_endpt_create_request(endpt, &pjsip_options_method, |
| 142 |
&uri, &from, &to, NULL, NULL, -1, NULL, &tdata); |
| 143 |
if (tdata) |
| 144 |
pjsip_tx_data_dec_ref(tdata); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
static void test_create_response(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 149 |
{ |
| 150 |
|
| 151 |
char *data_copy = (char*)pj_pool_alloc(pool, size + 1); |
| 152 |
pj_memcpy(data_copy, data, size); |
| 153 |
data_copy[size] = '\0'; |
| 154 |
|
| 155 |
pjsip_parser_err_report err_list; |
| 156 |
pj_list_init(&err_list); |
| 157 |
|
| 158 |
pjsip_msg *msg = pjsip_parse_msg(pool, data_copy, size, &err_list); |
| 159 |
if (!msg || msg->type != PJSIP_REQUEST_MSG) |
| 160 |
return; |
| 161 |
|
| 162 |
|
| 163 |
pjsip_rx_data rdata; |
| 164 |
pj_bzero(&rdata, sizeof(rdata)); |
| 165 |
rdata.msg_info.msg = msg; |
| 166 |
rdata.tp_info.pool = pool; |
| 167 |
rdata.tp_info.transport = loop_transport; |
| 168 |
|
| 169 |
|
| 170 |
rdata.msg_info.via = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL); |
| 171 |
rdata.msg_info.from = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL); |
| 172 |
rdata.msg_info.to = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL); |
| 173 |
rdata.msg_info.cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL); |
| 174 |
rdata.msg_info.cid = (pjsip_cid_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL); |
| 175 |
|
| 176 |
|
| 177 |
if (!rdata.msg_info.via || !rdata.msg_info.from || !rdata.msg_info.to || |
| 178 |
!rdata.msg_info.cseq || !rdata.msg_info.cid) |
| 179 |
return; |
| 180 |
|
| 181 |
pjsip_tx_data *tdata = NULL; |
| 182 |
if (pjsip_endpt_create_response(endpt, &rdata, 200, NULL, &tdata) == PJ_SUCCESS && tdata) |
| 183 |
pjsip_tx_data_dec_ref(tdata); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
static void test_create_ack_cancel(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 188 |
{ |
| 189 |
if (size < 30) |
| 190 |
return; |
| 191 |
|
| 192 |
|
| 193 |
pj_str_t uri = {(char*)data, size > 100 ? 100 : (pj_ssize_t)size}; |
| 194 |
pj_ssize_t from_len = (size > 110) ? 50 : ((size > 10) ? (pj_ssize_t)(size - 10) : 0); |
| 195 |
pj_ssize_t to_len = (size > 120) ? 50 : ((size > 20) ? (pj_ssize_t)(size - 20) : 0); |
| 196 |
pj_str_t from = {(char*)data + 10, from_len}; |
| 197 |
pj_str_t to = {(char*)data + 20, to_len}; |
| 198 |
|
| 199 |
pjsip_tx_data *invite = NULL; |
| 200 |
if (pjsip_endpt_create_request(endpt, &pjsip_invite_method, |
| 201 |
&uri, &from, &to, |
| 202 |
NULL, NULL, -1, NULL, &invite) != PJ_SUCCESS || !invite) |
| 203 |
return; |
| 204 |
|
| 205 |
|
| 206 |
pjsip_tx_data *cancel_tdata = NULL; |
| 207 |
pjsip_endpt_create_cancel(endpt, invite, &cancel_tdata); |
| 208 |
if (cancel_tdata) |
| 209 |
pjsip_tx_data_dec_ref(cancel_tdata); |
| 210 |
|
| 211 |
pjsip_tx_data_dec_ref(invite); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
static void test_process_route_set(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 216 |
{ |
| 217 |
if (size < 30) |
| 218 |
return; |
| 219 |
|
| 220 |
|
| 221 |
pj_str_t uri = {(char*)data, size > 100 ? 100 : (pj_ssize_t)size}; |
| 222 |
pj_ssize_t from_len = (size > 110) ? 50 : ((size > 10) ? (pj_ssize_t)(size - 10) : 0); |
| 223 |
pj_ssize_t to_len = (size > 120) ? 50 : ((size > 20) ? (pj_ssize_t)(size - 20) : 0); |
| 224 |
pj_str_t from = {(char*)data + 10, from_len}; |
| 225 |
pj_str_t to = {(char*)data + 20, to_len}; |
| 226 |
|
| 227 |
pjsip_tx_data *tdata = NULL; |
| 228 |
if (pjsip_endpt_create_request(endpt, &pjsip_options_method, |
| 229 |
&uri, &from, &to, |
| 230 |
NULL, NULL, -1, NULL, &tdata) != PJ_SUCCESS) |
| 231 |
return; |
| 232 |
|
| 233 |
if (!tdata) |
| 234 |
return; |
| 235 |
|
| 236 |
pjsip_host_info dest_info; |
| 237 |
pj_bzero(&dest_info, sizeof(dest_info)); |
| 238 |
pjsip_process_route_set(tdata, &dest_info); |
| 239 |
|
| 240 |
pjsip_tx_data_dec_ref(tdata); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
extern int |
| 245 |
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 246 |
{ |
| 247 |
|
| 248 |
static int initialized = 0; |
| 249 |
if (!initialized) { |
| 250 |
pj_status_t status; |
| 251 |
pj_log_set_level(0); |
| 252 |
|
| 253 |
status = pj_init(); |
| 254 |
if (status != PJ_SUCCESS) |
| 255 |
return 0; |
| 256 |
|
| 257 |
status = pjlib_util_init(); |
| 258 |
if (status != PJ_SUCCESS) |
| 259 |
return 0; |
| 260 |
|
| 261 |
pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, |
| 262 |
PJSIP_TEST_MEM_SIZE); |
| 263 |
|
| 264 |
status = pjsip_endpt_create(&caching_pool.factory, "fuzz", &endpt); |
| 265 |
if (status != PJ_SUCCESS || !endpt) |
| 266 |
return 0; |
| 267 |
|
| 268 |
status = pjsip_tsx_layer_init_module(endpt); |
| 269 |
if (status != PJ_SUCCESS) |
| 270 |
return 0; |
| 271 |
|
| 272 |
status = pjsip_loop_start(endpt, &loop_transport); |
| 273 |
if (status != PJ_SUCCESS) |
| 274 |
return 0; |
| 275 |
|
| 276 |
tsx_user.name = pj_str("tsx-user"); |
| 277 |
tsx_user.id = -1; |
| 278 |
tsx_user.priority = PJSIP_MOD_PRIORITY_APPLICATION; |
| 279 |
tsx_user.on_tsx_state = &on_tsx_state; |
| 280 |
|
| 281 |
status = pjsip_endpt_register_module(endpt, &tsx_user); |
| 282 |
if (status != PJ_SUCCESS) |
| 283 |
return 0; |
| 284 |
|
| 285 |
initialized = 1; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
pj_pool_t *pool = pjsip_endpt_create_pool(endpt, "fuzz", POOL_SIZE, POOL_SIZE); |
| 290 |
|
| 291 |
|
| 292 |
pj_time_val timeout = {0, 0}; |
| 293 |
|
| 294 |
test_uas_transaction(pool, Data, Size); |
| 295 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 296 |
|
| 297 |
test_uac_transaction(pool, Data, Size); |
| 298 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 299 |
|
| 300 |
test_create_request(pool, Data, Size); |
| 301 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 302 |
|
| 303 |
test_create_response(pool, Data, Size); |
| 304 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 305 |
|
| 306 |
test_create_ack_cancel(pool, Data, Size); |
| 307 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 308 |
|
| 309 |
test_process_route_set(pool, Data, Size); |
| 310 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 311 |
|
| 312 |
|
| 313 |
pjsip_endpt_release_pool(endpt, pool); |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |