From 7d85d87f4d5c35fd5b2d38adaef63dfbfa542ccc Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Tue, 5 Nov 2024 09:39:43 +0900 Subject: [PATCH] Clear padding of PgStat_HashKey when handling pgstats entries PgStat_HashKey is currently initialized in a way that could result in random data if the structure has any padding bytes. The structure has no padding bytes currently, fortunately, but it could become a problem should the structure change at some point in the future. The code is changed to use some memset(0) so as any padding would be handled properly, as it would be surprising to see random failures in the pgstats entry lookups. PgStat_HashKey is a structure internal to pgstats, and an ABI change could be possible in the scope of a bug fix, so backpatch down to 15 where this has been introduced. Author: Bertrand Drouvot Reviewed-by: Jelte Fennema-Nio, Michael Paquier Discussion: https://postgr.es/m/Zyb7RW1y9dVfO0UH@ip-10-97-1-34.eu-west-3.compute.internal Backpatch-through: 15 --- src/backend/utils/activity/pgstat.c | 3 +++ src/backend/utils/activity/pgstat_shmem.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index be48432cc38..ea8c5691e87 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -938,6 +938,9 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid) pgstat_prep_snapshot(); + /* clear padding */ + memset(&key, 0, sizeof(struct PgStat_HashKey)); + key.kind = kind; key.dboid = dboid; key.objid = objid; diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c index a09c6fee055..c1b7ff76b12 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -432,11 +432,18 @@ PgStat_EntryRef * pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create, bool *created_entry) { - PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid}; + PgStat_HashKey key; PgStatShared_HashEntry *shhashent; PgStatShared_Common *shheader = NULL; PgStat_EntryRef *entry_ref; + /* clear padding */ + memset(&key, 0, sizeof(struct PgStat_HashKey)); + + key.kind = kind; + key.dboid = dboid; + key.objid = objid; + /* * passing in created_entry only makes sense if we possibly could create * entry. @@ -908,10 +915,17 @@ pgstat_drop_database_and_contents(Oid dboid) bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid) { - PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid}; + PgStat_HashKey key; PgStatShared_HashEntry *shent; bool freed = true; + /* clear padding */ + memset(&key, 0, sizeof(struct PgStat_HashKey)); + + key.kind = kind; + key.dboid = dboid; + key.objid = objid; + /* delete local reference */ if (pgStatEntryRefHash) {