Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' (PgSQL)#5182
Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' (PgSQL)#5182renecannao merged 2 commits intov3.0from
Conversation
Accesses by 'stats___pgsql_processlist' to 'myconn->query.ptr' could lead to invalid memory accesses, as the pointed query could already have been free by the session after being issued.
As per the GCC manual, calls to 'strn*' function family shall be replaced by 'memcpy' when truncation (no null termination) is expected.
Summary of ChangesHello @JavierJF, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a critical memory safety fix for the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
|
Can one of the admins verify this patch? |
There was a problem hiding this comment.
Code Review
This pull request addresses a potential use-after-free vulnerability in SHOW PROCESSLIST for PostgreSQL sessions by changing how the current query is retrieved. Instead of accessing it through a potentially stale backend connection pointer, it now safely uses the session's CurrentQuery object. This is a solid improvement that enhances stability.
Additionally, the PR replaces a strncpy call with memcpy for safer handling of query strings that may not be null-terminated. I've pointed out an area where this improvement could be extended for consistency and to prevent potential truncation issues.
| memcpy(res, query_ptr, query_len - 3); | ||
| memcpy(res + (query_len - 3), "...", 3); | ||
| } else { | ||
| strncpy(res, query_ptr, query_len); |
There was a problem hiding this comment.
For consistency and correctness, this strncpy should also be replaced with memcpy, similar to the change in the if (trunc_query) block. The buffer pointed to by query_ptr is not guaranteed to be a null-terminated C-string and may contain embedded null characters. strncpy would stop at the first null character, leading to an incorrectly truncated query. Using memcpy ensures that the specified number of bytes are copied, preserving the integrity of the query string.
memcpy(res, query_ptr, query_len);


Description
This PR addresses the same issue as #5180 but for
PgSQL_Sessionand offers the same solution.