T09 · Insecure Skill Coding Practices
- Location
scripts/safe-clean.sh:27- Finding
Recursive Temporary-Directory Deletion Can Remove Recently Modified Nested Content
- Content
View full analysis
Vulnerability Details
File Location:
scripts/safe-clean.sh, lines 27–28
Vulnerability Type: Unsafe recursive deletion based on parent modification time
Risk Level: Mediumbash find /tmp -xdev -mindepth 1 -mtime +3 -print -exec rm -rf {} + 2>/dev/null || true find /var/tmp -xdev -mindepth 1 -mtime +7 -print -exec rm -rf {} + 2>/dev/null || trueTechnical Analysis
Each
findcommand selects an entry according to that entry's modification time and passes the selected path torm -rf. If the selected entry is a directory, the deletion is recursive and is not restricted by the ages of files contained inside it.A top-level temporary directory can have an old modification timestamp while containing recently modified files. Updating the contents of an existing nested file does not necessarily update the modification time of every ancestor directory. Consequently, a directory that satisfies
-mtime +3or-mtime +7may contain active, recent data that does not independently satisfy the retention threshold.The use of
-xdevprevents crossing filesystem boundaries but does not address this recursive age-checking problem. Suppressing errors with2>/dev/null || truecan also conceal partial deletion or operational failures.Attack Path
- An application creates a directory under
/tmpor/var/tmp. - The directory becomes older than the configured threshold.
- The application continues modifying an existing nested file without changing the top-level directory's modification time.
- A user invokes the documented safe-cleanup workflow.
findselects the old top-level directory.rm -rfrecursively removes the directory, including its recently modified nested content.
Impact Assessment
The script is designed to run with root-level access and can therefore delete temporary content belonging to any local user or service. Potentially affected content includes active applicati ...[truncated 348 chars]
- An application creates a directory under
- Remediation
View remediation
Remediation Suggestions
- Select stale files individually rather than recursively deleting a directory solely because the directory itself is old.
- Delete directories only after their eligible contents have been removed and the directories are confirmed empty.
- Exclude sockets, lock files, mount points, and known service runtime directories.
- Use a dry-run mode that prints candidates and reclaimed-size estimates before deletion.
- Require explicit confirmation before recursively deleting non-empty directories.
- Consider a safer pattern such as:
bash find /tmp -xdev -type f -mtime +3 -print find /tmp -xdev -type f -mtime +3 -delete find /tmp -xdev -depth -type d -empty -mtime +3 -deleteApply equivalent handling to
/var/tmpwith its intended retention period. Service-aware cleanup facilities such assystemd-tmpfilesshould be preferred where available.
