listen: ip bind: fix numeric iface parse overrunning caller sockaddr_in - #3646
Closed
saghul wants to merge 3 commits into
Closed
listen: ip bind: fix numeric iface parse overrunning caller sockaddr_in#3646saghul wants to merge 3 commits into
saghul wants to merge 3 commits into
Conversation
lws_interface_to_sa() may be handed a buffer as small as a struct sockaddr_in: lws_socket_bind()'s AF_INET path passes its 16-byte serv_addr4 (with addrlen = sizeof(serv_addr4)). When ifname is not a real interface name, the fallback casts that buffer to lws_sockaddr46 * and calls lws_sa46_parse_numeric_address(), which begins with memset(sa46, 0, sizeof(*sa46)); sizeof(lws_sockaddr46) is 28 bytes when IPv6 is enabled (it embeds a struct sockaddr_in6), so this writes 12 bytes past a 16-byte caller buffer. ASan reports a stack-buffer-overflow in lws_sa46_parse_numeric_address() for any listen bind to a numeric IPv4 address (eg, lws_socket_bind() -> lws_interface_to_sa() with iface "127.0.0.1"); in a release build the overrun silently clobbers adjacent stack. addrlen is already passed for exactly this reason but was ignored on this path. Parse into a local full-width lws_sockaddr46 and memcpy back only min(sa46_socklen(), addrlen) bytes, so the caller's buffer is never overrun regardless of family. The 28-byte memset was introduced with the recent lws_sa46_parse_numeric_address() rework; the (lws_sockaddr46 *)addr cast predates it and was harmless while the function only wrote the parsed family's fields.
|
lws-team
force-pushed
the
main
branch
6 times, most recently
from
July 27, 2026 20:02
558a432 to
4e59faf
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.



lws_interface_to_sa() may be handed a buffer as small as a struct
sockaddr_in: lws_socket_bind()'s AF_INET path passes its 16-byte
serv_addr4 (with addrlen = sizeof(serv_addr4)). When ifname is not a
real interface name, the fallback casts that buffer to lws_sockaddr46 *
and calls lws_sa46_parse_numeric_address(), which begins with
sizeof(lws_sockaddr46) is 28 bytes when IPv6 is enabled (it embeds a
struct sockaddr_in6), so this writes 12 bytes past a 16-byte caller
buffer. ASan reports a stack-buffer-overflow in
lws_sa46_parse_numeric_address() for any listen bind to a numeric IPv4
address (eg, lws_socket_bind() -> lws_interface_to_sa() with iface
"127.0.0.1"); in a release build the overrun silently clobbers adjacent
stack.
addrlen is already passed for exactly this reason but was ignored on
this path. Parse into a local full-width lws_sockaddr46 and memcpy back
only min(sa46_socklen(), addrlen) bytes, so the caller's buffer is never
overrun regardless of family.
The 28-byte memset was introduced with the recent
lws_sa46_parse_numeric_address() rework; the (lws_sockaddr46 *)addr cast
predates it and was harmless while the function only wrote the parsed
family's fields.