Headline
CVE-2023-22741: Merge pull request #182 from QiuhaoLi/fix-stun-parser-oob · freeswitch/sofia-sip@da53e4f
Sofia-SIP is an open-source SIP User-Agent library, compliant with the IETF RFC3261 specification. In affected versions Sofia-SIP lacks both message length and attributes length checks when it handles STUN packets, leading to controllable heap-over-flow. For example, in stun_parse_attribute(), after we get the attribute’s type and length value, the length will be used directly to copy from the heap, regardless of the message’s left size. Since network users control the overflowed length, and the data is written to heap chunks later, attackers may achieve remote code execution by heap grooming or other exploitation methods. The bug was introduced 16 years ago in sofia-sip 1.12.4 (plus some patches through 12/21/2006) to in tree libs with git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3774 d0543943-73ff-0310-b7d9-9358b9ac24b2. Users are advised to upgrade. There are no known workarounds for this vulnerability.
@@ -87,6 +87,13 @@ int stun_parse_message(stun_msg_t *msg)
/* parse header first */
p = msg->enc_buf.data;
if (get16(p, 2) > (msg->enc_buf.size - 20))
{
SU_DEBUG_3(("%s: Error STUN Message Length is too big.\n", __func__));
return -1;
}
msg->stun_hdr.msg_type = get16(p, 0);
msg->stun_hdr.msg_len = get16(p, 2);
memcpy(msg->stun_hdr.tran_id, p + 4, STUN_TID_BYTES);
@@ -98,8 +105,8 @@ int stun_parse_message(stun_msg_t *msg)
len = msg->stun_hdr.msg_len;
p = msg->enc_buf.data + 20;
msg->stun_attr = NULL;
while (len > 0) {
i = stun_parse_attribute(msg, p);
while (len >= 4) { // Type (2) + Length (2) + Value (variable) min attribute size
i = stun_parse_attribute(msg, p, len);
if (i <= 0 || i > len) {
SU_DEBUG_3(("%s: Error parsing attribute.\n", __func__));
return -1;
@@ -111,7 +118,7 @@ int stun_parse_message(stun_msg_t *msg)
return 0;
}
int stun_parse_attribute(stun_msg_t *msg, unsigned char *p)
int stun_parse_attribute(stun_msg_t *msg, unsigned char *p, size_t left_len)
{
int len;
uint16_t attr_type;
@@ -120,6 +127,12 @@ int stun_parse_attribute(stun_msg_t *msg, unsigned char *p)
attr_type = get16(p, 0);
len = get16(p, 2);
if ((left_len - 4) < len) // make sure we have enough space for attribute
{
SU_DEBUG_3(("%s: Error STUN attr len is too big.\n", __func__));
return -1;
}
SU_DEBUG_5(("%s: received attribute: Type %02X, Length %d - %s\n",
__func__, attr_type, len, stun_attr_phrase(attr_type)));
Related news
Gentoo Linux Security Advisory 202407-10 - Multiple vulnerabilities have been discovered in Sofia-SIP, the worst of which can lead to remote code execution. Versions prior to 1.13.16 are affected.
Debian Linux Security Advisory 5410-1 - Multiple security issues were discovered in Sofia-SIP, a SIP User-Agent library, which could result in denial of service.
Ubuntu Security Notice 5932-1 - It was discovered that Sofia-SIP incorrectly handled specially crafted SDP packets. A remote attacker could use this issue to cause applications using Sofia-SIP to crash, leading to a denial of service, or possibly execute arbitrary code. This issue only affected Ubuntu 16.04 ESM, Ubuntu 18.04 LTS, Ubuntu 20.04 LTS and Ubuntu 22.04 LTS. It was discovered that Sofia-SIP incorrectly handled specially crafted UDP packets. A remote attacker could use this issue to cause applications using Sofia-SIP to crash, leading to a denial of service.