Conversation
os.getcwd() was called eagerly as the default= value of the -out argument, which is evaluated when add_argument() is called, not when the argument is actually absent from the command line. If the process CWD has been deleted (e.g. during pytest runs inside a package manager sandbox), this raises FileNotFoundError before any test body runs. Move the os.getcwd() call to the assignment site (results_directory), where it is only reached after parse_args() succeeds and only when -out was not supplied by the caller.
The binwalk scan helper does os.chdir(tmpdirname) to work around binwalk's lack of an output-directory option. It saves and restores the CWD using os.getcwd() / os.chdir(saved_dir). Two problems with this approach: 1. os.getcwd() raises FileNotFoundError if a previous test left the process in a since-deleted temp directory. 2. binwalk uses multiprocessing internally; the spawned child process inherits the parent's CWD (tmpdirname). On some platforms/configs Python's multiprocessing.spawn serialises the CWD and tries to restore it in the child. If tmpdirname is deleted between the spawn and the child starting, spawn.py raises FileNotFoundError. Fix: open the current directory as an O_RDONLY fd before chdir'ing, then restore with os.fchdir(). An open fd keeps the inode alive even if the path is later removed, and fchdir never has to resolve a path. If the initial open fails (CWD already gone), fall back to self.results_directory which is always a valid path at this point.
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.
Problems
Two separate
FileNotFoundErrorcrash sites were found while packaging stegoveritas for Pentoo Linux:pentoo/pentoo-overlay#2923
1.
add_argumentdefault evaluated eagerly (line 179)Python evaluates
default=whenadd_argument()is called, not when-outis absent. If the process CWD has been deleted at that point,os.getcwd()raisesFileNotFoundErrorbefore any test body runs.Fix: use
default=Noneand resolve lazily at theresults_directoryassignment site.2.
os.getcwd()/os.chdir()in binwalk scan path (line 98)Two issues here:
os.getcwd()fails immediately.multiprocessinginternally. On some platforms Python'smultiprocessing.spawnserialises the parent CWD and tries to restore it in the child. Iftmpdirnameis removed between spawn and child startup,spawn.pyraisesFileNotFoundError.Fix: save the current directory as an
O_RDONLYfd withos.open('.')and restore withos.fchdir(). An open fd keeps the inode alive even if the path is later unlinked, andfchdirnever has to resolve a path. Fall back toself.results_directoryif the initialopenfails (CWD already gone).Note: a separate failure (
AttributeError: module 'capstone' has no attribute 'CS_ARCH_ARM64') is caused by a capstone 5.x API change inbinwalk/modules/disasm.pyand is outside the scope of this PR.