Skip to content

Commit 5f2bd28

Browse files
authored
Merge pull request #622 from onekey-sec/621-add-timeout
Add execution timeout to Command Extractor
2 parents ae8e1ea + 2c9e9ac commit 5f2bd28

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

unblob/extractors/command.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
from structlog import get_logger
77

88
from unblob.models import DirectoryExtractor, ExtractError, Extractor
9-
from unblob.report import ExtractCommandFailedReport, ExtractorDependencyNotFoundReport
9+
from unblob.report import (
10+
ExtractCommandFailedReport,
11+
ExtractorDependencyNotFoundReport,
12+
ExtractorTimedOut,
13+
)
1014

1115
if TYPE_CHECKING:
1216
import io
1317

18+
# value that is high enough not to block long running execution such as extraction of large
19+
# disk images, but small enough to make sure unblob finish its execution at some point.
20+
COMMAND_TIMEOUT = 12 * 60 * 60
21+
1422
logger = get_logger()
1523

1624

@@ -45,6 +53,7 @@ def no_op():
4553
cmd,
4654
stdout=stdout_file,
4755
stderr=subprocess.PIPE,
56+
timeout=COMMAND_TIMEOUT,
4857
)
4958
if res.returncode != 0:
5059
error_report = ExtractCommandFailedReport(
@@ -65,6 +74,13 @@ def no_op():
6574
**error_report.asdict(),
6675
)
6776
raise ExtractError(error_report) from None
77+
except subprocess.TimeoutExpired as e:
78+
error_report = ExtractorTimedOut(cmd=e.cmd, timeout=e.timeout)
79+
logger.error(
80+
"Extract command timed out.",
81+
**error_report.asdict(),
82+
)
83+
raise ExtractError(error_report) from None
6884
finally:
6985
cleanup()
7086

unblob/report.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ class ExtractorDependencyNotFoundReport(ErrorReport):
103103
dependencies: List[str]
104104

105105

106+
@attr.define(kw_only=True, frozen=True)
107+
class ExtractorTimedOut(ErrorReport):
108+
"""Describes an error when the extractor execution timed out."""
109+
110+
severity: Severity = Severity.ERROR
111+
cmd: str
112+
timeout: float
113+
114+
106115
@attr.define(kw_only=True, frozen=True)
107116
class MaliciousSymlinkRemoved(ErrorReport):
108117
"""Describes an error when malicious symlinks have been removed from disk."""

0 commit comments

Comments
 (0)