diff options
| author | 2026-11-17 04:20:00 +0100 | |
|---|---|---|
| committer | 2026-11-17 04:20:00 +0100 | |
| commit | 64a031522dd7565c7e9a4506b3e732f602972634 (patch) | |
| tree | 2f095f26de1a14ce8c05044ea5beb3479fed84fd | |
| parent | fa0f79190142bc309307967c058f89c1b36eb6b8 (diff) | |
| download | libimobiledevice-64a031522dd7565c7e9a4506b3e732f602972634.tar.gz libimobiledevice-64a031522dd7565c7e9a4506b3e732f602972634.tar.bz2 | |
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 <karnakarthoorpu@gmail.com>
| -rw-r--r-- | src/afc.c | 6 |
1 files changed, 5 insertions, 1 deletions
| @@ -290,7 +290,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t | |||
| 290 | } | 290 | } |
| 291 | 291 | ||
| 292 | /* then, read the attached packet */ | 292 | /* then, read the attached packet */ |
| 293 | if (header.this_length < sizeof(AFCPacket)) { | 293 | if (header.this_length < sizeof(AFCPacket) || header.entire_length < sizeof(AFCPacket) || header.entire_length > UINT32_MAX || header.this_length > header.entire_length) { |
| 294 | debug_info("Invalid AFCPacket header received!"); | 294 | debug_info("Invalid AFCPacket header received!"); |
| 295 | return AFC_E_OP_HEADER_INVALID; | 295 | return AFC_E_OP_HEADER_INVALID; |
| 296 | } | 296 | } |
| @@ -309,6 +309,10 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t | |||
| 309 | this_len = (uint32_t)header.this_length - sizeof(AFCPacket); | 309 | this_len = (uint32_t)header.this_length - sizeof(AFCPacket); |
| 310 | 310 | ||
| 311 | buf = (char*)malloc(entire_len); | 311 | buf = (char*)malloc(entire_len); |
| 312 | if (!buf) { | ||
| 313 | debug_info("Failed to allocate %u bytes for packet buffer", entire_len); | ||
| 314 | return AFC_E_NO_MEM; | ||
| 315 | } | ||
| 312 | if (this_len > 0) { | 316 | if (this_len > 0) { |
| 313 | recv_len = 0; | 317 | recv_len = 0; |
| 314 | err = service_to_afc_error(service_receive(client->parent, buf, this_len, &recv_len)); | 318 | err = service_to_afc_error(service_receive(client->parent, buf, this_len, &recv_len)); |
