Skip to content

ZIP: Fix infinite loop + significant code cleanup - #1548

Merged
val-ms merged 2 commits into
Cisco-Talos:mainfrom
val-ms:CLAM-2827-zip-cleanup
Aug 13, 2025
Merged

val-ms merged 2 commits into
Cisco-Talos:mainfrom
val-ms:CLAM-2827-zip-cleanup

Conversation

@val-ms

@val-ms val-ms commented Aug 6, 2025

Copy link
Copy Markdown
Contributor

An infinite loop may occur when scanning some malformed ZIP files.

I introduced this issue in 96c00b6 with this line:

// decrement coff by 1 to account for the increment at the end of the loop
coff -= 1;

The problem is that the function may return 0, which should indicate that there are no more files. The result was that coff would stay the same and the loop would repeat.

This issue is in 1.5 development and affects the 1.5.0 beta but does not affect any production versions.

Fixes: #1534

Special thanks to Sophie0x2E for an initial fix, proposed in #1539
In review, I was uncomfortable with other existing code and decided to to a more significant overhaul of the error handling in the ZIP module.

In addition to cleanup, this commit has some functional changes:

  • When parsing a central directory file header inside of parse_central_directory_file_header(), it will now fail out if the "extra length" or "comment length" fields would exceced the length of the archive. That doesn't mean the associated local file header won't be parsed later, but it won't use the central directory file header to find it. Instead, the ZIP module will have to find the local file header by searching for extra records not listed in the central directory.

    This change was mostly to tidy up complex error handling.

  • Add two FTM new signatures to identify split ZIP archives.

    This signature identifies the first segment (first file) in a split or spanned ZIP archive. It may also be found on a single-segment "split" archive, depending on the ZIP archiver. 0:0:504b0708504b0304:ZIP (First segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP

    Practically speaking, this new signature makes it so ClamAV identifies the file as a ZIP right away without having to rely on SFX_ZIP detection. Extraction is then handled by the ZIP cli_unzip function rather than extracting each with cli_unzip_single which handles SFX_ZIP entries.

    Note: ClamAV isn't capable of finding additional files on disk to support handling the additional segments. So it doesn't make any difference with handling those other files.

    This signature is for single-segment split/spanned archives, depending on the ZIP archiver. 0:0:504b0303504b0304:ZIP (Single-segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP Like the first one, this also means we won't rely on SFX_ZIP detection and will treat this files as regular ZIPs.

  • Added a test file to verify that ClamAV can extract a single-file "split" ZIP.

  • Added a clamscan test with test files to verify that scanning a split archive across two segments correctly extracts the properly formed zip file entries. Sadly, we can't join the segments to extract everything.

@val-ms

val-ms commented Aug 6, 2025 •

Copy link
Copy Markdown
Contributor Author

The windows ctest in Jenkins are failing with:

E           AssertionError: Expected item `"FileName":"logo.2.png",` not found in output:

The test debug output contains this instead:
"FileName":"logos/logo.2.png",

It seems like the "cli_basename" function is not accounting for Windows forward-slash. I will have to investigate.

jhumlick
jhumlick previously approved these changes Aug 8, 2025
Comment thread libclamav/filetypes_int.h Resolved
val-ms added 2 commits August 11, 2025 18:14
An infinite loop may occur when scanning some malformed ZIP files.

I introduced this issue in 96c00b6
with this line:

```c
// decrement coff by 1 to account for the increment at the end of the loop
coff -= 1;
```

The problem is that the function may return 0, which should
indicate that there are no more files. The result was that
`coff` would stay the same and the loop would repeat.

This issue is in 1.5 development and affects the 1.5.0 beta but
does not affect any production versions.

Fixes: Cisco-Talos#1534

Special thanks to Sophie0x2E for an initial fix, proposed in
Cisco-Talos#1539
In review, I was uncomfortable with other existing code and
decided to to a more significant overhaul of the error handling
in the ZIP module.

In addition to cleanup, this commit has some functional changes:

- When parsing a central directory file header inside of
  `parse_central_directory_file_header()`, it will now fail out if the
  "extra length" or "comment length" fields would exceced the length of
  the archive. That doesn't mean the associated local file header won't
  be parsed later, but it won't use the central directory file header
  to find it. Instead, the ZIP module will have to find the local file
  header by searching for extra records not listed in the central directory.

  This change was mostly to tidy up complex error handling.

- Add two FTM new signatures to identify split ZIP archives.

  This signature identifies the first segment (first file) in a split or
  spanned ZIP archive. It may also be found on a single-segment "split"
  archive, depending on the ZIP archiver.
  ```
  0:0:504b0708504b0304:ZIP (First segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP
  ```

  Practically speaking, this new signature makes it so ClamAV identifies
  the file as a ZIP right away without having to rely on SFX_ZIP detection.
  Extraction is then handled by the ZIP `cli_unzip` function rather than
  extracting each with `cli_unzip_single` which handles SFX_ZIP entries.

  Note: ClamAV isn't capable of finding additional files on disk to support
  handling the additional segments. So it doesn't make any difference with
  handling those other files.

  This signature is for single-segment split/spanned archives, depending
  on the ZIP archiver.
  ```
  0:0:504b0303504b0304:ZIP (Single-segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP
  ```
  Like the first one, this also means we won't rely on SFX_ZIP detection
  and will treat this files as regular ZIPs.

- Added a test file to verify that ClamAV can extract a single-file
  "split" ZIP.

- Added a clamscan test with test files to verify that scanning a split
  archive across two segments correctly extracts the properly formed zip
  file entries. Sadly, we can't join the segments to extract everything.
On Windows, the cli_basename function should treat both '/' and '\' as path
separators. Most Windows APIs also accept both.

On Linux/Unix, it makes sense when using a filepath that is more for
informational purposes or where it may have come from a Windows system,
to treat the '\' as a path separator.
But in situations where the the path is needed for some critical action,
like moving or deleting a file, we can't treat it as a path separator.
@val-ms
val-ms force-pushed the CLAM-2827-zip-cleanup branch from 84ec997 to 18854bf Compare August 11, 2025 22:14
@val-ms
val-ms requested a review from jhumlick August 11, 2025 22:15

@jhumlick jhumlick left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@val-ms
val-ms merged commit 6afc75f into Cisco-Talos:main Aug 13, 2025
@val-ms
val-ms deleted the CLAM-2827-zip-cleanup branch August 13, 2025 21:12
val-ms added a commit to val-ms/clamav that referenced this pull request Aug 13, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in Cisco-Talos#1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
val-ms added a commit to val-ms/clamav that referenced this pull request Aug 14, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in Cisco-Talos#1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
val-ms added a commit to val-ms/clamav that referenced this pull request Aug 14, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in Cisco-Talos#1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
val-ms added a commit to val-ms/clamav that referenced this pull request Aug 14, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in Cisco-Talos#1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
val-ms added a commit that referenced this pull request Aug 15, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in #1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
jhumlick pushed a commit to jhumlick/clamav that referenced this pull request Nov 4, 2025
I accidentally introduced a NULL-dereference bug when scanning any OOXML
file in Cisco-Talos#1548

I overlooked the test failure out of haste. 😔

The NULL-dereference happens because the `unzip_search()` feature
allowed searching some other file than the one that is currently being
scanned, which you would do by setting `ctx` to NULL and setting an
`fmap` parameter instead.
In practice, the current layer's `fmap` from the `ctx` was always passed in.

This fix makes it so the `unzip_search()` and related functions only
take the `ctx` parameter and do not have and `fmap` or `fsize` field
(Note: the `fsize` was never needed, because `fmap->len` take care of that).

CLAM-2837
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed zip files cause infinity loop in unzip module (1.5.0 beta)

2 participants