Skip to content

rustls: apply memory function overrides, fixing an ECH buffer free - #17830

Closed
vszakats wants to merge 1 commit into
curl:masterfrom
vszakats:rutls-memfix
Closed

vszakats wants to merge 1 commit into
curl:masterfrom
vszakats:rutls-memfix

Conversation

@vszakats

@vszakats vszakats commented Jul 6, 2025 •

Copy link
Copy Markdown
Member

Fixing:

Cherry-picked from #17827

Fixing:

- a local pair of malloc/free to use curl's memory allocators,
  and participate in memory tracing when enabled.

- a raw free() in ECH code that's malloced in lib code, causing
  an invalid free, also reported by valgrind (in non-unity builds).

Cherry-picked from curl#17827
@vszakats vszakats added the TLS label Jul 6, 2025
@cpu

cpu commented Jul 6, 2025

Copy link
Copy Markdown
Contributor

Thanks for taking a look at this.

a raw free() in ECH code that's malloced in lib code, causing an invalid free, also reported by valgrind (in non-unity builds).

Can you walk me through how 49807e2 fixes this for my own education? My naive assumption based on the description above is that something alloc'd by librustls was being free()'d in vtls/rusts.c (?) and so I thought the fix would involve replacing free() with a rustls_xxx API call that freed the resource. Am I totally off-base?

@vszakats

vszakats commented Jul 6, 2025 •

Copy link
Copy Markdown
Member Author

Thanks for taking a look at this.

a raw free() in ECH code that's malloced in lib code, causing an invalid free, also reported by valgrind (in non-unity builds).

Can you walk me through how 49807e2 fixes this for my own education? My naive assumption based on the description above is that something alloc'd by librustls was being free()'d in vtls/rusts.c (?) and so I thought the fix would involve replacing free() with a rustls_xxx API call that freed the resource. Am I totally off-base?

No, and sure, here's my understanding of this:

libcurl code overrides free/malloc with its own implementation (primarily
to allow libcurl user code overriding them with their implementation).
The same mechanics support curl's Memory Tracking (aka CURLDEBUG),
when enabled (it's enabled by default in debug-enabled builds, but it can
also be enabled on its own.)

To apply these overrides to a source file, it needs to include curl_memory.h
and memdebug.h.

In rustls.c there is only 3 affected calls in total. I think the malloc/free pair
(for cipher_suites) could well be replaced with a ruslt_xxx equivalent,
if that's more desirable here. (The buffer is passed to a Rustls API, but its
ownership remains within rustls.c AFAICS.)

The remaining free() call:

curl/lib/vtls/rustls.c

Lines 1002 to 1004 in beb64e6

if(data->set.tls_ech & CURLECH_CLA_CFG && data->set.str[STRING_ECH_CONFIG]) {
free(ech_config);
}

is called on echconfiglist. That's allocated in lib/httpsrr.c, using curl's
overridden allocator. So that must use its overridden free pair.

@cpu

cpu commented Jul 6, 2025 •

Copy link
Copy Markdown
Contributor

@vszakats Ah! Thank you for the explanation. That makes sense to me and I see where I was confused 👍

I think the malloc/free pair (for cipher_suites) could well be replaced with a ruslt_xxx equivalent, if that's more desirable here

I think those are OK, and there isn't a rustls_supported_ciphersuite_free() to slot in (or a provided collection/slice of rustls_supported_ciphersuites managed librustls-side). IIRC it's because the pointers have a static lifetime from the concrete implementations baked in to the crypto provider, they're not a dynamic resource.

@vszakats vszakats changed the title rustls: apply memory function overrides rustls: apply memory function overrides, fix a memory free in ECH builds Jul 6, 2025
@vszakats vszakats changed the title rustls: apply memory function overrides, fix a memory free in ECH builds rustls: apply memory function overrides, fix freeing an ECH buffer Jul 6, 2025
@vszakats vszakats changed the title rustls: apply memory function overrides, fix freeing an ECH buffer rustls: apply memory function overrides, fixing an ECH buffer free Jul 6, 2025
@vszakats vszakats closed this in cbf261e Jul 6, 2025
@vszakats
vszakats deleted the rutls-memfix branch July 6, 2025 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants