Integer overflow leading to buffer overflow
An integer overflow in Perl's environment variable handling allows an attacker to cause a heap buffer overflow by supplying very long strings as the name and value. This memory corruption could lead to arbitrary code execution.
Originally disclosed on HackerOne
- Triaged in 7 months
- Fixed in 11 months
- Paid in 11 months
- Disclosed after 11 months
There exists an integer overflow in Perl_my_setenv @ util.c : 2070
2070: void Perl_my_setenv(pTHX_ const char *nam, const char val) { ... 2166: const int nlen = strlen(nam); ... 2171: vlen = strlen(val); 2172: new_env = (char)safesysmalloc((nlen + vlen + 2) * sizeof(char));
Here in a 64 bit version of Perl, since the arguments nam and val are user controlled, the 32 bit integers nlen and vlen are also under the control of the attacker. Therefore, if nam and val are two very long strings (for example, 2147483647 bytes long), the addition at line 2172 would result in an integer overflow.
The new_env would therefore be a chunk of a size which is smaller than the sum of the lengths of the two input strings.
This new_env is subsequently used in a call to memcpy to copy nlen bytes from nam followed by vlen bytes from val.
This results in a buffer overflow on the heap with attacker controlled input.
Please find attached a PoC which demonstrates the buffer overflow. Please note that the attached PoC consumes large amounts of memory and results in a segmentation fault on a 64 bit Ubuntu 16.04 system running a 64 bit version of perl.
This segmentation fault occurs due to the fact that the memcpy tries to write outside the initial heap boundary.
This vulnerability has been recognised as a serious security issue and has been assigned the identifier CVE-2018-18311 by the developers.
Impact
Memory corruption with attacker controlled input which can lead to arbitrary code execution
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
The lengths of the two strings were stored in 32-bit integers, so their sum could overflow before being used to allocate memory, leading to a heap buffer overflow during the subsequent copy.
Impact
Memory corruption with attacker-controlled input could lead to arbitrary code execution, though the provided proof of concept only caused a segmentation fault on a 64-bit Ubuntu 16.04 system.
What the attacker needed
Attacker controls environment variable name and value; very long strings (e.g., 2GB each); large memory consumption
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.
When a program copies text into a fixed-size memory area, it must first calculate how much space is needed. Here, the calculation used a smaller number type than the actual string lengths, so the total wrapped around to a tiny value. The program then copied far more data than it had allocated, overwriting nearby memory and crashing 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.