Commit Graph

10752 Commits

Author SHA1 Message Date
ragusaa
1c6746853f Fixed heap buffer overflow while loading signatures
There is a possible overflow read when loading PDB and WDB phishing
signatures.

This issue is not a vulnerability.

Changed const char pointers to uint8_t pointers when they are to be used
with data, as well as removing asserts and adding additional error
checking.

Thank you Michał Dardas for reporting this issue.

This fix also resolves:
- https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43845
- https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43812
- https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43866

This commit also fixes a minor leak of pattern matching trans nodes
that was observed when testing with the MPOOL module disabled.
2022-05-16 18:29:25 -07:00
Micah Snyder
3dff2e2f52 Bumped version from 0.105.0 -> 1.0.0-devel
FLEVEL 150 -> 160

Update the NEWS to have:
- a section for the new version.
- the latest details about all the previous patch versions.
2022-05-10 10:24:20 -07:00
Micah Snyder
382fe2b649 Update 0.105.0 release notes, remove '-rc2' version suffix 2022-05-01 20:28:28 -07:00
Micah Snyder
2397b7b01c libclamav: Fix benign uninitialized variable used
In the core `cli_magic_scan()` function, early termination of the scan
before hashing the file, such as a return from the prescan-callback,
could result in the hashed_size variable being uninitalized when calling
into `cache_add()`. That's okay because we now skip caching of the md5 hash
when it is NULL, hence "benign". This is just a minor code quality fix.
2022-05-01 12:24:19 -07:00
Mickey Sola
7df8437a87 Single commit to add clam mods to regex code
Changes include:
	* Change include of system regex headers to internal
	* Add cli prefix to regex functions
	* Change cli_regcomp to cli_regcomp_real to work with the
	  others_common.c regex interface
	* Optimize re_guts struct:
	  - Reordering fields allows the struct to fit within 16 bytes vs 20
	    bytes. This helps to fix a bug on legacy 64-bit systems where
	    there was a behaviour difference between 32 and 64 systems.
	  - see bb 474 for further details
	* Fix out of memory condition
	  - see bb 849 for further details
	  - reported by Gianluigi Tiesi <sherpya*netfarm.it>
	* Remove duplicate nomem check
	* Avoid passing out-of-range values to isalnum
	  - reported by Nigel
	* Avoid name collisions on AIX
	* Fix compiler warnings
	* Fix error path leak in regex/engine.c
	* Fix regex when sizeof(void*) != sizeof(long) for 64bit Windows
	  - see bb 2232 for further Details
	  - reported by Martin Olsen
	* Add error case safety checks and cleanups
	* Add patch for 'possible' heap overflow
	  - see bb11264 for further details
	  - patch submitted by the Debian team
	* Use clam internal allocation functions
	* Replace bounds check asserts with if checks (asserts are compiled
	  out of production builds)

Contributors to the above include:
	* Nigel Horne
	* aCaB
	* Török Edvin
	* David Raynor
	* Shawn Webb
	* Steven Morgan
	* Micah Snyder
	* Mickey Sola
2022-05-01 12:24:19 -07:00
Mickey Sola
87cdd70037 regex - Update internal regex to latest version
Updated using the openbsd github repo using the code in this directory:
https://github.com/openbsd/src/tree/master/lib/libc/regex

This build will not function without its child commit, which introduces
clam specific modifications. The two have been separated to make future
upgrades easier.
2022-05-01 12:24:19 -07:00
Micah Snyder
d1746ba0a5 Fix possible double-free in OLE2 document parser
If realloc() is called with size == 0, realloc() will free the pointer
and return NULL. Unless you check for this and set the pointer to NULL,
the pointer may later be free'd again after the `done:` label.

This commit fixes it by using the new CLI_REALLOC macro. CLI_REALLOC
uses `cli_realloc()` that both limits the amoutn of memory that may be
allocated and also will return NULL if you try to set size == 0, WITHOUT
free'ing the memory.

Resolves: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=44040
2022-05-01 12:24:19 -07:00
Micah Snyder
1bfb0522cf Minor type safety improvements for FMAP reads
The `fmap_readn()` function returns a size_t.
On error, it returns (size_t)-1. The SIS, SWF, and TNEF parsers were
storing the result as a signed int and then checking if < 0 for the
error case.

Added a CLI_ISCONTAINED_2_0_TO() macro, like the CLI_ISCONTAINED_0_TO()
macro for use when the big buffer offset == 0, to eliminate pointless
warnings.

Fix enum return type for functions in SWF parser.
Errors are enums, not ints! There's a difference.

Fix if-check in SWF parser where we relied on integer overflow for error
checking.
2022-05-01 12:24:19 -07:00
Micah Snyder
8011786315 PE parser error handling, type safety, warnings
In the PE parser when reading up to 4KB following the entrypoint
there's is no call to verify if the read failed. Later it is assumed
that the read succeeded and that the data in the buffer is valid.
I believe the correct response is to bail out if the read failed.

I also fixed some warnings:
- The the max # of PE sections was effectively disabled by setting it
to the max size of a uint16_t, so the max-check was pointless.
- Some undocumented switch fall-throughs were throwing warnings as well.
- Unsigned integer subtraction results in a signed value, which was
throwing warnings when compared with another unsigned value. The
substraction for `peinfo->nsections - 1` won't be less than 0 though
because we've already verified that nsections != 0 so we can just cast
the result of the subtraction back to an unsigned value to silence the
warning safely.
2022-05-01 12:24:19 -07:00
Micah Snyder
b75efe48f6 Fix possible infinite loop reading CHM archives
According to the mspack documentation:
  [The mspack read() callback function should] return the number of bytes
  successfully read (this can be less than the number requested), zero to mark
  the end of file, or less than zero to indicate an error.
  The library does not "retry" reads and assumes short reads are due to
  EOF, so you should avoid returning short reads because of transient errors.

Our implementation returns the number of bytes read, as required, but
fails to update the internal offset when reading from an fmap. This
means that if mspack does retry a partial read, it will return the same
bytes in perpetuity.

This commit updates the offset for partial reads.

It also appears as though our implementation would return more bytes
than read if a partial read occurs if using a file descriptor instead of
an fmap. I've corrected this to return the number of bytes read.

Thank you to Michał Dardas for reporting this issue.
2022-05-01 12:24:19 -07:00
Andy Ragusa
264fa910a0 Added error checking to TIFF parser
The TIFF parser is missing offset checks when iterating through the
directory entries and could end up in an infinite loop.

Added additional error checking to TIFF parser to check for offsets not
being before the current offset.

Thanks to Michał Dardas for reporting this.
2022-05-01 12:24:19 -07:00
Andy Ragusa
67f9267c32 Fixed memory leaks in javascript normalizer
Fixed leak where a malloced pointer was overwritten, and separate
leak where a returned textbuf struct was not cleaned up.
Thanks to Michał Dardas for reporting this.
2022-05-01 12:24:19 -07:00
Mickey Sola
9b026033b4 Fix NULL param crash when caching
Since converting the hash variable from a stack array to a pointer, the
pointer may now be NULL if the file is truncated after the scan starts
but before the hash is calculated. This race condition would result in
a NULL pointer dereference and crash.

This commit adds additional NULL parameter checks.

Thanks to Alexander Patrakov and Antoine Gatineau for reporting this issue.

Resolves: https://github.com/Cisco-Talos/clamav/issues/440
2022-05-01 12:24:19 -07:00
Micah Snyder
d9c8cab5be Windows: Fix utf8 filepath issues
* Windows: Fix utf8 filename support issue

The function used to verify the real path for the file being
scanned fails on some utf8 filenames, like:
  file_with_ümlaut.eicar.com

The scan continues despite the failure, but the error reported from
clamd and warnings from clamscan are confusing because it looks like
the scan failed even if a verdict shows up later.

The fix here is to convert the path from utf8 to utf16 and then use
the CreateFileW() API to grab the file handle for the real path check.

Resolves: https://github.com/Cisco-Talos/clamav/issues/418
Resolves: CLAM-1782

* Windows: Fix utf8 libclamav logging issues

Print to the log using rust eprint() instead of fputs()

Rust's eprint() function supports full utf8, while fputs() gets
confused and prints stuff like:
  file_with_├╝mlaut.eicar.com
instead of:
  file_with_ümlaut.eicar.com
2022-04-30 19:11:03 -07:00
Micah Snyder
d86811ad47 Test: Add test for alerting on images extracted from XLS
Using a fuzzy hash test for the clamav daemon JPEG attached to the XLS
document.  Not yet testing PNG, because the fuzzy hash implementation
isn't properly hashing that file, yet.

This test is for a regression where malware detection wasn't properly
being tracked for OLE2 (XLS) image extraction / scanning.
2022-04-29 14:21:06 -07:00
Micah Snyder
907af3ab14 XLS parser: Fix alert lost for image in XLS
The code to extract images from XLS documents isn't recording any alerts
that may be found when scanning those XLS documents. The logic to record
alerts was almost entirely done except left disconnected in one spot.

This fixes that broken link, propagating the alert all the way up.
2022-04-29 14:21:06 -07:00
ragusaa
7b464ab882 Fix small leak when loading invalid FTM signatures
Resolves: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43844
2022-04-19 15:46:27 -07:00
m-sola
8b2bd58362 1611 - Fix bound-checking to wdb pattern buffer (#548)
During wdb load, it was possible to go beyond the bounds
of the pattern buffer due to two subsequent increment ops
with no bounds checking in between.

This issue was reported by external researchers and
they provided the fix as well.

Based on our own research, this is a defect but not a vulnerability.

Co-authored-by: Mickey Sola <micksola@cisco.com>
2022-04-15 17:09:16 -07:00
Andy Ragusa
e51920dfe8 Free correct variable in signature load error handling
We don't allocate a copy of the signature name to store in the AC
pattern structure for logical signature patterns because it is already
stored in the logical signature structure. But oss-fuzz found that we're
freeing that virname in when an error happens even if it wasn't copied.

This fix checks the allocation before MPOOL_FREE.

Since virname is passed in, and only cloned under certain condtions,
check to see that it has actually been cloned before freeing it in any
cleanup code.

Resolves: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45205
2022-04-14 15:24:35 -07:00
Răzvan Cojocaru
9c466f605e Minor fixes: remove a couple of unused variables
* clamonacc: fix unused variable compile-time warning

Remove unused variable 'ret' from onas_queue.c and get rid of the
following compile-time warning:

~/clamav/clamonacc/scan/onas_queue.c: In function ‘onas_scan_queue_th’:
~/clamav/clamonacc/scan/onas_queue.c:161:9: warning: unused variable ‘ret’ [-Wunused-variable]
 161 |     int ret;
     |         ^~~

* libclamav: fix unused variable compile-time warning

Remove unused variable 'err' from libclamav/png.c, and get rid of
the following compile-time warning:

~/clamav/libclamav/png.c: In function ‘cli_parsepng’:
~/clamav/libclamav/png.c:101:9: warning: unused variable ‘err’ [-Wunused-variable]
  101 |     int err = Z_OK;
      |         ^~~
2022-04-13 08:53:58 -07:00
ragusaa
2c91aa741e Fix benign 1-byte buffer over-write in OLE2 parser
The office art structure for OLE2 documents records the file name
length using a `uint8_t`, meaning the name may be up to 255 bytes in
length, not including the null terminating byte. If the length is
255 then the parser will write the null-terminating byte just after
the end of the name buffer on the stack.

This issue does not cause a crash and is not a vulnerability.

This fix extends the size of stack array to account for the null
terminator.

Thank you Michał Dardas for reporting this issue.
2022-04-04 15:29:03 -07:00
Micah Snyder
d209051073 0.105 release prep: Set version suffix to '-rc2' (#527)
Also update release notes in the NEWS to describe and credit
the IPv6 compatibility fix.
2022-04-03 21:25:06 -07:00
m-sola
9d9caf396a Fix benign 1-byte stack buffer overwrite when loading PDB/WDB regex signatures
The check of pattern_len against FILEBUF is largely meaningless since
pattern is derived from a strchr() call against buffer (with length FILEBUFF).

This fix ensures that the relative size is checked against max buffer size
which prevents overwriting stack memory with a single null byte. 

Resolves: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45247
2022-04-01 13:39:57 -07:00
Micah Snyder
2d99b49797 fuzz: stability of parallel fuzz testing
The fuzz targets that write a temp file currently use the same filename
every time. One of the users identified that if the tests are running
in parallel mode, many processes are accessing the same file.
This results in unstable input to the API being tested, where the file
may be overwritten as the function is being tested.

This commit fixes it by putting the fuzz process PID in the filenames
for the scanfile and dbload fuzz targets.

Resolves: https://github.com/Cisco-Talos/clamav/issues/432

Also fixed a CMake bug that built an extra fuzz target file that doesn't
serve any purpose.

Resolves: https://github.com/Cisco-Talos/clamav/issues/431
2022-03-30 13:17:02 -07:00
Micah Snyder
0d96061e2f Update generated sys.rs internal Rust bindings 2022-03-29 12:48:06 -07:00
Micah Snyder
9e14ffab36 PE parser: fix recently introduced NULL dereference crash
Commit f82492aef4 fixed a crash in Windows
debug builds but in so doing accidentally introduced a possible crash
when scanning PE files that lack import tables.  The issue being that
the openssl hashing functions try to "finish" a hash that was never
started.

This commit fixes the issue by returning CL_BREAK instead of CL_SUCCESS
when the import table doesn't exist or RVA is invalid so that we can
differentiate between successfully calculating the hashes and
successfully skipping the hashing process.
2022-03-29 12:48:06 -07:00
ragusaa
b30d9c54b2 Phishing database load: Fix benign heap buffer overflow
A heap buffer overflow could occur during resource cleanup if a
malloc fails when adding a regex pattern to the phishing suffix tree.
The solution is to increment suffix_cnt after cli_realloc succeeds.

The issue was identified using fault injection and is not a vulnerability.

Resolves: https://github.com/Cisco-Talos/clamav/issues/429
2022-03-26 10:10:26 -07:00
Micah Snyder
89b72cb002 Sigtool: Add --fuzzy-img option to generate image fuzzy hash
Add `sigtool --fuzzy-img` option to generate image fuzzy hash.

Also fix assorted warnings, mostly ensuring enough buffer space so format
strings aren't truncated.

For the dsig change: the returned string is allocated and is not const.
The caller will have to free it.
2022-03-24 16:11:50 -07:00
Andre Breiler
cbea8806c9 docker: make clamd listen on IPv4 and v6 ADDR_ANY 2022-03-23 18:05:12 -07:00
ragusaa
4373e8f234 Fix possible invalid free (#507)
'new' is allocated by mpool, so should be freed by the mpool free
function. 


This issue is not a vulnerability



Resolves: https://github.com/Cisco-Talos/clamav/issues/430
2022-03-22 17:06:22 -07:00
Micah Snyder
0ad864ab83 Yara: Fix support for regex strings
In 0.104 and prior, the function for adding a logical subsignature
was being used for NDB sigs, FTM, sigs, *and* Yara strings.

When cleaning up the logic for handling different types of logical
sig subsignatures, we accidentally broke support for regex strings in
yara rules.

This commit adds new logic for recording if the Yara string is a regex
string, by adding a regex subsig opt. Then in a new function for adding
different types of Yara strings, we check if it's a regex string or not
before adding as either a PCRE pattern or as an AC/BM pattern.

Resolves: https://github.com/Cisco-Talos/clamav/issues/494

Also add a basic test for yara regex rule.
2022-03-22 16:52:18 -07:00
Micah Snyder
15eef50656 Code cleanup: Refactor to clean up formatting issues
Refactored the clamscan code that determines 'what to scan' in order
to clean up some very messy logic and also to get around a difference in
how vscode and clang-format handle formatting #ifdef blocks in the
middle of an else/if.

In addition to refactoring, there is a slight behavior improvement. With
this change, doing `clamscan blah -` will now scan `blah` and then also
scan `stdin`.  You can even do `clamscan - blah` to now scan `stdin` and
then scan `blah`. Before, The `-` had to be the only "filename" argument
in order to scan from stdin.

In addition, added a bunch of extra empty lines or changing multi-line
function calls to single-line function calls in order to get around a
bug in clang-format with these two options do not playing nice together:
- AlignConsecutiveAssignments: true
- AlignAfterOpenBracket: true

AlignAfterOpenBracket is not taking account the spaces inserted by
AlignConsecutiveAssignments, so you end up with stuff like this:
```c
    bleeblah = 1;
    blah     = function(arg1,
                    arg2,
                    arg3);

                //  ^--- these args 4-left from where they should be.
```

VSCode, meanwhile, somehow fixes this whitespace issue so code that is
correctly formatted by VSCode doesn't have this bug, meaning that:

1. The clang-format check in GH Actions fails.
2. We'd all have to stop using format-on-save in VSCode and accept the
  bug if we wanted those GH Actions tests to pass.

Adding an empty line before variable assignments from multi-line
function calls evades the buggy behavior.

This commit should resolve the clang-format github action test failures,
for now.
2022-03-22 10:42:46 -07:00
Micah Snyder
c215b1245f EGG: Fix small memory leak for EGG's with encrypted files
EGG archives may have individually encrypted files, or may specify
encryption for the whole archive. For those that have individually
encrypted files, the clean-up code neglects to free the encrypt
structure, which holds 2 pointers (16 bytes on 64bit machines).

This commit adds that missing free.

Thank you Michał Dardas for reporting this issue.
2022-03-21 17:18:50 -07:00
Micah Snyder
30561caff5 0.105 release prep: Update NEWS.md release notes
Also add release notes from 0.103 and 0.104 patch versions published
during the development of 0.105.

Also Update llvm support details in the INSTALL.md file.
2022-03-10 14:13:19 -08:00
Micah Snyder
181ed79a5f 0.105 release prep: Set version suffix to '-rc' 2022-03-10 14:13:19 -08:00
Micah Snyder
ddf7224837 Remove compatibility layer for llvm versions before 3 2022-03-09 20:35:42 -08:00
Micah Snyder
3acada6069 Tests: silence LGTM-com static analysis warning 2022-03-09 20:35:42 -08:00
Andy Ragusa
c92227e051 Removed dead code support for llvm versions before 8. 2022-03-09 20:35:42 -08:00
Micah Snyder
b893a77e3f CMake: disable CMAKE_FIND_PACKAGE_PREFER_CONFIG option for libxml2
CMake has a pretty sweet alternative to pkg-config or to the older
`package-config` script that you'd run with `--libs`, `--ldflags`, etc.
CMake can (optionally) install a YourPackageConfig.cmake alongside your
libs under `<prefix>/lib/cmake/<pkg>/<pkg>Config.cmake`.

If you build something with -D CMAKE_FIND_PACKAGE_PREFER_CONFIG=TRUE,
then it will find that and use that to bring real CMake targets into
your build system for your dependencies, guaranteeing that you get all
the right include paths, library paths, ldflags, etc.

See: https://cmake.org/cmake/help/latest/variable/CMAKE_FIND_PACKAGE_PREFER_CONFIG.html

This works great for us for Curl and LLVM... not so much for libxml2.
Ideally we'd figure out what's wrong and support libxml2's package
config.cmake file, but for now this workaround lets us ignore libxml2
and continu to use this feature for the other libs, like LLVM.
2022-03-09 20:35:42 -08:00
Micah Snyder
2551fcf0f7 Support uppercase TARGET_ARCH_TYPE
The TARGET_ARCH_TYPE variable comes from $env:PROCESSOR_ARCHITECTURE,
which at least on my machine is AMD64, not amd64.
2022-03-09 20:35:42 -08:00
Micah Snyder
ed57b85074 CMake, LLVM, Win32: Fix link issue when LLVM lib list are full paths 2022-03-09 20:35:42 -08:00
Micah Snyder
426dd461f6 CMake: Fix win32 linking issue affecting unit tests w/ LLVM runtime
I observed undefined symbol errors when linking the bytecode_runtime
object with the check_* test executables on Windows when using LLVM
built from source. I'm not sure why, exactly, but these symbols should
all be present in the ClamAV::libclamav library that we're linking with,
so I don't know why we link with the object library targets in addition
to the libclamav shared/or/static library target.

This commit removes linking with those extra object library targets.
2022-03-09 20:35:42 -08:00
Micah Snyder
b07b1a65cb Fix linker issues with global variable used in tests
The `have_clamjit` global is used in the unit tests but doesn't appear
to be exported when I was testing the external LLVM runtime support PR,
resulting in an undefined symbol issue. Converting this to a function
that returns 0 or 1 instead of a global variable resolved the issue.
2022-03-09 20:35:42 -08:00
Micah Snyder
8bf70207d5 CMake: Fix LLVM linking issues: libclamav_rust, -ltinfo
We must pass the LLVM library dependencies to the libclamav_rust
build.rs script so it links the libclamav_rust unit test executable with
LLVM.

Also:
- We can remove the libtinfo dependency that was hardcoded for the LLVM
  3.6 support, and must remove it for the build to work on Alpine, macOS.
- Also, increased the libcheck default timeout from 60s to 300s after
  experiencing a failure while testing this.
- Also made one of the valgrind suppressions more generic to account for
  inline optimization differences observed in testing this.
2022-03-09 20:35:42 -08:00
Andy Ragusa
90904d5a7a CMake: Make BYTECODE_RUNTIME parameter case-insensitive
Also:
- Removed an unused DEBUG_TYPE define that was throwing warnings.
- Added default behavior for unexpected platform OS to reduce warnings.
2022-03-09 20:35:42 -08:00
Andy Ragusa
4b83bcf0c5 Updated llvm runtime to support llvm versions 8, 9, 10, 11, 12
Modified bytecode JIT code to support llvm versions 8, 9, 10, 11, 12.
Additionally updated FindLLVM.cmake to the current version, found at
https://github.com/ldc-developers/ldc/blob/master/cmake/Modules/FindLLVM.cmake,
as well as making modifications suggested by Micah Snyder to check VERSION_LESS
in CMakeLists.txt to avoid having modifications to FindLLVM.cmake.
2022-03-09 20:35:42 -08:00
Micah Snyder
d1656ee241 Increase default file maximums
MaxFileSize        25M  -> 100M
MaxScanSize        100M -> 400M
StreamMaxLength    25M  -> 100M
MaxEmbeddedPE      10M  -> 40M
MaxHTMLNormalize   10M  -> 40M
MaxHTMLNoTags      2M   -> 8M
MaxScriptNormalize 5M   -> 20M
PCREMaxFileSIze    25M  -> 100M
2022-03-09 16:47:44 -08:00
Micah Snyder
943f4c4e0e Tests: Increase default timeout to 10 minutes
I observed the libclamav_valgrind test exceeding the 200sec timeout and
have heard of occasional timeouts from users.
2022-03-02 21:44:48 -07:00
Micah Snyder
5756f0eab8 Update generated sys.rs file 2022-03-02 21:44:48 -07:00
Andy Ragusa
76dab42a30 DB load, CRB: Fix benign heap buffer overflow
Validate the length of the crt subject before memcpying it.
This resolves a possible multibyte heap buffer overread.
We determiend that this issue is not a vulnerability.

Fixes: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43828
2022-03-02 21:44:48 -07:00