Security hardening: missing integer overflow check in curl_load_library()
The function that builds a DLL path on Windows computes its heap allocation size without checking for integer overflow, so on 32-bit builds the size can wrap and produce a buffer smaller than the strings later copied into it. The text states no attacker-controlled input currently reaches this code, so no exploitation scenario is identified; the risk is a latent heap corruption primitive.
Originally disclosed on HackerOne
- Disclosed after 24 hours
Summary
A missing integer overflow check was identified in lib/system_win32.c::curl_load_library()
when calculating the buffer size for a DLL path. On 32-bit Windows builds, the unchecked
size calculation can wrap around, resulting in an undersized heap allocation followed by
unbounded string copies via _tcscpy().
While the current call sites only pass fixed, trusted strings and no immediate attack vector is identified, the presence of a latent heap corruption primitive places this issue beyond pure informational hardening and justifies a Low severity classification.
Affected Code
File: lib/system_win32.c
Function: curl_load_library(LPCTSTR filename)
UINT systemdirlen = GetSystemDirectory(NULL, 0);
if(systemdirlen) {
size_t filenamelen = _tcslen(filename);
TCHAR *path = curlx_malloc(sizeof(TCHAR) *
(systemdirlen + 1 + filenamelen));
if(path && GetSystemDirectory(path, systemdirlen)) {
_tcscpy(path + _tcslen(path), TEXT("\\"));
_tcscpy(path + _tcslen(path), filename);
}
curlx_free(path);
}
Technical Details
Integer Overflow Condition
systemdirlenis of typeUINT(32-bit)filenamelenis of typesize_t- The allocation size is computed as:
sizeof(TCHAR) * (systemdirlen + 1 + filenamelen)
On 32-bit platforms, size_t arithmetic wraps on overflow. If filenamelen is large,
the multiplication can overflow, resulting in a much smaller allocation than intended.
Memory Safety Impact
After allocation, _tcscpy() is used twice without any bounds checking. If the buffer
is undersized due to arithmetic wrap-around, this creates a reliable heap corruption
condition at the code level, independent of current reachability.
Why This Is Low Severity (Not Informational)
1. Heap Corruption Primitive Exists
Even though the current code paths are not attacker-controlled, the function contains a complete heap corruption pattern (overflowable size calculation + unbounded copy). This exceeds a stylistic or theoretical issue.
2. Violation of libcurl’s Own Defensive Patterns
Other parts of the codebase explicitly guard against integer overflows in allocation
calculations (e.g. vauth/ntlm.c using SIZE_MAX checks). This inconsistency weakens
the overall security posture and makes future maintenance riskier.
3. Long-Term and Supply-Chain Risk
curl_load_library() is a general-purpose internal utility. As libcurl evolves or is
embedded by third-party software, future refactoring or reuse could unintentionally
pass unvalidated input to this function, immediately exposing a heap corruption flaw.
4. Low-Cost, High-Value Fix
The issue can be resolved with a simple bounds check and without changing runtime behavior in valid cases. This makes it a practical security fix rather than a purely informational observation.
Recommended Fix
Add an explicit overflow check before allocation, consistent with existing libcurl practices:
if(filenamelen > (SIZE_MAX / sizeof(TCHAR)) - systemdirlen - 1) {
return NULL;
}
TCHAR *path = curlx_malloc(sizeof(TCHAR) *
(systemdirlen + 1 + filenamelen));
Severity Assessment
- Severity: Low
- Type: Integer overflow leading to potential heap-based buffer overflow
- Current Exploitability: None identified
- Risk Category: Latent memory corruption / future reachability risk
References
- CWE-190: Integer Overflow or Wraparound
- CWE-122: Heap-based Buffer Overflow
- CERT C: INT30-C (Ensure that unsigned integer operations do not wrap)
- libcurl precedent: overflow checks in
vauth/ntlm.c
Impact
Impact
At present, no direct attacker-controlled input reaches this code path, and no immediate exploitation scenario is identified in the current libcurl codebase.
However, this implementation contains a complete heap corruption primitive (integer overflow in allocation size calculation followed by unbounded string copies). If this function were to become reachable through future refactoring, new features, or third-party reuse, an attacker could potentially achieve:
- Heap memory corruption on 32-bit Windows builds
- Process crash (denial of service)
- Potential arbitrary code execution, depending on allocator behavior and surrounding memory layout
Because libcurl is a widely embedded library, such latent issues increase software supply-chain risk and maintenance fragility. Fixing this proactively eliminates a memory corruption condition before it becomes externally reachable.
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 allocation size is computed by adding and multiplying lengths without an overflow check, and the resulting buffer is then filled with unbounded string copies.
Impact
No attacker-controlled input reaches this path today, but if it became reachable an attacker could potentially cause heap corruption on 32-bit Windows builds, a process crash, or possibly arbitrary code execution depending on allocator behaviour.
What the attacker needed
32-bit Windows build; future code path passing untrusted input
The fix
An explicit overflow check should be added before the allocation, consistent with checks already used elsewhere in the codebase; the text does not say a fix was applied.
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 needs memory for a piece of text, it first works out how long the text is and asks the system for that many bytes. Here that sum can wrap around on 32-bit Windows, like an odometer rolling past its maximum, so the program reserves far less space than it needs and then copies the text in anyway, writing past the end of the reserved area. The report says nothing currently lets an outsider control that text, so it is a flaw waiting for a future way in rather than a live attack.
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.