DetectionEngine::queue_event(GID_SIP, SIP_EVENT_INVALID_VERSION);
}
- space = strchr(buff, ' ');
+ space = (const char*)memchr(buff, ' ', next - buff);
if (space == nullptr)
return false;
statusCode = SnortStrtoul(space + 1, nullptr, 10);
msg->dlgID.fromTagHash = strToHash(msg->from_tag,msg->fromTagLen);
break;
}
- buff = (const char*)memchr(buff + 1, ';', msg->fromLen);
+ buff = (const char*)memchr(buff + 1, ';', end - (buff + 1));
}
userStart = (const char*)memchr(msg->from, ':', msg->fromLen);
msg->dlgID.toTagHash = strToHash(msg->to_tag,msg->toTagLen);
break;
}
- buff = (const char*)memchr(buff + 1, ';', msg->toLen);
+ buff = (const char*)memchr(buff + 1, ';', end - (buff + 1));
}
return SIP_PARSE_SUCCESS;
{
int length;
const char* spaceIndex;
- char* next;
SIP_MediaData* mdata;
if (nullptr == msg->mediaSession)
if ((nullptr == spaceIndex)||(spaceIndex == end))
return SIP_PARSE_ERROR;
+ // Compute bounded token range for the port field: [port_start, token_end)
+ const char* port_start = spaceIndex + 1;
+ if (port_start >= end)
+ return SIP_PARSE_ERROR;
+
+ const char* token_end = (const char*)memchr(port_start, ' ', end - port_start);
+ if (!token_end)
+ token_end = end;
+
+ if (token_end <= port_start)
+ return SIP_PARSE_ERROR;
+
+ // Copy bounded token into an exact-sized, NUL-terminated heap buffer
+ size_t token_len = (size_t)(token_end - port_start);
+ char* buf = (char*)snort_alloc(token_len + 1);
+ memcpy(buf, port_start, token_len);
+ buf[token_len] = '\0';
+
+ // Allocate media data
mdata = (SIP_MediaData*)snort_calloc(sizeof(SIP_MediaData));
- mdata->mport = (uint16_t)SnortStrtoul(spaceIndex + 1, &next, 10);
- if ((nullptr != next)&&('/'==next[0]))
- mdata->numPort = (uint8_t)SnortStrtoul(spaceIndex + 1, &next, 10);
+ // Parse mport from the local, NUL-terminated copy
+ char* next = nullptr;
+ mdata->mport = (uint16_t)SnortStrtoul(buf, &next, 10);
+
+ // Safely check for optional numPort using the local buffer bounds
+ if ((next != nullptr) && (next >= buf) && (next < buf + token_len))
+ {
+ if (*next == '/' && (next + 1) < (buf + token_len))
+ {
+ char* next2 = nullptr;
+ mdata->numPort = (uint8_t)SnortStrtoul(next + 1, &next2, 10);
+ }
+ }
// Put
mdata->nextM = msg->mediaSession->medias;
mdata->maddress = msg->mediaSession->maddress_default;
msg->mediaSession->medias = mdata;
+ snort_free(buf);
return SIP_PARSE_SUCCESS;
}