| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv) |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2013 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import testlib |
| 9 |
|
| 10 |
|
| 11 |
@testlib.nondestructive |
| 12 |
class TestSession(testlib.MachineCase): |
| 13 |
|
| 14 |
@testlib.skipImage("TODO: fix for OpenSUSE", "opensuse-tumbleweed") |
| 15 |
def testBasic(self): |
| 16 |
m = self.machine |
| 17 |
b = self.browser |
| 18 |
|
| 19 |
def wait_session(should_exist): |
| 20 |
if should_exist: |
| 21 |
m.execute("until loginctl list-sessions | grep admin; do sleep 1; done") |
| 22 |
else: |
| 23 |
# restart logind to mop up empty "closing" sessions, like in testlib terminate_sessions() |
| 24 |
m.execute("""while true; do |
| 25 |
OUT=$(loginctl list-sessions) |
| 26 |
echo "$OUT" | grep admin || break |
| 27 |
if echo "$OUT | grep closing"; then |
| 28 |
systemctl stop systemd-logind |
| 29 |
fi |
| 30 |
sleep 3 |
| 31 |
done""") |
| 32 |
|
| 33 |
wait_session(should_exist=False) |
| 34 |
|
| 35 |
# Login |
| 36 |
self.login_and_go("/system") |
| 37 |
wait_session(should_exist=True) |
| 38 |
|
| 39 |
# Check session type |
| 40 |
if not m.ws_container: |
| 41 |
# Systemd 256 also shows a class=manager session for admin |
| 42 |
session_id = m.execute("loginctl list-sessions | grep -v manager | awk '/admin/ {print $1}'").strip() |
| 43 |
self.assertEqual(m.execute(f"loginctl show-session -p Type {session_id}").strip(), "Type=web") |
| 44 |
|
| 45 |
if not m.ws_container and m.image != "arch": |
| 46 |
# starts ssh-agent in session |
| 47 |
bridge = m.execute("pgrep -u admin cockpit-bridge").strip() |
| 48 |
out = m.execute(f"grep --null-data SSH_AGENT_PID /proc/{bridge}/environ | xargs -0") |
| 49 |
agent = out.split("=")[1].strip() |
| 50 |
self.assertIn("ssh-agent", m.execute(f"readlink /proc/{agent}/exe").strip()) |
| 51 |
|
| 52 |
# Logout |
| 53 |
b.logout() |
| 54 |
b.wait_visible("#login") |
| 55 |
wait_session(should_exist=False) |
| 56 |
|
| 57 |
# cleans up ssh-agent |
| 58 |
m.execute("while pgrep -u admin ssh-agent; do sleep 1; done", timeout=10) |
| 59 |
|
| 60 |
# Login again |
| 61 |
b.set_val("#login-user-input", "admin") |
| 62 |
b.set_val("#login-password-input", "foobar") |
| 63 |
b.click('#login-button') |
| 64 |
b.enter_page("/system") |
| 65 |
wait_session(should_exist=True) |
| 66 |
|
| 67 |
# Kill session from the outside |
| 68 |
m.execute("loginctl terminate-user admin") |
| 69 |
wait_session(should_exist=False) |
| 70 |
|
| 71 |
b.relogin("/system", "admin") |
| 72 |
wait_session(should_exist=True) |
| 73 |
|
| 74 |
# Kill session from the inside |
| 75 |
b.logout() |
| 76 |
wait_session(should_exist=False) |
| 77 |
|
| 78 |
# try to pwn $SSH_AGENT_PID via pam_env's user_readenv=1 (CVE-2024-6126) |
| 79 |
|
| 80 |
if m.ws_container: |
| 81 |
# not using cockpit's PAM config |
| 82 |
return |
| 83 |
|
| 84 |
# this is enabled by default in tools/cockpit.debian.pam, as well as |
| 85 |
# Debian/Ubuntu's /etc/pam.d/sshd; but not in Fedora/RHEL |
| 86 |
if "debian" not in m.image and "ubuntu" not in m.image: |
| 87 |
self.write_file("/etc/pam.d/cockpit", "session required pam_env.so user_readenv=1\n", append=True) |
| 88 |
victim_pid = m.spawn("sleep infinity", "sleep.log") |
| 89 |
self.addCleanup(m.execute, f"kill {victim_pid} || true") |
| 90 |
self.write_file("/home/admin/.pam_environment", f"SSH_AGENT_PID DEFAULT={victim_pid}\n", owner="admin") |
| 91 |
|
| 92 |
b.login_and_go() |
| 93 |
wait_session(should_exist=True) |
| 94 |
# starts ssh-agent in session |
| 95 |
m.execute("pgrep -u admin ssh-agent") |
| 96 |
# but the session has the modified SSH_AGENT_PID |
| 97 |
bridge = m.execute("pgrep -u admin cockpit-bridge").strip() |
| 98 |
agent = m.execute(f"grep --null-data SSH_AGENT_PID /proc/{bridge}/environ | xargs -0 | sed 's/.*=//'").strip() |
| 99 |
self.assertEqual(agent, str(victim_pid)) |
| 100 |
|
| 101 |
# logging out still kills the actual ssh-agent, not the victim pid |
| 102 |
b.logout() |
| 103 |
wait_session(should_exist=False) |
| 104 |
m.execute("while pgrep -u admin ssh-agent; do sleep 1; done", timeout=10) |
| 105 |
m.execute(f"test -e /proc/{victim_pid}") |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == '__main__': |
| 109 |
testlib.test_main() |