Skip to content

Commit 23fd454

Browse files
committed
Add simple_malloc file-capability heap injection reproducer
1 parent 5011567 commit 23fd454

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎test/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,13 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "SanitizedDebug")
523523
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/simple_malloc-ut.sh
524524
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
525525

526+
add_test(
527+
NAME simple_malloc-file-capabilities
528+
COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/simple_malloc-file-capabilities-ut.sh
529+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
530+
set_tests_properties(simple_malloc-file-capabilities PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120
531+
LABELS "privileged")
532+
526533
add_test(
527534
NAME simple_malloc-with-event-reordering
528535
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/simple_malloc-ut.sh
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
3+
# Reproduce #562: file capabilities suppress LD_PRELOAD in secure execution.
4+
# This test asserts the current limitation, with allocation-positive controls.
5+
set -euo pipefail
6+
7+
skip() {
8+
echo "SKIP: $*"
9+
exit 77
10+
}
11+
12+
[[ ${EUID} -eq 0 ]] || skip "requires root for file capabilities and --switch_user"
13+
for tool in setcap setpriv getent timeout tail; do
14+
command -v "${tool}" >/dev/null || skip "requires ${tool}"
15+
done
16+
17+
builddir=${PWD}
18+
scriptdir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
19+
test_user=nobody
20+
test_uid=$(getent passwd "${test_user}" | cut -d: -f3) || skip "requires user ${test_user}"
21+
test_gid=$(getent passwd "${test_user}" | cut -d: -f4)
22+
[[ ${test_uid} -ne 0 ]] || skip "requires a non-root test user"
23+
24+
# /tmp and build mounts may be nosuid, so use a private copy on the root filesystem.
25+
tmpdir=$(mktemp -d /usr/local/bin/ddprof-filecaps.XXXXXX) || skip "cannot create test directory"
26+
trap 'rm -rf -- "${tmpdir}"' EXIT
27+
chmod 755 "${tmpdir}"
28+
fixture=${tmpdir}/simple_malloc
29+
cp "${builddir}/test/simple_malloc" "${fixture}"
30+
chmod 755 "${fixture}"
31+
32+
setcap cap_net_bind_service=ep "${fixture}" || skip "cannot set file capabilities"
33+
probe=$(setpriv --reuid="${test_uid}" --regid="${test_gid}" --clear-groups \
34+
"${fixture}" --show-security-context --loop 1 2>&1) || skip "cannot execute capability-marked fixture: ${probe}"
35+
grep -q "at_secure=1" <<< "${probe}" || skip "file capabilities did not trigger AT_SECURE (nosuid or restricted capabilities)"
36+
setcap -r "${fixture}"
37+
38+
export DD_PROFILING_NATIVE_USE_EMBEDDED_LIB=1
39+
export LD_LIBRARY_PATH=${builddir}
40+
export DD_PROFILING_NATIVE_CONFIG=${scriptdir}/configs/simple_malloc_config.toml
41+
42+
check() {
43+
local name=$1 expected_secure=$2 expected_preload=$3
44+
local log_file=${tmpdir}/${name}.log output=${tmpdir}/${name}.out
45+
local profiler_pid samples
46+
47+
echo "--- ${name} ---"
48+
if ! timeout 30 "${builddir}/ddprof" --switch_user "${test_user}" -o "${log_file}" \
49+
"${fixture}" --show-security-context --malloc 1048576 --realloc 0 \
50+
--loop 64 --spin 1000 >"${output}" 2>&1; then
51+
cat "${output}" "${log_file}"
52+
return 1
53+
fi
54+
profiler_pid=$(grep -m1 -oP ' ddprof\[\K[0-9]+(?=\]: Starting profiler)' "${log_file}")
55+
timeout 30 tail --pid="${profiler_pid}" -f /dev/null
56+
57+
cat "${output}"
58+
grep -q "^TestSecurity: uid=${test_uid} euid=${test_uid} at_secure=${expected_secure} ld_preload=${expected_preload}$" "${output}"
59+
samples=$(grep -c 'sample\[type=alloc-space;.*;do_lot_of_allocations' "${log_file}" || true)
60+
echo "Allocation samples in do_lot_of_allocations: ${samples}"
61+
if [[ ${expected_secure} -eq 1 ]]; then
62+
[[ ${samples} -eq 0 ]]
63+
else
64+
[[ ${samples} -gt 0 ]] || { cat "${log_file}"; return 1; }
65+
fi
66+
}
67+
68+
check no-capabilities 0 set
69+
setcap cap_net_bind_service=ep "${fixture}"
70+
check file-capabilities 1 unset
71+
setcap -r "${fixture}"
72+
check capabilities-removed 0 set
73+
74+
echo "REPRODUCED #562: AT_SECURE strips LD_PRELOAD and allocation samples disappear."

‎test/simple_malloc.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <dlfcn.h>
1616
#include <functional>
1717
#include <iostream>
18+
#include <sys/auxv.h>
1819
#include <sys/resource.h>
1920
#include <thread>
2021
#include <unistd.h>
@@ -280,6 +281,7 @@ int main(int argc, char *argv[]) {
280281
unsigned int nb_threads{1};
281282

282283
Options opts;
284+
bool show_security_context = false;
283285
std::vector<std::string> exec_args;
284286

285287
constexpr size_t k_default_malloc_size{1000};
@@ -338,6 +340,8 @@ int main(int argc, char *argv[]) {
338340
->check(CLI::Bound(-20, 19)); // NOLINT(readability-magic-numbers)
339341
app.add_flag("--stop", opts.stop,
340342
"Stop process just after spawning fork / threads");
343+
app.add_flag("--show-security-context", show_security_context,
344+
"Print UID, AT_SECURE, and LD_PRELOAD presence");
341345

342346
if (opts.nice != 0) {
343347
setpriority(PRIO_PROCESS, 0, opts.nice);
@@ -355,6 +359,11 @@ int main(int argc, char *argv[]) {
355359
# endif
356360

357361
CLI11_PARSE(app, argc, argv);
362+
if (show_security_context) {
363+
printf("TestSecurity: uid=%u euid=%u at_secure=%lu ld_preload=%s\n",
364+
static_cast<unsigned>(getuid()), static_cast<unsigned>(geteuid()),
365+
getauxval(AT_SECURE), getenv("LD_PRELOAD") ? "set" : "unset");
366+
}
358367
# ifdef USE_DD_PROFILING
359368
if (start_profiling && ddprof_start_profiling() != 0) {
360369
fprintf(stderr, "Failed to start profiling\n");

0 commit comments

Comments
 (0)