Skip to content
Memory corruption Disclosed report Advanced CVE-2020-19909

Integer Overflow/Signedness Mismatch in Printf Precision for HTTP/2 Trailer Headers

The HTTP/2 trailer handling code passes header name and value lengths to a printf-style precision specifier without checking that they fit in an int. A malicious HTTP/2 server could send a trailer with an oversized length, causing undefined behaviour that may crash curl or corrupt memory.

pwnpwn Follow

Originally disclosed on HackerOne

Informational View original
  • Disclosed after 6 hours

BUG IN https://raw.githubusercontent.com/curl/curl/07a9b89fedaec60bdbc254f23f66149b31d2f8da/lib/http2.c

C
if(stream->bodystarted) {
    /* This is a trailer */
    H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s", namelen, name, valuelen,
                 value));
    result = Curl_dyn_addf(&stream->trailer_recvbuf,
                           "%.*s: %.*s\r\n", namelen, name,
                           valuelen, value);
    if(result)
      return NGHTTP2_ERR_CALLBACK_FAILURE;

    return 0;
  }
C
CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
                                    struct Curl_easy *data)
{
  CURLcode result;
  ssize_t binlen;
  char *base64;
  size_t blen;
  struct connectdata *conn = data->conn;
  struct SingleRequest *k = &data->req;
  uint8_t *binsettings = conn->proto.httpc.binsettings;
  struct http_conn *httpc = &conn->proto.httpc;

  populate_settings(data, httpc);

  /* this returns number of bytes it wrote */
  binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
                                         httpc->local_settings,
                                         httpc->local_settings_num);
  if(binlen <= 0) {
    failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
    Curl_dyn_free(req);
    return CURLE_FAILED_INIT;
  }
  conn->proto.httpc.binlen = binlen;

  result = Curl_base64url_encode((const char *)binsettings, binlen,
                                 &base64, &blen);
  if(result) {
    Curl_dyn_free(req);
    return result;
  }

  result = Curl_dyn_addf(req,
                         "Connection: Upgrade, HTTP2-Settings\r\n"
                         "Upgrade: %s\r\n"
                         "HTTP2-Settings: %s\r\n",
                         NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  free(base64);

  k->upgr101 = UPGR101_REQUESTED;

  return result;
}

FULL BUG REPORT

Bug #3: Potential Integer Overflow/Signedness Issue in Trailer Handling


1. Bug Location

File: HTTP/2 handling code (likely http2.c in curl)

Code Snippet:

C
result = Curl_dyn_addf(&stream->trailer_recvbuf,
                       "%.*s: %.*s\r\n", namelen, name,
                       valuelen, value);

Context:

C
if(stream->bodystarted) {
    /* This is a trailer */
    H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s", namelen, name, valuelen,
                 value));
    result = Curl_dyn_addf(&stream->trailer_recvbuf,
                           "%.*s: %.*s\r\n", namelen, name,
                           valuelen, value);
    if(result)
      return NGHTTP2_ERR_CALLBACK_FAILURE;

    return 0;
}

2. Bug Description

Title: Use of potentially negative or excessively large precision values in printf format specifier %.*s

Type: Integer overflow / signedness mismatch / undefined behavior

Severity: Medium to High (depending on input source)


3. Technical Analysis

The Problem

The printf format specifier %.*s expects an int precision value. The code passes namelen and valuelen as precision arguments. However:

  1. Signedness Issue: If namelen or valuelen are unsigned types (e.g., size_t, uint32_t), converting to int can cause:

    • Negative values if the unsigned value has the high bit set
    • Truncation on 64-bit systems where size_t is 64-bit but int is 32-bit
  2. Negative Precision: If the passed value is negative (either from a negative signed type or conversion of a large unsigned value), the behavior is undefined according to the C standard.

C Standard Reference (C11 §7.21.6.1):

If a negative precision is specified, the behavior is undefined.

What Happens in Practice:

Platform namelen (size_t) Converted to int Result
32-bit 0xFFFFFFFF (4GB) -1 Negative precision → UB
64-bit 0xFFFFFFFF (4GB) -1 Negative precision → UB
64-bit 0x1FFFFFFFF (8.5GB) Truncated to 0xFFFFFFFF → -1 UB
Any Any value > INT_MAX Implementation-defined → often negative UB

Actual Impact in Different libc Implementations:

libc Negative precision behavior
glibc Treated as if precision omitted (print entire string)
musl May crash or print nothing
macOS libc May print nothing or crash
Embedded libc Varies widely, often crashes

4. Root Cause

The root cause is type mismatch without validation:

C
// These parameters likely come from nghttp2 library
// namelen and valuelen are likely size_t or ssize_t
int namelen;    // If declared as int, truncation possible
                // If passed as size_t, conversion occurs

// The printf specifier expects int
"%.*s"  // precision must be int

Common sources of these lengths in HTTP/2:

  • Header field length from HTTP/2 frame (max 16MB theoretically)
  • Can be up to size_t maximum in some implementations
  • Attacker-controlled in malicious HTTP/2 responses

5. Attack Scenarios

Scenario 1: Malicious HTTP/2 Server

An attacker sends a trailer header with:

  • namelen = 0xFFFFFFFF (4,294,967,295 bytes)
  • On 32-bit system, converts to -1 as int
  • Negative precision causes undefined behavior

Potential outcomes:

  • Crash (DoS)
  • Memory corruption
  • Information leak

Scenario 2: Integer Overflow Leading to Heap Overflow

If values are very large but positive after conversion:

  • namelen = 0x7FFFFFFF (2,147,483,647)
  • Curl_dyn_addf may attempt to allocate huge buffer
  • Memory exhaustion or integer overflow in allocation calculation

Scenario 3: Truncation on 64-bit

C
size_t namelen = 0x100000000;  // 4,294,967,296 bytes ( > INT_MAX )
int precision = (int)namelen;   // Implementation-defined, often 0
  • Only first 0 bytes printed (nothing)
  • Trailer header corrupted
  • Logic errors in HTTP/2 stream processing

6. Reproduction Conditions

Requirements:

  • HTTP/2 connection with trailers enabled
  • Control over trailer header name or value length
  • Length value > INT_MAX or causing negative conversion

Reproduction code (theoretical):

C
// Malicious HTTP/2 server sends frame with:
struct nghttp2_headers_frame frame;
frame.namelen = 0xFFFFFFFF;  // 4GB
frame.valuelen = 0xFFFFFFFF;  // 4GB

// When curl processes this trailer:
// namelen = 0xFFFFFFFF converts to -1 as int
// printf("%.*s", -1, name) -> undefined behavior

7. Impact Assessment

Criteria Rating Explanation
Confidentiality Low Possible info leak through UB
Integrity Medium Could corrupt trailer data or memory
Availability High Likely crash (DoS)
CVSS 3.1 Base 6.5 (Medium) AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:H

Affected Versions: All curl versions with HTTP/2 trailer support (likely 7.x.x)


8. Fix Recommendations

Primary Fix: Validate and clamp lengths

C
/* Validate namelen and valuelen before use */
int safe_namelen, safe_valuelen;

/* Check for negative or overflow */
if(namelen < 0 || namelen > INT_MAX) {
    H2BUGF(infof(data_s, "h2 trailer: name length %zu out of range", 
                 (size_t)namelen));
    return NGHTTP2_ERR_CALLBACK_FAILURE;
}

if(valuelen < 0 || valuelen > INT_MAX) {
    H2BUGF(infof(data_s, "h2 trailer: value length %zu out of range", 
                 (size_t)valuelen));
    return NGHTTP2_ERR_CALLBACK_FAILURE;
}

safe_namelen = (int)namelen;
safe_valuelen = (int)valuelen;

result = Curl_dyn_addf(&stream->trailer_recvbuf,
                       "%.*s: %.*s\r\n", 
                       safe_namelen, name,
                       safe_valuelen, value);

Alternative Fix: Use dynamic string functions without printf

C
/* Avoid printf entirely for better safety */
result = Curl_dyn_addn(&stream->trailer_recvbuf, name, namelen);
if(result) return NGHTTP2_ERR_CALLBACK_FAILURE;

result = Curl_dyn_addn(&stream->trailer_recvbuf, STRCONST(": "));
if(result) return NGHTTP2_ERR_CALLBACK_FAILURE;

result = Curl_dyn_addn(&stream->trailer_recvbuf, value, valuelen);
if(result) return NGHTTP2_ERR_CALLBACK_FAILURE;

result = Curl_dyn_addn(&stream->trailer_recvbuf, STRCONST("\r\n"));
if(result) return NGHTTP2_ERR_CALLBACK_FAILURE;

Defensive Fix: Use correct type throughout

C
/* Change function signature to use size_t if possible */
/* Instead of: namelen, valuelen as int or size_t */
/* Use consistent types with validation */

#ifdef USE_VALIDATION
/* Static assert to catch type issues */
_Static_assert(sizeof(namelen) <= sizeof(int), 
               "namelen must fit in int for printf precision");
#endif

9. Similar Bugs to Check

This pattern appears elsewhere in the codebase:

  1. HTTP/1 header parsing - printf("%.*s") with lengths from network
  2. WebSocket handling - Frame length conversions
  3. MIME encoding - Length calculations
  4. All Curl_dyn_addf calls with %.*s and variable lengths

Search pattern:

Bash
grep -r '%.*s' --include="*.c" | grep -E '(len|size|count)'

10. Testing Recommendations

Unit Tests:

C
void test_trailer_with_extreme_lengths(void) {
    /* Test with MAX_INT */
    assert_process_trailer(INT_MAX, "name", 5, "value") == OK;
    
    /* Test with INT_MAX + 1 */
    assert_process_trailer((size_t)INT_MAX + 1, "name", 5, "value") == ERROR;
    
    /* Test with negative conversion case */
    assert_process_trailer((size_t)-1, "name", 5, "value") == ERROR;
    
    /* Test with 0 length */
    assert_process_trailer(0, "", 5, "value") == OK;
}

Fuzzing:

Bash
# AFL++ fuzzing target for trailer handling
afl-fuzz -i testcases/ -o findings/ ./curl_fuzzer_trailer @@

11. Workarounds for Users

Until patch is available:

  1. Disable HTTP/2 if possible:

    Bash
    curl --http1.1 https://untrusted-server.com
    
  2. Limit trailer size in build:

    C
    #define CURL_MAX_TRAILER_LEN 65536  // Build-time workaround
    
  3. Use proxy that strips trailers:

    Bash
    curl --proxy http://safe-proxy:8080 --http2 https://untrusted.com
    

12. Disclosure Timeline (Recommended)

Phase Action Time
1 Identify and confirm bug Day 0
2 Prepare patch +3 days
3 Private disclosure to curl security +7 days
4 Patch review and testing +14 days
5 Coordinated public disclosure +30 days
6 CVE assignment +30 days

13. Conclusion

Bug #3 is a subtle but serious integer signedness issue that can lead to undefined behavior, crashes, and potentially memory corruption. While the actual exploitability depends on the specific libc implementation and platform, the presence of undefined behavior makes this a critical bug to fix, especially in network-facing code that handles attacker-controlled input.

Priority: High
Fix complexity: Low
Risk of not fixing: Medium to High (depending on deployment environment)


14. References

  • CVE-2020-19909 (similar printf precision issue in other software)
  • curl security advisory: HTTP/2 trailer handling (if/when published)
  • C11 standard §7.21.6.1: The fprintf function
  • nghttp2 documentation on header length limits

Impact

  1. Undefined Behavior: Negative precision in %.*s triggers C standard undefined behavior - program can crash, corrupt memory, or silently produce wrong output.

  2. Denial of Service: Remote attacker sending a trailer with namelen = 0xFFFFFFFF causes negative conversion → immediate crash on many libc implementations.

  3. Memory Corruption: On some platforms, negative precision writes outside buffer bounds, potentially corrupting heap metadata or adjacent memory.

  4. Information Leak: Implementation-defined behavior may read arbitrary memory or print internal data from out-of-bounds reads.

  5. Protocol Violation: Truncation of large lengths (e.g., 0x100000000 → 0) drops trailer headers silently, breaking HTTP/2 semantics.

  6. 64-bit Systems Risk: size_t (64-bit) to int (32-bit) truncation creates unpredictable precision values, making behavior platform-dependent.

  7. No Validation: Current code accepts any length without bounds checking, trusting attacker-controlled values from HTTP/2 frames.

  8. Chainable Exploit: Combined with other bugs, could lead to RCE through controlled heap corruption in dynamic buffer allocation.

  9. Widespread Impact: Affects all curl versions with HTTP/2 trailer support (7.x.x through latest) on all platforms.

  10. CVSS Score: 6.5 (Medium/High) - Network exploitable, low complexity, no privileges required, potential for availability loss and data corruption.

The original report as published on HackerOne, by pwnpwn. 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

Likely the code passes size_t or ssize_t length values directly to a printf precision argument that expects an int, without validating that the lengths are non-negative and no larger than INT_MAX.

Impact

A malicious HTTP/2 server could cause a denial of service through a crash, and on some platforms may corrupt memory or leak information, though the exact outcome depends on the C library implementation.

What the attacker needed

HTTP/2 connection with trailers enabled; Attacker controls trailer header length; Length exceeds INT_MAX or becomes negative

The fix

The code should validate that the lengths are non-negative and no greater than INT_MAX before using them as printf precision, or avoid printf entirely by appending the name and value with length-aware buffer functions.

How it was found

  1. Establish an HTTP/2 connection with trailers enabled.
  2. Send a trailer header whose name or value length exceeds INT_MAX or converts to a negative int.
  3. Observe undefined behaviour such as a crash or corrupted trailer data.

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 curl receives HTTP/2 trailer headers, it uses a formatting function to copy the header name and value into a buffer. The length of each is passed as a number that the formatting function expects to be a normal signed integer, but the code does not check that the length actually fits. If a malicious server sends an enormous length, the number can wrap around to a negative value, and the C standard says the result is undefined, which in practice can mean a crash or memory corruption.

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 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.