pg_stat_get_activity(): integer overflow due to (int) * (int) for MemoryContextAllocHuge()
Jakub Wartak <jakub.wartak@enterprisedb.com>
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Robert Haas <robertmhaas@gmail.com>, Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Date: 2023-09-27T06:41:55Z
Lists: pgsql-hackers
Attachments
- 0001-Adjust-pgstat_track_activity_query_size-to-be-of-siz.patch (application/octet-stream) patch 0001
Hi -hackers,
We've got customer report that high max_connections (3k) with high
pgstat_track_activity_query_size (1MB) ends up with:
postgres=# select * from pg_stat_get_activity(NULL);
ERROR: invalid memory alloc request size 18446744072590721024
postgres=# select version();
version
------------------------------------------------------------------------------------------------------
PostgreSQL 17devel on x86_64-pc-linux-gnu, compiled by gcc (Debian
10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)
it's in:
#0 errstart (elevel=elevel@entry=21, domain=domain@entry=0x0) at elog.c:358
#1 0x000055971cafc9a8 in errstart_cold (elevel=elevel@entry=21,
domain=domain@entry=0x0) at elog.c:333
#2 0x000055971cb018b7 in MemoryContextAllocHuge (context=<optimized
out>, size=18446744072590721024) at mcxt.c:1594
#3 0x000055971ce2fd59 in pgstat_read_current_status () at backend_status.c:767
#4 0x000055971ce30ab1 in pgstat_read_current_status () at backend_status.c:1167
#5 pgstat_fetch_stat_numbackends () at backend_status.c:1168
#6 0x000055971ceccc7f in pg_stat_get_activity (fcinfo=0x55971e239ff8)
at pgstatfuncs.c:308
It seems to be integer overflow due to (int) * (int), while
MemoryContextAllocHuge() allows taking Size(size_t) as parameter. I
get similar behaviour with:
size_t val = (int)1048576 * (int)3022;
Attached patch adjusts pgstat_track_activity_query_size to be of
size_t from int and fixes the issue.
Regards,
-Jakub Wartak.
Commits
-
Avoid memory size overflow when allocating backend activity buffer
- 9b3900cdb734 12.17 landed
- a0b013625908 13.13 landed
- f91c87b31463 14.10 landed
- 95e91da66cf5 15.5 landed
- 75f31a3f2674 16.1 landed
- a956bd3fa9d9 17.0 landed