Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,90 @@ jobs:
- name: test installer upgrade and recovery paths
if: matrix.name == 'linux-x64'
run: bash install/test-install.sh
# Linux kernel sensor (#59). Runs the agent as an unprivileged user with
# exactly the systemd unit's capabilities (sudo setpriv), starts 50
# short-lived /bin/true processes and expects at least 50 process_start
# events from audit netlink. If the runner refuses audit netlink the
# step proves the auditd log path instead and says so in the log.
- name: prove Linux kernel sensor (audit netlink, 50 short-lived processes)
if: matrix.name == 'linux-x64'
timeout-minutes: 5
run: |
set -euo pipefail
bin="$PWD/zig-out/bin/tawny-agent"
caps="audit_control,audit_read,dac_read_search,kill,sys_ptrace"
plus="+${caps//,/,+}"
sudo useradd --system --no-create-home --shell /usr/sbin/nologin tawny-ci || true
out="$RUNNER_TEMP/sensor.out"
# On any failure, show the agent's own output (panics land there).
trap 'rc=$?; if [[ $rc -ne 0 ]]; then echo "--- sensor output ($out) ---"; cat "$out" 2>/dev/null || true; fi' EXIT
# Another audit daemon would own the socket; stop it if present.
sudo systemctl stop auditd 2>/dev/null || true
sudo setpriv --reuid=tawny-ci --regid=tawny-ci --init-groups \
--inh-caps="$plus" --ambient-caps="$plus" --bounding-set="-all,$plus" \
"$bin" --linux-sensor-dump --seconds 8 > "$out" 2>&1 &
dump=$!
for _ in $(seq 1 50); do
if grep -q '^READY' "$out" 2>/dev/null; then break; fi
sleep 0.1
done
grep '^SENSOR_HEALTH' "$out"
source="$(sed -n 's/^READY\t\([a-z_]*\)\t.*/\1/p' "$out")"
echo "sensor source: $source"
for _ in $(seq 1 50); do /bin/true; done
wait "$dump"
tail -n 1 "$out"
if [[ "$source" == "audit_netlink" ]]; then
# /bin/true may resolve elsewhere (gnutrue on Ubuntu 26.04), so match the real path.
true_img="$(readlink -f /bin/true)"
# Count process_start lines only; process_exit carries the same image.
n="$(grep $'^EVENT\tprocess_start\t' "$out" | grep -cF "\"image\":\"$true_img\"" || true)"
echo "/bin/true resolves to $true_img"
echo "process_start events for /bin/true: $n"
test "$n" -ge 50
grep -q $'^EVENT\tprocess_start\t.*"entity_id":"[a-z2-7]\\{26\\}"' "$out"
grep -q $'^EVENT\tprocess_exit\t' "$out"
# The agent's rules are gone after a clean stop.
if command -v auditctl >/dev/null 2>&1 && sudo auditctl -l | grep -q 'key=tawny'; then
echo "audit rules left behind after a clean stop" >&2
exit 1
fi
else
echo "::warning::audit netlink was not available to the sensor (source=$source); proving the auditd log path instead"
# /home/runner is not traversable by tawny-ci, so use a shared dir.
# The runner appends and tawny-ci only reads.
logdir="$(mktemp -d /tmp/tawny-audit.XXXXXX)"
chmod 0755 "$logdir"
log="$logdir/audit.log"
: > "$log"
chmod 0644 "$log"
sudo setpriv --reuid=tawny-ci --regid=tawny-ci --init-groups \
--inh-caps=-all --ambient-caps=-all --bounding-set=-all \
"$bin" --linux-sensor-dump --seconds 4 --source audit_log --audit-log "$log" > "$out" 2>&1 &
dump=$!
sleep 1
python3 - "$log" <<'PY'
import sys
with open(sys.argv[1], "a") as f:
for i in range(50):
s = 7000 + i
t = f"1731200200.{i:03d}"
f.write(f'type=SYSCALL msg=audit({t}:{s}): arch=c000003e syscall=59 success=yes exit=0 ppid=1 pid={20000+i} uid=0 comm="true" exe="/usr/bin/true"\n')
f.write(f'type=EXECVE msg=audit({t}:{s}): argc=1 a0="/bin/true"\n')
PY
wait "$dump"
tail -n 1 "$out"
n="$(grep -c $'^EVENT\tprocess_start\t' "$out" || true)"
test "$n" -ge 50
fi
# With no capabilities the sensor falls back and says degraded.
nocaps="$RUNNER_TEMP/sensor-nocaps.out"
sudo setpriv --reuid=tawny-ci --regid=tawny-ci --init-groups \
--inh-caps=-all --ambient-caps=-all --bounding-set=-all \
"$bin" --linux-sensor-dump --seconds 1 --audit-log /nonexistent --tetragon-export /nonexistent > "$nocaps" 2>&1
cat "$nocaps"
grep -q $'^READY\tproc_poller\tdegraded' "$nocaps"
grep -q '"status":"degraded"' "$nocaps"
- name: package CI artifact
run: |
set -euo pipefail
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ coverage/
# Zig 0.17 unpacks fetched packages into the project
zig-pkg/
__pycache__/
# Test fixtures named *.log are inputs, not logs.
!agent/src/platform/linux/testdata/*.log
99 changes: 59 additions & 40 deletions agent/install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -565,46 +565,14 @@ if [[ "$dry_run" -ne 1 ]]; then
chmod 0755 "$binary_path"
fi

if [[ "$os_family" == "macos" ]]; then
if [[ "$dry_run" -eq 1 ]]; then
plist_preview="$(mktemp "${TMPDIR:-/tmp}/tawny-agent.plist.XXXXXX")"
write_launchd_plist "$plist_preview"
if command -v plutil >/dev/null 2>&1; then
plutil -lint "$plist_preview" >/dev/null
fi
if [[ "$enable_macos_dns_logging" -eq 1 && "$service_scope" == "system" ]]; then
enable_mdns_private_logging
fi
printf '[dry-run] write and bootstrap %s launchd job %s (log %s):\n' \
"$service_scope" "$plist_path" "$log_dir/agent.log"
sed 's/^/[dry-run] /' "$plist_preview"
rm -f "$plist_preview"
else
prepare_log_file
mkdir -p "$(dirname "$plist_path")"
plist_candidate="$(mktemp "$(dirname "$plist_path")/.tawny-agent.plist.XXXXXX")"
write_launchd_plist "$plist_candidate"
plutil -lint "$plist_candidate" >/dev/null
if [[ "$service_scope" == "system" ]]; then
chown root:wheel "$plist_candidate"
fi
chmod 0644 "$plist_candidate"
mv -f "$plist_candidate" "$plist_path"
if [[ "$enable_macos_dns_logging" -eq 1 && "$service_scope" == "system" ]]; then
enable_mdns_private_logging
fi
launchctl bootout "$launchd_domain/$launchd_label" >/dev/null 2>&1 || true
launchctl bootstrap "$launchd_domain" "$plist_path"
launchctl kickstart -k "$launchd_domain/$launchd_label"
launchctl print "$launchd_domain/$launchd_label" >/dev/null
fi
else
service_path="/etc/systemd/system/tawny-agent.service"
if [[ "$dry_run" -eq 1 ]]; then
printf '[dry-run] write and enable hardened systemd service %s\n' "$service_path"
else
service_candidate="$(mktemp --suffix=.service "$(dirname "$service_path")/tawny-agent.XXXXXX")"
cat > "$service_candidate" <<EOF
# Capabilities the Linux unit grants the tawny user, and nothing else:
# audit netlink (CAP_AUDIT_READ, CAP_AUDIT_CONTROL), other users' /proc
# entries (CAP_SYS_PTRACE, CAP_DAC_READ_SEARCH) and kill_process (CAP_KILL).
linux_capabilities="CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_DAC_READ_SEARCH CAP_KILL CAP_SYS_PTRACE"

write_systemd_unit() {
local dest="$1"
cat > "$dest" <<EOF
[Unit]
Description=Tawny endpoint agent
Documentation=https://github.com/jusso-dev/Tawny
Expand All @@ -623,6 +591,11 @@ ExecStart="$binary_path"
Restart=on-failure
RestartSec=5
UMask=0027
# Kernel telemetry and response (#59, docs/production-agent-hardening.md).
# Only these capabilities, as ambient capabilities for User=tawny; the
# bounding set holds nothing else, so NoNewPrivileges stays meaningful.
AmbientCapabilities=$linux_capabilities
CapabilityBoundingSet=$linux_capabilities
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
Expand All @@ -645,6 +618,52 @@ SystemCallArchitectures=native
[Install]
WantedBy=multi-user.target
EOF
}

if [[ "$os_family" == "macos" ]]; then
if [[ "$dry_run" -eq 1 ]]; then
plist_preview="$(mktemp "${TMPDIR:-/tmp}/tawny-agent.plist.XXXXXX")"
write_launchd_plist "$plist_preview"
if command -v plutil >/dev/null 2>&1; then
plutil -lint "$plist_preview" >/dev/null
fi
if [[ "$enable_macos_dns_logging" -eq 1 && "$service_scope" == "system" ]]; then
enable_mdns_private_logging
fi
printf '[dry-run] write and bootstrap %s launchd job %s (log %s):\n' \
"$service_scope" "$plist_path" "$log_dir/agent.log"
sed 's/^/[dry-run] /' "$plist_preview"
rm -f "$plist_preview"
else
prepare_log_file
mkdir -p "$(dirname "$plist_path")"
plist_candidate="$(mktemp "$(dirname "$plist_path")/.tawny-agent.plist.XXXXXX")"
write_launchd_plist "$plist_candidate"
plutil -lint "$plist_candidate" >/dev/null
if [[ "$service_scope" == "system" ]]; then
chown root:wheel "$plist_candidate"
fi
chmod 0644 "$plist_candidate"
mv -f "$plist_candidate" "$plist_path"
if [[ "$enable_macos_dns_logging" -eq 1 && "$service_scope" == "system" ]]; then
enable_mdns_private_logging
fi
launchctl bootout "$launchd_domain/$launchd_label" >/dev/null 2>&1 || true
launchctl bootstrap "$launchd_domain" "$plist_path"
launchctl kickstart -k "$launchd_domain/$launchd_label"
launchctl print "$launchd_domain/$launchd_label" >/dev/null
fi
else
service_path="/etc/systemd/system/tawny-agent.service"
if [[ "$dry_run" -eq 1 ]]; then
printf '[dry-run] write and enable hardened systemd service %s:\n' "$service_path"
unit_preview="$(mktemp "${TMPDIR:-/tmp}/tawny-agent.service.XXXXXX")"
write_systemd_unit "$unit_preview"
sed 's/^/[dry-run] /' "$unit_preview"
rm -f "$unit_preview"
else
service_candidate="$(mktemp --suffix=.service "$(dirname "$service_path")/tawny-agent.XXXXXX")"
write_systemd_unit "$service_candidate"
systemd-analyze verify "$service_candidate"
chown root:root "$service_candidate"
chmod 0644 "$service_candidate"
Expand Down
64 changes: 64 additions & 0 deletions agent/install/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,68 @@ if [[ "$(uname -s)" == "Darwin" ]]; then
done
fi

if [[ "$(uname -s)" == "Linux" ]]; then
# The dry run prints the systemd unit. It must keep User=tawny and every
# hardening line, and grant exactly the documented capabilities (#59).
unit_out="$test_dir/tawny-agent.service"
bash "$installer" \
--install-dir "$install_dir" \
--config-path "$config_path" \
--state-dir "$state_dir" \
--binary-path "$local_binary" \
--sha256 "$sha256" \
--skip-attestation \
--dry-run | sed -n 's/^\[dry-run\] //p' > "$unit_out"
expected_caps="CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_DAC_READ_SEARCH CAP_KILL CAP_SYS_PTRACE"
for key in AmbientCapabilities CapabilityBoundingSet; do
lines="$(grep -c "^${key}=" "$unit_out" || true)"
if [[ "$lines" != "1" ]]; then
echo "unit has $lines ${key}= lines, want 1" >&2
exit 1
fi
got="$(sed -n "s/^${key}=//p" "$unit_out" | tr ' ' '\n' | sed '/^$/d' | sort | tr '\n' ' ' | sed 's/ $//')"
if [[ "$got" != "$expected_caps" ]]; then
echo "unit ${key} is '$got', want '$expected_caps'" >&2
exit 1
fi
done
for expected in \
'User=tawny' \
'Group=tawny' \
'UMask=0027' \
'NoNewPrivileges=true' \
'PrivateDevices=true' \
'PrivateTmp=true' \
'ProtectClock=true' \
'ProtectControlGroups=true' \
'ProtectHome=read-only' \
'ProtectKernelLogs=true' \
'ProtectKernelModules=true' \
'ProtectKernelTunables=true' \
'ProtectSystem=strict' \
"ReadWritePaths=\"$state_dir\"" \
'LockPersonality=true' \
'MemoryDenyWriteExecute=true' \
'RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK' \
'RestrictNamespaces=true' \
'RestrictRealtime=true' \
'RestrictSUIDSGID=true' \
'SystemCallArchitectures=native'; do
if ! grep -Fx -- "$expected" "$unit_out" >/dev/null; then
echo "unit missing: $expected" >&2
exit 1
fi
done
if grep -E '^(User|Group)=root|^(PermissionsStartOnly|Privileged)' "$unit_out" >/dev/null; then
echo "unit runs with root privileges" >&2
exit 1
fi
if command -v systemd-analyze >/dev/null 2>&1; then
systemd-analyze verify "$unit_out" 2>&1 | grep -iE 'capabilit|unknown (key|lvalue)' && {
echo "systemd-analyze rejected the capability lines" >&2
exit 1
}
fi
fi

printf 'installer smoke tests passed\n'
Loading
Loading