Skip to content

bugfix: balancer.bind_to_local_addr() crashed the worker on an invalid address. - #2539

Open
perfgao wants to merge 1 commit into
openresty:masterfrom
perfgao:bugfix-balancer-bind-declined-addr
Open

perfgao wants to merge 1 commit into
openresty:masterfrom
perfgao:bugfix-balancer-bind-declined-addr

Conversation

@perfgao

@perfgao perfgao commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

ngx_parse_addr_port() returns NGX_DECLINED when the text is not an address; NGX_ERROR only means an allocation failed. ngx_http_lua_ffi_balancer_bind_to_local_addr() only checks for NGX_ERROR:

rc = ngx_parse_addr_port(r->pool, local, (u_char *) addr, addr_len);
if (rc == NGX_ERROR) {

So balancer.bind_to_local_addr("not-an-ip") returns true, and an ngx_addr_t whose sockaddr was never set (it comes from ngx_palloc()) is stored in bp->local. ngx_event_connect_peer() then calls ngx_inet_get_port() and bind() on that pointer:

[lua] balancer_by_lua(nginx.conf:11):4: bind_to_local_addr returned ok=true err=nil while connecting to upstream
[alert] worker process 2710294 exited on signal 11

The Lua wrapper only checks that the argument is a string, so any string that is not an address reaches this path, for example a typo in the configuration or an address taken from request data.

This change treats anything but NGX_OK as an invalid address and keeps the previous bp->local, which is what ngx_stream_lua_ffi_balancer_bind_to_local_addr() already does. With it, the call returns nil, "invalid addr not-an-ip" and the request goes on with the previously bound address.

TEST 6 in t/138-balancer-upstream-bind.t binds 127.0.0.2, then "not-an-ip", and expects the error to be logged and the upstream to see 127.0.0.2. Without the fix the worker crashes and the test fails; with it t/138-balancer-upstream-bind.t passes. Tested against nginx 1.31.1 in an OpenResty build; the function and the test file are the same as on master.

…d address.

ngx_parse_addr_port() returns NGX_DECLINED when the text is not an
address; NGX_ERROR only means an allocation failed. The FFI function only
checked for NGX_ERROR, so an invalid address such as "not-an-ip" was
reported as success, and an ngx_addr_t whose sockaddr was never set (it
comes from ngx_palloc()) was published to bp->local. The upstream connect
then called ngx_inet_get_port() and bind() on that pointer, and the worker
exited on signal 11.

Treat anything but NGX_OK as an invalid address and keep the previous
bp->local, as ngx_stream_lua_ffi_balancer_bind_to_local_addr() already
does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant