#!/bin/sh
# sharingan-scrub UID [--keep DIR]: end every process of UID and remove everything it left behind (files on every mount,
# SysV IPC), so nothing a bot wrote down reaches whatever runs next in the sandbox. The match worker runs it (as root) after
# each rotation of duplicate deals; the runner after a requirements install, keeping the installed packages (--keep, which
# the runner then seals root-owned and read-only). Exit 0 only when UID is gone without a trace (outside DIR).
set -u
u="${1:-}"
keep=""
[ "${2:-}" = "--keep" ] && keep="${3:-}"
case "$u" in '' | *[!0-9]*) echo "usage: sharingan-scrub UID [--keep DIR]" >&2; exit 2 ;; esac
case "$keep" in '' | /?*) ;; *) echo "sharingan-scrub: --keep needs an absolute directory" >&2; exit 2 ;; esac
[ "$u" -ge 1000 ] || { echo "sharingan-scrub: refusing uid $u" >&2; exit 2; }

# live (non-zombie) processes of $u; zombies hold nothing and are reaped by init
alive() {
  for s in /proc/[0-9]*/status; do
    awk -v u="$u" '/^State:/ { z = ($2 == "Z") } /^Uid:/ { for (i = 2; i <= 5; i++) if ($i == u && !z) f = 1 } END { exit !f }' "$s" 2>/dev/null && return 0
  done
  return 1
}
i=0
while alive; do
  # kill(-1) as $u reaches every process $u owns (and nothing else)
  setpriv --reuid="$u" --regid="$u" --clear-groups --inh-caps=-all --bounding-set=-all sh -c 'kill -9 -1' 2>/dev/null
  i=$((i + 1))
  [ "$i" -lt 200 ] || { echo "sharingan-scrub: processes of uid $u survive" >&2; exit 1; }
  sleep 0.05
done

# SysV shared memory, semaphores, message queues it created or owns: removed by id as root (an owner can re-mode an
# object so even it can't list it), then checked gone. Columns: shm/sem/msg id is field 2, uid/cuid are named in the header.
ipc_left() {
  for t in shm sem msg; do
    f=/proc/sysvipc/$t
    [ -r "$f" ] || continue
    awk -v u="$u" -v t="$t" 'NR == 1 { for (i = 1; i <= NF; i++) { if ($i == "uid") cu = i; if ($i == "cuid") cc = i } next }
                             ($cu == u || $cc == u) { print t, $2 }' "$f"
  done
}
ipc_left | while read -r t id; do
  case $t in shm) ipcrm -m "$id" ;; sem) ipcrm -s "$id" ;; msg) ipcrm -q "$id" ;; esac 2>/dev/null
done
if [ -n "$(ipc_left)" ]; then
  echo "sharingan-scrub: SysV IPC of uid $u survives" >&2
  exit 1
fi

# every file or directory it owns, on every real filesystem (tmpfs mounts included)
awk '$3 !~ /^(proc|sysfs|cgroup|cgroup2|devpts|securityfs|debugfs|tracefs|bpf|pstore|configfs|fusectl|binfmt_misc|autofs|hugetlbfs|overlay_skip)$/ { print $2 }' /proc/mounts |
  while read -r m; do
    if [ -n "$keep" ]; then
      find "$m" -xdev -path "$keep" -prune -o \( -user "$u" -o -group "$u" \) -print0 2>/dev/null | sort -rz | xargs -0 -r rm -rf 2>/dev/null
    else
      find "$m" -xdev \( -user "$u" -o -group "$u" \) -depth -exec rm -rf {} + 2>/dev/null
    fi
  done
if find / /tmp /var/tmp /dev/shm -xdev ${keep:+-path "$keep" -prune -o} \( -user "$u" -o -group "$u" \) -print -quit 2>/dev/null | grep -q .; then
  echo "sharingan-scrub: files of uid $u survive" >&2
  exit 1
fi
# directory times are the last thing it touched there: reset them
touch /tmp /var/tmp /dev/shm 2>/dev/null
exit 0
