Skip to content
Memory corruption Disclosed report Advanced

55: Heap-buffer-overflow read in `curl_formadd_ccsid()` with binary form data

In the IBM i-specific curl_formadd_ccsid() wrapper, placing CURLFORM_CONTENTTYPE before an explicit content length causes pending binary content to be converted using strlen() instead of the declared length, producing an undersized heap allocation. Core form processing then copies the full declared length from that buffer, reading adjacent heap memory into the generated multipart body.

giant_anteater Follow

Originally disclosed on HackerOne

  • Disclosed after 14 days

Summary

curl_formadd_ccsid() in projects/OS400/ccsidcurl.c performs a heap-buffer-overflow read when CURLFORM_CONTENTTYPE appears before an explicit length (CURLFORM_CONTENTLEN / CURLFORM_CONTENTSLENGTH) for binary CURLFORM_COPYCONTENTS data containing embedded NUL bytes. The overread bytes are copied into the generated multipart form body.

Affected versions

IBM i/OS400-specific API in curl 7.17.0 through 8.21.0, plus affected 8.22.0 prerelease/development versions.

Root cause

When curl_formadd_ccsid() encounters CURLFORM_CONTENTTYPE, it treats it as a separator and immediately converts any pending CURLFORM_COPYCONTENTS via Curl_formadd_convert() (line 897), then clears both the content and length indexes (lines 902-903). Because the explicit length has not been seen yet, Curl_formadd_convert() falls back to strlen(value) + 1 (line 675). For a NUL-leading binary body this produces a 1-byte allocation (line 690). The later CURLFORM_CONTENTLEN is still forwarded to core curl_formadd(), which copies the full declared length from that undersized buffer — a heap-buffer-overflow read.

There is also a secondary overread: the premature strlen() at line 675 reads beyond the caller's buffer if the binary content is not NUL-terminated.

Impact

The confirmed primitive is an out-of-bounds heap read whose contents are copied into the generated multipart request body. If that request crosses a trust boundary, adjacent heap data may be disclosed to the receiving server.

Constraints: the affected API is IBM i-specific and deprecated (curl_formadd is superseded by curl_mime). The trigger requires the application to supply binary content with the specific option ordering. I have not reproduced this natively on IBM i.

Reproduction

  1. Build an affected curl release, starting with 7.17.0, including the IBM i CCSID compatibility wrappers and with AddressSanitizer or an equivalent heap-bounds detector enabled.
  2. Write a small application using the deprecated curl_formadd_ccsid() API.
  3. Prepare binary form content with a truthful explicit length and an embedded NUL byte, preferably as the first byte so premature string-length calculation produces the smallest possible converted allocation.
  4. Add a form field name and then add the binary data with CURLFORM_COPYCONTENTS.
  5. Place CURLFORM_CONTENTTYPE after the binary content but before its corresponding CURLFORM_CONTENTLEN or CURLFORM_CONTENTSLENGTH.
  6. Place the truthful explicit content length after the content type and terminate the option list normally.
  7. Observe that encountering the content type causes the CCSID wrapper to convert the pending content before it has seen the explicit length.
  8. Confirm that the conversion path falls back to strlen(value) + 1; for NUL-leading data, this creates a one-byte converted allocation.
  9. Confirm that the later explicit length is still forwarded to the core curl_formadd() implementation.
  10. Serialize or submit the generated multipart form.
  11. Observe the vulnerable result: core form processing copies the full declared content length from the undersized converted allocation, and the memory checker reports a heap out-of-bounds read.
  12. If execution continues under a recovering memory checker, inspect the multipart body and confirm that bytes adjacent to the converted allocation can replace or follow the intended binary data.
  13. Repeat with bounded binary content that has no NUL terminator and confirm the secondary overread caused by the premature strlen() itself.
  14. As an ordering control, place the explicit content length before CURLFORM_CONTENTTYPE.
  15. Confirm that the control completes without a sanitizer error and preserves every supplied binary byte.
  16. As a second control, omit CURLFORM_CONTENTTYPE while retaining the explicit length and confirm correct serialization.
  17. If native IBM i instrumentation is unavailable, compile the production wrapper in a sanitizer-enabled environment with minimal platform declarations and use a same-CCSID conversion path so platform conversion stubs are not executed.
  18. Confirm a fix by deferring content conversion until its explicit length is known, preserving the pending content state across the content-type option, or rejecting the unsafe ordering.
  19. Repeat all three option orderings and verify that bounded binary data is processed exactly according to its explicit length without memory overreads.

Expected result

Form option order must not cause binary content to be treated as a NUL-terminated string. Once supplied, the explicit length must govern conversion and copying of the associated content.

Vulnerable result

CURLFORM_CONTENTTYPE triggers conversion before the later length is known, producing an undersized allocation. Core form processing subsequently trusts the explicit length and reads beyond that allocation into adjacent heap memory.

Suggested fix

Do not convert pending content at CURLFORM_CONTENTTYPE until the explicit length is known. Either defer conversion until end-of-parameter-list (where the length will have been seen), or preserve the content index across the CURLFORM_CONTENTTYPE separator when no length has been recorded yet.

Impact

An application using the IBM i curl_formadd_ccsid() wrapper with attacker-influenced binary form data may read beyond a heap allocation and include adjacent process memory in the generated multipart request. This could disclose sensitive heap contents to the receiving server or crash the process, although exploitation requires the deprecated platform-specific API and the vulnerable option ordering.

The original report as published on HackerOne, by giant_anteater. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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 wrapper converts pending content as soon as it sees CURLFORM_CONTENTTYPE, before the explicit length option has been processed, so the conversion falls back to strlen(value) + 1 and allocates a buffer far smaller than the declared length.

Impact

An application using the wrapper with attacker-influenced binary form data may read beyond a heap allocation and include adjacent process memory in the multipart request, potentially disclosing heap contents to the receiving server or crashing the process, though this requires the deprecated IBM i-specific API and the vulnerable option ordering.

What the attacker needed

IBM i platform with CCSID wrapper API; Application uses curl_formadd_ccsid(); Binary content with embedded NUL byte; Content type option placed before length

The fix

Conversion of pending content should be deferred until the explicit length is known, either by waiting until the end of the option list or by preserving the content index across the content-type separator.

How it was found

  1. Build an affected curl release with the IBM i CCSID wrappers and a heap-bounds detector such as AddressSanitizer.
  2. Write a small application that uses the deprecated curl_formadd_ccsid() API.
  3. Prepare binary form content with an embedded NUL byte, ideally as the first byte, and a truthful explicit length.
  4. Add a field name, then the binary data with CURLFORM_COPYCONTENTS.
  5. Place CURLFORM_CONTENTTYPE after the content but before CURLFORM_CONTENTLEN or CURLFORM_CONTENTSLENGTH, then give the explicit length and end the option list.
  6. Serialize or submit the generated multipart form and observe the memory checker report a heap out-of-bounds read.

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.

This is a bug in a helper function that builds multipart form uploads on IBM i systems. When options are given in a certain order, the code guesses the size of a piece of binary data by looking for a zero byte instead of using the size the programmer supplied, so it allocates a buffer that is too small. Later it copies the full amount of data anyway, reading past the end of that small buffer and pulling in whatever memory sits next to it.

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.

More by giant_anteater

View all

93 more disclosed reports, best first.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.