Repository navigation
Description
trivy fs and trivy rootfs fail and write no report when a file is removed while Trivy walks the filesystem. On busy machines this happens often, because build output, package manager caches and temp files come and go during the scan. Scanning the host from a container fails the same way on /proc entries of processes that exit during the scan.
Steps to reproduce
dir="$(mktemp -d)"
mkdir "$dir/scan" "$dir/cache"
for i in $(seq 1 2000); do : > "$dir/scan/keep-$i.tmp"; done
mkdir "$dir/scan/churn"
(cd "$dir/scan/churn" && while :; do touch $(seq -f 'c%g.tmp' 1 2000); rm -f -- c*.tmp; done) &
churn=$!
for run in 1 2 3 4 5 6 7 8; do
trivy fs --format cyclonedx --cache-dir "$dir/cache" --output /dev/null "$dir/scan" || break
done
kill "$churn"
The loop stops at the first failed run with this error.
walk dir error: unknown error with <dir>/scan/churn/c151.tmp: file info error: lstat <dir>/scan/churn/c151.tmp: no such file or directory
The expected result is that Trivy skips the missing file and scans the rest of the tree.
A file can disappear at three points, and each of them fails the scan.
- The walker calls
lstatthroughd.Info()inpkg/fanal/walker/fs.go, andonErrorstops the walk on any error except permission errors. The same happens when a subdirectory disappears before it is read. AnalyzeFileinpkg/fanal/analyzer/analyzer.goopens the file for each analyzer and skips only permission errors.- Post-analyzers read the files through
mapfsafter the whole analysis step.ReadDirinpkg/mapfs/file.gocallsos.Statfor every file in the directory, so one missing file fails the whole directory.fsutils.WalkDiralso stops whenOpenfails. The post-analyzer returns the error, andPostAnalyzefails the scan.
Skipping every "not exist" error is not enough. Trivy has no separate check that the scan target exists, and today trivy fs /nonexistent fails only because of the same walk error. The fix must skip missing files inside the tree but still fail when the target itself does not exist.
Activity