Skip to content
Memory corruption Disclosed report Intermediate

Incorrect sizeof() in Rustls Backend Memory Allocation

The code allocates memory for an array of cipher suite pointers using the size of the pointer variable instead of the size of the pointed-to element. On 64-bit systems this happens to be the same size, so the bug is masked, but on other architectures or if the type changes it could cause underallocation.

pelioro Follow

Originally disclosed on HackerOne

  • Disclosed after under an hour

Summary

There's a bug in lib/vtls/rustls.c where malloc() uses sizeof(cipher_suites) instead of sizeof(*cipher_suites). This allocates memory based on pointer size rather than element size.

Steps To Reproduce

  1. Look at lib/vtls/rustls.c line 530:
C
const struct rustls_supported_ciphersuite **cipher_suites = NULL;
  1. Then line 589:
C
cipher_suites = malloc(sizeof(cipher_suites) * (cipher_suites_len));

The problem: sizeof(cipher_suites) returns the size of a pointer (8 bytes on 64-bit), not the size of what it points to.

The Bug

C
// Line 530 - cipher_suites is a double pointer
const struct rustls_supported_ciphersuite **cipher_suites = NULL;

// Line 589 - Wrong: sizeof(pointer) instead of sizeof(element)
cipher_suites = malloc(sizeof(cipher_suites) * (cipher_suites_len));

Should be:

C
cipher_suites = malloc(sizeof(*cipher_suites) * cipher_suites_len);

Why This Matters

On 64-bit systems, this happens to work because:

  • sizeof(cipher_suites) = 8 (pointer size)
  • sizeof(*cipher_suites) = 8 (pointer to pointer size)

But the code is still wrong. It's using the wrong pattern and violates C best practices. If you look at the rest of the curl codebase, you'll see the correct pattern sizeof(*ptr) is used everywhere else.

Proof

I wrote a small test program that shows the issue:

C
const struct rustls_supported_ciphersuite **cipher_suites = NULL;

// What the code does now (WRONG)
size_t wrong = sizeof(cipher_suites);  // = 8 (pointer size)

// What it should do (CORRECT)  
size_t correct = sizeof(*cipher_suites);  // = 8 (element size)

On 64-bit: both are 8, so bug is masked On 32-bit or if struct changes: could cause underallocation

Impact

  • Type: Incorrect buffer size calculation
  • Currently works by accident on 64-bit
  • Code quality issue that should be fixed
  • Violates the pattern used everywhere else in curl

The original report as published on HackerOne, by pelioro. 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 developer used sizeof on the pointer variable instead of on the pointed-to type when calculating the allocation size.

Impact

On 64-bit systems the allocation size happens to be correct, so the bug is masked; on 32-bit systems or if the struct changes it could cause underallocation.

What the attacker needed

None

How it was found

  1. Locate the declaration of the cipher_suites pointer in the rustls backend file.
  2. Find the malloc call that allocates the array of cipher suite pointers.
  3. Observe that the size argument uses sizeof on the pointer variable rather than on the element type.

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 you ask for memory in C, you say how many items and how big each is. Here the programmer used the size of the address-holding variable, not the item. On most modern computers those sizes match, so nothing breaks, but elsewhere it could underallocate.

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 pelioro

View all

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