Skip to content

Commit 849f6b9

Browse files
committed
test: check coredump handling in containers & namespaces
This is partially based on upstream's 097e287, which tests coredump forwarding (that we don't have in RHEL 9). It also provides basic coverage for RHEL-29430 (generating stack traces for processes in containers without coredump fowarding). rhel-only Related: RHEL-29430
1 parent 646e8da commit 849f6b9

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

test/test-functions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2619,7 +2619,7 @@ inst_binary() {
26192619
# ls, stat - pulls in nss_systemd with certain options (like ls -l) when
26202620
# nsswitch.conf uses [SUCCESS=merge] (like on Arch Linux)
26212621
# tar - called by machinectl in TEST-25
2622-
if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$bin" =~ /(chown|getent|login|ls|stat|su|tar|useradd|userdel)$ ]]; then
2622+
if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$bin" =~ /(chown|getent|login|id|ls|stat|su|tar|useradd|userdel)$ ]]; then
26232623
wrap_binary=1
26242624
fi
26252625

test/units/testsuite-74.coredump.sh

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,68 @@ rm -fv /run/systemd/coredump.conf.d/99-external.conf
7474
# Wait a bit for the coredumps to get processed
7575
timeout 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $CORE_TEST_BIN | wc -l) -lt 4 ]]; do sleep 1; done"
7676

77+
# RHEL9: following part is taken out of 097e28736aed9280dfac0f8e8096deca71bac813 but slightly tweaked, since
78+
# in RHEL9 we don't have the support for coredump forwarding
79+
CONTAINER="testsuite-74-container"
80+
TESTUSER_UID="$(id -u testuser)"
81+
TESTUSER_GID="$(id -g testuser)"
82+
83+
mkdir -p "/var/lib/machines/$CONTAINER"
84+
mkdir -p "/run/systemd/system/systemd-nspawn@$CONTAINER.service.d"
85+
# Bind-mounting /etc into the container kinda defeats the purpose of --volatile=,
86+
# but we need the ASan-related overrides scattered across /etc
87+
cat > "/run/systemd/system/systemd-nspawn@$CONTAINER.service.d/override.conf" << EOF
88+
[Service]
89+
ExecStart=
90+
ExecStart=systemd-nspawn --quiet --link-journal=try-guest --keep-unit --machine=%i --boot \
91+
--volatile=yes --directory=/ --bind-ro=/etc --inaccessible=/etc/machine-id
92+
EOF
93+
systemctl daemon-reload
94+
95+
machinectl start "$CONTAINER"
96+
timeout 60 bash -xec "until systemd-run -M '$CONTAINER' -q --wait --pipe true; do sleep .5; done"
97+
machinectl copy-to "$CONTAINER" "$MAKE_DUMP_SCRIPT"
98+
99+
run_namespaced_coredump_tests() {
100+
local TS
101+
102+
# Make a couple of coredumps in a full-fleged container
103+
TS="$(date +"%s.%N")"
104+
[[ "$(coredumpctl list --since="@$TS" -q --no-legend /usr/bin/sleep | wc -l)" -eq 0 ]]
105+
[[ "$(coredumpctl list --since="@$TS" -q --no-legend /usr/bin/sleep _UID="$TESTUSER_UID" | wc -l)" -eq 0 ]]
106+
systemd-run -M "testuser@$CONTAINER" --user -q --wait --pipe "$MAKE_DUMP_SCRIPT" "/usr/bin/sleep" "SIGABRT"
107+
systemd-run -M "$CONTAINER" -q --wait --pipe "$MAKE_DUMP_SCRIPT" "/usr/bin/sleep" "SIGTRAP"
108+
# Wait a bit for the coredumps to get processed
109+
timeout 30 bash -c "while [[ \$(coredumpctl list --since=@$TS -q --no-legend /usr/bin/sleep | wc -l) -ne 2 ]]; do sleep 1; done"
110+
coredumpctl list
111+
[[ "$(coredumpctl list --since="@$TS" -q --no-legend /usr/bin/sleep _UID="$TESTUSER_UID" _GID="$TESTUSER_GID" | wc -l)" -eq 1 ]]
112+
113+
# Simplified version of the above - not a full container, just a mount & pid namespace
114+
TS="$(date +"%s.%N")"
115+
unshare --mount --pid --fork --mount-proc /bin/bash -xec "$MAKE_DUMP_SCRIPT /usr/bin/sleep SIGABRT"
116+
timeout 30 bash -c "while [[ \$(coredumpctl list --since=@$TS -q --no-legend /usr/bin/sleep | wc -l) -ne 1 ]]; do sleep 1; done"
117+
TS="$(date +"%s.%N")"
118+
unshare --setuid="$TESTUSER_UID" --setgid="$TESTUSER_GID" --mount --pid --fork --mount-proc /bin/bash -xec "$MAKE_DUMP_SCRIPT /usr/bin/sleep SIGABRT"
119+
timeout 30 bash -c "while [[ \$(coredumpctl list --since=@$TS -q --no-legend /usr/bin/sleep _UID=$TESTUSER_UID _GID=$TESTUSER_GID | wc -l) -ne 1 ]]; do sleep 1; done"
120+
}
121+
122+
# First, run the tests with default systemd-coredumpd settings
123+
run_namespaced_coredump_tests
124+
125+
# And now with SYSTEMD_COREDUMP_ALLOW_NAMESPACE_CHANGE=1 (RHEL-only)
126+
cat >/tmp/coredump-handler.sh <<EOF
127+
#!/bin/bash
128+
export SYSTEMD_COREDUMP_ALLOW_NAMESPACE_CHANGE=1
129+
exec /usr/lib/systemd/systemd-coredump "\$@"
130+
EOF
131+
chmod +x /tmp/coredump-handler.sh
132+
sysctl -w kernel.core_pattern="|/tmp/coredump-handler.sh %P %u %g %s %t %c %h"
133+
run_namespaced_coredump_tests
134+
135+
# Restore the original coredump handler
136+
sysctl -p /usr/lib/sysctl.d/50-coredump.conf
137+
sysctl kernel.core_pattern
138+
77139
coredumpctl
78140
SYSTEMD_LOG_LEVEL=debug coredumpctl
79141
coredumpctl --help
@@ -89,7 +151,7 @@ coredumpctl --json=pretty | jq
89151
coredumpctl --json=off
90152
coredumpctl --root=/
91153
coredumpctl --directory=/var/log/journal
92-
coredumpctl --file="/var/log/journal/$(</etc/machine-id)/system.journal"
154+
coredumpctl --file="/var/log/journal/$(</etc/machine-id)"/*.journal
93155
coredumpctl --since=@0
94156
coredumpctl --since=yesterday --until=tomorrow
95157
# We should have a couple of externally stored coredumps

0 commit comments

Comments
 (0)