Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-stun.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2023 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
22 #include <pjlib.h>
23 #include <pjlib-util.h>
24
25 #include <pjnath.h>
26
27 #define kMinInputLength 24
28 #define kMaxInputLength 5120
29
30 /* Fixed credentials for consistent authentication testing */
31 #define FIXED_USERNAME "testuser"
32 #define FIXED_PASSWORD "testpass123"
33 #define FIXED_REALM "testrealm.org"
34 #define FIXED_NONCE "1234567890abcdef"
35
36 /* Alternative credentials for testing mismatches */
37 #define ALT_USERNAME "altuser"
38 #define ALT_PASSWORD "altpass456"
39
40 pj_pool_factory *mem;
41
42 /* TURN session callback stubs */
43 static pj_status_t turn_on_send_pkt(pj_turn_session *sess,
44 const pj_uint8_t *pkt,
45 unsigned pkt_len,
46 const pj_sockaddr_t *dst_addr,
47 unsigned dst_addr_len)
48 {
49 PJ_UNUSED_ARG(sess);
50 PJ_UNUSED_ARG(pkt);
51 PJ_UNUSED_ARG(pkt_len);
52 PJ_UNUSED_ARG(dst_addr);
53 PJ_UNUSED_ARG(dst_addr_len);
54 return PJ_SUCCESS;
55 }
56
57 static void turn_on_rx_data(pj_turn_session *sess,
58 void *pkt,
59 unsigned pkt_len,
60 const pj_sockaddr_t *peer_addr,
61 unsigned addr_len)
62 {
63 PJ_UNUSED_ARG(sess);
64 PJ_UNUSED_ARG(pkt);
65 PJ_UNUSED_ARG(pkt_len);
66 PJ_UNUSED_ARG(peer_addr);
67 PJ_UNUSED_ARG(addr_len);
68 }
69
70 static void turn_on_state(pj_turn_session *sess,
71 pj_turn_state_t old_state,
72 pj_turn_state_t new_state)
73 {
74 PJ_UNUSED_ARG(sess);
75 PJ_UNUSED_ARG(old_state);
76 PJ_UNUSED_ARG(new_state);
77 }
78
79 /* ICE session callback stubs */
80 static void ice_on_rx_data(pj_ice_strans *ice_st,
81 unsigned comp_id,
82 void *pkt,
83 pj_size_t size,
84 const pj_sockaddr_t *src_addr,
85 unsigned src_addr_len)
86 {
87 PJ_UNUSED_ARG(ice_st);
88 PJ_UNUSED_ARG(comp_id);
89 PJ_UNUSED_ARG(pkt);
90 PJ_UNUSED_ARG(size);
91 PJ_UNUSED_ARG(src_addr);
92 PJ_UNUSED_ARG(src_addr_len);
93 }
94
95 static void ice_on_ice_complete(pj_ice_strans *ice_st,
96 pj_ice_strans_op op,
97 pj_status_t status)
98 {
99 PJ_UNUSED_ARG(ice_st);
100 PJ_UNUSED_ARG(op);
101 PJ_UNUSED_ARG(status);
102 }
103
104 int stun_parse(uint8_t *data, size_t Size)
105 {
106 pj_pool_t *pool;
107 pj_stun_msg *msg = NULL;
108 pj_stun_msg *response = NULL;
109 pj_stun_auth_cred cred;
110 pj_str_t username, password, realm, nonce;
111 unsigned char *encoded_buf;
112 pj_size_t encoded_len;
113
114 /* Extract all flags at the beginning */
115 uint8_t flag_cred_username = data[0] & 0x01;
116 uint8_t flag_cred_password = data[0] & 0x02;
117 uint8_t flag_msg_type = data[1];
118 uint8_t flag_error_code = data[2] & 0x01;
119 uint8_t flag_unknown_attrs = data[2] & 0x02;
120 uint8_t flag_turn_enabled = data[3] & 0x01;
121 uint8_t flag_ice_enabled = data[3] & 0x04;
122
123 /* Advance data pointer and adjust size */
124 data += 4;
125 Size -= 4;
126
127 /* Use fixed credentials based on extracted flags */
128 if (flag_cred_username) {
129 username = pj_str(ALT_USERNAME);
130 } else {
131 username = pj_str(FIXED_USERNAME);
132 }
133
134 if (flag_cred_password) {
135 password = pj_str(ALT_PASSWORD);
136 } else {
137 password = pj_str(FIXED_PASSWORD);
138 }
139
140 realm = pj_str(FIXED_REALM);
141 nonce = pj_str(FIXED_NONCE);
142
143 pool = pj_pool_create(mem, "stun_fuzz", 4096, 4096, NULL);
144 encoded_buf = pj_pool_alloc(pool, kMaxInputLength);
145
146 /* Path 1: Decode existing STUN message using full remaining data */
147 pj_stun_msg_decode(pool, data, Size,
148 PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET,
149 &msg, NULL, NULL);
150
151 /* Path 2: Authenticate (execute regardless of decode result for coverage) */
152 pj_bzero(&cred, sizeof(cred));
153 cred.type = PJ_STUN_AUTH_CRED_STATIC;
154 cred.data.static_cred.username = username;
155 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
156 cred.data.static_cred.data = password;
157 cred.data.static_cred.realm = realm;
158 cred.data.static_cred.nonce = nonce;
159
160 if (msg) {
161 pj_stun_authenticate_request(data, (unsigned)Size,
162 msg, &cred, pool, NULL, NULL);
163 }
164
165 /* Path 3: Create new STUN messages (different types based on flag) */
166 {
167 int msg_type;
168 int type_selector = flag_msg_type % 8;
169
170 /* Select proper STUN message type (method + class) */
171 if (type_selector < 2) {
172 msg_type = PJ_STUN_BINDING_REQUEST;
173 } else if (type_selector < 4) {
174 msg_type = PJ_STUN_ALLOCATE_REQUEST;
175 } else if (type_selector < 6) {
176 msg_type = PJ_STUN_REFRESH_REQUEST;
177 } else {
178 msg_type = PJ_STUN_BINDING_INDICATION;
179 }
180
181 pj_stun_msg_create(pool, msg_type, PJ_STUN_MAGIC, NULL, &response);
182 }
183
184 /* Path 4: Add various attributes to message using full remaining data */
185 if (response) {
186 pj_sockaddr_in addr;
187 pj_str_t software = pj_str("FuzzTest");
188
189 /* Add USERNAME attribute */
190 pj_stun_msg_add_string_attr(pool, response, PJ_STUN_ATTR_USERNAME, &username);
191
192 /* Add REALM attribute */
193 pj_stun_msg_add_string_attr(pool, response, PJ_STUN_ATTR_REALM, &realm);
194
195 /* Add NONCE attribute */
196 pj_stun_msg_add_string_attr(pool, response, PJ_STUN_ATTR_NONCE, &nonce);
197
198 /* Add SOFTWARE attribute */
199 pj_stun_msg_add_string_attr(pool, response, PJ_STUN_ATTR_SOFTWARE, &software);
200
201 /* Add MAPPED-ADDRESS attribute using data */
202 pj_bzero(&addr, sizeof(addr));
203 addr.sin_family = pj_AF_INET();
204 if (Size >= 6) {
205 pj_memcpy(&addr.sin_addr, data, 4);
206 pj_memcpy(&addr.sin_port, data + 4, 2);
207 }
208 pj_stun_msg_add_sockaddr_attr(pool, response, PJ_STUN_ATTR_MAPPED_ADDR,
209 PJ_FALSE, (pj_sockaddr_t*)&addr, sizeof(addr));
210
211 /* Add XOR-MAPPED-ADDRESS */
212 pj_stun_msg_add_sockaddr_attr(pool, response, PJ_STUN_ATTR_XOR_MAPPED_ADDR,
213 PJ_TRUE, (pj_sockaddr_t*)&addr, sizeof(addr));
214
215 /* Add ERROR-CODE attribute based on flag */
216 if (flag_error_code && Size >= 7) {
217 int err_code = (data[6] % 200) + 400;
218 pj_str_t err_reason = pj_str("Test Error");
219 pj_stun_msg_add_errcode_attr(pool, response, err_code, &err_reason);
220 }
221
222 /* Add UNKNOWN-ATTRIBUTES if decode found unknown attrs */
223 if (msg && flag_unknown_attrs && Size >= 8) {
224 pj_uint16_t unknown_attrs[4] = {0x8001, 0x8002, 0x8003, 0x8004};
225 pj_stun_msg_add_unknown_attr(pool, response,
226 (data[7] % 4) + 1, unknown_attrs);
227 }
228
229 /* Path 5: Add MESSAGE-INTEGRITY and encode with proper key */
230 pj_stun_msg_add_msgint_attr(pool, response);
231
232 /* Create authentication key for MESSAGE-INTEGRITY calculation */
233 pj_str_t key;
234 pj_stun_create_key(pool, &key, &realm, &username,
235 PJ_STUN_PASSWD_PLAIN, &password);
236
237 /* Encode message with MESSAGE-INTEGRITY */
238 pj_stun_msg_encode(response, encoded_buf, kMaxInputLength,
239 0, &key, &encoded_len);
240
241 /* Path 7: Attribute lookup and manipulation */
242 pj_stun_msg_find_attr(response, PJ_STUN_ATTR_USERNAME, 0);
243 pj_stun_msg_find_attr(response, PJ_STUN_ATTR_REALM, 0);
244 pj_stun_msg_find_attr(response, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0);
245 }
246
247 /* Path 8: Create error response (only if msg is a request) */
248 if (msg && PJ_STUN_IS_REQUEST(msg->hdr.type) && Size >= 9) {
249 pj_str_t err_msg = pj_str("Unauthorized");
250 pj_stun_msg_create_response(pool, msg, (data[8] % 200) + 400,
251 &err_msg, &response);
252 }
253
254 /* Path 9: TURN session operations - sendto, packet reception, and allocate requests */
255 if (Size >= 20 && flag_turn_enabled) {
256 pj_turn_session *turn_sess = NULL;
257 pj_turn_session_cb turn_cb;
258 pj_stun_config stun_cfg;
259 pj_ioqueue_t *ioqueue = NULL;
260 pj_timer_heap_t *timer_heap = NULL;
261 pj_status_t status;
262
263 /* Setup STUN config for TURN */
264 pj_stun_config_init(&stun_cfg, mem, 0, ioqueue, timer_heap);
265 status = pj_ioqueue_create(pool, 4, &ioqueue);
266 if (status != PJ_SUCCESS) goto on_turn_cleanup;
267 status = pj_timer_heap_create(pool, 16, &timer_heap);
268 if (status != PJ_SUCCESS) goto on_turn_cleanup;
269 stun_cfg.ioqueue = ioqueue;
270 stun_cfg.timer_heap = timer_heap;
271
272 /* Create TURN session */
273 pj_bzero(&turn_cb, sizeof(turn_cb));
274 turn_cb.on_send_pkt = &turn_on_send_pkt;
275 turn_cb.on_rx_data = &turn_on_rx_data;
276 turn_cb.on_state = &turn_on_state;
277
278 status = pj_turn_session_create(&stun_cfg, "turn_fuzz", pj_AF_INET(),
279 PJ_TURN_TP_UDP, NULL, &turn_cb, 0, NULL, &turn_sess);
280
281 if (status == PJ_SUCCESS && turn_sess) {
282 /* Configure TURN server and credentials */
283 pj_str_t srv_addr_str = pj_str("127.0.0.1");
284 int default_port = 3478;
285 pj_stun_auth_cred cred;
286
287 pj_turn_session_set_server(turn_sess, &srv_addr_str, default_port, NULL);
288
289 pj_bzero(&cred, sizeof(cred));
290 cred.type = PJ_STUN_AUTH_CRED_STATIC;
291 cred.data.static_cred.username = pj_str(FIXED_USERNAME);
292 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
293 cred.data.static_cred.data = pj_str(FIXED_PASSWORD);
294 pj_turn_session_set_credential(turn_sess, &cred);
295
296 /* Test TURN sendto API */
297 if (Size >= 6) {
298 pj_sockaddr_in peer_addr;
299 pj_bzero(&peer_addr, sizeof(peer_addr));
300 peer_addr.sin_family = pj_AF_INET();
301 pj_memcpy(&peer_addr.sin_addr, data, 4);
302 pj_memcpy(&peer_addr.sin_port, data + 4, 2);
303
304 size_t payload_len = Size - 6;
305 if (payload_len > 512) payload_len = 512;
306
307 if (payload_len > 0) {
308 pj_turn_session_sendto(turn_sess, data + 6, payload_len,
309 (pj_sockaddr_t*)&peer_addr, sizeof(peer_addr));
310 }
311 }
312
313 /* Test TURN packet reception API */
314 if (Size >= 40) {
315 pj_turn_session_on_rx_pkt_param prm;
316 pj_sockaddr_in src_addr;
317 size_t pkt_len = Size;
318 if (pkt_len > 1500) pkt_len = 1500;
319
320 pj_bzero(&src_addr, sizeof(src_addr));
321 src_addr.sin_family = pj_AF_INET();
322 src_addr.sin_port = pj_htons(default_port);
323 src_addr.sin_addr.s_addr = pj_htonl(0x7F000001);
324
325 pj_bzero(&prm, sizeof(prm));
326 prm.pkt = data;
327 prm.pkt_len = pkt_len;
328 prm.src_addr = (pj_sockaddr*)&src_addr;
329 prm.src_addr_len = sizeof(src_addr);
330
331 pj_turn_session_on_rx_pkt2(turn_sess, &prm);
332 }
333
334 pj_turn_session_destroy(turn_sess, PJ_SUCCESS);
335 }
336
337 on_turn_cleanup:
338 if (timer_heap) pj_timer_heap_destroy(timer_heap);
339 if (ioqueue) pj_ioqueue_destroy(ioqueue);
340 }
341
342 /* Path 10: ICE session operations - connectivity checks and packet handling */
343 if (Size >= 50 && flag_ice_enabled) {
344 pj_ice_strans *ice_st = NULL;
345 pj_ice_strans_cb ice_cb;
346 pj_ice_strans_cfg ice_cfg;
347 pj_stun_config stun_cfg;
348 pj_ioqueue_t *ioqueue = NULL;
349 pj_timer_heap_t *timer_heap = NULL;
350 pj_status_t status;
351
352 /* Setup infrastructure */
353 status = pj_ioqueue_create(pool, 8, &ioqueue);
354 if (status != PJ_SUCCESS) goto on_ice_cleanup;
355 status = pj_timer_heap_create(pool, 32, &timer_heap);
356 if (status != PJ_SUCCESS) goto on_ice_cleanup;
357 pj_stun_config_init(&stun_cfg, mem, 0, ioqueue, timer_heap);
358
359 /* Create ICE transport */
360 pj_bzero(&ice_cb, sizeof(ice_cb));
361 ice_cb.on_rx_data = &ice_on_rx_data;
362 ice_cb.on_ice_complete = &ice_on_ice_complete;
363
364 pj_ice_strans_cfg_default(&ice_cfg);
365 ice_cfg.stun_cfg = stun_cfg;
366 ice_cfg.af = pj_AF_INET();
367
368 status = pj_ice_strans_create("ice_fuzz", &ice_cfg, 1,
369 NULL, &ice_cb, &ice_st);
370
371 if (status == PJ_SUCCESS && ice_st) {
372 pj_str_t local_ufrag = pj_str(FIXED_USERNAME);
373 pj_str_t local_pwd = pj_str(FIXED_PASSWORD);
374
375 /* Initialize ICE with role based on flag */
376 pj_ice_sess_role role = (data[0] & 0x04) ?
377 PJ_ICE_SESS_ROLE_CONTROLLING :
378 PJ_ICE_SESS_ROLE_CONTROLLED;
379
380 status = pj_ice_strans_init_ice(ice_st, role, &local_ufrag, &local_pwd);
381
382 if (status == PJ_SUCCESS) {
383 /* Query ICE state - exercises state management code */
384 pj_ice_strans_get_state(ice_st);
385 pj_ice_strans_has_sess(ice_st);
386 pj_ice_strans_sess_is_running(ice_st);
387 pj_ice_strans_sess_is_complete(ice_st);
388
389 /* Test candidate enumeration if we have data */
390 if (Size >= 20) {
391 unsigned comp_id = 1;
392 unsigned cand_cnt = 8;
393 pj_ice_sess_cand cands[8];
394 pj_ice_sess_cand def_cand;
395
396 pj_ice_strans_enum_cands(ice_st, comp_id, &cand_cnt, cands);
397 pj_ice_strans_get_def_cand(ice_st, comp_id, &def_cand);
398 }
399 }
400
401 pj_ice_strans_destroy(ice_st);
402 }
403
404 on_ice_cleanup:
405 if (timer_heap) pj_timer_heap_destroy(timer_heap);
406 if (ioqueue) pj_ioqueue_destroy(ioqueue);
407 }
408
409 pj_pool_release(pool);
410 return 0;
411 }
412
413
414 extern int
415 LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
416 {
417 int ret = 0;
418 uint8_t *data;
419 pj_caching_pool caching_pool;
420
421 if (Size < kMinInputLength || Size > kMaxInputLength) {
422 return 1;
423 }
424
425 /* Add NULL byte */
426 data = (uint8_t *)calloc((Size+1), sizeof(uint8_t));
427 memcpy((void *)data, (void *)Data, Size);
428
429 /* init Calls */
430 pj_init();
431 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
432 pj_log_set_level(0);
433
434 mem = &caching_pool.factory;
435
436 /* Call fuzzer */
437 ret = stun_parse(data, Size);
438
439 free(data);
440 pj_caching_pool_destroy(&caching_pool);
441
442 return ret;
443 }