Skip to content

Commit 18116c8

Browse files
authored
test: ensure privesc doesn't happen on normal role (#107)
Requires generating the test/expected/event_triggers.out dynamically.
1 parent fb37cd0 commit 18116c8

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ results/*.out
99
.history
1010
result*
1111
tags
12+
test/expected/event_triggers.out

Makefile

+30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ OBJS = src/supautils.o src/privileged_extensions.o src/drop_trigger_grants.o src
1111

1212
PG_VERSION = $(strip $(shell $(PG_CONFIG) --version | $(GREP) -oP '(?<=PostgreSQL )[0-9]+'))
1313
PG_GE16 = $(shell test $(PG_VERSION) -ge 16; echo $$?)
14+
PG_GE14 = $(shell test $(PG_VERSION) -ge 14; echo $$?)
1415
SYSTEM = $(shell uname -s)
1516

1617
TESTS := $(wildcard test/sql/*.sql)
@@ -26,5 +27,34 @@ endif
2627
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
2728
REGRESS_OPTS = --use-existing --inputdir=test
2829

30+
GENERATED_OUT = test/expected/event_triggers.out
31+
EXTRA_CLEAN = $(GENERATED_OUT)
32+
2933
PGXS := $(shell $(PG_CONFIG) --pgxs)
3034
include $(PGXS)
35+
36+
.PHONY: $(GENERATED_OUT)
37+
$(GENERATED_OUT): $(GENERATED_OUT).in
38+
ifeq ($(PG_GE16), 0)
39+
sed \
40+
-e '/<\/\?PG_GE_16>/d' \
41+
-e '/<PG_GE_13>/,/<\/PG_GE_13>/d' \
42+
-e '/<PG_GE_14>/,/<\/PG_GE_14>/d' \
43+
$? > $@
44+
else ifeq ($(PG_GE14), 0)
45+
sed \
46+
-e '/<\/\?PG_GE_14>/d' \
47+
-e '/<PG_GE_13>/,/<\/PG_GE_13>/d' \
48+
-e '/<PG_GE_16>/,/<\/PG_GE_16>/d' \
49+
$? > $@
50+
else
51+
sed \
52+
-e '/<\/\?PG_GE_13>/d' \
53+
-e '/<PG_GE_14>/,/<\/PG_GE_14>/d' \
54+
-e '/<PG_GE_16>/,/<\/PG_GE_16>/d' \
55+
$? > $@
56+
endif
57+
58+
# extra dep for target in pgxs.mk
59+
installcheck: $(GENERATED_OUT)
60+

test/expected/event_triggers.out renamed to test/expected/event_triggers.out.in

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ create or replace function become_super()
1212
$$
1313
begin
1414
raise notice 'transforming % to superuser', current_user;
15-
alter role current_user superuser;
15+
alter role rolecreator superuser;
1616
end;
1717
$$;
1818
grant all on schema public to privileged_role;
@@ -80,6 +80,28 @@ select count(*) = 1 as only_one_super from pg_roles where rolsuper;
8080
t
8181
(1 row)
8282

83+
-- privesc won't happen because the event trigger function will fire with the privileges
84+
-- of the current role (this is pg default behavior)
85+
set role rolecreator;
86+
\echo
87+
88+
create table dummy();
89+
NOTICE: the event trigger is executed for rolecreator
90+
NOTICE: transforming rolecreator to superuser
91+
<PG_GE_16>
92+
ERROR: permission denied to alter role
93+
DETAIL: Only roles with the SUPERUSER attribute may change the SUPERUSER attribute.
94+
</PG_GE_16>
95+
<PG_GE_14>
96+
ERROR: must be superuser to alter superuser roles or change superuser attribute
97+
</PG_GE_14>
98+
<PG_GE_13>
99+
ERROR: must be superuser to alter superusers
100+
</PG_GE_13>
101+
CONTEXT: SQL statement "alter role rolecreator superuser"
102+
PL/pgSQL function become_super() line 4 at SQL statement
103+
\echo
104+
83105
-- limitation: create extension won't fire event triggers due to implementation details (we switch to superuser temporarily to create them and we don't fire evtrigs for superusers)
84106
set role rolecreator;
85107
\echo

test/sql/event_triggers.sql

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ create or replace function become_super()
1313
$$
1414
begin
1515
raise notice 'transforming % to superuser', current_user;
16-
alter role current_user superuser;
16+
alter role rolecreator superuser;
1717
end;
1818
$$;
1919

@@ -74,6 +74,14 @@ execute procedure become_super();
7474
create table super_duper_stuff();
7575
select count(*) = 1 as only_one_super from pg_roles where rolsuper;
7676

77+
-- privesc won't happen because the event trigger function will fire with the privileges
78+
-- of the current role (this is pg default behavior)
79+
set role rolecreator;
80+
\echo
81+
82+
create table dummy();
83+
\echo
84+
7785
-- limitation: create extension won't fire event triggers due to implementation details (we switch to superuser temporarily to create them and we don't fire evtrigs for superusers)
7886
set role rolecreator;
7987
\echo

0 commit comments

Comments
 (0)