| 1 |
-- |
| 2 |
-- Regression Tests for Label Management |
| 3 |
-- |
| 4 |
|
| 5 |
-- |
| 6 |
-- Setup |
| 7 |
-- |
| 8 |
CREATE TABLE t1 (a int, b text); |
| 9 |
INSERT INTO t1 VALUES (1, 'aaa'), (2, 'bbb'), (3, 'ccc'); |
| 10 |
CREATE TABLE t2 AS SELECT * FROM t1 WHERE a % 2 = 0; |
| 11 |
|
| 12 |
CREATE FUNCTION f1 () RETURNS text |
| 13 |
AS 'SELECT sepgsql_getcon()' |
| 14 |
LANGUAGE sql; |
| 15 |
|
| 16 |
CREATE FUNCTION f2 () RETURNS text |
| 17 |
AS 'SELECT sepgsql_getcon()' |
| 18 |
LANGUAGE sql; |
| 19 |
SECURITY LABEL ON FUNCTION f2() |
| 20 |
IS 'system_u:object_r:sepgsql_trusted_proc_exec_t:s0'; |
| 21 |
|
| 22 |
CREATE FUNCTION f3 () RETURNS text |
| 23 |
AS 'BEGIN |
| 24 |
RAISE EXCEPTION ''an exception from f3()''; |
| 25 |
RETURN NULL; |
| 26 |
END;' LANGUAGE plpgsql; |
| 27 |
SECURITY LABEL ON FUNCTION f3() |
| 28 |
IS 'system_u:object_r:sepgsql_trusted_proc_exec_t:s0'; |
| 29 |
|
| 30 |
CREATE FUNCTION f4 () RETURNS text |
| 31 |
AS 'SELECT sepgsql_getcon()' |
| 32 |
LANGUAGE sql; |
| 33 |
SECURITY LABEL ON FUNCTION f4() |
| 34 |
IS 'system_u:object_r:sepgsql_nosuch_trusted_proc_exec_t:s0'; |
| 35 |
|
| 36 |
CREATE FUNCTION f5 (text) RETURNS bool |
| 37 |
AS 'SELECT sepgsql_setcon($1)' |
| 38 |
LANGUAGE sql; |
| 39 |
SECURITY LABEL ON FUNCTION f5(text) |
| 40 |
IS 'system_u:object_r:sepgsql_regtest_trusted_proc_exec_t:s0'; |
| 41 |
|
| 42 |
CREATE TABLE auth_tbl(uname text, credential text, label text); |
| 43 |
INSERT INTO auth_tbl |
| 44 |
VALUES ('foo', 'acbd18db4cc2f85cedef654fccc4a4d8', 'sepgsql_regtest_foo_t:s0'), |
| 45 |
('var', 'b2145aac704ce76dbe1ac7adac535b23', 'sepgsql_regtest_var_t:s0'), |
| 46 |
('baz', 'b2145aac704ce76dbe1ac7adac535b23', 'sepgsql_regtest_baz_t:s0'); |
| 47 |
SECURITY LABEL ON TABLE auth_tbl |
| 48 |
IS 'system_u:object_r:sepgsql_secret_table_t:s0'; |
| 49 |
|
| 50 |
CREATE FUNCTION auth_func(text, text) RETURNS bool |
| 51 |
LANGUAGE sql |
| 52 |
AS 'SELECT sepgsql_setcon(regexp_replace(sepgsql_getcon(), ''_r:.*$'', ''_r:'' || label)) |
| 53 |
FROM auth_tbl WHERE uname = $1 AND credential = $2'; |
| 54 |
SECURITY LABEL ON FUNCTION auth_func(text,text) |
| 55 |
IS 'system_u:object_r:sepgsql_regtest_trusted_proc_exec_t:s0'; |
| 56 |
|
| 57 |
CREATE TABLE foo_tbl(a int, b text); |
| 58 |
INSERT INTO foo_tbl VALUES (1, 'aaa'), (2,'bbb'), (3,'ccc'), (4,'ddd'); |
| 59 |
SECURITY LABEL ON TABLE foo_tbl |
| 60 |
IS 'system_u:object_r:sepgsql_regtest_foo_table_t:s0'; |
| 61 |
|
| 62 |
CREATE TABLE var_tbl(x int, y text); |
| 63 |
INSERT INTO var_tbl VALUES (2,'xxx'), (3,'yyy'), (4,'zzz'), (5,'xyz'); |
| 64 |
SECURITY LABEL ON TABLE var_tbl |
| 65 |
IS 'system_u:object_r:sepgsql_regtest_var_table_t:s0'; |
| 66 |
|
| 67 |
CREATE TABLE foo_ptbl(o int, p text) PARTITION BY RANGE (o); |
| 68 |
CREATE TABLE foo_ptbl_ones PARTITION OF foo_ptbl FOR VALUES FROM ('0') TO ('10'); |
| 69 |
CREATE TABLE foo_ptbl_tens PARTITION OF foo_ptbl FOR VALUES FROM ('10') TO ('100'); |
| 70 |
|
| 71 |
INSERT INTO foo_ptbl VALUES (0, 'aaa'), (9,'bbb'), (10,'ccc'), (99,'ddd'); |
| 72 |
SECURITY LABEL ON TABLE foo_ptbl |
| 73 |
IS 'system_u:object_r:sepgsql_regtest_foo_table_t:s0'; |
| 74 |
|
| 75 |
CREATE TABLE var_ptbl(q int, r text) PARTITION BY RANGE (q); |
| 76 |
CREATE TABLE var_ptbl_ones PARTITION OF var_ptbl FOR VALUES FROM ('0') TO ('10'); |
| 77 |
CREATE TABLE var_ptbl_tens PARTITION OF var_ptbl FOR VALUES FROM ('10') TO ('100'); |
| 78 |
|
| 79 |
INSERT INTO var_ptbl VALUES (0,'xxx'), (9,'yyy'), (10,'zzz'), (99,'xyz'); |
| 80 |
SECURITY LABEL ON TABLE var_ptbl |
| 81 |
IS 'system_u:object_r:sepgsql_regtest_var_table_t:s0'; |
| 82 |
|
| 83 |
-- |
| 84 |
-- Tests for default labeling behavior |
| 85 |
-- |
| 86 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0 |
| 87 |
CREATE TABLE t3 (s int, t text); |
| 88 |
INSERT INTO t3 VALUES (1, 'sss'), (2, 'ttt'), (3, 'uuu'); |
| 89 |
|
| 90 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_dba_t:s0 |
| 91 |
CREATE TABLE t4 (m int, n text); |
| 92 |
INSERT INTO t4 VALUES (1,'mmm'), (2,'nnn'), (3,'ooo'); |
| 93 |
|
| 94 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0 |
| 95 |
CREATE TABLE tpart (o int, p text) PARTITION BY RANGE (o); |
| 96 |
|
| 97 |
CREATE TABLE tpart_ones PARTITION OF tpart FOR VALUES FROM ('0') TO ('10'); |
| 98 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_dba_t:s0 |
| 99 |
CREATE TABLE tpart_tens PARTITION OF tpart FOR VALUES FROM ('10') TO ('100'); |
| 100 |
|
| 101 |
INSERT INTO tpart VALUES (0, 'aaa'); |
| 102 |
INSERT INTO tpart VALUES (9, 'bbb'); |
| 103 |
INSERT INTO tpart VALUES (99, 'ccc'); |
| 104 |
|
| 105 |
SELECT objtype, objname, label FROM pg_seclabels |
| 106 |
WHERE provider = 'selinux' AND objtype = 'table' AND objname in ('t1', 't2', 't3', |
| 107 |
'tpart', |
| 108 |
'tpart_ones', |
| 109 |
'tpart_tens') |
| 110 |
ORDER BY objname COLLATE "C" ASC; |
| 111 |
SELECT objtype, objname, label FROM pg_seclabels |
| 112 |
WHERE provider = 'selinux' AND objtype = 'column' AND (objname like 't3.%' |
| 113 |
OR objname like 't4.%' |
| 114 |
OR objname like 'tpart.%' |
| 115 |
OR objname like 'tpart_ones.%' |
| 116 |
OR objname like 'tpart_tens.%') |
| 117 |
ORDER BY objname COLLATE "C" ASC; |
| 118 |
|
| 119 |
-- |
| 120 |
-- Tests for SECURITY LABEL |
| 121 |
-- |
| 122 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_dba_t:s0 |
| 123 |
SECURITY LABEL ON TABLE t1 |
| 124 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- ok |
| 125 |
SECURITY LABEL ON TABLE t2 |
| 126 |
IS 'invalid security context'; -- be failed |
| 127 |
SECURITY LABEL ON COLUMN t2 |
| 128 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- be failed |
| 129 |
SECURITY LABEL ON COLUMN t2.b |
| 130 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- ok |
| 131 |
SECURITY LABEL ON TABLE tpart |
| 132 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- ok |
| 133 |
SECURITY LABEL ON TABLE tpart |
| 134 |
IS 'invalid security context'; -- failed |
| 135 |
SECURITY LABEL ON COLUMN tpart |
| 136 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- failed |
| 137 |
SECURITY LABEL ON COLUMN tpart.o |
| 138 |
IS 'system_u:object_r:sepgsql_ro_table_t:s0'; -- ok |
| 139 |
|
| 140 |
-- |
| 141 |
-- Tests for Trusted Procedures |
| 142 |
-- |
| 143 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0 |
| 144 |
SET sepgsql.debug_audit = true; |
| 145 |
SET client_min_messages = log; |
| 146 |
SELECT f1(); -- normal procedure |
| 147 |
SELECT f2(); -- trusted procedure |
| 148 |
SELECT f3(); -- trusted procedure that raises an error |
| 149 |
SELECT f4(); -- failed on domain transition |
| 150 |
SELECT sepgsql_getcon(); -- client's label must be restored |
| 151 |
|
| 152 |
-- |
| 153 |
-- Test for Dynamic Domain Transition |
| 154 |
-- |
| 155 |
|
| 156 |
-- validation of transaction aware dynamic-transition |
| 157 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c25 |
| 158 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c15'); |
| 159 |
SELECT sepgsql_getcon(); |
| 160 |
|
| 161 |
SELECT sepgsql_setcon(NULL); -- failed to reset |
| 162 |
SELECT sepgsql_getcon(); |
| 163 |
|
| 164 |
BEGIN; |
| 165 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c12'); |
| 166 |
SELECT sepgsql_getcon(); |
| 167 |
|
| 168 |
SAVEPOINT svpt_1; |
| 169 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c9'); |
| 170 |
SELECT sepgsql_getcon(); |
| 171 |
|
| 172 |
SAVEPOINT svpt_2; |
| 173 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c6'); |
| 174 |
SELECT sepgsql_getcon(); |
| 175 |
|
| 176 |
SAVEPOINT svpt_3; |
| 177 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c3'); |
| 178 |
SELECT sepgsql_getcon(); |
| 179 |
|
| 180 |
ROLLBACK TO SAVEPOINT svpt_2; |
| 181 |
SELECT sepgsql_getcon(); -- should be 's0:c0.c9' |
| 182 |
|
| 183 |
ROLLBACK TO SAVEPOINT svpt_1; |
| 184 |
SELECT sepgsql_getcon(); -- should be 's0:c0.c12' |
| 185 |
|
| 186 |
ABORT; |
| 187 |
SELECT sepgsql_getcon(); -- should be 's0:c0.c15' |
| 188 |
|
| 189 |
BEGIN; |
| 190 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c8'); |
| 191 |
SELECT sepgsql_getcon(); |
| 192 |
|
| 193 |
SAVEPOINT svpt_1; |
| 194 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c4'); |
| 195 |
SELECT sepgsql_getcon(); |
| 196 |
|
| 197 |
ROLLBACK TO SAVEPOINT svpt_1; |
| 198 |
SELECT sepgsql_getcon(); -- should be 's0:c0.c8' |
| 199 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0:c0.c6'); |
| 200 |
|
| 201 |
COMMIT; |
| 202 |
SELECT sepgsql_getcon(); -- should be 's0:c0.c6' |
| 203 |
|
| 204 |
-- sepgsql_regtest_user_t is not available dynamic-transition, |
| 205 |
-- unless sepgsql_setcon() is called inside of trusted-procedure |
| 206 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0:c0.c15 |
| 207 |
|
| 208 |
-- sepgsql_regtest_user_t has no permission to switch current label |
| 209 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0'); -- failed |
| 210 |
SELECT sepgsql_getcon(); |
| 211 |
|
| 212 |
-- trusted procedure allows to switch, but unavailable to override MCS rules |
| 213 |
SELECT f5('unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0:c0.c7'); -- OK |
| 214 |
SELECT sepgsql_getcon(); |
| 215 |
|
| 216 |
SELECT f5('unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0:c0.c31'); -- Failed |
| 217 |
SELECT sepgsql_getcon(); |
| 218 |
|
| 219 |
SELECT f5(NULL); -- Failed |
| 220 |
SELECT sepgsql_getcon(); |
| 221 |
|
| 222 |
BEGIN; |
| 223 |
SELECT f5('unconfined_u:unconfined_r:sepgsql_regtest_user_t:s0:c0.c3'); -- OK |
| 224 |
SELECT sepgsql_getcon(); |
| 225 |
|
| 226 |
ABORT; |
| 227 |
SELECT sepgsql_getcon(); |
| 228 |
|
| 229 |
-- |
| 230 |
-- Test for simulation of typical connection pooling server |
| 231 |
-- |
| 232 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_pool_t:s0 |
| 233 |
|
| 234 |
-- we shouldn't allow to switch client label without trusted procedure |
| 235 |
SELECT sepgsql_setcon('unconfined_u:unconfined_r:sepgsql_regtest_foo_t:s0'); |
| 236 |
|
| 237 |
SELECT * FROM auth_tbl; -- failed, no permission to reference |
| 238 |
|
| 239 |
-- switch to "foo" |
| 240 |
SELECT auth_func('foo', 'acbd18db4cc2f85cedef654fccc4a4d8'); |
| 241 |
|
| 242 |
SELECT sepgsql_getcon(); |
| 243 |
|
| 244 |
SELECT * FROM foo_tbl; -- OK |
| 245 |
SELECT * FROM foo_ptbl; -- OK |
| 246 |
|
| 247 |
SELECT * FROM var_tbl; -- failed |
| 248 |
SELECT * FROM var_ptbl; -- failed |
| 249 |
|
| 250 |
SELECT * FROM auth_tbl; -- failed |
| 251 |
|
| 252 |
SELECT sepgsql_setcon(NULL); -- end of session |
| 253 |
SELECT sepgsql_getcon(); |
| 254 |
|
| 255 |
-- the pooler cannot touch these tables directly |
| 256 |
SELECT * FROM foo_tbl; -- failed |
| 257 |
SELECT * FROM foo_ptbl; -- failed |
| 258 |
|
| 259 |
SELECT * FROM var_tbl; -- failed |
| 260 |
SELECT * FROM var_ptbl; -- failed |
| 261 |
|
| 262 |
-- switch to "var" |
| 263 |
SELECT auth_func('var', 'b2145aac704ce76dbe1ac7adac535b23'); |
| 264 |
|
| 265 |
SELECT sepgsql_getcon(); |
| 266 |
|
| 267 |
SELECT * FROM foo_tbl; -- failed |
| 268 |
SELECT * FROM foo_ptbl; -- failed |
| 269 |
|
| 270 |
SELECT * FROM var_tbl; -- OK |
| 271 |
SELECT * FROM var_ptbl; -- OK |
| 272 |
|
| 273 |
SELECT * FROM auth_tbl; -- failed |
| 274 |
|
| 275 |
SELECT sepgsql_setcon(NULL); -- end of session |
| 276 |
|
| 277 |
-- misc checks |
| 278 |
SELECT auth_func('var', 'invalid credential'); -- not works |
| 279 |
SELECT sepgsql_getcon(); |
| 280 |
|
| 281 |
-- |
| 282 |
-- Clean up |
| 283 |
-- |
| 284 |
-- @SECURITY-CONTEXT=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0-s0:c0.c255 |
| 285 |
DROP TABLE IF EXISTS t1 CASCADE; |
| 286 |
DROP TABLE IF EXISTS t2 CASCADE; |
| 287 |
DROP TABLE IF EXISTS t3 CASCADE; |
| 288 |
DROP TABLE IF EXISTS t4 CASCADE; |
| 289 |
DROP TABLE IF EXISTS tpart CASCADE; |
| 290 |
DROP FUNCTION IF EXISTS f1() CASCADE; |
| 291 |
DROP FUNCTION IF EXISTS f2() CASCADE; |
| 292 |
DROP FUNCTION IF EXISTS f3() CASCADE; |
| 293 |
DROP FUNCTION IF EXISTS f4() CASCADE; |
| 294 |
DROP FUNCTION IF EXISTS f5(text) CASCADE; |