Skip to content
Memory corruption Disclosed report Advanced

Out-of-bounds read in HTTP method handling causes undefined behavior and potential crash This is sharp, Gaurav. We’ve got a real memory-safety bug ins

A method string in libcurl's HTTP request structure was copied without a null terminator, so later string comparisons and length calls could read past the allocated buffer. This could crash the program (denial of service) and cause CONNECT requests to be misclassified.

gaurav_7777 Follow

Originally disclosed on HackerOne

  • Disclosed after 6 hours

Summary -​‍​‌‍​‍‌​‍​‌‍​‍‌ Component: libcurl core HTTP handling (HTTP/2 request translation and CONNECT detection)

  • Type: out-of-bounds read resulting from missing null-termination
  • Impact: Behavior not defined by the specification, the program can crash (DoS) and CONNECT requests can be misclassified
  • Root cause: The method string was copied without a null ​‍​‌‍​‍‌​‍​‌‍​‍‌termin

Affected​‍​‌‍​‍‌​‍​‌‍​‍‌ code paths

  • Struct layout:

  • struct httpreq { ... char method[1]; }

  • Allocation and copy (no NUL):

  • req = calloc(1, sizeof(*req) + m_len);

  • memcpy(req->method, method, m_len);

  • Unsafe uses:

  • strcmp("CONNECT", req->method) — assumes null-terminated string

  • strlen(req->method) — sizes HTTP/2 pseudo-header :method

Direct risk: Both strcmp and strlen may access memory beyond what has been allocated if req->method[m_len] is out-of-bounds. The impact varies with the allocator/layout; ASan will always detect ​‍​‌‍​‍‌​‍​‌‍​‍‌this.

Steps​‍​‌‍​‍‌​‍​‌‍​‍‌ to reproduce (concise)

  • Prereqs: make sure CMake and nghttp2 are installed
  • What version outputs are you having:
Bash
 cmake --version 
Text
→ 3.26.2
Bash
pkg-config --modversion libnghttp2 
Text
→ 1.52.0

Build curl with ASan + HTTP/2:

Bash
mkdir -p build && cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS='-O1 -g -fsanitize=address' -DCMAKE_EXE_LINKER_FLAGS='-fsanitize=address' -DCURL_USE_NGHTTP2=ON
Bash
cmake --build build -j"$(nproc)"

Verify binary and features:

Bash
./src/curl ​‍​‌‍​‍‌​‍​‌‍​‍‌--version
image.png
image.png From the original on HackerOne

Impact

Impact

-​‍​‌‍​‍‌​‍​‌‍​‍‌ Denial of Service/crash: Out of bounds read occurring during the creation of HTTP/2 pseudo-headers or checking CONNECT logic. -​‍​‌‍​‍‌​‍​‌‍​‍‌ Logic flaw: The function for detecting CONNECT might behave differently due to a comparison of an uninitialized value being performed.

  • Trigger surface: Any path which produces a struct httpreq (e.g., custom methods) and then executes HTTP/2 translation or CONNECT ​‍​‌‍​‍‌​‍​‌‍​‍‌checks

Proposed​‍​‌‍​‍‌​‍​‌‍​‍‌ repair (minimal and robust)

  • In both creators, not only add a null terminator but also allocate memory for ​‍​‌‍​‍‌​‍​‌‍​‍‌it:
Diff
- req = calloc(1, sizeof(*req) + m_len);
+ req = calloc(1, sizeof(*req) + m_len + 1);
  if(!req) goto out;
  memcpy(req->method, method, m_len);
+ req->method[m_len] = '\0';

-​‍​‌‍​‍‌​‍​‌‍​‍‌ Locations to change: Curl_http_req_make Curl_http_req_make2 This change removes local undefined reads without changing the logic. Optional hardening: replace ​‍​‌‍​‍‌​‍​‌‍​‍‌strcmp("CONNECT",

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

This report concerns libcurl, the library component of curl, and specifically its core HTTP handling. Two code paths are involved: the translation of a request into HTTP/2 pseudo-headers, and the detection of CONNECT requests. Both paths operate on a struct httpreq, which stores the HTTP method string in a flexible-array-style member declared as char method[1].

The researcher describes the bug as an out-of-bounds read caused by a missing null terminator on the copied method string. Because the method is later passed to strcmp and strlen, both of which assume a null-terminated string, the code can read past the end of the allocation. The report classifies the impact as behaviour not defined by the specification, a possible crash (denial of service), and possible misclassification of CONNECT requests.

Setup

The reproduction requires CMake and nghttp2. The researcher records the versions used:

Bash
 cmake --version
Text
→ 3.26.2
Bash
pkg-config --modversion libnghttp2
Text
→ 1.52.0

curl is then built with AddressSanitizer and HTTP/2 support enabled, in a Debug build with -O1 -g:

Bash
mkdir -p build && cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS='-O1 -g -fsanitize=address' -DCMAKE_EXE_LINKER_FLAGS='-fsanitize=address' -DCURL_USE_NGHTTP2=ON
Bash
cmake --build build -j"$(nproc)"

The resulting binary is checked for its version and feature set:

Bash
./src/curl --version
image.png
image.png From the original on HackerOne

The report includes a screenshot at this point.

Finding it

The researcher points at the struct layout and the allocation and copy logic. The method member is declared as a single-character array at the end of the struct:

Text
struct httpreq { ... char method[1]; }

The allocation reserves space for the struct plus the method length, and the copy writes exactly that many bytes, with no room for a terminator:

Text
req = calloc(1, sizeof(*req) + m_len);
memcpy(req->method, method, m_len);

Two later uses treat req->method as a C string:

Text
strcmp("CONNECT", req->method) — assumes null-terminated string
strlen(req->method) — sizes HTTP/2 pseudo-header :method

Because req->method[m_len] lies outside the allocated region, both calls may read beyond the allocation. The researcher notes that the visible impact depends on the allocator and memory layout, but that AddressSanitizer will always detect the read.

Exploitation

Step 1: confirm the toolchain versions

The researcher first records the CMake and nghttp2 versions with the two commands shown in the Setup section, obtaining 3.26.2 and 1.52.0 respectively. These establish the environment in which the bug reproduces.

Step 2: build curl with ASan and HTTP/2

The build is configured with -fsanitize=address in both the compiler and linker flags, a Debug build type, and -DCURL_USE_NGHTTP2=ON so that the HTTP/2 translation path is compiled in. The build is then run in parallel with cmake --build build -j"$(nproc)".

Step 3: verify the binary

Running ./src/curl --version confirms the instrumented binary and its features, as shown in the screenshot above. The report does not include the text of that output beyond the image.

Step 4: trigger the affected paths

The report does not give a concrete request or command that triggers the read. It states the trigger surface instead: any path that produces a struct httpreq (for example, a custom method) and then executes HTTP/2 translation or the CONNECT checks. The report states that ASan will always detect the read.

Root cause

The method string is copied into the allocation without a null terminator. The allocation is sized as sizeof(*req) + m_len, which leaves no byte for the terminator, and the memcpy writes only m_len bytes. The struct's char method[1] declaration is the usual flexible-array idiom, so the terminator has to be accounted for explicitly in the allocation size.

Nothing in the two consumers, strcmp("CONNECT", req->method) and strlen(req->method), checks the length before scanning. They rely on the terminator that was never written, so they can walk past the end of the allocation. The report notes that the outcome varies with the allocator and layout, and that ASan always catches it.

Impact

The researcher lists two consequences:

  1. Denial of service or crash: the out-of-bounds read occurs while HTTP/2 pseudo-headers are being created or while the CONNECT logic is being checked.
  2. Logic flaw: the CONNECT detection can behave differently because it compares a value that was never properly terminated, so a request could be misclassified.

The trigger surface is any path that builds a struct httpreq, such as one carrying a custom method, and then runs HTTP/2 translation or the CONNECT checks. The report does not claim a specific attacker-controlled scenario beyond this, and the practical severity depends on the allocator and memory layout.

Fix

The proposed repair is minimal: allocate one extra byte and write the terminator after the copy, in both creators of the struct.

Diff
- req = calloc(1, sizeof(*req) + m_len);
+ req = calloc(1, sizeof(*req) + m_len + 1);
  if(!req) goto out;
  memcpy(req->method, method, m_len);
+ req->method[m_len] = '\0';

The locations to change are Curl_http_req_make and Curl_http_req_make2. The researcher notes that this removes the undefined reads without changing the logic. As optional hardening, the report begins to suggest replacing strcmp("CONNECT", ...) but the text is cut off at that point.

Variants to test

  • Custom HTTP methods on any libcurl-based client that negotiates HTTP/2: a positive result is an ASan report of an out-of-bounds read in the :method pseudo-header construction.
  • Requests whose method is exactly CONNECT or close to it, sent over an HTTP/2 connection: a positive result is the CONNECT detection behaving differently because the comparison read past the allocation.
  • Any other code that stores a length-delimited string in a flexible-array member sized without a terminator: a positive result is a strlen or strcmp on that member reading past the allocation under ASan.
  • Builds with different allocators or optimisation levels: a positive result is a crash or misclassification whose occurrence depends on the allocator and memory layout.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

curl is a program that fetches things from the internet. You have probably used it without knowing: many apps and websites use it behind the scenes to download files, call web services or talk to other servers. curl comes in two parts. There is the command-line tool you can run yourself, and there is a library called libcurl that other programs link into their own code. This report is about libcurl, the library part.

Inside libcurl there is code that handles HTTP, the language browsers and servers use to exchange web pages and data. Two pieces of that code are involved here. One piece takes a request and translates it into the format used by HTTP/2, a newer version of HTTP. The other piece looks at a request to decide whether it is a special kind called CONNECT. Both pieces work on a small internal record called struct httpreq, which stores the request's method. The method is the word at the start of an HTTP request, such as GET (fetch a page) or POST (send data).

The researcher found that when libcurl copies the method into that record, it forgets to add a special marker at the end. In C, the programming language libcurl is written in, text is stored as a run of characters followed by a zero byte, called a null terminator. Functions that work with text keep reading until they hit that zero. If the zero is missing, they keep going into memory that was never meant to be read. That is called an out-of-bounds read. The report says the result is behaviour the specification does not define, a possible crash (a denial of service, meaning the program stops working), and the possibility that CONNECT requests are misclassified.

The affected code paths

The report starts with the layout of the record. The method is declared as a one-character array at the very end of the struct:

Text
struct httpreq { ... char method[1]; }

A struct is a bundle of related values kept together in memory, like a form with several fields. Here the last field is method, declared as an array of one character. This is a common C trick: the real text is stored immediately after the struct, and the one-character declaration just marks where it begins. Because the size is not fixed in advance, whoever allocates the memory has to reserve enough room for the struct plus the whole method text, and also for the terminating zero byte.

The report then shows how the memory is reserved and filled:

Text
req = calloc(1, sizeof(*req) + m_len);
memcpy(req->method, method, m_len);

Line by line:

  • req = calloc(1, sizeof(*req) + m_len); asks the system for a block of memory big enough for one struct plus m_len bytes, where m_len is the length of the method text. calloc also fills the block with zeros. The problem is the size: it stops exactly at the end of the method text, leaving no room for the zero byte that should follow it.
  • memcpy(req->method, method, m_len); copies exactly m_len bytes from the original method into the new block. It copies the letters but not a terminator, because there is nowhere to put one.

Two later pieces of code then treat that text as a normal C string:

Text
strcmp("CONNECT", req->method) — assumes null-terminated string
strlen(req->method) — sizes HTTP/2 pseudo-header :method
  • strcmp("CONNECT", req->method) compares the stored method with the word CONNECT. strcmp walks both strings character by character until it finds a difference or a zero byte. If the stored method has no zero byte, it keeps walking past the end of the allocated block.
  • strlen(req->method) measures how long the method is, again by walking until it finds a zero byte. That length is then used to build the :method pseudo-header, which is how HTTP/2 carries the request method. A pseudo-header is a special header that starts with a colon and describes the request itself rather than its content.

The direct risk, as the report puts it, is that both strcmp and strlen may access memory beyond what was allocated, because req->method[m_len] sits outside the block. What actually happens depends on what the memory allocator placed next to the block and how the program is laid out. AddressSanitizer, a tool that watches memory accesses during a test run, will always detect the read.

Steps to reproduce

The report gives a short recipe. First, make sure CMake and nghttp2 are installed. CMake is a tool that prepares and runs builds of C programs. nghttp2 is a library that implements HTTP/2, and curl uses it when HTTP/2 support is switched on.

The researcher records the versions used:

Bash
 cmake --version
Text
→ 3.26.2
Bash
pkg-config --modversion libnghttp2
Text
→ 1.52.0
  • cmake --version prints which version of CMake is installed. The output was 3.26.2.
  • pkg-config --modversion libnghttp2 asks a helper tool called pkg-config which version of the nghttp2 library is installed. The output was 1.52.0.

Recording versions matters because a bug may only show up with particular tools or libraries. If someone else cannot reproduce the problem, the first question is whether they are using the same versions.

Next, curl is built with AddressSanitizer and HTTP/2 support switched on, as a Debug build with -O1 -g:

Bash
mkdir -p build && cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS='-O1 -g -fsanitize=address' -DCMAKE_EXE_LINKER_FLAGS='-fsanitize=address' -DCURL_USE_NGHTTP2=ON
Bash
cmake --build build -j"$(nproc)"

Line by line for the first command:

  • mkdir -p build creates a folder called build to hold the compiled files, and does nothing if it already exists.
  • cmake -B build tells CMake to prepare the build inside that folder.
  • -DCMAKE_BUILD_TYPE=Debug selects a debug build, which keeps extra information useful for finding problems.
  • -DCMAKE_C_FLAGS='-O1 -g -fsanitize=address' passes options to the C compiler. -O1 applies light optimisation, -g keeps information that links the compiled code back to the source lines, and -fsanitize=address adds AddressSanitizer, the memory checker.
  • -DCMAKE_EXE_LINKER_FLAGS='-fsanitize=address' makes sure the same memory checker is included when the final program is linked together.
  • -DCURL_USE_NGHTTP2=ON switches on HTTP/2 support through nghttp2, so the code path that builds the :method pseudo-header is compiled in.

For the second command:

  • cmake --build build runs the actual compilation inside the build folder.
  • -j"$(nproc)" builds several files at once. nproc reports how many processor cores the machine has, so the build uses all of them and finishes faster.

Finally, the resulting program is checked for its version and features:

Bash
./src/curl --version
  • ./src/curl runs the curl program that was just built, from the src folder inside the build tree.
  • --version asks it to print its version and the features it was compiled with.
image.png
image.png From the original on HackerOne

The report includes a screenshot at this point. It is a way of confirming that the program you are about to test is the instrumented one, with the features you expect, rather than some other curl already installed on the machine.

Finding it

The researcher points at the struct layout and at the allocation and copy logic. The method member is declared as a single-character array at the end of the struct:

Text
struct httpreq { ... char method[1]; }

The allocation reserves space for the struct plus the method length, and the copy writes exactly that many bytes, with no room for a terminator:

Text
req = calloc(1, sizeof(*req) + m_len);
memcpy(req->method, method, m_len);

Two later uses treat req->method as a C string:

Text
strcmp("CONNECT", req->method) — assumes null-terminated string
strlen(req->method) — sizes HTTP/2 pseudo-header :method

Because req->method[m_len] lies outside the allocated region, both calls may read beyond the allocation. The researcher notes that the visible impact depends on the allocator and memory layout, but that AddressSanitizer will always detect the read.

Exploitation

Step 1: confirm the toolchain versions

The researcher first records the CMake and nghttp2 versions with the two commands shown earlier, obtaining 3.26.2 and 1.52.0 respectively. These establish the environment in which the bug reproduces. If the versions differ, the same steps may behave differently, so writing them down makes the test repeatable.

Step 2: build curl with ASan and HTTP/2

The build is configured with -fsanitize=address in both the compiler and linker flags, a Debug build type, and -DCURL_USE_NGHTTP2=ON so that the HTTP/2 translation path is compiled in. The build is then run in parallel with cmake --build build -j"$(nproc)". AddressSanitizer is the key ingredient: without it, a read a few bytes past a block might go unnoticed, because the program simply reads whatever happens to be there. With it, the out-of-bounds read is always detected.

Step 3: verify the binary

Running ./src/curl --version confirms the instrumented binary and its features, as shown in the screenshot above. The report does not include the text of that output beyond the image.

Step 4: trigger the affected paths

The report does not give a concrete request or command that triggers the read. It states the trigger surface instead: any path that produces a struct httpreq (for example, a custom method) and then executes HTTP/2 translation or the CONNECT checks. The report states that AddressSanitizer will always detect the read. A custom method means any method word other than the usual GET, POST and so on, which a client can send when it needs to.

Root cause

The method string is copied into the allocation without a null terminator. The allocation is sized as sizeof(*req) + m_len, which leaves no byte for the terminator, and the memcpy writes only m_len bytes. The struct's char method[1] declaration is the usual flexible-array idiom, so the terminator has to be accounted for explicitly in the allocation size.

Nothing in the two consumers, strcmp("CONNECT", req->method) and strlen(req->method), checks the length before scanning. They rely on the terminator that was never written, so they can walk past the end of the allocation. The report notes that the outcome varies with the allocator and layout, and that AddressSanitizer always catches it.

A simple comparison: imagine a row of numbered lockers, and a note that says "read until you find an empty locker". If nobody left an empty locker at the end of your row, you will keep opening lockers that belong to other people. The note is not wrong; the setup was. Here the setup is the allocation that forgot to reserve the empty locker.

Impact

The researcher lists two consequences:

  1. Denial of service or crash: the out-of-bounds read occurs while HTTP/2 pseudo-headers are being created or while the CONNECT logic is being checked. A denial of service means the program stops working, so whatever it was doing for the user fails.
  2. Logic flaw: the CONNECT detection can behave differently because it compares a value that was never properly terminated, so a request could be misclassified. A CONNECT request asks a proxy to open a tunnel to another server. If it is treated as an ordinary request, or the other way round, the program does the wrong thing.

The trigger surface is any path that builds a struct httpreq, such as one carrying a custom method, and then runs HTTP/2 translation or the CONNECT checks. The report does not claim a specific attacker-controlled scenario beyond this, and the practical severity depends on the allocator and memory layout.

Fix

The proposed repair is minimal: allocate one extra byte and write the terminator after the copy, in both places that create the struct.

Diff
- req = calloc(1, sizeof(*req) + m_len);
+ req = calloc(1, sizeof(*req) + m_len + 1);
  if(!req) goto out;
  memcpy(req->method, method, m_len);
+ req->method[m_len] = '\0';

Line by line:

  • The removed line asked for exactly enough memory for the struct and the method text. The added line asks for one more byte, which is where the terminator will go.
  • if(!req) goto out; is unchanged: if the memory request failed, the code jumps to cleanup instead of using a null pointer.
  • memcpy(req->method, method, m_len); is unchanged: it still copies the method text.
  • req->method[m_len] = '\0'; is new. It writes a zero byte immediately after the copied text. In C source, '\0' means the zero character. Now strcmp and strlen have the marker they expect and stop at the right place.

The locations to change are Curl_http_req_make and Curl_http_req_make2, the two functions that create the struct. The researcher notes that this removes the undefined reads without changing the logic. As optional hardening, the report begins to suggest replacing strcmp("CONNECT", ...) but the text is cut off at that point.

Variants to test

  • Custom HTTP methods on any libcurl-based client that negotiates HTTP/2: a positive result is an AddressSanitizer report of an out-of-bounds read in the :method pseudo-header construction. A heap-buffer-overflow is AddressSanitizer's name for reading or writing past a block of memory obtained from the heap, the area where programs request memory at runtime.
  • Requests whose method is exactly CONNECT or close to it, sent over an HTTP/2 connection: a positive result is the CONNECT detection behaving differently because the comparison read past the allocation.
  • Any other code that stores a length-delimited string in a flexible-array member sized without a terminator: a positive result is a strlen or strcmp on that member reading past the allocation under AddressSanitizer. A length-delimited string is one where the length is tracked separately instead of relying on a terminator.
  • Builds with different allocators or optimisation levels: a positive result is a crash or misclassification whose occurrence depends on the allocator and memory layout.

Glossary

  • AddressSanitizer (ASan): a tool added to a program at build time that watches memory accesses and stops the program with a report when it reads or writes outside a valid block.
  • Allocator: the part of the system that hands out blocks of memory to a program and takes them back.
  • Buffer over-read: reading past the end of a block of memory that was allocated, without writing to it.
  • calloc: a C function that reserves a block of memory and fills it with zeros.
  • CMake: a tool that prepares and runs builds of C and C++ programs from a description of the project.
  • CONNECT: an HTTP method that asks a proxy to open a tunnel to another server.
  • curl: a command-line program and library for transferring data over the internet.
  • Debug build: a build that keeps extra information and skips heavy optimisation, making problems easier to find.
  • Denial of service (DoS): a failure that stops a program or service from working for its users.
  • Flexible-array idiom: a C pattern where a struct ends with a one-element array and the real data is stored right after the struct in the same allocation.
  • Heap: the area of memory a program requests at runtime, as opposed to memory fixed when the program is compiled.
  • HTTP: the protocol browsers and servers use to exchange web pages and data.
  • HTTP/2: a newer version of HTTP that carries requests in a binary format with pseudo-headers.
  • libcurl: the library part of curl, linked into other programs.
  • memcpy: a C function that copies a given number of bytes from one place in memory to another.
  • Method: the word at the start of an HTTP request, such as GET, POST or CONNECT.
  • nghttp2: a library that implements HTTP/2, used by curl when HTTP/2 support is enabled.
  • Null terminator: a zero byte placed after text in C to mark where the text ends.
  • Out-of-bounds read: reading memory outside the block that was allocated for the data.
  • pkg-config: a helper tool that reports which versions of installed libraries are available.
  • Pseudo-header: a special HTTP/2 header starting with a colon that describes the request itself, such as :method.
  • strcmp: a C function that compares two strings character by character until it finds a difference or a terminator.
  • strlen: a C function that measures a string by counting characters until it finds a terminator.
  • Struct: a bundle of related values kept together in memory under one name.
  • Undefined behaviour: a situation the language specification does not define, so the result depends on the compiler, the allocator and the machine.

More by gaurav_7777

View all

1 more disclosed report, 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.