Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-sip.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 #include <pjlib.h>
24 #include <pjlib-util.h>
25 #include <pjsip.h>
26 #include <pjsip/sip_types.h>
27 #include <pjsip/sip_multipart.h>
28 #include <pjsip/sip_tel_uri.h>
29 #include <pjsip/sip_auth.h>
30 #include <pjsip/sip_transaction.h>
31 #include <pjsip/sip_dialog.h>
32
33 pjsip_endpoint *endpt;
34 pj_caching_pool caching_pool;
35
36 #define POOL_SIZE 8000
37 #define PJSIP_TEST_MEM_SIZE (2*1024*1024)
38
39 #define kMinInputLength 10
40 #define kMaxInputLength 5120
41
42 /* Transaction user module */
43 static pjsip_module tsx_user_module;
44
45 /* Transaction state callback */
46 static void on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
47 {
48 PJ_UNUSED_ARG(tsx);
49 PJ_UNUSED_ARG(event);
50 }
51
52 /* Standard header types to test */
53 static const pjsip_hdr_e hdr_types[] = {
54 PJSIP_H_AUTHORIZATION,
55 PJSIP_H_PROXY_AUTHORIZATION,
56 PJSIP_H_WWW_AUTHENTICATE,
57 PJSIP_H_PROXY_AUTHENTICATE,
58 PJSIP_H_ROUTE,
59 PJSIP_H_RECORD_ROUTE,
60 PJSIP_H_CONTACT
61 };
62
63 static pjsip_msg* parse_message(pj_pool_t *pool, char *data, size_t size)
64 {
65 pjsip_parser_err_report err_list;
66 pj_list_init(&err_list);
67 return pjsip_parse_msg(pool, data, size, &err_list);
68 }
69
70 /* Multipart body parsing */
71 static void do_test_multipart(pj_pool_t *pool, pjsip_msg *msg)
72 {
73 if (!msg->body)
74 return;
75
76 pjsip_msg_body *body = msg->body;
77
78 if (body->content_type.type.slen > 0 &&
79 pj_stricmp2(&body->content_type.type, "multipart") == 0)
80 {
81 pjsip_msg_body *multipart_body = NULL;
82 if (body->data && body->len > 0) {
83 multipart_body = pjsip_multipart_parse(pool, (char *)body->data,
84 body->len, &body->content_type, 0);
85 if (!multipart_body)
86 return;
87 } else {
88 multipart_body = body;
89 }
90
91 pjsip_multipart_part *part = pjsip_multipart_get_first_part(multipart_body);
92
93 while (part) {
94 pjsip_media_type ctype_app, ctype_text;
95
96 ctype_app.type = pj_str("application");
97 ctype_app.subtype = pj_str("sdp");
98 ctype_text.type = pj_str("text");
99 ctype_text.subtype = pj_str("plain");
100
101 pjsip_multipart_find_part(multipart_body, &ctype_app, NULL);
102 pjsip_multipart_find_part(multipart_body, &ctype_text, NULL);
103
104 part = pjsip_multipart_get_next_part(multipart_body, part);
105 }
106
107 pjsip_media_type search_type;
108 search_type.type = pj_str("application");
109 search_type.subtype = pj_str("sdp");
110 pjsip_multipart_find_part(multipart_body, &search_type, NULL);
111 }
112 }
113
114 /* Tel URI parsing */
115 static void do_test_tel_uri(pj_pool_t *pool, char *data, size_t size)
116 {
117 pj_str_t uri_str;
118 pjsip_uri *uri;
119
120 if (size < 4 || size > 256)
121 return;
122
123 uri_str.ptr = data;
124 uri_str.slen = (pj_ssize_t)size;
125
126 uri = pjsip_parse_uri(pool, uri_str.ptr, uri_str.slen, 0);
127
128 if (uri && size > 20) {
129 pj_str_t uri2_str;
130 pjsip_uri *uri2;
131
132 uri2_str.ptr = data + 10;
133 uri2_str.slen = (pj_ssize_t)(size - 10);
134
135 uri2 = pjsip_parse_uri(pool, uri2_str.ptr, uri2_str.slen, 0);
136 if (uri2) {
137 pjsip_uri_cmp(PJSIP_URI_IN_FROMTO_HDR, uri, uri2);
138 pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI, uri, uri2);
139 }
140 }
141 }
142
143 /* Test UAS transaction creation and message feeding */
144 static void do_test_transaction_layer(pjsip_msg *msg, pjsip_rx_data *rdata)
145 {
146 pj_str_t tsx_key;
147 pjsip_via_hdr *via_hdr;
148
149 if (!msg || msg->type != PJSIP_REQUEST_MSG || !rdata)
150 return;
151
152 /* Verify required headers exist */
153 if (!pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL) ||
154 !pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL))
155 return;
156
157 via_hdr = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL);
158 if (!via_hdr)
159 return;
160
161 /* Skip transaction creation if transport not available */
162 if (!rdata->tp_info.transport)
163 return;
164
165 /* Ensure From/To headers have tags */
166 pjsip_from_hdr *from_hdr = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
167 pjsip_to_hdr *to_hdr = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
168
169 if (from_hdr && pj_stricmp2(&from_hdr->tag, "") == 0)
170 from_hdr->tag = pj_str("from-tag-123");
171
172 if (to_hdr && pj_stricmp2(&to_hdr->tag, "") == 0)
173 to_hdr->tag = pj_str("to-tag-456");
174
175 /* Ensure Via header has valid host/port for response addressing */
176 if (via_hdr->sent_by.host.slen == 0) {
177 via_hdr->sent_by.host = pj_str("127.0.0.1");
178 via_hdr->sent_by.port = 5060;
179 }
180
181 /* Test transaction key generation */
182 if (pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAS,
183 &msg->line.req.method, rdata) == PJ_SUCCESS) {
184 pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE);
185 }
186 }
187
188 /* Test synthetic transaction creation */
189 static void do_test_transaction_synthetic(void)
190 {
191 pj_str_t target_uri_str = pj_str("sip:[email protected]");
192 pj_str_t from_str = pj_str("sip:[email protected]");
193 pj_str_t to_str = pj_str("sip:[email protected]");
194 pj_str_t call_id_str = pj_str("[email protected]");
195 pjsip_tx_data *tdata;
196
197 if (pjsip_endpt_create_request(endpt, &pjsip_invite_method,
198 &target_uri_str, &from_str, &to_str,
199 NULL, &call_id_str, -1, NULL, &tdata) == PJ_SUCCESS) {
200 pjsip_tx_data_dec_ref(tdata);
201 }
202 }
203
204 /* Test UAC transaction response handling */
205 static void do_test_transaction_uac(pjsip_msg *msg, pjsip_rx_data *rdata)
206 {
207 pj_str_t tsx_key;
208
209 if (!msg || msg->type != PJSIP_RESPONSE_MSG || !rdata)
210 return;
211
212 /* Verify CSeq header exists */
213 if (!pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL))
214 return;
215
216 /* Skip if no transport available */
217 if (!rdata->tp_info.transport)
218 return;
219
220 /* Test transaction key generation for responses */
221 if (pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC,
222 &rdata->msg_info.cseq->method, rdata) == PJ_SUCCESS) {
223 pjsip_tsx_layer_find_tsx(&tsx_key, PJ_FALSE);
224 }
225 }
226
227 /* Test authentication client */
228 static void do_test_auth_client(pj_pool_t *pool, pjsip_msg *msg)
229 {
230 pjsip_auth_clt_sess auth_sess;
231 pjsip_hdr *hdr = NULL;
232 pj_status_t status;
233
234 /* Only test with 401/407 responses */
235 if (!msg || msg->type != PJSIP_RESPONSE_MSG)
236 return;
237
238 if (msg->line.status.code != 401 && msg->line.status.code != 407)
239 return;
240
241 /* Initialize auth client session */
242 if (pjsip_auth_clt_init(&auth_sess, endpt, pool, 0) != PJ_SUCCESS)
243 return;
244
245 /* Set up credentials */
246 pjsip_cred_info cred;
247 pj_bzero(&cred, sizeof(cred));
248 cred.realm = pj_str("test");
249 cred.username = pj_str("user");
250 cred.data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
251 cred.data = pj_str("password");
252
253 if (pjsip_auth_clt_set_credentials(&auth_sess, 1, &cred) != PJ_SUCCESS)
254 goto cleanup;
255
256 /* Parse authentication headers */
257 if (msg->line.status.code == 401)
258 hdr = pjsip_msg_find_hdr(msg, PJSIP_H_WWW_AUTHENTICATE, NULL);
259 else
260 hdr = pjsip_msg_find_hdr(msg, PJSIP_H_PROXY_AUTHENTICATE, NULL);
261
262 if (hdr) {
263 /* Create a dummy request to test reinit */
264 pjsip_tx_data *tdata = NULL;
265 pjsip_method method;
266 pj_str_t target_uri = pj_str("sip:server.com");
267 pj_str_t from_uri = pj_str("sip:client@local");
268 pj_str_t to_uri = pj_str("sip:server.com");
269 pj_str_t contact = pj_str("sip:client@local");
270
271 pjsip_method_set(&method, PJSIP_REGISTER_METHOD);
272
273 status = pjsip_endpt_create_request(endpt, &method, &target_uri,
274 &from_uri, &to_uri, &contact,
275 NULL, -1, NULL, &tdata);
276 if (status != PJ_SUCCESS || !tdata)
277 goto cleanup;
278
279 /* Initialize request with auth */
280 pjsip_auth_clt_init_req(&auth_sess, tdata);
281
282 /* Create rx_data from the 401/407 message */
283 pjsip_rx_data rdata;
284 pj_bzero(&rdata, sizeof(rdata));
285 rdata.msg_info.msg = msg;
286 rdata.msg_info.info = NULL;
287 rdata.msg_info.len = 1024;
288
289 /* Try to reinit with the challenge */
290 pjsip_tx_data *new_tdata = NULL;
291 if (pjsip_auth_clt_reinit_req(&auth_sess, &rdata, tdata, &new_tdata) == PJ_SUCCESS && new_tdata) {
292 pjsip_tx_data_dec_ref(new_tdata);
293 }
294
295 pjsip_tx_data_dec_ref(tdata);
296 }
297
298 cleanup:
299 pjsip_auth_clt_deinit(&auth_sess);
300 }
301
302 /* Test dialog creation */
303 static void do_test_dialog(pjsip_msg *msg)
304 {
305 pjsip_dialog *dlg = NULL;
306 pjsip_to_hdr *to;
307 pjsip_from_hdr *from;
308 pj_str_t local_uri = pj_str("sip:[email protected]");
309 pj_str_t remote_uri = pj_str("sip:[email protected]");
310 pj_str_t target = pj_str("sip:[email protected]");
311
312 if (!msg || msg->type != PJSIP_REQUEST_MSG)
313 return;
314
315 if (msg->line.req.method.id != PJSIP_INVITE_METHOD)
316 return;
317
318 /* Get To and From headers */
319 to = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
320 from = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
321 if (!to || !from)
322 return;
323
324 /* Create UAC dialog */
325 if (pjsip_dlg_create_uac(pjsip_ua_instance(), &local_uri, NULL,
326 &remote_uri, &target, &dlg) == PJ_SUCCESS && dlg) {
327 pjsip_dlg_inc_lock(dlg);
328
329 /* Build route set from all Route headers */
330 pjsip_route_hdr route_set;
331 pjsip_route_hdr *route;
332 pj_list_init(&route_set);
333 route = (pjsip_route_hdr*)
334 pjsip_msg_find_hdr(msg, PJSIP_H_ROUTE, NULL);
335 while (route) {
336 pjsip_route_hdr *route_clone = (pjsip_route_hdr*)
337 pjsip_hdr_clone(dlg->pool, route);
338 pj_list_push_back(&route_set, route_clone);
339 route = (pjsip_route_hdr*)
340 pjsip_msg_find_hdr(msg, PJSIP_H_ROUTE,
341 route->next);
342 }
343 if (!pj_list_empty(&route_set)) {
344 pjsip_dlg_set_route_set(dlg, &route_set);
345 }
346
347 pjsip_dlg_dec_lock(dlg);
348 }
349 }
350
351 extern int
352 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
353 {
354 char *DataFx;
355 pj_pool_t *pool;
356 pjsip_msg *msg;
357 static int initialized = 0;
358
359 if (Size < kMinInputLength || Size > kMaxInputLength)
360 return 1;
361
362 DataFx = (char *)calloc((Size+1), sizeof(char));
363 if (DataFx == NULL)
364 return 0;
365 memcpy((void *)DataFx, (void *)Data, Size);
366
367 if (!initialized) {
368 pj_log_set_level(0);
369
370 if (pj_init() != PJ_SUCCESS || pjlib_util_init() != PJ_SUCCESS) {
371 free(DataFx);
372 return 0;
373 }
374
375 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy,
376 PJSIP_TEST_MEM_SIZE);
377
378 if (pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt) != PJ_SUCCESS || !endpt) {
379 free(DataFx);
380 return 0;
381 }
382
383 if (pjsip_tsx_layer_init_module(endpt) != PJ_SUCCESS ||
384 pjsip_loop_start(endpt, NULL) != PJ_SUCCESS) {
385 free(DataFx);
386 return 0;
387 }
388
389 pjsip_ua_init_module(endpt, NULL);
390
391 /* Initialize transaction user module */
392 pj_bzero(&tsx_user_module, sizeof(tsx_user_module));
393 tsx_user_module.name = pj_str("tsx-user");
394 tsx_user_module.id = -1;
395 tsx_user_module.priority = PJSIP_MOD_PRIORITY_APPLICATION;
396 tsx_user_module.on_tsx_state = &on_tsx_state;
397 pjsip_endpt_register_module(endpt, &tsx_user_module);
398
399 do_test_transaction_synthetic();
400
401 initialized = 1;
402 }
403
404 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
405 msg = parse_message(pool, DataFx, Size);
406
407 if (msg) {
408 pjsip_rx_data rdata;
409 pj_sockaddr_in remote_addr;
410 pjsip_transport *fake_transport = NULL;
411 int i;
412
413 do_test_multipart(pool, msg);
414
415 /* Test named headers */
416 const char *hdr_names[] = {
417 "Replaces", "Refer-To", "Refer-Sub", "Subscription-State",
418 "Session-Expires", "Min-SE", "RSeq", "RAck"
419 };
420 for (i = 0; i < (int)(sizeof(hdr_names) / sizeof(hdr_names[0])); i++) {
421 pj_str_t hdr_name = pj_str((char *)hdr_names[i]);
422 pjsip_msg_find_hdr_by_name(msg, &hdr_name, NULL);
423 }
424
425 /* Test standard header types */
426 for (i = 0; i < (int)(sizeof(hdr_types) / sizeof(hdr_types[0])); i++) {
427 pjsip_msg_find_hdr(msg, hdr_types[i], NULL);
428 }
429
430 /* Setup rx_data for transaction testing */
431 pj_bzero(&rdata, sizeof(rdata));
432 rdata.msg_info.msg = msg;
433 rdata.msg_info.len = Size;
434 rdata.msg_info.info = DataFx;
435 rdata.tp_info.pool = pool;
436
437 /* Populate header shortcuts */
438 rdata.msg_info.from = (pjsip_from_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
439 rdata.msg_info.to = (pjsip_to_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
440 rdata.msg_info.via = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL);
441 rdata.msg_info.cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL);
442 rdata.msg_info.cid = (pjsip_cid_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL);
443 rdata.msg_info.max_fwd = (pjsip_max_fwd_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_MAX_FORWARDS, NULL);
444
445 /* Setup transport info */
446 pj_bzero(&remote_addr, sizeof(remote_addr));
447 remote_addr.sin_family = PJ_AF_INET;
448 remote_addr.sin_addr.s_addr = pj_htonl(0x7F000001);
449 remote_addr.sin_port = pj_htons(5060);
450
451 pj_memcpy(&rdata.pkt_info.src_addr, &remote_addr, sizeof(remote_addr));
452 rdata.pkt_info.src_addr_len = sizeof(remote_addr);
453 if (Size < sizeof(rdata.pkt_info.packet)) {
454 pj_memcpy(rdata.pkt_info.packet, DataFx, Size);
455 rdata.pkt_info.len = Size;
456 } else {
457 pj_memcpy(rdata.pkt_info.packet, DataFx, sizeof(rdata.pkt_info.packet));
458 rdata.pkt_info.len = sizeof(rdata.pkt_info.packet);
459 }
460
461 /* Acquire dummy UDP transport */
462 if (pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP,
463 &remote_addr, sizeof(remote_addr),
464 NULL, &fake_transport) == PJ_SUCCESS) {
465 rdata.tp_info.transport = fake_transport;
466 }
467
468 do_test_transaction_layer(msg, &rdata);
469
470 if (msg->type == PJSIP_RESPONSE_MSG) {
471 do_test_transaction_uac(msg, &rdata);
472 }
473
474 do_test_auth_client(pool, msg);
475 do_test_dialog(msg);
476
477 /* Release transport */
478 if (fake_transport) {
479 pjsip_transport_dec_ref(fake_transport);
480 }
481 }
482
483 do_test_tel_uri(pool, DataFx, Size);
484
485 pjsip_endpt_release_pool(endpt, pool);
486 free(DataFx);
487
488 return 0;
489 }
490