| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv) |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2016 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import testlib |
| 9 |
from lib.constants import TEST_OS_DEFAULT |
| 10 |
|
| 11 |
# simulate httpd accessing user ~/public_html without httpd_enable_homedirs |
| 12 |
SELINUX_SEBOOL_ALERT_SCRIPT = """ |
| 13 |
set -e |
| 14 |
mkdir -p ~/selinux_temp |
| 15 |
cd ~/selinux_temp |
| 16 |
cp /bin/ls ls |
| 17 |
chcon -t httpd_exec_t ls |
| 18 |
su -l -c 'mkdir public_html; echo world > public_html/hello.txt' admin |
| 19 |
# this won't work, but it generates an error |
| 20 |
runcon -u system_u -r system_r -t httpd_t -- ./ls ~admin/public_html || true |
| 21 |
""" |
| 22 |
|
| 23 |
SELINUX_RESTORECON_ALERT_SCRIPT = """ |
| 24 |
set -e |
| 25 |
mkdir -p ~/.ssh |
| 26 |
ssh-keygen -t rsa -f ~/.ssh/test-avc-rsa -N "" |
| 27 |
mv -f ~/.ssh/authorized_keys ~/.ssh/authorized_keys.test-avc |
| 28 |
cat .ssh/test-avc-rsa.pub >> ~/.ssh/authorized_keys |
| 29 |
chcon -t httpd_exec_t ~/.ssh/authorized_keys |
| 30 |
auditctl -D |
| 31 |
auditctl -w ~/.ssh/authorized_keys -p a |
| 32 |
ssh -o StrictHostKeyChecking=no -o 'BatchMode=yes' -i ~/.ssh/test-avc-rsa localhost || true |
| 33 |
mv -f ~/.ssh/authorized_keys.test-avc ~/.ssh/authorized_keys |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
# https://github.com/fedora-selinux/selinux-policy/issues/2947 |
| 38 |
@testlib.skipDaily("nightly has selinux set to permissive") |
| 39 |
class TestSelinux(testlib.MachineCase): |
| 40 |
provision = { |
| 41 |
"0": {}, |
| 42 |
"ansible_machine": {"image": TEST_OS_DEFAULT} |
| 43 |
} |
| 44 |
|
| 45 |
@testlib.skipImage("No setroubleshoot", "debian-*", "ubuntu-*", "arch", "opensuse-tumbleweed") |
| 46 |
@testlib.skipOstree("No setroubleshoot") |
| 47 |
@testlib.skipBrowser("Firefox fails to recognize 'clipboard-read' permission", "firefox") |
| 48 |
def testTroubleshootAlerts(self): |
| 49 |
b = self.browser |
| 50 |
m = self.machine |
| 51 |
|
| 52 |
# Do some local modifications |
| 53 |
m.execute("semanage fcontext --add -t samba_share_t /var/tmp/") |
| 54 |
m.execute("semanage boolean --modify --on daemons_dump_core") |
| 55 |
self.allow_journal_messages("audit: type=1405 .* bool=daemons_dump_core val=1 old_val=0.*") |
| 56 |
self.allow_journal_messages(".*couldn't .* /org/fedoraproject/Setroubleshootd: GDBus.Error:org.freedesktop.DBus.Error.NoReply:.*") |
| 57 |
|
| 58 |
self.login_and_go("/selinux") |
| 59 |
|
| 60 |
dismiss_sel = ".selinux-alert-dismiss" |
| 61 |
|
| 62 |
# wait for Modifications table to initialize |
| 63 |
b.wait_text_matches(".modifications-table", "Allow.*daemons.*core") |
| 64 |
|
| 65 |
# httpd_read_user_content should be off by default |
| 66 |
self.assertIn("-> off", m.execute("getsebool httpd_read_user_content")) |
| 67 |
# and not part of modifications |
| 68 |
b.wait_not_in_text(".modifications-table", "httpd to read user content") |
| 69 |
|
| 70 |
def wait_for_alert(text): |
| 71 |
# wait for the alert to appear |
| 72 |
with b.wait_timeout(60): |
| 73 |
b.wait_in_text("#selinux-alerts", text) |
| 74 |
|
| 75 |
# figure out which row it is |
| 76 |
row_id = b.attr(f"#selinux-alerts tr:contains('{text}')", "data-row-id") |
| 77 |
return f"#selinux-alerts tr[data-row-id='{row_id}']" |
| 78 |
|
| 79 |
######################################################### |
| 80 |
# trigger an sebool alert |
| 81 |
# this triggers two solutions -- the preferred sebool module, which can be applied automatically, |
| 82 |
# and the fallback "report a bug" ausearch one, which cannot be applied automatically |
| 83 |
self.machine.execute(SELINUX_SEBOOL_ALERT_SCRIPT) |
| 84 |
|
| 85 |
row_selector = wait_for_alert("ls from read access on the directory") |
| 86 |
|
| 87 |
# expand it to see details |
| 88 |
toggle_selector = row_selector + " .pf-v6-c-table__toggle button" |
| 89 |
b.click(toggle_selector) |
| 90 |
|
| 91 |
# this should have two alerts, but there seems to be a known issue that some messages are delayed |
| 92 |
# b.wait_in_text(row_selector, "2 occurrences") |
| 93 |
|
| 94 |
panel_selector = row_selector + " + tr.pf-m-expanded .ct-listing-panel-body" |
| 95 |
|
| 96 |
# manual label change solution is present |
| 97 |
relabel_selector = panel_selector + " .selinux-details:contains('public_content_t')" |
| 98 |
b.wait_in_text(relabel_selector, "You need to change the label on public_html to public_content_t") |
| 99 |
b.wait_in_text(relabel_selector, "Unable to apply this solution automatically") |
| 100 |
|
| 101 |
# an automatic solution is present for sebool |
| 102 |
solution_id = b.attr(".selinux-details:contains('httpd_enable_homedirs')", "data-solution-id") |
| 103 |
sebool_selector1 = panel_selector + f" .selinux-details[data-solution-id='{solution_id}']" |
| 104 |
b.wait_in_text(sebool_selector1, "by enabling the 'httpd_enable_homedirs' boolean.") |
| 105 |
|
| 106 |
# sabotage the solution command to test failure alerts |
| 107 |
sebool_path = m.execute("command -v setsebool").strip() |
| 108 |
m.execute(f"chmod a-x {sebool_path}") |
| 109 |
b.click(f"{sebool_selector1} button{self.default_btn_class}") |
| 110 |
b.wait_in_text(f"{sebool_selector1} .pf-v6-c-alert", "Solution failed") |
| 111 |
b.wait_in_text(f"{sebool_selector1} .pf-v6-c-alert", "setsebool: Permission denied") |
| 112 |
|
| 113 |
# we can't re-attempt the solution, dismiss it |
| 114 |
b.click(row_selector + " input[type=checkbox]") |
| 115 |
b.click(dismiss_sel) |
| 116 |
b.wait_not_present(row_selector) |
| 117 |
|
| 118 |
# dismiss all non-test related alerts for pixel test purposes |
| 119 |
while True: |
| 120 |
# we either have no alerts, or an expand button in the table |
| 121 |
b.wait_js_cond( |
| 122 |
"ph_in_text('body', 'No SELinux alerts.') || ph_is_present('tbody .pf-v6-c-table__toggle')" |
| 123 |
) |
| 124 |
if "No SELinux alerts." in b.text('body'): |
| 125 |
break |
| 126 |
num_alerts = b.count("#selinux-alerts tbody") |
| 127 |
b.click('tbody:first-of-type input[type=checkbox]') |
| 128 |
b.click(dismiss_sel) |
| 129 |
# wait for the dismissed alert to go away |
| 130 |
b.wait_count("#selinux-alerts tbody", num_alerts - 1) |
| 131 |
|
| 132 |
# unbreak sebool, and trigger the error again |
| 133 |
m.execute(f"chmod a+x {sebool_path}; rm -r ~admin/public_html") |
| 134 |
if m.image.startswith("rhel-8"): |
| 135 |
# HACK: RHEL 8's setroubleshootd sometimes misses the second alert |
| 136 |
m.execute("systemctl restart setroubleshootd") |
| 137 |
b.reload() |
| 138 |
b.enter_page("/selinux") |
| 139 |
|
| 140 |
self.machine.execute(SELINUX_SEBOOL_ALERT_SCRIPT) |
| 141 |
row_selector = wait_for_alert("ls from read access on the directory") |
| 142 |
b.click(row_selector + " .pf-v6-c-table__toggle button") |
| 143 |
|
| 144 |
# Recompute selectors for the changed row id |
| 145 |
panel_selector = row_selector + " + tr.pf-m-expanded .ct-listing-panel-body" |
| 146 |
sebool_selector1 = panel_selector + f" .selinux-details[data-solution-id='{solution_id}']" |
| 147 |
|
| 148 |
# applying solution works now |
| 149 |
b.click(sebool_selector1 + " button" + self.default_btn_class) |
| 150 |
b.wait_in_text(sebool_selector1 + " .pf-v6-c-alert__title", "Solution applied successfully") |
| 151 |
self.assertIn("-> on", m.execute("getsebool httpd_enable_homedirs")) |
| 152 |
# system modifications automatically update for this new sebool |
| 153 |
# was "enable homedir", is now "read home directory" |
| 154 |
b.wait_text_matches(".modifications-table", "Allow httpd to .* home ?dir") |
| 155 |
|
| 156 |
# Second sebool solution can still be applied separately |
| 157 |
solution_id = b.attr(".selinux-details:contains('httpd_unified')", "data-solution-id") |
| 158 |
sebool_selector2 = panel_selector + f" .selinux-details[data-solution-id='{solution_id}']" |
| 159 |
b.wait_text(sebool_selector2 + " button" + self.default_btn_class, "Apply this solution") |
| 160 |
|
| 161 |
# now dismiss the alert |
| 162 |
b.click(row_selector + " input[type=checkbox]") |
| 163 |
b.assert_pixels("#app", "selinux", ignore=["table .ct-listing-panel-actions"]) |
| 164 |
b.click(dismiss_sel) |
| 165 |
b.wait_not_present(row_selector) |
| 166 |
|
| 167 |
if not m.image.startswith(("rhel-10", "centos-10")): |
| 168 |
######################################################### |
| 169 |
# trigger a fixable restorecon alert |
| 170 |
self.machine.execute(SELINUX_RESTORECON_ALERT_SCRIPT) |
| 171 |
|
| 172 |
row_selector = wait_for_alert("read access on the file /root/.ssh/authorized_keys") |
| 173 |
panel_selector = row_selector + " + tr.pf-m-expanded .ct-listing-panel-body" |
| 174 |
|
| 175 |
# expand it to see details |
| 176 |
toggle_selector = row_selector + " .pf-v6-c-table__toggle button" |
| 177 |
b.click(toggle_selector) |
| 178 |
|
| 179 |
try: |
| 180 |
# a solution is present |
| 181 |
b.wait_in_text(row_selector + " + tr.pf-m-expanded .ct-listing-panel-body", "you can run restorecon") |
| 182 |
|
| 183 |
# and it can be applied |
| 184 |
solution_id = b.attr(".selinux-details:contains('you can run restorecon')", "data-solution-id") |
| 185 |
btn_sel = panel_selector + f" .selinux-details[data-solution-id='{solution_id}'] button{self.default_btn_class}" |
| 186 |
b.click(btn_sel) |
| 187 |
|
| 188 |
# the button should disappear |
| 189 |
b.wait_not_present(btn_sel) |
| 190 |
|
| 191 |
# the fix should be applied |
| 192 |
b.wait_in_text(row_selector + "+ tr .selinux-details .pf-v6-c-alert__title", "Solution applied successfully") |
| 193 |
|
| 194 |
# and the button should not come back |
| 195 |
b.wait_not_present(btn_sel) |
| 196 |
except testlib.Error: |
| 197 |
print("==== sealert -l '*' ====") |
| 198 |
print(m.execute("sealert -l '*'")) |
| 199 |
print("==== audit.log ====") |
| 200 |
print(m.execute("cat /var/log/audit/audit.log")) |
| 201 |
print("====================") |
| 202 |
raise |
| 203 |
|
| 204 |
b.click(row_selector + " input[type=checkbox]") |
| 205 |
b.click(dismiss_sel) |
| 206 |
b.wait_not_present(row_selector) |
| 207 |
|
| 208 |
b.grant_permissions("clipboard-read", "clipboard-write") |
| 209 |
|
| 210 |
b.wait_visible(".pf-v6-c-data-list__cell:contains(\"daemons to\")") |
| 211 |
b.wait_visible(".pf-v6-c-data-list__cell:contains(\"fcontext -a -f a -t samba_share_t -r 's0' '/var/tmp/'\")") |
| 212 |
b.click("button:contains('View automation script')") |
| 213 |
ansible_script_sel = ".automation-script-modal .pf-v6-c-modal-box__body section:nth-child(2) textarea" |
| 214 |
shell_script_sel = ".automation-script-modal .pf-v6-c-modal-box__body section:nth-child(3) textarea" |
| 215 |
b.click("button:contains('Shell')") |
| 216 |
b.wait_in_text(shell_script_sel, "boolean -D") |
| 217 |
b.wait_in_text(shell_script_sel, "fcontext -D") |
| 218 |
b.wait_in_text(shell_script_sel, "boolean -m -1 daemons_dump_core") |
| 219 |
b.wait_in_text(shell_script_sel, "fcontext -a -f a -t samba_share_t -r 's0' '/var/tmp/'") |
| 220 |
|
| 221 |
# Check ansible |
| 222 |
def normalize(script: str) -> str: |
| 223 |
# HACK: `permissive` type is exported only since policycoreutils 3.0 |
| 224 |
# See https://github.com/SELinuxProject/selinux/commit/3a9b4505b |
| 225 |
# Combining of versions < 3.0 with versions >= 3.0 provides a bit |
| 226 |
# different outputs. |
| 227 |
script = script.replace("permissive -D\n", "") |
| 228 |
script = script.replace("boolean -m -1 selinuxuser_execmod\n", "") |
| 229 |
return script |
| 230 |
|
| 231 |
b.click("button:contains('Ansible')") |
| 232 |
b.wait_text_matches(ansible_script_sel, "Allow.*daemons.*core") |
| 233 |
|
| 234 |
ansible_m = self.machines["ansible_machine"] |
| 235 |
ansible_m.execute("semanage module -D") |
| 236 |
ansible_m.write("roles/selinux/tasks/main.yml", b.text(ansible_script_sel)) |
| 237 |
se_before = normalize(ansible_m.execute("semanage export")) |
| 238 |
ansible_m.execute("ansible -m include_role -a name=selinux localhost") |
| 239 |
se_after = normalize(ansible_m.execute("semanage export")) |
| 240 |
local = normalize(m.execute("semanage export")) |
| 241 |
|
| 242 |
self.assertNotEqual(se_before, se_after) |
| 243 |
self.assertEqual(local, se_after) |
| 244 |
|
| 245 |
# Check that ansible is idempotent |
| 246 |
m.execute("semanage boolean --modify --off daemons_dump_core") |
| 247 |
b.reload() |
| 248 |
b.enter_page("/selinux") |
| 249 |
with b.wait_timeout(30): |
| 250 |
b.wait_text_matches(".pf-v6-c-data-list__cell:contains(\"daemons\")", "Disallow.*daemons.*core") |
| 251 |
b.click("button:contains('View automation script')") |
| 252 |
ansible_m.write("roles/selinux/tasks/main.yml", b.text(ansible_script_sel)) |
| 253 |
se_before = se_after |
| 254 |
ansible_m.execute("ansible -m include_role -a name=selinux localhost") |
| 255 |
se_after = normalize(ansible_m.execute("semanage export")) |
| 256 |
local = normalize(m.execute("semanage export")) |
| 257 |
self.assertNotEqual(se_before, se_after) |
| 258 |
self.assertEqual(local, se_after) |
| 259 |
|
| 260 |
# Check the content of clipboard by pasting the clipboard to the terminal |
| 261 |
b.click("button:contains('Shell script')") |
| 262 |
b.wait_in_text(shell_script_sel, "boolean -D") |
| 263 |
b.click(".automation-script-modal .btn-clipboard") |
| 264 |
b.go("/system/terminal") |
| 265 |
b.enter_page("/system/terminal") |
| 266 |
b.focus('.terminal') |
| 267 |
b.input_text('"\n') |
| 268 |
# Right click to open context menu and paste |
| 269 |
b.mouse(".terminal", "click", btn=2) |
| 270 |
b.click('.contextMenu li:nth-child(2) button') |
| 271 |
b.wait_in_text(".xterm-accessibility-tree", "semanage import <<EOF") |
| 272 |
b.wait_in_text(".xterm-accessibility-tree", "boolean -D") |
| 273 |
b.wait_in_text(".xterm-accessibility-tree", "boolean -m -0 daemons_dump_core") |
| 274 |
b.wait_in_text(".xterm-accessibility-tree", "fcontext -a -f a -t samba_share_t -r 's0' '/var/tmp/'") |
| 275 |
|
| 276 |
m.execute("semanage boolean -D; semanage fcontext -D; semanage module -D") |
| 277 |
b.go("/selinux") |
| 278 |
b.enter_page("/selinux") |
| 279 |
b.reload() |
| 280 |
b.enter_page("/selinux") |
| 281 |
with b.wait_timeout(30): |
| 282 |
if m.image.startswith("rhel"): |
| 283 |
# HACK: rhc sets itself as permissive on RHEL; https://issues.redhat.com/browse/RHEL-20352 |
| 284 |
b.wait_text_matches("ul[aria-label='System modifications']", "No system modifications|permissive -a rhcd_t") |
| 285 |
else: |
| 286 |
b.wait_text("ul[aria-label='System modifications']", "No system modifications") |
| 287 |
b.relogin("/selinux", "admin", superuser=False) |
| 288 |
b.wait_text("ul[aria-label='System modifications']", "The logged in user is not permitted to view system modifications") |
| 289 |
|
| 290 |
|
| 291 |
# https://github.com/fedora-selinux/selinux-policy/issues/2947 |
| 292 |
@testlib.skipDaily("nightly has selinux set to permissive") |
| 293 |
@testlib.skipImage("No cockpit-selinux", "debian-*", "ubuntu-*", "arch", "opensuse-tumbleweed") |
| 294 |
@testlib.skipOstree("No cockpit-selinux") |
| 295 |
@testlib.nondestructive |
| 296 |
class TestSelinuxEnforcing(testlib.MachineCase): |
| 297 |
def test(self): |
| 298 |
b = self.browser |
| 299 |
m = self.machine |
| 300 |
|
| 301 |
self.login_and_go("/selinux") |
| 302 |
|
| 303 |
######################################################### |
| 304 |
# wait for the page to be present |
| 305 |
b.wait_in_text("body", "SELinux policy") |
| 306 |
|
| 307 |
def assertEnforce(active: bool) -> None: |
| 308 |
# polled every 10s |
| 309 |
with b.wait_timeout(20): |
| 310 |
b.wait_visible(".pf-v6-c-switch__input" + (":checked" if active else ":not(:checked)")) |
| 311 |
|
| 312 |
# SELinux should be enabled and enforcing at the beginning |
| 313 |
assertEnforce(active=True) |
| 314 |
self.addCleanup(m.execute, "setenforce 1") |
| 315 |
m.execute("getenforce | grep -q 'Enforcing'") |
| 316 |
|
| 317 |
# now set to permissive using the ui button |
| 318 |
b.click(".pf-v6-c-switch__input") |
| 319 |
assertEnforce(active=False) |
| 320 |
m.execute("getenforce | grep -q 'Permissive'") |
| 321 |
|
| 322 |
# when in permissive mode, expect a warning |
| 323 |
b.wait_in_text("div.selinux-policy-ct", "Setting deviates") |
| 324 |
|
| 325 |
# switch back using cli, ui should react |
| 326 |
m.execute("setenforce 1") |
| 327 |
assertEnforce(active=True) |
| 328 |
|
| 329 |
# warning should disappear |
| 330 |
b.wait_not_in_text("div.selinux-policy-ct", "Setting deviates") |
| 331 |
|
| 332 |
# Switch to another page |
| 333 |
b.switch_to_top() |
| 334 |
b.go("/system") |
| 335 |
b.enter_page("/system") |
| 336 |
|
| 337 |
# Now on another page change the status |
| 338 |
m.execute("setenforce 0") |
| 339 |
|
| 340 |
# Switch back into the page and we get the updated status |
| 341 |
b.switch_to_top() |
| 342 |
b.go("/selinux") |
| 343 |
b.enter_page("/selinux") |
| 344 |
assertEnforce(active=False) |
| 345 |
b.wait_in_text("div.selinux-policy-ct", "Setting deviates") |
| 346 |
|
| 347 |
|
| 348 |
if __name__ == '__main__': |
| 349 |
testlib.test_main() |