Implement Drop for Directory in tsk. - #244
Open
grrrrrrrrr wants to merge 2 commits into
Open
Conversation
`Directory` wraps a raw `TSK_FS_DIR` pointer obtained from `tsk_fs_dir_open` or `tsk_fs_dir_open_meta`. In SleuthKit, directory structures are dynamically allocated and must be freed with `tsk_fs_dir_close`. Because `Directory` lacked a `Drop` implementation, every opened directory leaked memory and underlying file handles. This change implements `Drop for Directory<'_>` calling `tsk_fs_dir_close` and updates `iter_entries` and `DirectoryIterator` to borrow `&self` with a decoupled iterator lifetime, allowing `Directory` instances to be cleanly dropped once iteration completes.
grrrrrrrrr
force-pushed
the
fix-tsk-directory-leak
branch
from
August 28, 2026 13:41
899fdc0 to
03b9e31
Compare
The File returned by Directory::file wraps TSK_FS_DIR::fs_file, which
tsk_fs_dir_close frees when the Directory is dropped. Returning it with
the file system lifetime 'a let it outlive the Directory, so with the
new Drop impl a pattern like
let f = { let d = fs.open_dir(p)?; d.file() };
compiled into a use-after-free. Tie the returned File to the borrow of
self instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Directorywraps a rawTSK_FS_DIRpointer obtained fromtsk_fs_dir_openortsk_fs_dir_open_meta. In SleuthKit, directorystructures are dynamically allocated and must be freed with
tsk_fs_dir_close. BecauseDirectorylacked aDropimplementation,every opened directory leaked memory and underlying file handles.
This change implements
Drop for Directory<'_>callingtsk_fs_dir_closeand relaxes
iter_entries/filefrom&mut selfto&self(theunderlying
tsk_fs_dir_gettakesconst TSK_FS_DIR *and returns freshlyallocated files, so shared iteration is sound).
tsk_fs_dir_closealso freesTSK_FS_DIR::fs_file, which is whatDirectory::filehands back. Returning thatFilewith the file systemlifetime
'alet it outlive theDirectory, so onceDirectoryhas aDropa pattern likebecomes a use-after-free.
Directory::filenow borrowsself, tying thereturned
Fileto theDirectoryit came from.Note:
Fileitself still has noDrop, so files fromopen_fileanditer_entriescontinue to leakTSK_FS_FILE. Fixing that needs anowned/borrowed distinction (the
walk_dircallback andDirectory::filemust not close their
File) and is left for a follow-up.