Back to Blog
critical SEVERITY10 min read

Critical Heap Buffer Overflow in SSDP Control Point: How Unbounded String Operations Put Networks at Risk

A critical heap buffer overflow vulnerability was discovered and patched in the SSDP control point implementation (`ssdp_ctrlpt.c`), where multiple unbounded `strcpy` and `strcat` operations constructed HTTP request buffers without any length validation. Network-received SSDP response fields — including service type strings and location URLs — could be crafted by an attacker to exceed buffer boundaries, potentially enabling arbitrary code execution or denial of service. The fix replaces the unsa

O
By Orbis AppSec
•Technically reviewed by Anupam Mediratta•Published May 28, 2026•Reviewed June 3, 2026

Answer Summary

A heap buffer overflow vulnerability (CWE-122) existed in the SSDP control point implementation due to unbounded strcpy() and strcat() operations on user-controlled network data including service type strings and location URLs. The fix replaces these unsafe functions with length-bounded alternatives like strncpy() and strncat(), or safer APIs that validate buffer boundaries before writing data.

Vulnerability at a Glance

cweCWE-122 (Heap-based Buffer Overflow), CWE-120 (Buffer Copy without Checking Size of Input)
fixReplace strcpy()/strcat() with strncpy()/strncat() and validate all string lengths before buffer operations
riskRemote code execution or denial of service via crafted SSDP network responses
languageC
root causeNetwork-received SSDP fields copied into fixed-size buffers without length validation
vulnerabilityHeap buffer overflow via unbounded string operations in SSDP control point

Severity: Critical | CWE: CWE-120 (Buffer Copy without Checking Size of Input) | File: upnp/src/ssdp/ssdp_ctrlpt.c


Introduction

Buffer overflows are among the oldest and most dangerous vulnerability classes in systems programming — and they keep showing up in production code. This post dives into a critical heap buffer overflow discovered in the SSDP (Simple Service Discovery Protocol) control point implementation of a UPnP library, explains exactly how it could be exploited, and walks through the fix that closes the door on this attack vector.

If you write C or C++ code that processes network data, this one is for you. Even if you don't, understanding why unbounded string operations are dangerous is essential knowledge for any developer who cares about the security of the systems they build.


What Is SSDP and Why Does It Matter?

SSDP (Simple Service Discovery Protocol) is the discovery mechanism at the heart of UPnP (Universal Plug and Play). It's how your smart TV finds your router, how a media server announces itself to clients, and how IoT devices broadcast their capabilities on a local network.

The control point side of SSDP is responsible for sending M-SEARCH requests — essentially asking "who's out there?" — and processing the responses. Because SSDP operates over UDP and accepts responses from any device on the network, it is a natural target for network-based attacks. Malicious or malformed responses are trivial to craft and inject.


The Vulnerability Explained

What Went Wrong

Inside CreateClientRequestPacket(), the code was responsible for building an HTTP-style M-SEARCH request buffer (RqstBuf). The original implementation did this in several stages using a combination of strcpy, strcat, and snprintf into an intermediate TempBuf:

// VULNERABLE CODE (before the fix)
char TempBuf[COMMAND_LEN];
const char *command = "M-SEARCH * HTTP/1.1\r\n";
const char *man = "MAN: \"ssdp:discover\"\r\n";

memset(TempBuf, 0, sizeof(TempBuf));
if (RqstBufSize <= strlen(command))
    return UPNP_E_INTERNAL_ERROR;
strcpy(RqstBuf, command);          // ⚠️ No overflow check after this point

// ... snprintf into TempBuf for the HOST header ...

if (RqstBufSize <= strlen(RqstBuf) + strlen(TempBuf))
    return UPNP_E_BUFFER_TOO_SMALL;
strcat(RqstBuf, TempBuf);          // ⚠️ Race between check and use

if (RqstBufSize <= strlen(RqstBuf) + strlen(man))
    return UPNP_E_BUFFER_TOO_SMALL;
strcat(RqstBuf, man);              // ⚠️ Another unbounded append

// ... and so on for MX header, ST header, etc.

There are several compounding problems here:

  1. strcpy without bounds checking — The initial strcpy(RqstBuf, command) checks that RqstBufSize > strlen(command), but this check only validates the first write. All subsequent strcat calls append to a buffer that is being filled incrementally, with each check only loosely validating the current state.

  2. TOCTOU-style logic errors — The pattern if (RqstBufSize <= strlen(RqstBuf) + strlen(TempBuf)) return error; strcat(RqstBuf, TempBuf); is fragile. The length check and the write are not atomic, and more importantly, the check uses <= instead of <, meaning a perfectly full buffer passes the check and then overflows by one byte on the strcat.

  3. Network-controlled input flows into the buffer — Fields like service type (ST) headers and location URLs come directly from network-received SSDP responses. An attacker on the same network segment can craft a response with an oversized ST field. When this value is concatenated into RqstBuf without strict length enforcement, the buffer overflows onto the heap.

How Could This Be Exploited?

Here's a realistic attack scenario:

  1. An attacker connects to the same local network as a device running the vulnerable UPnP control point (common in home networks, hotel Wi-Fi, corporate LANs, or any shared network).
  2. The control point broadcasts an SSDP M-SEARCH request.
  3. The attacker's machine responds with a crafted SSDP response containing an oversized ST (Service Type) field — for example, a string of several kilobytes instead of the expected short identifier like upnp:rootdevice.
  4. The vulnerable code copies this attacker-controlled string into RqstBuf via strcat, writing far beyond the allocated heap buffer.
  5. The overflow corrupts adjacent heap metadata or data, potentially enabling:
    - Arbitrary code execution via heap metadata manipulation
    - Denial of service via heap corruption and crash
    - Information disclosure if heap layout can be influenced to leak adjacent memory

Because SSDP uses UDP and requires no authentication, this attack requires no credentials, no prior access, and no user interaction — just network adjacency.

CWE-120: The Classic "Dangerous Function" Problem

This vulnerability is classified under CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow'). The C standard library functions strcpy and strcat are considered dangerous precisely because they perform no bounds checking. The destination buffer's size is never consulted — they simply write until they hit a null terminator in the source string.

The CERT C Coding Standard, MISRA C, and virtually every modern secure coding guideline explicitly prohibit the use of strcpy and strcat in code that handles externally-sourced data.


The Fix

What Changed

The fix replaces the fragmented, multi-step string construction approach with a single, offset-tracked snprintf pattern. Here's the corrected code:

// FIXED CODE (after the patch)
size_t offset = 0;

switch (AddressFamily) {
case AF_INET:
    rc = snprintf(RqstBuf,
        RqstBufSize,
        "M-SEARCH * HTTP/1.1\r\n"
        "HOST: %s:%d\r\n"
        "MAN: \"ssdp:discover\"\r\n",
        SSDP_IP,
        SSDP_PORT);
    break;
case AF_INET6:
    rc = snprintf(RqstBuf,
        RqstBufSize,
        "M-SEARCH * HTTP/1.1\r\n"
        "HOST: [%s]:%d\r\n"
        "MAN: \"ssdp:discover\"\r\n",
        SSDP_IPV6_LINKLOCAL,
        SSDP_PORT);
    break;
default:
    return UPNP_E_INVALID_ARGUMENT;
}

if (rc < 0 || (size_t)rc >= RqstBufSize)
    return UPNP_E_INTERNAL_ERROR;
offset = (size_t)rc;

if (Mx > 0) {
    rc = snprintf(RqstBuf + offset,
        RqstBufSize - offset,
        "MX: %d\r\n",
        Mx);
    if (rc < 0 || (size_t)rc >= RqstBufSize - offset)
        return UPNP_E_BUFFER_TOO_SMALL;
    offset += (size_t)rc;
}

Why This Fix Works

Let's break down the security improvements:

1. snprintf Enforces Hard Limits

snprintf(dest, n, ...) will never write more than n bytes (including the null terminator) to the destination buffer. There is no scenario in which it overflows — if the formatted output would exceed the limit, it is truncated and the function returns the number of bytes that would have been written, allowing the caller to detect truncation.

2. Offset Tracking Eliminates Cumulative Errors

Instead of using strlen(RqstBuf) to find the current end of the buffer (which is O(n) and error-prone), the fix maintains an explicit offset variable. Each snprintf call writes to RqstBuf + offset with a remaining capacity of RqstBufSize - offset. This is both more efficient and more correct — there's no possibility of miscounting.

3. Truncation Is Detected and Rejected

After each snprintf, the return value rc is checked:
- rc < 0 → encoding error, return failure
- (size_t)rc >= RqstBufSize - offset → output was truncated (or would have been), return failure

This means the code fails safely rather than silently producing a malformed or truncated buffer that could cause downstream issues.

4. The Intermediate Buffer Is Eliminated

The original code used TempBuf[COMMAND_LEN] as a staging area, introducing an extra potential overflow point. The fix writes directly into RqstBuf with full size awareness, removing an entire class of intermediate buffer issues.

5. Consolidation Reduces Attack Surface

By consolidating the static header lines (M-SEARCH, HOST, MAN) into a single snprintf call, the fix reduces the number of operations that touch the buffer — fewer operations means fewer opportunities for mistakes.


Before vs. After: A Side-by-Side Comparison

Aspect Before (Vulnerable) After (Fixed)
String functions used strcpy, strcat, snprintf snprintf only
Bounds enforcement Manual, error-prone checks Enforced by snprintf
Buffer position tracking strlen(RqstBuf) (O(n), fragile) Explicit offset variable
Truncation detection Partial (only for TempBuf) Full (every write checked)
Intermediate buffers TempBuf[COMMAND_LEN] None
Network input handling Unsafe concatenation Bounded write with failure

Key Takeaways

  1. strcpy and strcat are dangerous — they perform no bounds checking and should never be used with data that originates from the network or user input.

  2. The "check then use" pattern is fragile — checking strlen before strcat is not sufficient. Use snprintf with explicit size limits instead.

  3. Network protocols are attack surfaces — SSDP operates without authentication on local networks. Any device on your Wi-Fi can send crafted SSDP responses. Buffer overflows in SSDP parsers are reachable with zero credentials.

  4. The fix is simple and elegant — replacing a tangle of strcpy/strcat calls with offset-tracked snprintf calls eliminates the vulnerability class entirely, not just the specific instance.

  5. Defense in depth matters — compiler hardening, runtime sanitizers, and static analysis complement correct code. Use all of them.

Buffer overflows in C have been known since the 1970s and have been responsible for some of the most devastating exploits in computing history — from the Morris Worm to countless remote code execution vulnerabilities in network daemons. The fact that they still appear in modern codebases is a reminder that secure coding requires constant vigilance, good tooling, and a healthy skepticism toward any code that touches a network socket.

Write safe code. Validate your inputs. And when in doubt, use snprintf.


This vulnerability was identified and fixed by OrbisAI Security using automated security scanning and AI-assisted code review.

Prevention and further reading

Related Articles

critical

Aardvark.Cef.Process.Core Shared Memory Handler: Unvalidated memcpy

The shared memory handler in Aardvark.Cef.Process.Core failed to validate that the `length` parameter passed from JavaScript did not exceed the destination buffer size before calling `memcpy`. This allowed a compromised Chromium renderer process to corrupt heap memory by supplying an oversized `handle->length` value to the `openMapping` function. The fix adds explicit bounds checking using `GetArrayBufferByteLength()` before the copy operation.

high

XShmGetImage Heap Corruption: Unvalidated Image Height in streamproxy

The `XShmGetImage` function in the X11 shared memory image path copies pixel data row-by-row using `memcpy` without validating that the source image height matches the destination buffer height. An attacker or compromised server could provide an oversized source image, causing writes beyond the allocated heap buffer and triggering heap corruption or code execution.

critical

SGX Enclave ecall_store_data memcpy Buffer Overflow in 256-Byte

The Intel SGX enclave's trusted bridge functions `ecall_store_data` and `ecall_retrieve_data` used `memcpy()` to move data into and out of a fixed 256-byte `secure_storage` buffer without validating that `data_len` fit within destination boundaries. An attacker providing oversized `data_len` values could corrupt enclave memory, breaking SGX's confidentiality guarantees.

critical

How Buffer Overflow via strcpy() Happens in C++ XML Parsers and How to Fix It

A critical buffer overflow vulnerability was discovered in `buildroot-external/package/libxmlparser/xmlParser.cpp`, where the `toXMLString` function used `_tcscpy()` to write XML escape sequences into a destination buffer without any bounds checking. An attacker supplying a crafted XML document could overflow the buffer and potentially execute arbitrary code. The fix replaces all five unsafe `_tcscpy()` calls with `memcpy()` calls that copy only the exact number of bytes required for each escape

critical

How Heap Buffer Overflows Happen in C++ ZIP Extraction and How to Fix Them

A critical heap buffer overflow vulnerability was discovered in `TKLiveSync/unzip.cpp`, where ZIP archive entry names were copied into a `PATH_MAX`-sized heap buffer using `strcpy()` without any length validation. Since the ZIP specification allows entry names up to 65,535 bytes — far exceeding typical `PATH_MAX` values of 1,024 to 4,096 bytes — a crafted archive could overflow the buffer and corrupt heap memory. The fix replaces the unsafe `strcpy`/`dirname` pattern with `std::string` operation

medium

How Integer Overflow happens in C++ image processing and how to fix it

A signed integer overflow in OpenCV's `bilateralFilter.cpp` allowed the buffer size calculation `cal_width * cal_height * cn` to wrap around to a small or negative value, causing `padding.resize()` to allocate far less memory than needed. Subsequent `memcpy` operations would then write beyond the allocated buffer, creating a heap corruption primitive. The fix is a single targeted cast to `size_t` that promotes the multiplication to unsigned 64-bit arithmetic before any overflow can occur.