Skip to content

bugfix: fix heap-use-after-free in receiveuntil after read error - #2520

Open
waterWang wants to merge 1 commit into
openresty:masterfrom
waterWang:fix/socket-tcp-receiveuntil-uaf
Open

bugfix: fix heap-use-after-free in receiveuntil after read error#2520
waterWang wants to merge 1 commit into
openresty:masterfrom
waterWang:fix/socket-tcp-receiveuntil-uaf

Conversation

@waterWang

Copy link
Copy Markdown

Closes #1146.

ngx_http_lua_socket_tcp_finalize_read_part clears cp->upstream
but leaves u->input_filter_ctx pointing at the compiled pattern.
After a read error the iterator userdata is garbage-collected; its
__gc handler sees cp->upstream == NULL and does not clear
u->input_filter_ctx, leaving a dangling pointer. A later
sock:close() dereferences it in ngx_http_lua_socket_tcp_finalize,
causing a heap-use-after-free.

Clear u->input_filter_ctx on both sides of the relationship when
finalizing the read part, matching what tcp_finalize already does.

Reported-by: BrookPanZM

ngx_http_lua_socket_tcp_finalize_read_part clears cp->upstream
but leaves u->input_filter_ctx pointing at the compiled pattern.
After a read error the iterator userdata becomes garbage-collected;
its __gc handler sees cp->upstream == NULL and does not clear
u->input_filter_ctx, leaving a dangling pointer. A later sock:close()
dereferences it in ngx_http_lua_socket_tcp_finalize, causing a
heap-use-after-free.

Clear u->input_filter_ctx on both sides of the relationship when
finalizing the read part, matching what tcp_finalize already does.
@zhuizhuhaomeng

Copy link
Copy Markdown
Contributor

@waterWang We need to add test cases to reproduce this issue althrough it seem harmness.

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.

2 participants