| 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 <pjsip.h> |
| 25 |
#include <pjsip/sip_multipart.h> |
| 26 |
|
| 27 |
#define kMinInputLength 10 |
| 28 |
#define kMaxInputLength 4096 |
| 29 |
|
| 30 |
pj_pool_factory *mem; |
| 31 |
pjsip_endpoint *endpt; |
| 32 |
static pj_caching_pool caching_pool; |
| 33 |
|
| 34 |
|
| 35 |
int multipart_parse(uint8_t *data, size_t size) { |
| 36 |
pj_pool_t *pool; |
| 37 |
char *msg_buf; |
| 38 |
pjsip_parser_err_report err_list; |
| 39 |
pjsip_msg *msg; |
| 40 |
pjsip_msg_body *body; |
| 41 |
pjsip_msg_body *multipart_body; |
| 42 |
size_t msg_size; |
| 43 |
|
| 44 |
pool = pjsip_endpt_create_pool(endpt, "multipart", 4096, 1024); |
| 45 |
if (!pool) |
| 46 |
return 0; |
| 47 |
|
| 48 |
|
| 49 |
const char *sip_header = |
| 50 |
"INVITE sip:[email protected] SIP/2.0\r\n" |
| 51 |
"From: <sip:[email protected]>\r\n" |
| 52 |
"To: <sip:[email protected]>\r\n" |
| 53 |
"Call-ID: [email protected]\r\n" |
| 54 |
"CSeq: 1 INVITE\r\n" |
| 55 |
"Via: SIP/2.0/UDP example.com\r\n" |
| 56 |
"Content-Type: multipart/mixed ; boundary=\"BOUNDARY123\"\r\n" |
| 57 |
"Content-Length: "; |
| 58 |
|
| 59 |
const char *boundary_start = "--BOUNDARY123\r\n"; |
| 60 |
const char *part_header = "Content-Type: text/plain\r\n\r\n"; |
| 61 |
const char *boundary_end = "\r\n--BOUNDARY123--\r\n"; |
| 62 |
|
| 63 |
size_t header_len = strlen(sip_header); |
| 64 |
size_t boundary_start_len = strlen(boundary_start); |
| 65 |
size_t part_header_len = strlen(part_header); |
| 66 |
size_t boundary_end_len = strlen(boundary_end); |
| 67 |
|
| 68 |
|
| 69 |
size_t body_len = boundary_start_len + part_header_len + size + boundary_end_len; |
| 70 |
|
| 71 |
char cl_buf[20]; |
| 72 |
int cl_len = pj_ansi_snprintf(cl_buf, sizeof(cl_buf), "%zu", body_len); |
| 73 |
|
| 74 |
if (cl_len <= 0 || cl_len >= (int)sizeof(cl_buf)) { |
| 75 |
pj_pool_release(pool); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
msg_size = header_len + cl_len + 4 + body_len; |
| 81 |
|
| 82 |
msg_buf = (char*)pj_pool_calloc(pool, msg_size + 1, 1); |
| 83 |
|
| 84 |
|
| 85 |
size_t pos = 0; |
| 86 |
pj_memcpy(msg_buf + pos, sip_header, header_len); |
| 87 |
pos += header_len; |
| 88 |
pj_memcpy(msg_buf + pos, cl_buf, cl_len); |
| 89 |
pos += cl_len; |
| 90 |
pj_memcpy(msg_buf + pos, "\r\n\r\n", 4); |
| 91 |
pos += 4; |
| 92 |
|
| 93 |
|
| 94 |
pj_memcpy(msg_buf + pos, boundary_start, boundary_start_len); |
| 95 |
pos += boundary_start_len; |
| 96 |
pj_memcpy(msg_buf + pos, part_header, part_header_len); |
| 97 |
pos += part_header_len; |
| 98 |
|
| 99 |
if (size > 0) { |
| 100 |
pj_memcpy(msg_buf + pos, data, size); |
| 101 |
pos += size; |
| 102 |
} |
| 103 |
|
| 104 |
pj_memcpy(msg_buf + pos, boundary_end, boundary_end_len); |
| 105 |
pos += boundary_end_len; |
| 106 |
|
| 107 |
|
| 108 |
pj_list_init(&err_list); |
| 109 |
msg = pjsip_parse_msg(pool, msg_buf, pos, &err_list); |
| 110 |
|
| 111 |
if (!msg || !msg->body) { |
| 112 |
pj_pool_release(pool); |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
body = msg->body; |
| 117 |
|
| 118 |
|
| 119 |
if (body->content_type.type.slen > 0 && |
| 120 |
pj_stricmp2(&body->content_type.type, "multipart") == 0 && |
| 121 |
body->data && body->len > 0) |
| 122 |
{ |
| 123 |
multipart_body = pjsip_multipart_parse(pool, (char *)body->data, |
| 124 |
body->len, &body->content_type, 0); |
| 125 |
|
| 126 |
if (multipart_body) { |
| 127 |
|
| 128 |
pjsip_multipart_part *part = pjsip_multipart_get_first_part(multipart_body); |
| 129 |
while (part) { |
| 130 |
if (part->body && part->body->data && part->body->len > 0) { |
| 131 |
(void)part->body->data; |
| 132 |
} |
| 133 |
part = pjsip_multipart_get_next_part(multipart_body, part); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
pj_pool_release(pool); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
extern int |
| 143 |
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 144 |
{ |
| 145 |
int ret = 0; |
| 146 |
uint8_t *data; |
| 147 |
static pj_bool_t initialized = PJ_FALSE; |
| 148 |
|
| 149 |
if (Size < kMinInputLength || Size > kMaxInputLength) |
| 150 |
return 0; |
| 151 |
|
| 152 |
data = (uint8_t *)calloc((Size+1), sizeof(uint8_t)); |
| 153 |
if (data == NULL) |
| 154 |
return 0; |
| 155 |
memcpy((void *)data, (void *)Data, Size); |
| 156 |
|
| 157 |
|
| 158 |
if (!initialized) { |
| 159 |
pj_log_set_level(0); |
| 160 |
|
| 161 |
if (pj_init() != PJ_SUCCESS) { |
| 162 |
free(data); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0); |
| 167 |
mem = &caching_pool.factory; |
| 168 |
|
| 169 |
|
| 170 |
if (pjsip_endpt_create(&caching_pool.factory, "fuzz", &endpt) != PJ_SUCCESS || !endpt) { |
| 171 |
free(data); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
initialized = PJ_TRUE; |
| 176 |
} |
| 177 |
|
| 178 |
ret = multipart_parse(data, Size); |
| 179 |
|
| 180 |
free(data); |
| 181 |
|
| 182 |
return ret; |
| 183 |
} |