summaryrefslogtreecommitdiffstats
path: root/contrib/sepgsql/test_sepgsql
blob: 3a29556d1ffc78f1a2e631b6f12f9ab38ad23c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/sh
#
# Run the sepgsql regression tests, after making a lot of environmental checks
# to try to ensure that the SELinux environment is set up appropriately and
# the database is configured correctly.
#
# Note that this must be run against an installed Postgres database.
# There's no equivalent of "make check", and that wouldn't be terribly useful
# since much of the value is in checking that you installed sepgsql into
# your database correctly.
#
# This must be run in the contrib/sepgsql directory of a Postgres build tree.
#

PG_BINDIR=`pg_config --bindir`

# we must move to contrib/sepgsql directory to run pg_regress correctly
cd `dirname $0`

echo
echo "============== checking selinux environment           =============="

# matchpathcon must be present to assess whether the installation environment
# is OK.
echo -n "checking for matchpathcon           ... "
if ! matchpathcon -n . >/dev/null 2>&1; then
    echo "not found"
    echo ""
    echo "The matchpathcon command must be available."
    echo "Please install it or update your PATH to include it"
    echo "(it is typically in '/usr/sbin', which might not be in your PATH)."
    echo "matchpathcon is typically included in the libselinux-utils package."
    exit 1
fi
echo "ok"

# runcon must be present to launch psql using the correct environment
echo -n "checking for runcon                 ... "
if ! runcon --help >/dev/null 2>&1; then
    echo "not found"
    echo ""
    echo "The runcon command must be available."
    echo "runcon is typically included in the coreutils package."
    echo ""
    exit 1
fi
echo "ok"

# check sestatus too, since that lives in yet another package
echo -n "checking for sestatus               ... "
if ! sestatus >/dev/null 2>&1; then
    echo "not found"
    echo ""
    echo "The sestatus command must be available."
    echo "sestatus is typically included in the policycoreutils package."
    echo ""
    exit 1
fi
echo "ok"

# check that the user is running in the unconfined_t domain
echo -n "checking current user domain        ... "
DOMAIN=`id -Z 2>/dev/null | sed 's/:/ /g' | awk '{print $3}'`
echo ${DOMAIN:-failed}
if [ "${DOMAIN}" != unconfined_t ]; then
    echo ""
    echo "The regression tests must be launched from the unconfined_t domain."
    echo ""
    echo "The unconfined_t domain is typically the default domain for user"
    echo "shell processes.  If the default has been changed on your system,"
    echo "you can revert the changes like this:"
    echo ""
    echo "  \$ sudo semanage login -d `whoami`"
    echo ""
    echo "Or, you can add a setting to log in using the unconfined_t domain:"
    echo ""
    echo "  \$ sudo semanage login -a -s unconfined_u -r s0-s0:c0.c255 `whoami`"
    echo ""
    exit 1
fi

# SELinux must be configured in enforcing mode
echo -n "checking selinux operating mode     ... "
CURRENT_MODE=`LANG=C sestatus | grep '^Current mode:' | awk '{print $3}'`
echo ${CURRENT_MODE:-failed}
if [ "${CURRENT_MODE}" = enforcing ]; then
    : OK
elif [ "${CURRENT_MODE}" = permissive -o "${CURRENT_MODE}" = disabled ]; then
    echo ""
    echo "Before running the regression tests, SELinux must be enabled and"
    echo "must be running in enforcing mode."
    echo ""
    echo "If SELinux is currently running in permissive mode, you can"
    echo "switch to enforcing mode using the 'setenforce' command."
    echo
    echo "  \$ sudo setenforce 1"
    echo ""
    echo "The system default setting is configured in /etc/selinux/config,"
    echo "or using a kernel boot parameter."
    echo ""
    exit 1
else
    echo ""
    echo "Unable to determine the current selinux operating mode.  Please"
    echo "verify that the sestatus command is installed and in your PATH."
    echo ""
    exit 1
fi

# 'sepgsql-regtest' policy module must be loaded
echo -n "checking for sepgsql-regtest policy ... "
SELINUX_MNT=`LANG=C sestatus | grep '^SELinuxfs mount:' | awk '{print $3}'`
if [ "$SELINUX_MNT" = "" ]; then
    echo "failed"
    echo ""
    echo "Unable to find SELinuxfs mount point."
    echo ""
    echo "The sestatus command should report the location where SELinuxfs"
    echo "is mounted, but did not do so."
    echo ""
    exit 1
fi
if [ ! -e "${SELINUX_MNT}/booleans/sepgsql_regression_test_mode" ]; then
    echo "failed"
    echo ""
    echo "The 'sepgsql-regtest' policy module appears not to be installed."
    echo "Without this policy installed, the regression tests will fail."
    echo "You can install this module using the following commands:"
    echo ""
    echo "  \$ make -f /usr/share/selinux/devel/Makefile"
    echo "  \$ sudo semodule -u sepgsql-regtest.pp"
    echo ""
    echo "To confirm that the policy package is installed, use this command:"
    echo ""
    echo "  \$ sudo semodule -l | grep sepgsql"
    echo ""
    exit 1
fi
echo "ok"

# Verify that sepgsql_regression_test_mode is active.
echo -n "checking whether policy is enabled  ... "
POLICY_STATUS=`getsebool sepgsql_regression_test_mode | awk '{print $3}'`
echo ${POLICY_STATUS:-failed}
if [ "${POLICY_STATUS}" != on ]; then
    echo ""
    echo "The SELinux boolean 'sepgsql_regression_test_mode' must be"
    echo "turned on in order to enable the rules necessary to run the"
    echo "regression tests."
    echo ""
    if [ "${POLICY_STATUS}" = "" ]; then
        echo "We attempted to determine the state of this Boolean using"
        echo "'getsebool', but that command did not produce the expected"
        echo "output.  Please verify that getsebool is available and in"
        echo "your PATH."
    else
        echo "You can turn on this variable using the following commands:"
        echo ""
        echo "  \$ sudo setsebool sepgsql_regression_test_mode on"
        echo ""
        echo "For security reasons, it is suggested that you turn off this"
        echo "variable when regression testing is complete and the associated"
        echo "rules are no longer needed."
    fi
    echo ""
    exit 1
fi
POLICY_STATUS=`getsebool sepgsql_enable_users_ddl | awk '{print $3}'`
echo ${POLICY_STATUS:-failed}
if [ "${POLICY_STATUS}" != on ]; then
    echo ""
    echo "The SELinux boolean 'sepgsql_enable_users_ddl' must be"
    echo "turned on in order to enable the rules necessary to run"
    echo "the regression tests."
    echo ""
    if [ "${POLICY_STATUS}" = "" ]; then
        echo "We attempted to determine the state of this Boolean using"
        echo "'getsebool', but that command did not produce the expected"
        echo "output.  Please verify that getsebool is available and in"
        echo "your PATH."
    else
        echo "You can turn on this variable using the following commands:"
        echo ""
        echo "  \$ sudo setsebool sepgsql_enable_users_ddl on"
        echo ""
        echo "For security reasons, it is suggested that you turn off this"
        echo "variable when regression testing is complete, unless you"
        echo "don't want to allow unprivileged users DDL commands."
    fi
    echo ""
    exit 1
fi

# 'psql' command must be executable from test domain
echo -n "checking whether we can run psql    ... "
CMD_PSQL="${PG_BINDIR}/psql"
if [ ! -e "${CMD_PSQL}" ]; then
    echo "not found"
    echo
    echo "${CMD_PSQL} was not found."
    echo "Check your PostgreSQL installation."
    echo
    exit 1
fi
runcon -t sepgsql_regtest_user_t "${CMD_PSQL}" --help >& /dev/null
if [ $? -ne 0 ]; then
    echo "failed"
    echo
    echo "${CMD_PSQL} must be executable from the"
    echo "sepgsql_regtest_user_t domain. That domain has restricted privileges"
    echo "compared to unconfined_t, so the problem may be the psql file's"
    echo "SELinux label. Try"
    echo
    PSQL_T=`matchpathcon -n "${CMD_PSQL}" | sed 's/:/ /g' | awk '{print $3}'`
    if [ "${PSQL_T}" = "user_home_t" ]; then
        # Installation appears to be in /home directory
        echo "  \$ sudo restorecon -R ${PG_BINDIR}"
        echo
        echo "Or, using chcon"
        echo
        echo "  \$ sudo chcon -t user_home_t ${CMD_PSQL}"
    else
        echo "  \$ sudo restorecon -R ${PG_BINDIR}"
        echo
        echo "Or, using chcon"
        echo
        echo "  \$ sudo chcon -t bin_t ${CMD_PSQL}"
    fi
    echo
    exit 1
fi
echo "ok"

# loadable module must be installed and not configured to permissive mode
echo -n "checking sepgsql installation       ... "
VAL="`${CMD_PSQL} -X -t -c 'SHOW sepgsql.permissive' template1 2>/dev/null`"
RETVAL="$?"
if [ $RETVAL -eq 2 ]; then
    echo "failed"
    echo ""
    echo "Could not connect to the database server."
    echo "Please check your PostgreSQL installation."
    echo ""
    exit 1
elif [ $RETVAL -ne 0 ]; then
    echo "failed"
    echo ""
    echo "The sepgsql module does not appear to be loaded.  Please verify"
    echo "that the 'shared_preload_libraries' setting in postgresql.conf"
    echo "includes 'sepgsql', and then restart the server."
    echo ""
    echo "See Installation section of the contrib/sepgsql documentation."
    echo ""
    exit 1
elif ! echo "$VAL" | grep -q 'off$'; then
    echo "failed"
    echo ""
    echo "The parameter 'sepgsql.permissive' is set to 'on'.  It must be"
    echo "turned off before running the regression tests."
    echo ""
    exit 1
fi
echo "ok"

# template1 database must be labeled
# NOTE: this test is wrong; we really ought to be checking template0.
# But we can't connect to that without extra pushups, and it's not worth it.
echo -n "checking for labels in template1    ... "
NUM=`${CMD_PSQL} -XAt -c 'SELECT count(*) FROM pg_catalog.pg_seclabel' template1 2>/dev/null`
if [ -z "${NUM}" ]; then
    echo "failed"
    echo ""
    echo "In order to test sepgsql, initial labels must be assigned within"
    echo "the 'template1' database.  These labels will be copied into the"
    echo "regression test database."
    echo ""
    echo "See Installation section of the contrib/sepgsql documentation."
    echo ""
    exit 1
fi
echo "found ${NUM}"

#
# checking complete - let's run the tests
#

echo
echo "============== running sepgsql regression tests       =============="

tests="label dml ddl alter misc"

# Check if the truncate permission exists in the loaded policy, and if so,
# run the truncate test
#
# Testing the TRUNCATE regression test can be done by manually adding
# the permission with CIL if necessary:
#     sudo semodule -cE base
#     sudo sed -i -E 's/(class db_table.*?) \)/\1 truncate\)/' base.cil
#     sudo semodule -i base.cil

if [ -f /sys/fs/selinux/class/db_table/perms/truncate ]; then
	tests+=" truncate"
fi

make REGRESS="$tests" REGRESS_OPTS="--launcher ./launcher" installcheck
# exit with the exit code provided by "make"