Skip to content
Memory corruption Disclosed report Advanced

Buffer overflow and affected url:-https://github.com/curl/curl/blob/master/docs/examples/hsts-preload.c

The report claims that the hstsread function in a curl example code does not properly check the length of the host string before copying it into a buffer, potentially leading to a buffer overflow. An attacker could exploit this to inject arbitrary code, but the report is speculative and lacks a concrete proof of concept.

cyberguardianrd Follow

Originally disclosed on HackerOne

Critical View original
  • Disclosed after 9 hours

Summary:

A buffer overflow, also known as a buffer overrun, occurs when a program or process attempts to write more data to a buffer than the buffer is allocated to hold. This can happen if the program does not properly check the length of the data before writing it to the buffer, or if the program allocates too little space for the buffer.

Steps To Reproduce:

[add details for how we can reproduce the issue]

  1. The hstsread function in the provided code does not properly check the length of the host string before copying it into the e->name buffer. This could lead to a buffer overflow, allowing an attacker to inject arbitrary code into the application.this could exploited by a malicious domain or website whose url should be long enough to overflow buffer as it's using strcpy function

Condition a malicious preload host is required to exploit this if it's meet government can use it for zero click attack

Recommendation:

The hstsread function should be modified to check the length of the host string before copying it into the e->name buffer. If the string is too long, the function should return an error code

Supporting Material/References:

[list any additional material (e.g. screenshots, logs, etc.)]

  • [attachment / reference]

Affected url:-https://github.com/curl/curl/blob/master/docs/examples/hsts-preload.c Here is the vulnerable code if attacker or government manipulate developer to add a very long domain name in hsts_preload then this will result remote code execution

#include <stdio.h> #include <string.h> #include <curl/curl.h>

struct entry { const char *name; const char *exp; };

static const struct entry preload_hosts[] = { { "example.com", "20370320 01:02:03" }, { "curl.se", "20370320 03:02:01" }, { NULL, NULL } /* end of list marker */ };

struct state { int index; };

/* "read" is from the point of the library, it wants data from us. One domain entry per invoke. */ static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e, void *userp) { const char *host; const char *expire; struct state *s = (struct state *)userp; (void)easy; host = preload_hosts[s->index].name; expire = preload_hosts[s->index++].exp;

if(host && (strlen(host) < e->namelen)) { strcpy(e->name, host); e->includeSubDomains = 0; strcpy(e->expire, expire); fprintf(stderr, "HSTS preload '%s' until '%s'\n", host, expire); } else return CURLSTS_DONE; return CURLSTS_OK; }

static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e, struct curl_index *i, void userp) { (void)easy; (void)userp; / we have no custom input */ printf("[%u/%u] %s %s\n", (unsigned int)i->index, (unsigned int)i->total, e->name, e->expire); return CURLSTS_OK; }

int main(void) { CURL *curl; CURLcode res;

curl = curl_easy_init(); if(curl) { struct state st = {0};

/* enable HSTS for this handle */
curl_easy_setopt(curl, CURLOPT_HSTS_CTRL, (long)CURLHSTS_ENABLE);

/* function to call at first to populate the cache before the transfer */
curl_easy_setopt(curl, CURLOPT_HSTSREADFUNCTION, hstsread);
curl_easy_setopt(curl, CURLOPT_HSTSREADDATA, &st);

/* function to call after transfer to store the new state of the HSTS
   cache */
curl_easy_setopt(curl, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
curl_easy_setopt(curl, CURLOPT_HSTSWRITEDATA, NULL);

/* use the domain with HTTP but due to the preload, it should do the
   transfer using HTTPS */
curl_easy_setopt(curl, CURLOPT_URL, "http://curl.se");

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
  fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);

} return 0; }

Impact

An attacker could exploit this vulnerability to inject arbitrary code into the application. This could allow the attacker to take control of the application and perform actions on behalf of the user.

The original report as published on HackerOne, by cyberguardianrd. 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 example code uses strcpy without verifying that the host string length is less than the buffer size, despite a length check that may be insufficient.

Impact

An attacker could potentially execute arbitrary code in the example code context, requiring a malicious preload host.

What the attacker needed

Malicious preload host; developer adds long domain to example

The fix

The hstsread function should be modified to check the length of the host string before copying it into the e->name buffer.

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.

A buffer overflow happens when a program writes more data into a fixed-size memory space than it can hold, which can corrupt nearby memory. In this case, the example code copies a domain name into a buffer without checking if it fits, which could let an attacker run their own code. However, the report is about example code, not the actual curl library, and no real attack is demonstrated.

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 cyberguardianrd

View all

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