Fix mishandling of column-level SELECT privileges for join aliases.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Feb 2021 15:14:09 +0000 (10:14 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Feb 2021 15:14:09 +0000 (10:14 -0500)
commitc028faf2a62ce476c8d689a33b12471dc98b80f2
tree803914b64bb959f09bea8f945162772e543cf702
parent6214e2b2280462cbc3aa1986e350e167651b3905
Fix mishandling of column-level SELECT privileges for join aliases.

scanNSItemForColumn, expandNSItemAttrs, and ExpandSingleTable would
pass the wrong RTE to markVarForSelectPriv when dealing with a join
ParseNamespaceItem: they'd pass the join RTE, when what we need to
mark is the base table that the join column came from.  The end
result was to not fill the base table's selectedCols bitmap correctly,
resulting in an understatement of the set of columns that are read
by the query.  The executor would still insist on there being at
least one selectable column; but with a correctly crafted query,
a user having SELECT privilege on just one column of a table would
nonetheless be allowed to read all its columns.

To fix, make markRTEForSelectPriv fetch the correct RTE for itself,
ignoring the possibly-mismatched RTE passed by the caller.  Later,
we'll get rid of some now-unused RTE arguments, but that risks
API breaks so we won't do it in released branches.

This problem was introduced by commit 9ce77d75c, so back-patch
to v13 where that came in.  Thanks to Sven Klemm for reporting
the problem.

Security: CVE-2021-20229
src/backend/parser/parse_relation.c
src/backend/parser/parse_target.c
src/test/regress/expected/privileges.out
src/test/regress/sql/privileges.sql