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

Add execution timeout to Command Extractor #622

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion unblob/extractors/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
from structlog import get_logger

from unblob.models import DirectoryExtractor, ExtractError, Extractor
from unblob.report import ExtractCommandFailedReport, ExtractorDependencyNotFoundReport
from unblob.report import (
ExtractCommandFailedReport,
ExtractorDependencyNotFoundReport,
ExtractorTimedOut,
)

if TYPE_CHECKING:
import io

# value that is high enough not to block long running execution such as extraction of large
# disk images, but small enough to make sure unblob finish its execution at some point.
COMMAND_TIMEOUT = 12 * 60 * 60

logger = get_logger()


Expand Down Expand Up @@ -45,6 +53,7 @@ def no_op():
cmd,
stdout=stdout_file,
stderr=subprocess.PIPE,
timeout=COMMAND_TIMEOUT,
)
if res.returncode != 0:
error_report = ExtractCommandFailedReport(
Expand All @@ -65,6 +74,13 @@ def no_op():
**error_report.asdict(),
)
raise ExtractError(error_report) from None
except subprocess.TimeoutExpired as e:
error_report = ExtractorTimedOut(cmd=e.cmd, timeout=e.timeout)
logger.error(
"Extract command timed out.",
**error_report.asdict(),
)
raise ExtractError(error_report) from None
finally:
cleanup()

Expand Down
9 changes: 9 additions & 0 deletions unblob/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class ExtractorDependencyNotFoundReport(ErrorReport):
dependencies: List[str]


@attr.define(kw_only=True, frozen=True)
class ExtractorTimedOut(ErrorReport):
"""Describes an error when the extractor execution timed out."""

severity: Severity = Severity.ERROR
cmd: str
timeout: float


@attr.define(kw_only=True, frozen=True)
class MaliciousSymlinkRemoved(ErrorReport):
"""Describes an error when malicious symlinks have been removed from disk."""
Expand Down