Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-sip-dialog.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026 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 #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 /* Split input data into three URI strings */
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 /* Create UAC dialog */
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 /* Parse and set route header from fuzzer data */
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 /* Create request within dialog */
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 /* Test session management */
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 /* Parse input data as SIP message */
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 /* Setup received message structure */
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 /* Validate required headers */
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 /* Set recvd_param required by PJSIP transport layer */
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 /* Skip if method doesn't create a dialog */
153 if (!pjsip_method_creates_dialog(&msg->line.req.method))
154 return;
155
156 /* Skip if To tag already exists */
157 if (rdata.msg_info.to->tag.slen != 0)
158 return;
159
160 /* Create UAS dialog from incoming request */
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 /* Create response within dialog */
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 /* Test session management */
174 pjsip_dlg_inc_session(dlg, &dialog_test_mod);
175 pjsip_dlg_dec_session(dlg, &dialog_test_mod);
176
177 /* Terminate the transaction to prevent memory leak */
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 /* Process events to cleanup terminated transactions */
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 /* Initialize PJLIB */
198 status = pj_init();
199 if (status != PJ_SUCCESS)
200 return 1;
201
202 /* Initialize memory pool factory */
203 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy,
204 PJSIP_TEST_MEM_SIZE);
205
206 /* Create SIP endpoint */
207 status = pjsip_endpt_create(&caching_pool.factory, "dialog-fuzzer", &endpt);
208 if (status != PJ_SUCCESS)
209 return 1;
210
211 /* Initialize transaction layer */
212 status = pjsip_tsx_layer_init_module(endpt);
213 if (status != PJ_SUCCESS)
214 return 1;
215
216 /* Initialize UA layer */
217 status = pjsip_ua_init_module(endpt, NULL);
218 if (status != PJ_SUCCESS)
219 return 1;
220
221 /* Initialize loop transport */
222 status = pjsip_loop_start(endpt, &loop_transport);
223 if (status != PJ_SUCCESS)
224 return 1;
225
226 /* Register dialog test module */
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 /* Disable logging for performance */
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 /* Create pool for this iteration */
248 pool = pjsip_endpt_create_pool(endpt, "dialog-fuzz", POOL_SIZE, POOL_SIZE);
249 if (!pool)
250 return 0;
251
252 /* Test both UAC and UAS dialog creation */
253 test_uac_dialog(pool, Data, Size);
254 test_uas_dialog(pool, Data, Size);
255
256 /* Release pool */
257 pjsip_endpt_release_pool(endpt, pool);
258
259 /* Final cleanup of any pending events */
260 timeout.sec = 0;
261 timeout.msec = 0;
262 pjsip_endpt_handle_events(endpt, &timeout);
263
264 return 0;
265 }