Skip to content

Commit 1f23d1c

Browse files
committed
Always require SELECT permission for ON CONFLICT DO UPDATE.
The update path of an INSERT ... ON CONFLICT DO UPDATE requires SELECT permission on the columns of the arbiter index, but it failed to check for that in the case of an arbiter specified by constraint name. In addition, for a table with row level security enabled, it failed to check updated rows against the table's SELECT policies when the update path was taken (regardless of how the arbiter index was specified). Backpatch to 9.5 where ON CONFLICT DO UPDATE and RLS were introduced. Security: CVE-2017-15099
1 parent 971983f commit 1f23d1c

File tree

8 files changed

+194
-11
lines changed

8 files changed

+194
-11
lines changed

src/backend/catalog/pg_constraint.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,104 @@ get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
814814
return conOid;
815815
}
816816

817+
/*
818+
* get_relation_constraint_attnos
819+
* Find a constraint on the specified relation with the specified name
820+
* and return the constrained columns.
821+
*
822+
* Returns a Bitmapset of the column attnos of the constrained columns, with
823+
* attnos being offset by FirstLowInvalidHeapAttributeNumber so that system
824+
* columns can be represented.
825+
*
826+
* *constraintOid is set to the OID of the constraint, or InvalidOid on
827+
* failure.
828+
*/
829+
Bitmapset *
830+
get_relation_constraint_attnos(Oid relid, const char *conname,
831+
bool missing_ok, Oid *constraintOid)
832+
{
833+
Bitmapset *conattnos = NULL;
834+
Relation pg_constraint;
835+
HeapTuple tuple;
836+
SysScanDesc scan;
837+
ScanKeyData skey[1];
838+
839+
/* Set *constraintOid, to avoid complaints about uninitialized vars */
840+
*constraintOid = InvalidOid;
841+
842+
/*
843+
* Fetch the constraint tuple from pg_constraint. There may be more than
844+
* one match, because constraints are not required to have unique names;
845+
* if so, error out.
846+
*/
847+
pg_constraint = heap_open(ConstraintRelationId, AccessShareLock);
848+
849+
ScanKeyInit(&skey[0],
850+
Anum_pg_constraint_conrelid,
851+
BTEqualStrategyNumber, F_OIDEQ,
852+
ObjectIdGetDatum(relid));
853+
854+
scan = systable_beginscan(pg_constraint, ConstraintRelidIndexId, true,
855+
NULL, 1, skey);
856+
857+
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
858+
{
859+
Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple);
860+
Datum adatum;
861+
bool isNull;
862+
ArrayType *arr;
863+
int16 *attnums;
864+
int numcols;
865+
int i;
866+
867+
/* Check the constraint name */
868+
if (strcmp(NameStr(con->conname), conname) != 0)
869+
continue;
870+
if (OidIsValid(*constraintOid))
871+
ereport(ERROR,
872+
(errcode(ERRCODE_DUPLICATE_OBJECT),
873+
errmsg("table \"%s\" has multiple constraints named \"%s\"",
874+
get_rel_name(relid), conname)));
875+
876+
*constraintOid = HeapTupleGetOid(tuple);
877+
878+
/* Extract the conkey array, ie, attnums of constrained columns */
879+
adatum = heap_getattr(tuple, Anum_pg_constraint_conkey,
880+
RelationGetDescr(pg_constraint), &isNull);
881+
if (isNull)
882+
continue; /* no constrained columns */
883+
884+
arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
885+
numcols = ARR_DIMS(arr)[0];
886+
if (ARR_NDIM(arr) != 1 ||
887+
numcols < 0 ||
888+
ARR_HASNULL(arr) ||
889+
ARR_ELEMTYPE(arr) != INT2OID)
890+
elog(ERROR, "conkey is not a 1-D smallint array");
891+
attnums = (int16 *) ARR_DATA_PTR(arr);
892+
893+
/* Construct the result value */
894+
for (i = 0; i < numcols; i++)
895+
{
896+
conattnos = bms_add_member(conattnos,
897+
attnums[i] - FirstLowInvalidHeapAttributeNumber);
898+
}
899+
}
900+
901+
systable_endscan(scan);
902+
903+
/* If no such constraint exists, complain */
904+
if (!OidIsValid(*constraintOid) && !missing_ok)
905+
ereport(ERROR,
906+
(errcode(ERRCODE_UNDEFINED_OBJECT),
907+
errmsg("constraint \"%s\" for table \"%s\" does not exist",
908+
conname, get_rel_name(relid))));
909+
910+
heap_close(pg_constraint, AccessShareLock);
911+
912+
return conattnos;
913+
}
914+
817915
/*
818916
* get_domain_constraint_oid
819917
* Find a constraint on the specified domain with the specified name.

src/backend/parser/parse_clause.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,9 +2910,26 @@ transformOnConflictArbiter(ParseState *pstate,
29102910

29112911
pstate->p_namespace = save_namespace;
29122912

2913+
/*
2914+
* If the arbiter is specified by constraint name, get the constraint
2915+
* OID and mark the constrained columns as requiring SELECT privilege,
2916+
* in the same way as would have happened if the arbiter had been
2917+
* specified by explicit reference to the constraint's index columns.
2918+
*/
29132919
if (infer->conname)
2914-
*constraint = get_relation_constraint_oid(RelationGetRelid(pstate->p_target_relation),
2915-
infer->conname, false);
2920+
{
2921+
Oid relid = RelationGetRelid(pstate->p_target_relation);
2922+
RangeTblEntry *rte = pstate->p_target_rangetblentry;
2923+
Bitmapset *conattnos;
2924+
2925+
conattnos = get_relation_constraint_attnos(relid, infer->conname,
2926+
false, constraint);
2927+
2928+
/* Make sure the rel as a whole is marked for SELECT access */
2929+
rte->requiredPerms |= ACL_SELECT;
2930+
/* Mark the constrained columns as requiring SELECT access */
2931+
rte->selectedCols = bms_add_members(rte->selectedCols, conattnos);
2932+
}
29162933
}
29172934

29182935
/*

src/backend/rewrite/rowsecurity.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
309309
{
310310
List *conflict_permissive_policies;
311311
List *conflict_restrictive_policies;
312+
List *conflict_select_permissive_policies = NIL;
313+
List *conflict_select_restrictive_policies = NIL;
312314

313315
/* Get the policies that apply to the auxiliary UPDATE */
314316
get_policies_for_relation(rel, CMD_UPDATE, user_id,
@@ -338,9 +340,6 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
338340
*/
339341
if (rte->requiredPerms & ACL_SELECT)
340342
{
341-
List *conflict_select_permissive_policies = NIL;
342-
List *conflict_select_restrictive_policies = NIL;
343-
344343
get_policies_for_relation(rel, CMD_SELECT, user_id,
345344
&conflict_select_permissive_policies,
346345
&conflict_select_restrictive_policies);
@@ -361,6 +360,21 @@ get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index,
361360
withCheckOptions,
362361
hasSubLinks,
363362
false);
363+
364+
/*
365+
* Add ALL/SELECT policies as WCO_RLS_UPDATE_CHECK WCOs, to ensure
366+
* that the final updated row is visible when taking the UPDATE
367+
* path of an INSERT .. ON CONFLICT DO UPDATE, if SELECT rights
368+
* are required for this relation.
369+
*/
370+
if (rte->requiredPerms & ACL_SELECT)
371+
add_with_check_options(rel, rt_index,
372+
WCO_RLS_UPDATE_CHECK,
373+
conflict_select_permissive_policies,
374+
conflict_select_restrictive_policies,
375+
withCheckOptions,
376+
hasSubLinks,
377+
true);
364378
}
365379
}
366380

src/include/catalog/pg_constraint_fn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ extern char *ChooseConstraintName(const char *name1, const char *name2,
6969
extern void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
7070
Oid newNspId, bool isType, ObjectAddresses *objsMoved);
7171
extern Oid get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok);
72+
extern Bitmapset *get_relation_constraint_attnos(Oid relid, const char *conname,
73+
bool missing_ok, Oid *constraintOid);
7274
extern Oid get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok);
7375

7476
extern Bitmapset *get_primary_key_attnos(Oid relid, bool deferrableOk,

src/test/regress/expected/privileges.out

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,22 @@ ERROR: permission denied for relation atest5
488488
INSERT INTO atest5(three) VALUES (4) ON CONFLICT (two) DO UPDATE set three = 10; -- fails (due to INSERT)
489489
ERROR: permission denied for relation atest5
490490
-- Check that the columns in the inference require select privileges
491-
-- Error. No privs on four
492-
INSERT INTO atest5(three) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 10;
491+
INSERT INTO atest5(four) VALUES (4); -- fail
493492
ERROR: permission denied for relation atest5
494493
SET SESSION AUTHORIZATION regress_user1;
494+
GRANT INSERT (four) ON atest5 TO regress_user4;
495+
SET SESSION AUTHORIZATION regress_user4;
496+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- fails (due to SELECT)
497+
ERROR: permission denied for relation atest5
498+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- fails (due to SELECT)
499+
ERROR: permission denied for relation atest5
500+
INSERT INTO atest5(four) VALUES (4); -- ok
501+
SET SESSION AUTHORIZATION regress_user1;
502+
GRANT SELECT (four) ON atest5 TO regress_user4;
503+
SET SESSION AUTHORIZATION regress_user4;
504+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- ok
505+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- ok
506+
SET SESSION AUTHORIZATION regress_user1;
495507
REVOKE ALL (one) ON atest5 FROM regress_user4;
496508
GRANT SELECT (one,two,blue) ON atest6 TO regress_user4;
497509
SET SESSION AUTHORIZATION regress_user4;

src/test/regress/expected/rowsecurity.out

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3363,9 +3363,10 @@ DROP TABLE r1;
33633363
--
33643364
SET SESSION AUTHORIZATION regress_rls_alice;
33653365
SET row_security = on;
3366-
CREATE TABLE r1 (a int);
3366+
CREATE TABLE r1 (a int PRIMARY KEY);
33673367
CREATE POLICY p1 ON r1 FOR SELECT USING (a < 20);
33683368
CREATE POLICY p2 ON r1 FOR UPDATE USING (a < 20) WITH CHECK (true);
3369+
CREATE POLICY p3 ON r1 FOR INSERT WITH CHECK (true);
33693370
INSERT INTO r1 VALUES (10);
33703371
ALTER TABLE r1 ENABLE ROW LEVEL SECURITY;
33713372
ALTER TABLE r1 FORCE ROW LEVEL SECURITY;
@@ -3392,6 +3393,18 @@ ALTER TABLE r1 FORCE ROW LEVEL SECURITY;
33923393
-- Error
33933394
UPDATE r1 SET a = 30 RETURNING *;
33943395
ERROR: new row violates row-level security policy for table "r1"
3396+
-- UPDATE path of INSERT ... ON CONFLICT DO UPDATE should also error out
3397+
INSERT INTO r1 VALUES (10)
3398+
ON CONFLICT (a) DO UPDATE SET a = 30 RETURNING *;
3399+
ERROR: new row violates row-level security policy for table "r1"
3400+
-- Should still error out without RETURNING (use of arbiter always requires
3401+
-- SELECT permissions)
3402+
INSERT INTO r1 VALUES (10)
3403+
ON CONFLICT (a) DO UPDATE SET a = 30;
3404+
ERROR: new row violates row-level security policy for table "r1"
3405+
INSERT INTO r1 VALUES (10)
3406+
ON CONFLICT ON CONSTRAINT r1_pkey DO UPDATE SET a = 30;
3407+
ERROR: new row violates row-level security policy for table "r1"
33953408
DROP TABLE r1;
33963409
-- Check dependency handling
33973410
RESET SESSION AUTHORIZATION;

src/test/regress/sql/privileges.sql

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,24 @@ INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = EXCLU
320320
INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set three = EXCLUDED.three;
321321
INSERT INTO atest5(two) VALUES (6) ON CONFLICT (two) DO UPDATE set one = 8; -- fails (due to UPDATE)
322322
INSERT INTO atest5(three) VALUES (4) ON CONFLICT (two) DO UPDATE set three = 10; -- fails (due to INSERT)
323+
323324
-- Check that the columns in the inference require select privileges
324-
-- Error. No privs on four
325-
INSERT INTO atest5(three) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 10;
325+
INSERT INTO atest5(four) VALUES (4); -- fail
326+
327+
SET SESSION AUTHORIZATION regress_user1;
328+
GRANT INSERT (four) ON atest5 TO regress_user4;
329+
SET SESSION AUTHORIZATION regress_user4;
330+
331+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- fails (due to SELECT)
332+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- fails (due to SELECT)
333+
INSERT INTO atest5(four) VALUES (4); -- ok
334+
335+
SET SESSION AUTHORIZATION regress_user1;
336+
GRANT SELECT (four) ON atest5 TO regress_user4;
337+
SET SESSION AUTHORIZATION regress_user4;
338+
339+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT (four) DO UPDATE set three = 3; -- ok
340+
INSERT INTO atest5(four) VALUES (4) ON CONFLICT ON CONSTRAINT atest5_four_key DO UPDATE set three = 3; -- ok
326341

327342
SET SESSION AUTHORIZATION regress_user1;
328343
REVOKE ALL (one) ON atest5 FROM regress_user4;

src/test/regress/sql/rowsecurity.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,10 +1487,11 @@ DROP TABLE r1;
14871487
--
14881488
SET SESSION AUTHORIZATION regress_rls_alice;
14891489
SET row_security = on;
1490-
CREATE TABLE r1 (a int);
1490+
CREATE TABLE r1 (a int PRIMARY KEY);
14911491

14921492
CREATE POLICY p1 ON r1 FOR SELECT USING (a < 20);
14931493
CREATE POLICY p2 ON r1 FOR UPDATE USING (a < 20) WITH CHECK (true);
1494+
CREATE POLICY p3 ON r1 FOR INSERT WITH CHECK (true);
14941495
INSERT INTO r1 VALUES (10);
14951496
ALTER TABLE r1 ENABLE ROW LEVEL SECURITY;
14961497
ALTER TABLE r1 FORCE ROW LEVEL SECURITY;
@@ -1512,6 +1513,17 @@ ALTER TABLE r1 FORCE ROW LEVEL SECURITY;
15121513
-- Error
15131514
UPDATE r1 SET a = 30 RETURNING *;
15141515

1516+
-- UPDATE path of INSERT ... ON CONFLICT DO UPDATE should also error out
1517+
INSERT INTO r1 VALUES (10)
1518+
ON CONFLICT (a) DO UPDATE SET a = 30 RETURNING *;
1519+
1520+
-- Should still error out without RETURNING (use of arbiter always requires
1521+
-- SELECT permissions)
1522+
INSERT INTO r1 VALUES (10)
1523+
ON CONFLICT (a) DO UPDATE SET a = 30;
1524+
INSERT INTO r1 VALUES (10)
1525+
ON CONFLICT ON CONSTRAINT r1_pkey DO UPDATE SET a = 30;
1526+
15151527
DROP TABLE r1;
15161528

15171529
-- Check dependency handling

0 commit comments

Comments
 (0)