summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2026-11-17 04:20:00 +0100
committerGravatar Nikias Bassen2026-11-17 04:20:00 +0100
commit64a031522dd7565c7e9a4506b3e732f602972634 (patch)
tree2f095f26de1a14ce8c05044ea5beb3479fed84fd
parentfa0f79190142bc309307967c058f89c1b36eb6b8 (diff)
downloadlibimobiledevice-64a031522dd7565c7e9a4506b3e732f602972634.tar.gz
libimobiledevice-64a031522dd7565c7e9a4506b3e732f602972634.tar.bz2
afc: validate entire_length/this_length in afc_receive_dataHEADmaster
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 <karnakarthoorpu@gmail.com>
-rw-r--r--src/afc.c6
1 files changed, 5 insertions, 1 deletions
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));