diff --git a/scripts/verify.sh b/scripts/verify.sh index 41e50a5..db7b189 100755 --- a/scripts/verify.sh +++ b/scripts/verify.sh @@ -150,10 +150,15 @@ verify_canary_check() { esac # ausearch reads stdin when it isn't a terminal, so --input-logs makes it read the audit logs. # ausearch -ts takes the date and the time as two arguments. It parses the date with the C - # locale's %x, which has a two-digit year. The event time is never before this second. - verify_date=$(date '+%m/%d/%y') - verify_time=$(date '+%H:%M:%S') - verify_start="$verify_date $verify_time" + # locale's %x, which has a two-digit year. One date call gives both, so they are from the same + # moment. + verify_start=$(date '+%m/%d/%y %H:%M:%S') + verify_date=${verify_start% *} + verify_time=${verify_start#* } + # The kernel stamps an event with a clock that runs a few ms behind the one date reads. So an + # event written right after the start time can carry the second before it, and ausearch -ts + # would skip it. Waiting one second puts the event after the start time. + [ "$verify_poll" -eq 0 ] || sleep 1 if ! printf 'canary %s\n' "$verify_start" >"$verify_canary" 2>/dev/null; then verify_report canary fail "cannot write $verify_canary" return diff --git a/tests/bats/verify.bats b/tests/bats/verify.bats index 32691f5..ee7ebe6 100644 --- a/tests/bats/verify.bats +++ b/tests/bats/verify.bats @@ -213,3 +213,14 @@ lost 0' [ "$status" -eq 0 ] [[ "$output" == *"check=failure_mode result=warn"* ]] } + +@test "verify waits a second after it takes the start time, before it writes the canary" { + write_state + # The kernel's event clock runs a few ms behind date, so an event written right after the + # start time can carry the second before it, and ausearch -ts would then skip it. + export AKIT_VERIFY_POLL=1 + akit_stub sleep '{ [ -e "$AKIT_ROOT/etc/auditd-kit/canary" ] && echo after || echo before; } >>"$AKIT_ROOT/sleeps"' + run sh "$verify" + [ "$status" -eq 0 ] + [ "$(sed -n 1p "$AKIT_ROOT/sleeps")" = before ] +}