postgresql/src/include
Alexander Korotkov ce146621f7 Prevent access to other sessions' temp tables
Commit b7b0f3f272 ("Use streaming I/O in sequential scans") routed
sequential scans through read_stream_next_buffer(), bypassing the
RELATION_IS_OTHER_TEMP() check in ReadBufferExtended().  As a result,
a superuser can attempt to read or modify temp tables of other
sessions through the read-stream path.  When the query plan uses no index,
SELECT/UPDATE/DELETE/MERGE silently see no rows / report zero affected rows,
and COPY produces an empty output -- because the buffer manager has no
visibility into the owning session's local buffers and silently returns
nothing.  Any query plan that uses, for instance, a btree index
still errors out via the existing check in ReadBufferExtended(), which
is reached from hio.c and nbtree respectively, but this is incidental.

Fix by enforcing RELATION_IS_OTHER_TEMP() at the three additional
buffer-manager entry points:

- read_stream_begin_impl() rejects the read at stream setup time,
  covering sequential and bitmap scans that go through the
  read-stream path.
- ReadBuffer_common() becomes the canonical place for the check,
  consolidating the existing one previously kept in
  ReadBufferExtended().  All ReadBufferExtended() callers go through
  ReadBuffer_common(), so the consolidation is behavior-preserving.
- StartReadBuffersImpl() catches direct callers of StartReadBuffers()
  that bypass both of the above.  This is currently defense-in-depth,
  but documents the contract for future code.

The companion test in src/test/modules/test_misc was added in the
preceding commit; this commit updates the assertions for SELECT,
UPDATE, DELETE, MERGE, and COPY (which previously documented the
bug as silent success) to expect the new error.

Author: Jim Jones <jim.jones@uni-muenster.de>
Author: Daniil Davydov <3danissimo@gmail.com>
Co-authored-by: Alexander Korotkov <aekorotkov@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Soumya S Murali <soumyamurali.work@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAJDiXghdFcZ8%3Dnh4G69te7iRr3Q0uFyXxb3ZdG09_GTNZXwH0g%40mail.gmail.com
Backpatch-through: 17
2026-05-14 15:01:17 +03:00
..
access Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
archive Update copyright for 2026 2026-01-01 13:24:10 -05:00
backup Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
bootstrap Reduce header inclusions via execnodes.h 2026-03-16 14:34:57 +01:00
catalog Do pre-release housekeeping on catalog data. 2026-05-13 10:54:44 -04:00
commands Harmonize function parameter names for Postgres 19. 2026-04-22 12:47:19 -04:00
common Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
datatype Update copyright for 2026 2026-01-01 13:24:10 -05:00
executor Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
fe_utils Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
foreign Add support for importing statistics from remote servers. 2026-04-08 19:15:00 +09:00
jit Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
lib Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
libpq Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
mb Remove MULE_INTERNAL encoding. 2026-04-08 17:40:06 +12:00
nodes Consider collation when proving subquery uniqueness 2026-05-05 10:23:31 +09:00
optimizer Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
parser Mark JumbleState as a const in the post_parse_analyze hook 2026-04-07 15:22:49 +09:00
partitioning Update copyright for 2026 2026-01-01 13:24:10 -05:00
pch Update copyright for 2026 2026-01-01 13:24:10 -05:00
port Add missing guard for __builtin_constant_p 2026-05-05 18:51:07 +07:00
portability Add missing PGDLLIMPORT markings 2026-04-08 15:49:33 +02:00
postmaster Harmonize function parameter names for Postgres 19. 2026-04-22 12:47:19 -04:00
regex Harden our regex engine against integer overflow in size calculations. 2026-05-11 05:13:46 -07:00
replication Harmonize function parameter names for Postgres 19. 2026-04-22 12:47:19 -04:00
rewrite Fix bogus calls in remove_self_join_rel() 2026-04-27 10:40:37 +09:00
snowball Update to latest Snowball sources. 2026-01-05 15:22:37 -05:00
statistics Add support for importing statistics from remote servers. 2026-04-08 19:15:00 +09:00
storage Make palloc_array() and friends safe against integer overflow. 2026-05-11 05:13:46 -07:00
tcop Avoid blocking indefinitely while finishing walsender shutdown 2026-05-01 12:12:44 +09:00
tsearch Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
utils Prevent access to other sessions' temp tables 2026-05-14 15:01:17 +03:00
.gitignore Use <stdint.h> and <inttypes.h> for c.h integers. 2024-12-04 15:05:38 +13:00
c.h Rework signal handler infrastructure to pass sender info as argument. 2026-04-15 07:30:34 -04:00
fmgr.h Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
funcapi.h Remove bits* typedefs. 2026-03-30 16:12:08 -05:00
getopt_long.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
Makefile Fix build inconsistency due to the generation of wait-event code 2026-02-02 08:02:39 +09:00
meson.build Ensure that all three build methods install the same set of files. 2026-02-16 15:20:15 -05:00
miscadmin.h Add missing PGDLLIMPORT markings 2026-04-08 15:49:33 +02:00
pg_config.h.in Add errdetail() with PID and UID about source of termination signal. 2026-04-07 10:22:33 -04:00
pg_config_manual.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
pg_getopt.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
pg_trace.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
pgstat.h Fix double-free in pg_stat_autovacuum_scores. 2026-04-09 13:07:06 -05:00
pgtar.h Harden astreamer tar parsing logic against archives it can't handle. 2026-04-02 12:20:36 -04:00
pgtime.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
port.h Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
postgres.h Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
postgres_ext.h Move pg_int64 back to postgres_ext.h 2025-09-16 10:48:56 +02:00
postgres_fe.h Update copyright for 2026 2026-01-01 13:24:10 -05:00
varatt.h Improve type handling of varlena structures 2026-02-11 07:33:24 +09:00
windowapi.h Update copyright for 2026 2026-01-01 13:24:10 -05:00