Skip to content

Commit dd5fec9

Browse files
htejedaKaian
authored andcommitted
Secure handling of content-length and warning headers
This patch enhances the security of SIP message processing by introducing bounds checking for the content-length and warning headers.
1 parent f3f8ed8 commit dd5fec9

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/sip.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ sip_validate_packet(packet_t *packet)
280280
uint32_t plen = packet_payloadlen(packet);
281281
u_char payload[MAX_SIP_PAYLOAD];
282282
regmatch_t pmatch[4];
283-
char cl_header[10];
283+
char cl_header[MAX_CONTENT_LENGTH_SIZE];
284284
int content_len;
285285
int bodylen;
286286

@@ -307,7 +307,15 @@ sip_validate_packet(packet_t *packet)
307307
return VALIDATE_PARTIAL_SIP;
308308
}
309309

310-
strncpy(cl_header, (const char *)payload + pmatch[2].rm_so, (int)pmatch[2].rm_eo - pmatch[2].rm_so);
310+
// Ensure the copy length does not exceed MAX_CONTENT_LENGTH_SIZE - 1
311+
int cl_match_len = pmatch[2].rm_eo - pmatch[2].rm_so;
312+
if (cl_match_len > MAX_CONTENT_LENGTH_SIZE - 1) {
313+
cl_match_len = MAX_CONTENT_LENGTH_SIZE - 1;
314+
}
315+
316+
strncpy(cl_header, (const char *)payload + pmatch[2].rm_so, cl_match_len);
317+
cl_header[cl_match_len] = '\0'; // Ensuring null termination
318+
311319
content_len = atoi(cl_header);
312320

313321
// Check if we have Body separator field
@@ -772,7 +780,7 @@ void
772780
sip_parse_extra_headers(sip_msg_t *msg, const u_char *payload)
773781
{
774782
regmatch_t pmatch[4];
775-
char warning[10];
783+
char warning[MAX_WARNING_SIZE];
776784

777785
// Reason text
778786
if (regexec(&calls.reg_reason, (const char *)payload, 2, pmatch, 0) == 0) {
@@ -782,8 +790,16 @@ sip_parse_extra_headers(sip_msg_t *msg, const u_char *payload)
782790

783791
// Warning code
784792
if (regexec(&calls.reg_warning, (const char *)payload, 2, pmatch, 0) == 0) {
785-
strncpy(warning, (const char *)payload + pmatch[1].rm_so, (int)pmatch[1].rm_eo - pmatch[1].rm_so);
786-
msg->call->warning = atoi(warning);
793+
794+
// Ensure the copy length does not exceed MAX_WARNING_SIZE - 1
795+
int warning_match_len = pmatch[1].rm_eo - pmatch[1].rm_so;
796+
if (warning_match_len > MAX_WARNING_SIZE - 1) {
797+
warning_match_len = MAX_WARNING_SIZE - 1;
798+
}
799+
strncpy(warning, (const char *)payload + pmatch[1].rm_so, warning_match_len);
800+
warning[warning_match_len] = '\0'; // Ensuring null termination
801+
802+
msg->call->warning = atoi(warning);
787803
}
788804
}
789805

src/sip.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#define MAX_SIP_PAYLOAD 10240
4848
#define MAX_CALLID_SIZE 1024
4949
#define MAX_XCALLID_SIZE 1024
50+
#define MAX_CONTENT_LENGTH_SIZE 10
51+
#define MAX_WARNING_SIZE 10
5052

5153
//! Shorter declaration of sip_call_list structure
5254
typedef struct sip_call_list sip_call_list_t;

0 commit comments

Comments
 (0)