Integer Underflow in src/var.c
The setvariable function in curl's src/var.c calculates a length by subtracting startoffset from endoffset without checking that startoffset is not greater than endoffset. This can produce a huge size_t value, leading to heap buffer overflow or denial of service when passed to addvariable.
- Disclosed after 32 hours
Summary:
A potential Integer Underflow vulnerability was identified in the setvariable function within src/var.c. the flaw occurs during the calculation of the variable content length (clen) when a byte range is specified. specifically, the code fails to validate if startoffset is greater than endoffset before performing subtraction, leading to a massive wrapped-around value when cast to size_t.
This leads to an inconsistent state where clen holds an impossibly large value, causing memory corruption when passed to downstream functions
URL : https://github.com/curl/curl/blob/master/src/var.c#L375
function: setvariable()
file : src/var.c:480
clen = (size_t)(endoffset - startoffset) + 1;
Proof of Concept
using a debugger (GDB) to simulate the logic bypass:
set a breakpoint at setvariable
Manipulate the offsets:set var startoffset = 100 set var endoffset = 50
Result: clen as size_t: 18446744073709551567

Workarounds
Add a sanity check to ensure startoffset does not exceed endoffset before calculation
Impact
This massive clen value is subsequently passed to addvariable(), where it is used as a length parameter for memory operations. this can lead to:
Heap Buffer Overflow: attempting to copy data using an invalid length
Denial of Service : triggering an out-of-memory condition or a crash
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, the code does not validate that startoffset is less than or equal to endoffset before subtracting them, causing an integer underflow when startoffset is greater.
Impact
This could lead to a heap buffer overflow or denial of service (out-of-memory or crash) when the huge length is used in memory operations.
What the attacker needed
Debugger access to manipulate variables
The fix
A sanity check should be added to ensure startoffset does not exceed endoffset before the calculation.
How it was found
- Set a breakpoint at the setvariable function in a debugger.
- Manipulate the offsets so that startoffset is 100 and endoffset is 50.
- Observe that clen becomes a very large size_t value.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In programming, when you subtract a larger number from a smaller one using unsigned integers, the result wraps around to a very large number. Here, the code calculates a length without checking that the start is before the end, so it gets an enormous length. That huge length is then used to copy data, which can overflow memory and crash the program.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.