Skip to content

Clear Rsr back-pointers when Statement relinquishes ownership - #9198

Open
dand-oss wants to merge 1 commit into
FirebirdSQL:masterfrom
dand-oss:fix/statement-rsr-lifetime
Open

dand-oss wants to merge 1 commit into
FirebirdSQL:masterfrom
dand-oss:fix/statement-rsr-lifetime

Conversation

@dand-oss

@dand-oss dand-oss commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

When forced statement cleanup encounters an invalid attachment handle, Statement::release() can destroy the Statement while its Rsr remains alive. Rsr::rsr_self then points into the freed Statement; the later Rsr destructor reads that freed member. Clear the back-pointer when Statement relinquishes its handle and in the Statement destructor when a handle remains.

Independently reproduced on Linux x86-64 using the Firebird 5 ASan/UBSan debug build with system allocation enabled. A focused test compiled the actual remote/client/interface.cpp and linked the existing production client objects and libraries. The before/after sources differed only by the two fix hunks, with the same test entry point appended:

Rsr* handle = FB_NEW Rsr;
Remote::Statement* statement = FB_NEW Remote::Statement(handle, nullptr, 3);
statement->addRef();
statement->release();
delete handle;

With the fix removed, ASan reports a heap-use-after-free in Rsr::~Rsr(): an 8-byte read from the Statement allocation freed by Remote::Statement::release(). With the fix applied, the same test exits 0 with no ASan/UBSan report. ASan used detect_leaks=0 and halt_on_error=1.

The current-master patch also passes GCC 16.2 C++20 syntax compilation and git diff --check. The runtime test exercises the production ownership and forced-cleanup path through fault injection; it is not an end-to-end WinGLUE session or a runtime test of master.

Clear rsr_self before Statement::statement member becomes invalid.  The Rsr::rsr_self pointer was left dangling when Statement was freed before the Rsr, causing use-after-free when Rsr destructor accessed it.

Added Statement destructor and explicit clear in freeClientData.
@dand-oss
dand-oss marked this pull request as ready for review October 7, 2026 09:15
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