Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-sip-transaction.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 /* Global PJSIP endpoint and memory pool */
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 /* Transaction user module for receiving transaction state callbacks */
36 static pjsip_module tsx_user;
37
38 /* Callback for transaction state changes (no-op for fuzzing) */
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 /* Test server-side (UAS) transaction creation and message handling */
46 static void test_uas_transaction(pj_pool_t *pool, const uint8_t *data, size_t size)
47 {
48 /* Null-terminate input for parser (required by pjsip_parse_msg API) */
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 /* Setup received message data structure before creating transaction */
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 /* Populate message info fields from parsed message headers */
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 /* Skip if required headers are missing */
74 if (!rdata.msg_info.via || !rdata.msg_info.from || !rdata.msg_info.to || !rdata.msg_info.cseq)
75 return;
76
77 /* Create UAS transaction with rdata */
78 pjsip_transaction *tsx = NULL;
79 if (pjsip_tsx_create_uas(&tsx_user, &rdata, &tsx) != PJ_SUCCESS || !tsx)
80 return;
81
82 /* Feed parsed message into transaction */
83 pjsip_tsx_recv_msg(tsx, &rdata);
84
85 /* Create response */
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 /* Force transaction termination to prevent leaks */
91 if (tsx->state != PJSIP_TSX_STATE_TERMINATED)
92 pjsip_tsx_terminate_async(tsx, 500);
93 }
94
95 /* Test client-side (UAC) transaction creation and sending */
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 /* Extract fuzzer input as SIP URIs */
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 /* Create outgoing request message */
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 /* Create UAC transaction */
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 /* Test sip_util.c request creation functions */
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 /* Extract URIs from fuzzer input */
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 /* Test sip_util.c response creation functions */
148 static void test_create_response(pj_pool_t *pool, const uint8_t *data, size_t size)
149 {
150 /* Null-terminate input for parser */
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 /* Initialize rdata with parsed message and required fields */
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 /* Populate message info fields from parsed message headers */
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 /* Skip if required headers are missing */
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 /* Test ACK and CANCEL message generation */
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 /* Extract URIs, ensuring no buffer over-read */
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 /* Create CANCEL request from INVITE */
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 /* Test route set processing in sip_util.c */
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 /* Extract URIs from fuzzer input */
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 /* Main fuzzer entry point */
244 extern int
245 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
246 {
247 /* One-time initialization of PJSIP stack */
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 /* Create per-iteration memory pool for stateless fuzzing */
289 pj_pool_t *pool = pjsip_endpt_create_pool(endpt, "fuzz", POOL_SIZE, POOL_SIZE);
290
291 /* Run all tests with same input, processing pending events between tests for cleanup */
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 /* Release pool to ensure stateless operation */
313 pjsip_endpt_release_pool(endpt, pool);
314
315 return 0;
316 }