Skip to content

Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' (PgSQL)#5182

Merged
renecannao merged 2 commits intov3.0from
v3.0-fix_use_after_free_in_pgsql_proclist
Oct 24, 2025
Merged

Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' (PgSQL)#5182
renecannao merged 2 commits intov3.0from
v3.0-fix_use_after_free_in_pgsql_proclist

Conversation

@JavierJF
Copy link
Collaborator

Description

This PR addresses the same issue as #5180 but for PgSQL_Session and offers the same solution.

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.
@JavierJF JavierJF changed the title Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' Fix potential 'use-after-free' by Admin 'SHOW PROCESSLIST' (PgSQL) Oct 24, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @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 PgSQL_Session component, specifically targeting a potential 'use-after-free' vulnerability that could occur when an administrator executes the 'SHOW PROCESSLIST' command. The changes ensure that query data is accessed from a reliably managed source and that string copying operations are performed using safer, more appropriate functions to prevent memory corruption. This PR mirrors a previous fix for a similar issue in another session type, providing a consistent solution across relevant components.

Highlights

  • Memory Safety Fix: Addresses a potential 'use-after-free' vulnerability within the PgSQL_Session component, specifically when an administrator executes the 'SHOW PROCESSLIST' command.
  • Query Data Access Refinement: Modifies the method of retrieving the current query's pointer and length, switching from direct access to mybe->server_myds->myconn->query to using CurrentQuery.QueryPointer and CurrentQuery.QueryLength for improved stability.
  • Enhanced String Copying: Replaces strncpy with memcpy for copying query strings, particularly for truncated queries, which enhances memory safety and correctness by ensuring precise byte-level copying.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@sonarqubecloud
Copy link

@renecannao
Copy link
Contributor

Can one of the admins verify this patch?

Copy link

@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 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);

Choose a reason for hiding this comment

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

medium

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);

@renecannao renecannao merged commit 9dd4d54 into v3.0 Oct 24, 2025
4 of 5 checks passed
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