|
| 1 | +import os |
| 2 | +import shlex |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +class SnapConfineCVE20268933Tests(unittest.TestCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + cls.repo_root = Path(__file__).resolve().parents[2] |
| 13 | + cls.function_file = ( |
| 14 | + cls.repo_root |
| 15 | + / "linPEAS" |
| 16 | + / "builder" |
| 17 | + / "linpeas_parts" |
| 18 | + / "functions" |
| 19 | + / "checkSnapConfineCVE20268933.sh" |
| 20 | + ) |
| 21 | + |
| 22 | + def _make_root(self, base, release="24.04", snap_version=None): |
| 23 | + root = base / "root" |
| 24 | + (root / "etc").mkdir(parents=True) |
| 25 | + (root / "etc" / "os-release").write_text( |
| 26 | + f'ID=ubuntu\nVERSION_ID="{release}"\n', encoding="utf-8" |
| 27 | + ) |
| 28 | + if snap_version is None: |
| 29 | + binary = root / "usr" / "lib" / "snapd" / "snap-confine" |
| 30 | + else: |
| 31 | + binary = ( |
| 32 | + root |
| 33 | + / "snap" |
| 34 | + / "snapd" |
| 35 | + / "current" |
| 36 | + / "usr" |
| 37 | + / "lib" |
| 38 | + / "snapd" |
| 39 | + / "snap-confine" |
| 40 | + ) |
| 41 | + meta = root / "snap" / "snapd" / "current" / "meta" |
| 42 | + meta.mkdir(parents=True) |
| 43 | + (meta / "snap.yaml").write_text( |
| 44 | + f"name: snapd\nversion: {snap_version}\n", encoding="utf-8" |
| 45 | + ) |
| 46 | + binary.parent.mkdir(parents=True, exist_ok=True) |
| 47 | + binary.write_text("#!/bin/sh\ntouch \"$EXECUTED_MARKER\"\n", encoding="utf-8") |
| 48 | + binary.chmod(0o755) |
| 49 | + return root, binary |
| 50 | + |
| 51 | + def _run_check(self, root, package_version="2.75+ubuntu24.04"): |
| 52 | + bindir = root.parent / "bin" |
| 53 | + bindir.mkdir() |
| 54 | + getcap = bindir / "getcap" |
| 55 | + getcap.write_text( |
| 56 | + '#!/bin/sh\nprintf "%s cap_chown,cap_sys_admin=p\\n" "$1"\n', |
| 57 | + encoding="utf-8", |
| 58 | + ) |
| 59 | + getcap.chmod(0o755) |
| 60 | + dpkg_query = bindir / "dpkg-query" |
| 61 | + dpkg_query.write_text( |
| 62 | + '#!/bin/sh\nprintf "%s\\n" "$FAKE_SNAPD_VERSION"\n', encoding="utf-8" |
| 63 | + ) |
| 64 | + dpkg_query.chmod(0o755) |
| 65 | + |
| 66 | + marker = root.parent / "executed" |
| 67 | + env = os.environ.copy() |
| 68 | + env.update( |
| 69 | + { |
| 70 | + "EXECUTED_MARKER": str(marker), |
| 71 | + "FAKE_SNAPD_VERSION": package_version, |
| 72 | + "PATH": f"{bindir}:{env['PATH']}", |
| 73 | + } |
| 74 | + ) |
| 75 | + body = "\n".join( |
| 76 | + [ |
| 77 | + f"ROOT_FOLDER={shlex.quote(str(root))}", |
| 78 | + "E=E", |
| 79 | + "SED_RED_YELLOW='&'", |
| 80 | + "SED_LIGHT_CYAN='&'", |
| 81 | + "SED_GREEN='&'", |
| 82 | + 'print_3title() { echo "TITLE: $1"; }', |
| 83 | + "print_info() { :; }", |
| 84 | + f". {shlex.quote(str(self.function_file))}", |
| 85 | + "checkSnapConfineCVE20268933", |
| 86 | + ] |
| 87 | + ) |
| 88 | + result = subprocess.run( |
| 89 | + ["sh", "-c", body], |
| 90 | + cwd=str(self.repo_root), |
| 91 | + env=env, |
| 92 | + capture_output=True, |
| 93 | + text=True, |
| 94 | + check=False, |
| 95 | + ) |
| 96 | + return result, marker |
| 97 | + |
| 98 | + def test_vulnerable_ubuntu_package_is_reported_without_executing_binary(self): |
| 99 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 100 | + root, binary = self._make_root(Path(tmpdir)) |
| 101 | + result, marker = self._run_check(root) |
| 102 | + self.assertFalse(marker.exists(), "the privileged binary must never be executed") |
| 103 | + |
| 104 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 105 | + self.assertIn("TITLE: Set-capabilities snap-confine", result.stdout) |
| 106 | + self.assertIn("Vulnerable to CVE-2026-8933", result.stdout) |
| 107 | + |
| 108 | + def test_release_specific_fixed_versions(self): |
| 109 | + cases = ( |
| 110 | + ("2.76+ubuntu22.04", "22.04", True), |
| 111 | + ("2.76+ubuntu22.04.1", "22.04", False), |
| 112 | + ("2.76+ubuntu24.04", "24.04", True), |
| 113 | + ("2.76+ubuntu24.04.1", "24.04", False), |
| 114 | + ("2.76+ubuntu26.04.2", "26.04", True), |
| 115 | + ("2.76+ubuntu26.04.3", "26.04", False), |
| 116 | + ("2.75.1", "snap", True), |
| 117 | + ("2.76.1", "snap", False), |
| 118 | + ) |
| 119 | + checks = [] |
| 120 | + for version, release, vulnerable in cases: |
| 121 | + kind = "snap" if release == "snap" else "deb" |
| 122 | + checks.append( |
| 123 | + "sc8933_version_is_vulnerable " |
| 124 | + f"{shlex.quote(version)} {kind} {release} && " |
| 125 | + f"echo {version}=yes || echo {version}=no" |
| 126 | + ) |
| 127 | + body = "\n".join( |
| 128 | + [f". {shlex.quote(str(self.function_file))}"] + checks |
| 129 | + ) |
| 130 | + result = subprocess.run( |
| 131 | + ["sh", "-c", body], |
| 132 | + cwd=str(self.repo_root), |
| 133 | + capture_output=True, |
| 134 | + text=True, |
| 135 | + check=False, |
| 136 | + ) |
| 137 | + |
| 138 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 139 | + for version, _, vulnerable in cases: |
| 140 | + expected = "yes" if vulnerable else "no" |
| 141 | + self.assertIn(f"{version}={expected}", result.stdout) |
| 142 | + |
| 143 | + def test_fixed_ubuntu_package_is_not_flagged(self): |
| 144 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 145 | + root, binary = self._make_root(Path(tmpdir)) |
| 146 | + result, _ = self._run_check(root, package_version="2.76+ubuntu24.04.1") |
| 147 | + |
| 148 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 149 | + self.assertIn("not in the known CVE-2026-8933 vulnerable range", result.stdout) |
| 150 | + self.assertNotIn("Vulnerable to CVE-2026-8933", result.stdout) |
| 151 | + |
| 152 | + def test_vulnerable_snap_revision_is_reported(self): |
| 153 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 154 | + root, binary = self._make_root(Path(tmpdir), snap_version="2.75.1") |
| 155 | + result, _ = self._run_check(root) |
| 156 | + |
| 157 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 158 | + self.assertIn("Vulnerable to CVE-2026-8933", result.stdout) |
| 159 | + self.assertIn("version 2.75.1", result.stdout) |
| 160 | + |
| 161 | + def test_setuid_snap_confine_is_excluded(self): |
| 162 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 163 | + root, binary = self._make_root(Path(tmpdir)) |
| 164 | + binary.chmod(0o4755) |
| 165 | + result, _ = self._run_check(root) |
| 166 | + |
| 167 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 168 | + self.assertEqual("", result.stdout) |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + unittest.main() |
0 commit comments