Skip to content
Memory corruption Disclosed report Advanced

Stack Buffer Overflow in curl's OpenSSL Provider Handling

The ossl_set_provider() function in curl copies a provider name into a fixed-size stack buffer without checking its length, so a name longer than 128 bytes overflows the buffer. An attacker who can control the provider name could overwrite stack memory, potentially leading to code execution, denial of service, or information disclosure.

oblivionsage Follow

Originally disclosed on HackerOne

  • Disclosed after 40 days

Summary

Hello curl Team,

I found a stack buffer overflow in curl's OpenSSL provider handling code. The bug is in ossl_set_provider() function located in lib/vtls/openssl.c. When a provider name longer than MAX_PROVIDER_LEN is passed, the function copies it to a fixed-size buffer without proper length checking, causing stack overflow

Vulnerability Location

The buffer overflow happens in lib/vtls/openssl.c at line 2003-2004:

C
memcpy(name, curlx_str(&prov), curlx_strlen(&prov));
name[curlx_strlen(&prov)] = 0;

Code Analysis

I was looking at curl's SSL engine handling and noticed something strange in the provider setup. Here is what happens:

  1. The ossl_set_provider() function defines a fixed buffer:
C
char name[MAX_PROVIDER_LEN + 1];  // line 1986

where MAX_PROVIDER_LEN is defined as 128 (line 1974 in the same file)

  1. Later in the function (line 2003-2004), it copies data from the provider string:
C
memcpy(name, curlx_str(&prov), curlx_strlen(&prov));
name[curlx_strlen(&prov)] = 0;

The problem is there's no check if curlx_strlen(&prov) is less than or equal to MAX_PROVIDER_LEN. This lets us overflow the stack-based buffer if we provide a string longer than 128 bytes

Also notice name[curlx_strlen(&prov)] = 0; this writes a null byte past the end of the buffer when overflow happens

Reproducing the Bug

I tried to find ways to trigger this issue. The code path is:

  1. ossl_set_engine() in lib/vtls/openssl.c calls ossl_set_provider()
  2. ossl_set_engine() is called by Curl_ssl_set_engine()
  3. Curl_ssl_set_engine() is triggered by CURLOPT_SSLENGINE option

I couldn't trigger a crash directly with curl command line because it stops with **"SSL Engine not found"**error before hitting the vulnerable code

Proof of Concept

I created a minimal PoC that reproduces the exact vulnerable code pattern:

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Same as in curl code
#define MAX_PROVIDER_LEN 128

typedef struct {
    char *data;
    size_t len;
} Curl_str;

// Mimicking curl's functions
char *curlx_str(Curl_str *s) {
    return s->data;
}

size_t curlx_strlen(Curl_str *s) {
    return s->len;
}

// Recreating the vulnerable function
void simulate_curl_provider_vulnerability(const char *iname) {
    char name[MAX_PROVIDER_LEN + 1];  // Same buffer size as curl
    Curl_str prov;
    
    printf("[+] Input: %s\n", iname);
    
    // Set up provider string
    prov.data = (char *)iname;
    prov.len = strlen(iname);
    
    printf("[+] Provider length: %zu\n", prov.len);
    printf("[+] Buffer size: %zu\n", sizeof(name));
    
    // VULNERABLE PART - exactly as in curl source
    memcpy(name, curlx_str(&prov), curlx_strlen(&prov));
    name[curlx_strlen(&prov)] = 0;
    
    printf("[+] Done\n");
}

int main(void) {
    char *overflow_string = malloc(300);
    
    // Creating long provider name to trigger overflow
    strcpy(overflow_string, "pkcs11:");
    for(int i = 0; i < 200; i++) {
        strcat(overflow_string, "A");
    }
    
    // Call the vulnerable function
    simulate_curl_provider_vulnerability(overflow_string);
    
    free(overflow_string);
    return 0;
}

When compiled with AddressSanitizer and run:

Bash
gcc -g -fsanitize=address -o poc poc.c
./poc

It clearly shows stack buffer overflow:

image.png
image.png From the original on HackerOne

Additional Notes

  • I tried to trigger this vulnerability directly using the curl command line tool but wasn't successful as curl returns an "SSL Engine not found" error before reaching the vulnerable code. However, the vulnerable code is clearly present in the curl source and confirmed with my PoC

  • I can provide more detailed analysis with GDB if needed

Impact

This bug allows overwriting stack memory with controlled content. In specific contexts, it could cause to:

  • Remote code execution
  • Denial of service
  • Information disclosure

The severity depends on how this code is used in applications. It is most dangerous if an attacker can control provider name input

Thank you for reviewing also I can provide more detailed analysis with GDB if needed I didnt want to make the report very long

The original report as published on HackerOne, by oblivionsage. 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 code uses memcpy to copy a provider string into a fixed-size stack buffer without first checking that the string length is within the buffer's capacity.

Impact

The overflow could allow overwriting stack memory with controlled content, potentially leading to remote code execution, denial of service, or information disclosure, though the reporter could not trigger it via the curl command line because an 'SSL Engine not found' error occurs first.

What the attacker needed

Attacker controls provider name; provider name longer than 128 bytes

How it was found

  1. Attempt to set CURLOPT_SSLENGINE to a provider name longer than 128 bytes, though the CLI may fail first.
  2. This triggers Curl_ssl_set_engine(), which calls ossl_set_engine(), which calls ossl_set_provider().
  3. ossl_set_provider() copies the long name into a fixed-size stack buffer, causing a stack buffer overflow.

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 stack buffer overflow happens when a program puts more data into a small fixed-size memory area than it can hold, so the extra data spills over and overwrites nearby memory. Here, curl copies a provider name into a 128-byte buffer without checking if the name is longer, so a long name can overwrite other data on the stack. If an attacker can supply that long name, they might be able to crash the program or even run their own code.

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 oblivionsage

View all

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