Skip to content

Commit

Permalink
Fix Heap-buffer-overflow in __parse_options (#1678)
Browse files Browse the repository at this point in the history
* Fix vuln crash-7d18f37e1f05e0ff4aa4dfa2f67dd738340ad9cf

* Move the heap overflow check before the allocation

* Terminating immediately when overflow is found

* Fix typo err

---------

Co-authored-by: dataisland <dataisland@outlook.com>
  • Loading branch information
oss-patch and cla7aye15I4nd authored Feb 3, 2025
1 parent 66ccbe8 commit a9e8239
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions 3rdParty/LightPcapNg/LightPcapNg/src/light_pcapng.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,28 @@ static struct _light_option *__parse_options(uint32_t **memory, const int32_t ma
opt->custom_option_code = *local_memory++;
opt->option_length = *local_memory++;

// PCPP patch
// Validate option_length
if (opt->option_length > max_len - 2 * sizeof(*local_memory)) {
free(opt);
return NULL;
}
// PCPP patch end

actual_length = (opt->option_length % alignment) == 0 ?
opt->option_length :
(opt->option_length / alignment + 1) * alignment;

if (actual_length > 0) {
opt->data = calloc(1, actual_length);
memcpy(opt->data, local_memory, actual_length);
local_memory += (sizeof(**memory) / sizeof(*local_memory)) * (actual_length / alignment);
// PCPP patch
// Validate actual_length
if (actual_length <= 0 || actual_length > max_len - 2 * sizeof(*local_memory)) {
free(opt);
return NULL;
}
opt->data = calloc(1, actual_length);
memcpy(opt->data, local_memory, actual_length);
local_memory += (sizeof(**memory) / sizeof(*local_memory)) * (actual_length / alignment);
// PCPP patch end

*memory = (uint32_t*)local_memory;
remaining_size = max_len - actual_length - 2 * sizeof(*local_memory);
Expand Down

0 comments on commit a9e8239

Please sign in to comment.