Skip to content

Commit

Permalink
test_signal_crashes: move suid binary creation into a function
Browse files Browse the repository at this point in the history
This makes reuse easier.
  • Loading branch information
schopin-pro committed Nov 22, 2024
1 parent e858011 commit e606d88
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions tests/integration/test_signal_crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import argparse
import collections
import contextlib
import datetime
import grp
import os
Expand All @@ -31,7 +32,7 @@
import time
import typing
import unittest
from collections.abc import Callable
from collections.abc import Callable, Iterator
from unittest.mock import MagicMock

import psutil
Expand All @@ -56,6 +57,18 @@
APPORT_PATH = get_data_directory() / "apport"
apport_binary = import_module_from_file(APPORT_PATH)


@contextlib.contextmanager
def create_suid(tmpdir: str = "/var/tmp") -> Iterator[str]:
"""Creates a `sleep` suid binary in a subdirectory of `tmpdir`."""
src_bin = os.path.realpath("/bin/sleep")
with tempfile.TemporaryDirectory(dir=tmpdir) as tempdir:
binary = f"{tempdir}/sleep"
shutil.copy(src_bin, binary)
os.chmod(binary, 0o4755)
yield binary


MAIL_UID = 8
test_package = "coreutils"
test_source = "coreutils"
Expand Down Expand Up @@ -684,20 +697,12 @@ def test_logging_stderr(self):
@unittest.skipIf(os.geteuid() != 0, "this test needs to be run as root")
def test_crash_setuid_keep(self) -> None:
"""Report generation for setuid program which stays root."""
# create suid root executable in a path we can modify which apport
# regards as likely packaged
(fd, myexe) = tempfile.mkstemp(dir="/var/tmp")
self.addCleanup(os.unlink, myexe)
with open(self.TEST_EXECUTABLE, "rb") as f:
os.write(fd, f.read())
os.close(fd)
os.chmod(myexe, 0o4755)

resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))

# if a user can crash a suid root binary, it should not create
# core files
self.do_crash(command=myexe, uid=MAIL_UID, suid_dumpable=2, cwd="/run")
with create_suid() as suid:
resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))
# if a user can crash a suid root binary, it should not create
# core files
# run test program in /run (which should only be writable to root)
self.do_crash(command=suid, uid=MAIL_UID, suid_dumpable=2, cwd="/run")

@unittest.skipUnless(os.path.exists("/bin/ping"), "this test needs /bin/ping")
@unittest.skipIf(os.geteuid() != 0, "this test needs to be run as root")
Expand Down Expand Up @@ -731,24 +736,17 @@ def test_crash_setuid_unpackaged(self) -> None:
"""Report generation for unpackaged setuid program."""
# create suid root executable in a path we can modify which apport
# regards as not packaged
(fd, myexe) = tempfile.mkstemp(dir="/tmp")
self.addCleanup(os.unlink, myexe)
with open(self.TEST_EXECUTABLE, "rb") as f:
os.write(fd, f.read())
os.close(fd)
os.chmod(myexe, 0o4755)

resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))

# if a user can crash a suid root binary, it should not create
# core files
self.do_crash(
command=myexe,
expect_corefile=False,
expect_report=False,
uid=MAIL_UID,
suid_dumpable=2,
)
with create_suid(tmpdir="/tmp") as suid:
resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))
# if a user can crash a suid root binary, it should not create
# core files
self.do_crash(
command=suid,
expect_corefile=False,
expect_report=False,
uid=MAIL_UID,
suid_dumpable=2,
)

def test_coredump_from_socket(self):
"""Forward a core dump through a socket.
Expand Down

0 comments on commit e606d88

Please sign in to comment.