mbedtls: client: fix ALPN stack-use-after-scope - #3658
Open
saghul wants to merge 1 commit into
Open
Conversation
In lws_ssl_client_bio_create() the final else branch copies the ALPN from the ah into a temp_alpn[] buffer scoped to that block and points alpn_comma at it, but lws_mbedtls_set_alpn(conn->ctx, alpn_comma) runs after the block has closed, so it reads (and lws_strncpy()s) out-of-scope stack. This path is taken for a client wsi with no stash that carries its ALPN in the ah header, e.g. the QUIC->TCP fallback reset (lws_client_h3_grace_cb -> lws_client_reset). It happens to work because the stack slot is usually not yet reused, but it is undefined behaviour; ASAN reports stack-use-after-scope at lws_mbedtls_set_alpn(). Hoist temp_alpn to function scope so it outlives the assignment.
|
saghul
added a commit
to saghul/txiki.js
that referenced
this pull request
Aug 21, 2026
Adds patches/lws-mbedtls-client-alpn-uaf.patch (upstreamed as warmcat/libwebsockets#3658) and the CMake infrastructure to apply libwebsockets patches to the submodule work tree at configure time, mirroring the existing mbedTLS QUIC patch scheme (idempotent forward/reverse apply --check). This makes the HTTP/3 native-upgrade PR ASAN-green as a whole without waiting on the upstream merge: the fallback path triggers a stack-use-after-scope in lws_ssl_client_bio_create() that the patch fixes. Drop the patch and this block once the lws submodule is bumped past the merge.
saghul
added a commit
to saghul/txiki.js
that referenced
this pull request
Aug 21, 2026
Adds patches/lws-mbedtls-client-alpn-uaf.patch (upstreamed as warmcat/libwebsockets#3658) and the CMake infrastructure to apply libwebsockets patches to the submodule work tree at configure time, mirroring the existing mbedTLS QUIC patch scheme (idempotent forward/reverse apply --check). This makes the HTTP/3 native-upgrade PR ASAN-green as a whole without waiting on the upstream merge: the fallback path triggers a stack-use-after-scope in lws_ssl_client_bio_create() that the patch fixes. Drop the patch and this block once the lws submodule is bumped past the merge.
saghul
added a commit
to saghul/txiki.js
that referenced
this pull request
Aug 21, 2026
Adds patches/lws-mbedtls-client-alpn-uaf.patch (upstreamed as warmcat/libwebsockets#3658) and the CMake infrastructure to apply libwebsockets patches to the submodule work tree at configure time, mirroring the existing mbedTLS QUIC patch scheme (idempotent forward/reverse apply --check). This makes the HTTP/3 native-upgrade PR ASAN-green as a whole without waiting on the upstream merge: the fallback path triggers a stack-use-after-scope in lws_ssl_client_bio_create() that the patch fixes. Drop the patch and this block once the lws submodule is bumped past the merge.
lws-team
force-pushed
the
main
branch
10 times, most recently
from
August 31, 2026 10:48
591ce34 to
bc69b6b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



In
lws_ssl_client_bio_create()(mbedtls backend), the finalelsebranch copies the ALPN from the ah into atemp_alpn[128]buffer scoped to that block and pointsalpn_commaat it:lws_mbedtls_set_alpn()runs after the block closes andlws_strncpy()salpn_commaintoctx->alpn_strings, so it reads out-of-scope stack.This branch is taken for a client wsi that has no stash and carries its ALPN in the ah header — e.g. the QUIC→TCP fallback reset path (
lws_client_h3_grace_cb→lws_client_reset), where an Alt-Svc-learned h3 origin whose QUIC path doesn't answer is retried over TCP. It happens to work in practice because the stack slot is usually not reused before the copy, but it is undefined behaviour.ASAN reports it as
stack-use-after-scopeinlws_mbedtls_set_alpn(vialws_strncpy), reached fromlws_ssl_client_bio_create.Fix: hoist
temp_alpnto function scope so it outlives the assignment. One-line move; no behaviour change on any path.Verified downstream (txiki.js, mbedtls + native h3 Alt-Svc upgrade/fallback): the fallback test went from a reproducible
stack-use-after-scopeunder ASAN to clean with this change.