-
Notifications
You must be signed in to change notification settings - Fork 17
fix(ggml): detect embedded ZIP polyglot payloads #1780
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
base: main
Are you sure you want to change the base?
Changes from all commits
a8ee962
e025c60
5a206b2
64e076a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -760,11 +760,26 @@ def _scan_gguf(self, f: BinaryIO, file_size: int, result: ScanResult) -> None: | |
| ) | ||
| result.bytes_scanned = max(result.bytes_scanned, f.tell()) | ||
|
|
||
| def _scan_zip_polyglot(self, result: ScanResult) -> bool: | ||
| """Inspect ZIP members even when GGUF metadata or tensor parsing fails.""" | ||
| def _scan_zip_polyglot(self, result: ScanResult, *, format_name: str = "GGUF") -> bool: | ||
| """Inspect ZIP members even when GGUF/GGML header parsing fails.""" | ||
| if not zipfile.is_zipfile(self.current_file_path): | ||
| return False | ||
|
|
||
| # `is_zipfile` only proves an end-of-central-directory signature is present: it returns True | ||
| # for any file whose trailing bytes happen to contain b"PK\x05\x06" followed by 18 bytes. | ||
| # Model tensor data hits that by chance, so a cleanly-readable archive carrying no members | ||
| # is not a polyglot - a hidden payload always has at least one entry. | ||
| # | ||
| # A directory that fails to open is NOT treated as benign here: it falls through to the | ||
| # preflight below so a corrupted or truncated archive still fails closed as incomplete. | ||
| try: | ||
| with zipfile.ZipFile(self.current_file_path) as embedded_archive: | ||
| embedded_members: list[str] | None = embedded_archive.namelist() | ||
| except (OSError, zipfile.BadZipFile): | ||
| embedded_members = None | ||
| if embedded_members is not None and not embedded_members: | ||
| return False | ||
|
Comment on lines
+780
to
+781
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a GGML polyglot contains a valid nonempty ZIP and an attacker appends a second empty EOCD record, Python's AGENTS.md reference: AGENTS.md:L115-L115 Useful? React with 👍 / 👎. |
||
|
|
||
| from .archive_dispatch import ( | ||
| _ZIP_CONTAINER_PREFLIGHT_REJECTED_PATHS_PRIVATE_METADATA_KEY, | ||
| merge_executable_zip_container_findings, | ||
|
|
@@ -776,7 +791,7 @@ def _scan_zip_polyglot(self, result: ScanResult) -> bool: | |
| self.current_file_path, | ||
| result, | ||
| archive_config, | ||
| context="GGUF trailing ZIP polyglot", | ||
| context=f"{format_name} trailing ZIP polyglot", | ||
| ) | ||
| rejected_paths = result._private_metadata.get( | ||
| _ZIP_CONTAINER_PREFLIGHT_REJECTED_PATHS_PRIVATE_METADATA_KEY, | ||
|
|
@@ -789,9 +804,9 @@ def _scan_zip_polyglot(self, result: ScanResult) -> bool: | |
| return False | ||
|
|
||
| result.add_check( | ||
| name="GGUF ZIP Polyglot Detection", | ||
| name=f"{format_name} ZIP Polyglot Detection", | ||
| passed=False, | ||
| message="GGUF file is also a valid ZIP archive and may contain hidden archive content", | ||
| message=f"{format_name} file is also a valid ZIP archive and may contain hidden archive content", | ||
| severity=IssueSeverity.CRITICAL, | ||
| location=self.current_file_path, | ||
| details={"embedded_format": "zip"}, | ||
|
|
@@ -1050,6 +1065,7 @@ def _scan_ggml( | |
| """Basic GGML file validation with security checks.""" | ||
| result.metadata["format"] = "ggml" | ||
| result.metadata["magic"] = magic.decode("ascii", "ignore") | ||
| self._scan_zip_polyglot(result, format_name="GGML") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a compressed ZIP member expands beyond the outer file size, this call merges the member scan's AGENTS.md reference: AGENTS.md:L137-L137 Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a ZIP member is itself a GGUF/GGML model, Useful? React with 👍 / 👎. |
||
|
|
||
| if file_size < 32: | ||
| result.add_check( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a GGUF/GGML polyglot whose central directory exceeds
max_zip_central_directory_sizeormax_zip_entries, constructingZipFileand callingnamelist()parses and materializes the attacker-controlled directory beforeZipScannercan enforce either configured bound. A sufficiently large directory can therefore consume excessive memory and CPU in the scanner despite the archive preflight limits; determine emptiness through the bounded preflight rather than opening the archive first.Useful? React with 👍 / 👎.