|
| 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." |
0 commit comments