Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LayerFilesystem iterdir and scandir #973

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JSCU-CNI
Copy link
Contributor

This PR prevents NotADirectoryError DirectoryFilesystemEntry exceptions from crashing plugins when iterating LayerFilesystem entries using recurse, iterdir or scandir.

@Horofic Horofic self-requested a review December 11, 2024 08:44
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 77.69%. Comparing base (334e6a4) to head (14758e6).

Files with missing lines Patch % Lines
dissect/target/plugins/filesystem/walkfs.py 33.33% 4 Missing ⚠️
dissect/target/filesystem.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #973      +/-   ##
==========================================
- Coverage   77.71%   77.69%   -0.02%     
==========================================
  Files         326      326              
  Lines       28559    28569      +10     
==========================================
+ Hits        22194    22198       +4     
- Misses       6365     6371       +6     
Flag Coverage Δ
unittests 77.69% <40.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

dissect/target/plugins/filesystem/walkfs.py Outdated Show resolved Hide resolved
@@ -1535,6 +1535,9 @@ def iterdir(self) -> Iterator[str]:
yielded = {".", ".."}
selfentry = self._resolve()
for fsentry in selfentry.entries:
if not fsentry.is_dir():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add unit tests?

@JSCU-CNI
Copy link
Contributor Author

Upon further debugging we discovered the root cause for these NotADirectoryError exceptions.

The Overlay2Filesystem uses the DirectoryFilesystem to map layer folders inside a target to a filesystem. The DirectoryFilesystem does not seem well suited for mapping TargetPath folders as it seems to originally have been created for mapping folders originating from the host system. This is where the problem starts.

When a TargetPath layer inside Overlay2Filesystem is mapped, at some point when iterating over the filesystem, DirectoryFilesystemEntry resolves an entry to a file entry on the host system using os.*. For privileged paths this will cause a PermissionDenied exception, which propagates to a FilesystemError and causes is_dir to be false.

This is undesired behaviour to say the least, so we replaced DirectoryFilesystem with a VirtualFilesystem and created a map_vdir method for VirtualFilesystem. Perhaps a proper VirtualDirectoryFilesystem implementation should be created at some point, but this seems like a valid solution for the meantime.

@Schamper
Copy link
Member

@JSCU-CNI can you still add a unit test that showcases the originally faulty behavior so that I can try some other ways to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants