From 64a031522dd7565c7e9a4506b3e732f602972634 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 17 Nov 2026 04:20:00 +0100 Subject: afc: validate entire_length/this_length in afc_receive_data The AFC packet header fields entire_length and this_length were only checked to ensure this_length >= sizeof(AFCPacket). Neither field was validated against the other, allowing a malicious or compromised device to trigger: - A heap buffer overflow (CWE-787): if this_length > entire_length, buf is malloc'd with the smaller entire_len but service_receive() writes this_len attacker-controlled bytes into it. - An integer underflow leading to a crash (CWE-191): if entire_length < sizeof(AFCPacket), entire_len underflows to a huge value, malloc() fails, and the returned NULL buf is later dereferenced. Add explicit checks that entire_length is at least sizeof(AFCPacket) and fits in 32 bits, and that this_length never exceeds entire_length, rejecting malformed packets with AFC_E_OP_HEADER_INVALID. Also handle malloc() failure by returning AFC_E_NO_MEM instead of proceeding with a NULL buffer. Reported September 1st, 2026 Reported-by: Karnakar Reddy --- src/afc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/afc.c b/src/afc.c index e2e5ba1..05f5353 100644 --- a/src/afc.c +++ b/src/afc.c @@ -290,7 +290,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } /* then, read the attached packet */ - if (header.this_length < sizeof(AFCPacket)) { + if (header.this_length < sizeof(AFCPacket) || header.entire_length < sizeof(AFCPacket) || header.entire_length > UINT32_MAX || header.this_length > header.entire_length) { debug_info("Invalid AFCPacket header received!"); return AFC_E_OP_HEADER_INVALID; } @@ -309,6 +309,10 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t this_len = (uint32_t)header.this_length - sizeof(AFCPacket); buf = (char*)malloc(entire_len); + if (!buf) { + debug_info("Failed to allocate %u bytes for packet buffer", entire_len); + return AFC_E_NO_MEM; + } if (this_len > 0) { recv_len = 0; err = service_to_afc_error(service_receive(client->parent, buf, this_len, &recv_len)); -- cgit v1.1-32-gdbae