Skip to content

MDEV-37556 Memory leak in proxy protocol with name resolution enabled#5135

Open
vaintroub wants to merge 1 commit into
10.6from
MDEV-37556
Open

MDEV-37556 Memory leak in proxy protocol with name resolution enabled#5135
vaintroub wants to merge 1 commit into
10.6from
MDEV-37556

Conversation

@vaintroub
Copy link
Copy Markdown
Member

When proxy protocol is used and --skip-name-resolve is not set, thd_set_peer_addr() is called twice per connection: once for the real TCP peer (in check_connection), and again for the proxied address (in handle_proxy_header). Each call invokes ip_to_hostname(), which allocates a hostname string (unless loopback connection is used) and stores it as thd->main_security_ctx.host. That code missed to free previously allocated hostname, which results into memory leak.

This is now fixed. Also added debug-only test to mysql_client_test, which fakes DNS and IP resolution the same way some perfschema tests do, to emulate remote TCP connection in MTR.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a memory leak in the proxy protocol for non-loopback connections (MDEV-37556) by freeing any previously resolved hostname before overwriting it in thd_set_peer_addr. It also adds a corresponding integration test test_proxy_header_dbug_remote_connection to verify the fix. The reviewer suggested improving portability by statically defining the binary representation of the IPv6 address instead of using inet_pton, which can introduce platform-specific socket library dependencies.

Comment thread tests/mysql_client_test.c
Comment thread tests/mysql_client_test.c
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a connection-time memory leak when Proxy Protocol is enabled and hostname resolution is active, by ensuring previously resolved hostnames are freed before being overwritten on subsequent thd_set_peer_addr() calls.

Changes:

  • Free any previously resolved THD::main_security_ctx.host before calling ip_to_hostname() again in thd_set_peer_addr().
  • Add a debug-only mysql_client_test case that forces non-loopback peer/proxied addresses via DBUG hooks to exercise the double-resolution path under MTR.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/mysql_client_test.c Adds a debug-only regression test that fakes peer address + DNS resolution to reproduce the double ip_to_hostname() scenario with Proxy Protocol.
sql/sql_connect.cc Frees an existing resolved hostname in main_security_ctx.host before overwriting it to prevent leaks on repeated thd_set_peer_addr() calls.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread tests/mysql_client_test.c Outdated
Comment on lines 1 to 4
--loose-enable-performance-schema
--max-allowed-packet=32000000
--proxy-protocol-networks=::1/32,127.0.0.0/8,localhost
--proxy-protocol-networks=::1/32,127.0.0.0/8,localhost,2001:db8::6:6
--sequence=on
Copy link
Copy Markdown
Member

@vuvova vuvova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix looks ok. two gemini comments look reasonable and probably deserve to be addressed.

When proxy protocol is used and --skip-name-resolve is not set,
thd_set_peer_addr() is called twice per connection: once for the real TCP peer (in check_connection),
and again for the proxied address (in handle_proxy_header).
Each call invokes ip_to_hostname(), which allocates a hostname string
(unless loopback connection is used) and stores it as
thd->main_security_ctx.host. That code missed to free previously
allocated hostname, which results into memory leak.

This is now fixed. Also added debug-only test to mysql_client_test, which
fakes DNS and IP resolution the same way some perfschema tests do, to
emulate remote TCP connection in MTR.
@vaintroub
Copy link
Copy Markdown
Member Author

fix looks ok. two gemini comments look reasonable and probably deserve to be addressed.

Copilot comments deserved to be addressed, which I also did now. Gemini not so much, it just nitpicked about inet_ntop, with its concerns about portability, but if it read the test attentively, it would find that it is already used, in the same file.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@vuvova
Copy link
Copy Markdown
Member

vuvova commented May 28, 2026

fix looks ok. two gemini comments look reasonable and probably deserve to be addressed.

Copilot comments deserved to be addressed, which I also did now. Gemini not so much, it just nitpicked about inet_ntop, with its concerns about portability, but if it read the test attentively, it would find that it is already used, in the same file.

Right. Sorry. I meant "Two non-resolved AI comments". Indeed, they were Copilot's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants