postgres.git / summary / log / commit / refs

commit    a0b0136259080c0441f4ff1c7d65d8a9d08360cf
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Tue Oct 03 06:37:23 2023 +0000
First released in: 17.0, 12.17, 13.13, 14.10, 15.5, 16.1

    Avoid memory size overflow when allocating backend activity buffer
    
    The code in charge of copying the contents of PgBackendStatus to local
    memory could fail on memory allocation because of an overflow on the
    amount of memory to use.  The overflow can happen when combining a high
    value track_activity_query_size (max at 1MB) with a large
    max_connections, when both multiplied get higher than INT32_MAX as both
    parameters treated as signed integers.  This could for example trigger
    with the following functions, all calling pgstat_read_current_status():
    - pg_stat_get_backend_subxact()
    - pg_stat_get_backend_idset()
    - pg_stat_get_progress_info()
    - pg_stat_get_activity()
    - pg_stat_get_db_numbackends()
    
    The change to use MemoryContextAllocHuge() has been introduced in
    8d0ddccec636, so backpatch down to 12.
    
    Author: Jakub Wartak
    Discussion: https://postgr.es/m/CAKZiRmw8QSNVw2qNK-dznsatQqz+9DkCquxP0GHbbv1jMkGHMA@mail.gmail.com
    Backpatch-through: 12


src/backend/postmaster/pgstat.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 1e2064456b0..bcf0de1a44b 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -3409,7 +3409,8 @@ pgstat_read_current_status(void) NAMEDATALEN * NumBackendStatSlots); localactivity = (char *) MemoryContextAllocHuge(pgStatLocalContext, - pgstat_track_activity_query_size * NumBackendStatSlots); + (Size) pgstat_track_activity_query_size * + (Size) NumBackendStatSlots); #ifdef USE_SSL localsslstatus = (PgBackendSSLStatus *) MemoryContextAlloc(pgStatLocalContext, [parent: 6103d2ccaed4]