Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd414bf

Browse files
committed
Be more wary about 32-bit integer overflow in pg_stat_statements.
We've heard a couple of reports of people having trouble with multi-gigabyte-sized query-texts files. It occurred to me that on 32-bit platforms, there could be an issue with integer overflow of calculations associated with the total query text size. Address that with several changes: 1. Limit pg_stat_statements.max to INT_MAX / 2 not INT_MAX. The hashtable code will bound it to that anyway unless "long" is 64 bits. We still need overflow guards on its use, but this helps. 2. Add a check to prevent extending the query-texts file to more than MaxAllocHugeSize. If it got that big, qtext_load_file would certainly fail, so there's not much point in allowing it. Without this, we'd need to consider whether extent, query_offset, and related variables shouldn't be off_t not size_t. 3. Adjust the comparisons in need_gc_qtexts() to be done in 64-bit arithmetic on all platforms. It appears possible that under duress those multiplications could overflow 32 bits, yielding a false conclusion that we need to garbage-collect the texts file, which could lead to repeatedly garbage-collecting after every hash table insertion. Per report from Bruno da Silva. I'm not convinced that these issues fully explain his problem; there may be some other bug that's contributing to the query-texts file becoming so large in the first place. But it did get that big, so #2 is a reasonable defense, and #3 could explain the reported performance difficulties. (See also commit 8bbe4cb, which addressed some related bugs. The second Discussion: link is the thread that led up to that.) This issue is old, and is primarily a problem for old platforms, so back-patch. Discussion: https://postgr.es/m/CAB+Nuk93fL1Q9eLOCotvLP07g7RAv4vbdrkm0cVQohDVMpAb9A@mail.gmail.com Discussion: https://postgr.es/m/[email protected]
1 parent d54fc7e commit dd414bf

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ _PG_init(void)
363363
&pgss_max,
364364
5000,
365365
100,
366-
INT_MAX,
366+
INT_MAX / 2,
367367
PGC_POSTMASTER,
368368
0,
369369
NULL,
@@ -1868,6 +1868,18 @@ qtext_store(const char *query, int query_len,
18681868

18691869
*query_offset = off;
18701870

1871+
/*
1872+
* Don't allow the file to grow larger than what qtext_load_file can
1873+
* (theoretically) handle. This has been seen to be reachable on 32-bit
1874+
* platforms.
1875+
*/
1876+
if (unlikely(query_len >= MaxAllocHugeSize - off))
1877+
{
1878+
errno = EFBIG; /* not quite right, but it'll do */
1879+
fd = -1;
1880+
goto error;
1881+
}
1882+
18711883
/* Now write the data into the successfully-reserved part of the file */
18721884
fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDWR | O_CREAT | PG_BINARY,
18731885
S_IRUSR | S_IWUSR);
@@ -2054,8 +2066,14 @@ need_gc_qtexts(void)
20542066
SpinLockRelease(&s->mutex);
20552067
}
20562068

2057-
/* Don't proceed if file does not exceed 512 bytes per possible entry */
2058-
if (extent < 512 * pgss_max)
2069+
/*
2070+
* Don't proceed if file does not exceed 512 bytes per possible entry.
2071+
*
2072+
* Here and in the next test, 32-bit machines have overflow hazards if
2073+
* pgss_max and/or mean_query_len are large. Force the multiplications
2074+
* and comparisons to be done in uint64 arithmetic to forestall trouble.
2075+
*/
2076+
if ((uint64) extent < (uint64) 512 * pgss_max)
20592077
return false;
20602078

20612079
/*
@@ -2065,7 +2083,7 @@ need_gc_qtexts(void)
20652083
* query length in order to prevent garbage collection from thrashing
20662084
* uselessly.
20672085
*/
2068-
if (extent < pgss->mean_query_len * pgss_max * 2)
2086+
if ((uint64) extent < (uint64) pgss->mean_query_len * pgss_max * 2)
20692087
return false;
20702088

20712089
return true;

0 commit comments

Comments
 (0)