fix: fail task and release machine on unexpected guest errors - #47
Open
itamarga wants to merge 1 commit into
Open
fix: fail task and release machine on unexpected guest errors#47itamarga wants to merge 1 commit into
itamarga wants to merge 1 commit into
Conversation
When the guest agent returns an error during analysis (e.g. an HTTP 500 while uploading the sample to /store), the resulting exception escaped CAPE's error handling and orphaned the task: - machine_running() only stopped/released the machine on the no-error path and in the dead-machine branch, so any other exception left the VM running and the machine locked forever. - launch_analysis() only finalized the task status for CuckooDeadMachine (-> pending) or a clean completion (-> completed), so any other exception left the task stuck in "running" indefinitely. The analysis timeout never fired because it only applies once the guest wait has begun. Fixes: - machine_running(): always stop/release the machine via a finally block (extracted into _stop_and_release_machine()), skipping only the dead-machine case which already removes the machine. - launch_analysis(): mark the task failed_analysis and unlock the machine on any unexpected exception. Add regression tests for both paths.
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.
Problem
When the guest agent returns an error during analysis — e.g. an HTTP 500 from
/storewhile the host uploads the sample — the resultingrequests.HTTPErrorescapes CAPE's error handling and orphans the task: the VM is left running, the machine stays locked, and the task sits inrunningforever. The analysis timeout never fires because it only applies oncewait_for_completion()has begun, which this never reaches.We hit this in production: three submissions whose sample filename collided with a pre-existing path in the guest's
%TEMP%each 500'd on upload and left all three machines locked for 3 days until manually cleaned up.Root cause (
lib/cuckoo/core/analysis_manager.py)machine_running()— thestop_machine()+release()cleanup sat after thetry/except, so it only ran on the no-error path. Theexceptonly catches(CuckooMachineError, CuckooGuestCriticalTimeout); any other exception throughyieldskipped cleanup entirely → VM left running + machine locked.launch_analysis()— only finalized task status forCuckooDeadMachine(→pending) or clean completion (→completed). Any other exception propagated out with the task leftrunning.Fix
machine_running(): always stop/release the machine via afinallyblock (extracted into_stop_and_release_machine()), skipping only the dead-machine case which already removes the machine.launch_analysis(): on any unexpected exception, mark the taskfailed_analysisand unlock the machine.Tests
Added regression tests in
tests/test_analysis_manager.py:test_machine_running_releases_machine_on_unexpected_errortest_launch_analysis_marks_failed_on_unexpected_errortests/test_analysis_manager.py: 19 passed, 5 skipped (pre-existing data-file skips).