# detect.sh — READ ONLY. For every profile file in /etc/apparmor.d: does the profile the KERNEL has loaded equal a # fresh compile of the file on disk? (the n100-clock-2026-11-11 method, per file AND per loaded profile) # fresh = sha256 of `apparmor_parser -S -K ` (compile to stdout: no load, cache not read, not written) # loaded = policy/profiles/*/raw_sha256 of each profile name the file defines (`apparmor_parser -N`) # cache = sha256 of the cached binary in the running kernel's cache folder main() { echo "#### $(hostname) $(date -u +%FT%TZ) kernel $(uname -r)" P=/sys/kernel/security/apparmor/policy/profiles cur=""; for d in /var/cache/apparmor/*/; do [ "$(sha256sum < $d/.features | cut -d' ' -f1)" = "$(aa-features-abi -x 2>/dev/null | sha256sum | cut -d' ' -f1)" ] && cur=$d; done echo "cache folder of the running kernel: ${cur:-none} all: $(ls -d /var/cache/apparmor/*/ | tr '\n' ' ')" declare -A L; for d in $P/*/; do n=$(cat $d/name 2>/dev/null); [ -n "$n" ] && L["$n"]=$(cat $d/raw_sha256 2>/dev/null); done echo "loaded profiles: ${#L[@]}" n=0; stale=0; cdiff=0; notloaded=0; nd=0; t0=$(date +%s.%N) for f in /etc/apparmor.d/*; do [ -f "$f" ] || continue; b=$(basename "$f"); [ -e /etc/apparmor.d/disable/$b ] && continue fresh=$(apparmor_parser -S -K "$f" 2>/dev/null | sha256sum | cut -d' ' -f1); n=$((n+1)) again=$(apparmor_parser -S -K "$f" 2>/dev/null | sha256sum | cut -d' ' -f1) # CONTROL ON THE METHOD (found 2026-11-14): a file whose two fresh compiles differ cannot be judged by a hash at all [ "$fresh" = "$again" ] || { nd=$((nd+1)); echo "NOT-DETERMINISTIC (two fresh compiles differ — not a staleness signal): $b ${fresh:0:12} ${again:0:12}"; continue; } c="-"; [ -n "$cur" ] && [ -f "$cur$b" ] && c=$(sha256sum < "$cur$b" | cut -d' ' -f1) [ "$c" != "-" ] && [ "$c" != "$fresh" ] && { cdiff=$((cdiff+1)); echo "CACHE-DIFFERS: $b cache=${c:0:12} fresh=${fresh:0:12} cache-mtime=$(stat -c %y "$cur$b" | cut -c1-19) source-mtime=$(stat -c %y "$f" | cut -c1-19)"; } for name in $(apparmor_parser -N "$f" 2>/dev/null); do l=${L["$name"]:-} if [ -z "$l" ]; then notloaded=$((notloaded+1)); continue; fi [ "$l" = "$fresh" ] || { stale=$((stale+1)); echo "LOADED-DIFFERS: file=$b profile=$name loaded=${l:0:12} fresh=${fresh:0:12}"; } done done echo "files=$n cache-differs=$cdiff loaded-differs=$stale profiles-not-loaded=$notloaded not-deterministic=$nd seconds=$(echo "$(date +%s.%N) - $t0" | bc)" } main