postgres.git / summary / log / commit / refs
commit 078110c0075c3773ff4c2d84d3e8808f0748b1fd
Author: Michael Paquier <michael@paquier.xyz>
Date: Wed Oct 14 03:05:15 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0
Fix potential use-after-frees in WAL sender
A WAL sender tracks its WAL reader and logical decoding context as two
static variables, which were left set after being freed for a
START_REPLICATION command. The WAL reader is used for in a physical
replication command, both the WAL reader and the logical decoding
context are used for a logical replication command.
Both variables are now cleared once their contents are released at the
end of each replication command, to keep the release of the resources
and the maintenance of the pointers close to each other. Some more
cleanup is done by WalSndErrorCleanup(), as well, once the WAL reader is
clear.
The assertion added by 879ad9f90e83 is moved before the execution of a
replication command, checking now that both the WAL reader and the
logical decoding context are never set before the execution of a new
command.
Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAJTYsWW+hdmmVt_A4hvGv2_5RWK8-kaOf5ncT65=5i_hyD=BHg@mail.gmail.com
Backpatch-through: 14
src/backend/replication/walsender.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index fcf22b7cd14..e4f1459d35e 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -338,6 +338,9 @@ WalSndErrorCleanup(void)
if (xlogreader != NULL && xlogreader->seg.ws_file >= 0)
wal_segment_close(xlogreader);
+ xlogreader = NULL;
+ logical_decoding_ctx = NULL;
+
if (MyReplicationSlot != NULL)
ReplicationSlotRelease();
@@ -859,6 +862,9 @@ StartReplication(StartReplicationCmd *cmd)
Assert(streamingDoneSending && streamingDoneReceiving);
}
+ XLogReaderFree(xlogreader);
+ xlogreader = NULL;
+
if (cmd->slotname)
ReplicationSlotRelease();
@@ -1363,6 +1369,8 @@ StartLogicalReplication(StartReplicationCmd *cmd)
WalSndLoop(XLogSendLogical);
FreeDecodingContext(logical_decoding_ctx);
+ logical_decoding_ctx = NULL;
+ xlogreader = NULL;
ReplicationSlotRelease();
replication_active = false;
@@ -1759,6 +1767,9 @@ exec_replication_command(const char *cmd_string)
CHECK_FOR_INTERRUPTS();
+ Assert(xlogreader == NULL);
+ Assert(logical_decoding_ctx == NULL);
+
/*
* Prepare to parse and execute the command.
*/
@@ -1892,7 +1903,6 @@ exec_replication_command(const char *cmd_string)
/* dupe, but necessary per libpqrcv_endstreaming */
EndReplicationCommand(cmdtag);
- Assert(xlogreader != NULL);
break;
}
[parent: 43eb4afde7a7]