2016-03-24 12:26:04 -04:00
|
|
|
/*
|
|
|
|
* Author: 웃 Sebastian Andrzej Siewior
|
2018-03-05 16:34:35 -05:00
|
|
|
* Summary: Glue code for libmspack handling.
|
2019-05-05 15:08:46 -04:00
|
|
|
*
|
|
|
|
* Acknowledgements: ClamAV uses Stuart Caie's libmspack to parse as number of
|
2018-03-05 16:34:35 -05:00
|
|
|
* Microsoft file formats.
|
2016-03-24 12:26:04 -04:00
|
|
|
* ✉ sebastian @ breakpoint ̣cc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2017-09-28 14:04:47 -04:00
|
|
|
#include <stdarg.h>
|
2017-10-18 15:52:19 -04:00
|
|
|
#include <stdio.h>
|
2016-03-24 12:26:04 -04:00
|
|
|
|
|
|
|
#include <mspack.h>
|
|
|
|
|
|
|
|
#include "clamav.h"
|
|
|
|
#include "fmap.h"
|
|
|
|
#include "scanners.h"
|
|
|
|
#include "others.h"
|
|
|
|
|
|
|
|
enum mspack_type {
|
2018-12-03 12:40:13 -05:00
|
|
|
FILETYPE_DUNNO,
|
|
|
|
FILETYPE_FMAP,
|
|
|
|
FILETYPE_FILENAME,
|
2016-03-24 12:26:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mspack_name {
|
2018-12-03 12:40:13 -05:00
|
|
|
fmap_t *fmap;
|
|
|
|
off_t org;
|
2016-03-24 12:26:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mspack_system_ex {
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_system ops;
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
uint64_t max_size;
|
2016-03-24 12:26:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mspack_handle {
|
2018-12-03 12:40:13 -05:00
|
|
|
enum mspack_type type;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
fmap_t *fmap;
|
|
|
|
off_t org;
|
|
|
|
off_t offset;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
FILE *f;
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
uint64_t max_size;
|
2016-03-24 12:26:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct mspack_file *mspack_fmap_open(struct mspack_system *self,
|
2018-12-03 12:40:13 -05:00
|
|
|
const char *filename, int mode)
|
2016-03-24 12:26:04 -04:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_name *mspack_name;
|
|
|
|
struct mspack_handle *mspack_handle;
|
|
|
|
struct mspack_system_ex *self_ex;
|
|
|
|
const char *fmode;
|
|
|
|
const struct mspack_system *mptr = self;
|
|
|
|
|
|
|
|
if (!filename) {
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mspack_handle = malloc(sizeof(*mspack_handle));
|
|
|
|
memset(mspack_handle, 0, (sizeof(*mspack_handle)));
|
|
|
|
if (!mspack_handle) {
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(mspack_handle, 0, sizeof(*mspack_handle));
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MSPACK_SYS_OPEN_READ:
|
|
|
|
mspack_handle->type = FILETYPE_FMAP;
|
|
|
|
|
|
|
|
mspack_name = (struct mspack_name *)filename;
|
|
|
|
mspack_handle->fmap = mspack_name->fmap;
|
|
|
|
mspack_handle->org = mspack_name->org;
|
|
|
|
mspack_handle->offset = 0;
|
|
|
|
|
|
|
|
return (struct mspack_file *)mspack_handle;
|
|
|
|
|
|
|
|
case MSPACK_SYS_OPEN_WRITE:
|
|
|
|
fmode = "wb";
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_OPEN_UPDATE:
|
|
|
|
fmode = "r+b";
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_OPEN_APPEND:
|
|
|
|
fmode = "ab";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cli_dbgmsg("%s() wrong mode\n", __func__);
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
mspack_handle->type = FILETYPE_FILENAME;
|
|
|
|
|
|
|
|
mspack_handle->f = fopen(filename, fmode);
|
|
|
|
if (!mspack_handle->f) {
|
|
|
|
cli_dbgmsg("%s() failed %d\n", __func__, __LINE__);
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_ex = (struct mspack_system_ex *)((char *)mptr - offsetof(struct mspack_system_ex, ops));
|
|
|
|
mspack_handle->max_size = self_ex->max_size;
|
|
|
|
return (struct mspack_file *)mspack_handle;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
|
|
|
out_err:
|
2018-12-03 12:40:13 -05:00
|
|
|
memset(mspack_handle, 0, (sizeof(*mspack_handle)));
|
|
|
|
free(mspack_handle);
|
|
|
|
mspack_handle = NULL;
|
|
|
|
return NULL;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mspack_fmap_close(struct mspack_file *file)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_handle *mspack_handle = (struct mspack_handle *)file;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!mspack_handle)
|
|
|
|
return;
|
2017-10-09 11:50:50 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (mspack_handle->type == FILETYPE_FILENAME)
|
|
|
|
if (mspack_handle->f)
|
|
|
|
fclose(mspack_handle->f);
|
2017-10-09 11:50:50 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memset(mspack_handle, 0, (sizeof(*mspack_handle)));
|
|
|
|
free(mspack_handle);
|
|
|
|
mspack_handle = NULL;
|
|
|
|
return;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mspack_fmap_read(struct mspack_file *file, void *buffer, int bytes)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_handle *mspack_handle = (struct mspack_handle *)file;
|
|
|
|
off_t offset;
|
|
|
|
size_t count;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (bytes < 0) {
|
|
|
|
cli_dbgmsg("%s() %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!mspack_handle) {
|
|
|
|
cli_dbgmsg("%s() %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mspack_handle->type == FILETYPE_FMAP) {
|
|
|
|
offset = mspack_handle->offset + mspack_handle->org;
|
|
|
|
|
|
|
|
ret = fmap_readn(mspack_handle->fmap, buffer, offset, bytes);
|
|
|
|
if (ret != bytes) {
|
|
|
|
cli_dbgmsg("%s() %d %d, %d\n", __func__, __LINE__, bytes, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
mspack_handle->offset += bytes;
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
count = fread(buffer, bytes, 1, mspack_handle->f);
|
|
|
|
if (count < 1) {
|
2019-05-05 15:08:46 -04:00
|
|
|
cli_dbgmsg("%s() %d %d, %zu\n", __func__, __LINE__, bytes, count);
|
2018-12-03 12:40:13 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mspack_fmap_write(struct mspack_file *file, void *buffer, int bytes)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_handle *mspack_handle = (struct mspack_handle *)file;
|
|
|
|
size_t count;
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
uint64_t max_size;
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
if (bytes < 0 || !mspack_handle) {
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mspack_handle->type == FILETYPE_FMAP) {
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
max_size = mspack_handle->max_size;
|
|
|
|
if (!max_size)
|
|
|
|
return bytes;
|
|
|
|
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
max_size = max_size < (uint64_t)bytes ? max_size : (uint64_t)bytes;
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
mspack_handle->max_size -= max_size;
|
|
|
|
|
|
|
|
count = fwrite(buffer, max_size, 1, mspack_handle->f);
|
|
|
|
if (count < 1) {
|
2019-05-05 15:08:46 -04:00
|
|
|
cli_dbgmsg("%s() err %d <%zu %d>\n", __func__, __LINE__, count, bytes);
|
2018-12-03 12:40:13 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mspack_fmap_seek(struct mspack_file *file, off_t offset, int mode)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_handle *mspack_handle = (struct mspack_handle *)file;
|
|
|
|
|
|
|
|
if (!mspack_handle) {
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mspack_handle->type == FILETYPE_FMAP) {
|
|
|
|
off_t new_pos;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MSPACK_SYS_SEEK_START:
|
|
|
|
new_pos = offset;
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_SEEK_CUR:
|
|
|
|
new_pos = mspack_handle->offset + offset;
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_SEEK_END:
|
|
|
|
new_pos = mspack_handle->fmap->len + offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (new_pos < 0 || new_pos > (off_t)mspack_handle->fmap->len) {
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mspack_handle->offset = new_pos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MSPACK_SYS_SEEK_START:
|
|
|
|
mode = SEEK_SET;
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_SEEK_CUR:
|
|
|
|
mode = SEEK_CUR;
|
|
|
|
break;
|
|
|
|
case MSPACK_SYS_SEEK_END:
|
|
|
|
mode = SEEK_END;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cli_dbgmsg("%s() err %d\n", __func__, __LINE__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fseek(mspack_handle->f, offset, mode);
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static off_t mspack_fmap_tell(struct mspack_file *file)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mspack_handle *mspack_handle = (struct mspack_handle *)file;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!mspack_handle)
|
|
|
|
return -1;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (mspack_handle->type == FILETYPE_FMAP)
|
|
|
|
return mspack_handle->offset;
|
2016-03-24 12:26:04 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
return (off_t)ftell(mspack_handle->f);
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mspack_fmap_message(struct mspack_file *file, const char *fmt, ...)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
UNUSEDPARAM(file);
|
|
|
|
|
|
|
|
if (UNLIKELY(cli_debug_flag)) {
|
|
|
|
va_list args;
|
|
|
|
char buff[BUFSIZ];
|
|
|
|
size_t len = sizeof("LibClamAV debug: ") - 1;
|
|
|
|
|
|
|
|
memset(buff, 0, BUFSIZ);
|
|
|
|
|
|
|
|
/* Add the prefix */
|
|
|
|
strncpy(buff, "LibClamAV debug: ", len);
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buff + len, sizeof(buff) - len - 2, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
/* Add a newline and a null terminator */
|
|
|
|
buff[strlen(buff)] = '\n';
|
|
|
|
buff[strlen(buff) + 1] = '\0';
|
|
|
|
|
|
|
|
fputs(buff, stderr);
|
|
|
|
}
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
2017-09-28 16:24:24 -04:00
|
|
|
|
2016-03-24 12:26:04 -04:00
|
|
|
static void *mspack_fmap_alloc(struct mspack_system *self, size_t num)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
UNUSEDPARAM(self);
|
|
|
|
void *addr = malloc(num);
|
|
|
|
if (addr) {
|
|
|
|
memset(addr, 0, num);
|
|
|
|
}
|
|
|
|
return addr;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mspack_fmap_free(void *mem)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
if (mem) {
|
2017-03-29 14:55:26 -04:00
|
|
|
free(mem);
|
|
|
|
mem = NULL;
|
|
|
|
}
|
|
|
|
return;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mspack_fmap_copy(void *src, void *dst, size_t num)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(dst, src, num);
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mspack_system mspack_sys_fmap_ops = {
|
2018-12-03 12:40:13 -05:00
|
|
|
.open = mspack_fmap_open,
|
|
|
|
.close = mspack_fmap_close,
|
|
|
|
.read = mspack_fmap_read,
|
|
|
|
.write = mspack_fmap_write,
|
|
|
|
.seek = mspack_fmap_seek,
|
|
|
|
.tell = mspack_fmap_tell,
|
|
|
|
.message = mspack_fmap_message,
|
|
|
|
.alloc = mspack_fmap_alloc,
|
|
|
|
.free = mspack_fmap_free,
|
|
|
|
.copy = mspack_fmap_copy,
|
2016-03-24 12:26:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
int cli_scanmscab(cli_ctx *ctx, off_t sfx_offset)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mscab_decompressor *cab_d;
|
|
|
|
struct mscabd_cabinet *cab_h;
|
|
|
|
struct mscabd_file *cab_f;
|
|
|
|
int ret = 0;
|
2017-10-04 08:30:25 -07:00
|
|
|
int files;
|
2018-12-03 12:40:13 -05:00
|
|
|
int virus_num = 0;
|
|
|
|
struct mspack_name mspack_fmap = {
|
libclamav: Fix scan recursion tracking
Scan recursion is the process of identifying files embedded in other
files and then scanning them, recursively.
Internally this process is more complex than it may sound because a file
may have multiple layers of types before finding a new "file".
At present we treat the recursion count in the scanning context as an
index into both our fmap list AND our container list. These two lists
are conceptually a part of the same thing and should be unified.
But what's concerning is that the "recursion level" isn't actually
incremented or decremented at the same time that we add a layer to the
fmap or container lists but instead is more touchy-feely, increasing
when we find a new "file".
To account for this shadiness, the size of the fmap and container lists
has always been a little longer than our "max scan recursion" limit so
we don't accidentally overflow the fmap or container arrays (!).
I've implemented a single recursion-stack as an array, similar to before,
which includes a pointer to each fmap at each layer, along with the size
and type. Push and pop functions add and remove layers whenever a new
fmap is added. A boolean argument when pushing indicates if the new layer
represents a new buffer or new file (descriptor). A new buffer will reset
the "nested fmap level" (described below).
This commit also provides a solution for an issue where we detect
embedded files more than once during scan recursion.
For illustration, imagine a tarball named foo.tar.gz with this structure:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
But suppose baz.exe embeds a ZIP archive and a 7Z archive, like this:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| baz.exe | PE | 0 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| │ └── hello.txt | ASCII | 2 | 0 |
| └── sfx.7z | 7Z | 1 | 1 |
| └── world.txt | ASCII | 2 | 0 |
(A) If we scan for embedded files at any layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| ├── foo.tar | TAR | 1 | 0 |
| │ ├── bar.zip | ZIP | 2 | 1 |
| │ │ └── hola.txt | ASCII | 3 | 0 |
| │ ├── baz.exe | PE | 2 | 1 |
| │ │ ├── sfx.zip | ZIP | 3 | 1 |
| │ │ │ └── hello.txt | ASCII | 4 | 0 |
| │ │ └── sfx.7z | 7Z | 3 | 1 |
| │ │ └── world.txt | ASCII | 4 | 0 |
| │ ├── sfx.zip | ZIP | 2 | 1 |
| │ │ └── hello.txt | ASCII | 3 | 0 |
| │ └── sfx.7z | 7Z | 2 | 1 |
| │ └── world.txt | ASCII | 3 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| └── sfx.7z | 7Z | 1 | 1 |
(A) is bad because it scans content more than once.
Note that for the GZ layer, it may detect the ZIP and 7Z if the
signature hits on the compressed data, which it might, though
extracting the ZIP and 7Z will likely fail.
The reason the above doesn't happen now is that we restrict embedded
type scans for a bunch of archive formats to include GZ and TAR.
(B) If we scan for embedded files at the foo.tar layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| ├── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 2 | 1 |
| │ └── hello.txt | ASCII | 3 | 0 |
| └── sfx.7z | 7Z | 2 | 1 |
| └── world.txt | ASCII | 3 | 0 |
(B) is almost right. But we can achieve it easily enough only scanning for
embedded content in the current fmap when the "nested fmap level" is 0.
The upside is that it should safely detect all embedded content, even if
it may think the sfz.zip and sfx.7z are in foo.tar instead of in baz.exe.
The biggest risk I can think of affects ZIPs. SFXZIP detection
is identical to ZIP detection, which is why we don't allow SFXZIP to be
detected if insize of a ZIP. If we only allow embedded type scanning at
fmap-layer 0 in each buffer, this will fail to detect the embedded ZIP
if the bar.exe was not compressed in foo.zip and if non-compressed files
extracted from ZIPs aren't extracted as new buffers:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.zip | ZIP | 0 | 0 |
| └── bar.exe | PE | 1 | 1 |
| └── sfx.zip | ZIP | 2 | 2 |
Provided that we ensure all files extracted from zips are scanned in
new buffers, option (B) should be safe.
(C) If we scan for embedded files at the baz.exe layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 3 | 1 |
| │ └── hello.txt | ASCII | 4 | 0 |
| └── sfx.7z | 7Z | 3 | 1 |
| └── world.txt | ASCII | 4 | 0 |
(C) is right. But it's harder to achieve. For this example we can get it by
restricting 7ZSFX and ZIPSFX detection only when scanning an executable.
But that may mean losing detection of archives embedded elsewhere.
And we'd have to identify allowable container types for each possible
embedded type, which would be very difficult.
So this commit aims to solve the issue the (B)-way.
Note that in all situations, we still have to scan with file typing
enabled to determine if we need to reassign the current file type, such
as re-identifying a Bzip2 archive as a DMG that happens to be Bzip2-
compressed. Detection of DMG and a handful of other types rely on
finding data partway through or near the ned of a file before
reassigning the entire file as the new type.
Other fixes and considerations in this commit:
- The utf16 HTML parser has weak error handling, particularly with respect
to creating a nested fmap for scanning the ascii decoded file.
This commit cleans up the error handling and wraps the nested scan with
the recursion-stack push()/pop() for correct recursion tracking.
Before this commit, each container layer had a flag to indicate if the
container layer is valid.
We need something similar so that the cli_recursion_stack_get_*()
functions ignore normalized layers. Details...
Imagine an LDB signature for HTML content that specifies a ZIP
container. If the signature actually alerts on the normalized HTML and
you don't ignore normalized layers for the container check, it will
appear as though the alert is in an HTML container rather than a ZIP
container.
This commit accomplishes this with a boolean you set in the scan context
before scanning a new layer. Then when the new fmap is created, it will
use that flag to set similar flag for the layer. The context flag is
reset those that anything after this doesn't have that flag.
The flag allows the new recursion_stack_get() function to ignore
normalized layers when iterating the stack to return a layer at a
requested index, negative or positive.
Scanning normalized extracted/normalized javascript and VBA should also
use the 'layer is normalized' flag.
- This commit also fixes Heuristic.Broken.Executable alert for ELF files
to make sure that:
A) these only alert if cli_append_virus() returns CL_VIRUS (aka it
respects the FP check).
B) all broken-executable alerts for ELF only happen if the
SCAN_HEURISTIC_BROKEN option is enabled.
- This commit also cleans up the error handling in cli_magic_scan_dir().
This was needed so we could correctly apply the layer-is-normalized-flag
to all VBA macros extracted to a directory when scanning the directory.
- Also fix an issue where exceeding scan maximums wouldn't cause embedded
file detection scans to abort. Granted we don't actually want to abort
if max filesize or max recursion depth are exceeded... only if max
scansize, max files, and max scantime are exceeded.
Add 'abort_scan' flag to scan context, to protect against depending on
correct error propagation for fatal conditions. Instead, setting this
flag in the scan context should guarantee that a fatal condition deep in
scan recursion isn't lost which result in more stuff being scanned
instead of aborting. This shouldn't be necessary, but some status codes
like CL_ETIMEOUT never used to be fatal and it's easier to do this than
to verify every parser only returns CL_ETIMEOUT and other "fatal
status codes" in fatal conditions.
- Remove duplicate is_tar() prototype from filestypes.c and include
is_tar.h instead.
- Presently we create the fmap hash when creating the fmap.
This wastes a bit of CPU if the hash is never needed.
Now that we're creating fmap's for all embedded files discovered with
file type recognition scans, this is a much more frequent occurence and
really slows things down.
This commit fixes the issue by only creating fmap hashes as needed.
This should not only resolve the perfomance impact of creating fmap's
for all embedded files, but also should improve performance in general.
- Add allmatch check to the zip parser after the central-header meta
match. That way we don't multiple alerts with the same match except in
allmatch mode. Clean up error handling in the zip parser a tiny bit.
- Fixes to ensure that the scan limits such as scansize, filesize,
recursion depth, # of embedded files, and scantime are always reported
if AlertExceedsMax (--alert-exceeds-max) is enabled.
- Fixed an issue where non-fatal alerts for exceeding scan maximums may
mask signature matches later on. I changed it so these alerts use the
"possibly unwanted" alert-type and thus only alert if no other alerts
were found or if all-match or heuristic-precedence are enabled.
- Added the "Heuristics.Limits.Exceeded.*" events to the JSON metadata
when the --gen-json feature is enabled. These will show up once under
"ParseErrors" the first time a limit is exceeded. In the present
implementation, only one limits-exceeded events will be added, so as to
prevent a malicious or malformed sample from filling the JSON buffer
with millions of events and using a tonne of RAM.
2021-09-11 14:15:21 -07:00
|
|
|
.fmap = ctx->fmap,
|
2018-12-03 12:40:13 -05:00
|
|
|
.org = sfx_offset,
|
|
|
|
};
|
|
|
|
struct mspack_system_ex ops_ex;
|
|
|
|
memset(&ops_ex, 0, sizeof(struct mspack_system_ex));
|
|
|
|
ops_ex.ops = mspack_sys_fmap_ops;
|
|
|
|
|
|
|
|
cab_d = mspack_create_cab_decompressor(&ops_ex.ops);
|
|
|
|
if (!cab_d) {
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
return CL_EUNPACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
cab_d->set_param(cab_d, MSCABD_PARAM_FIXMSZIP, 1);
|
2018-09-18 09:29:01 -04:00
|
|
|
#if MSCABD_PARAM_SALVAGE
|
2018-12-03 12:40:13 -05:00
|
|
|
cab_d->set_param(cab_d, MSCABD_PARAM_SALVAGE, 1);
|
2018-09-18 09:29:01 -04:00
|
|
|
#endif
|
2018-09-16 19:24:40 -04:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cab_h = cab_d->open(cab_d, (char *)&mspack_fmap);
|
|
|
|
if (!cab_h) {
|
|
|
|
ret = CL_EFORMAT;
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
goto out_dest;
|
|
|
|
}
|
|
|
|
files = 0;
|
|
|
|
for (cab_f = cab_h->files; cab_f; cab_f = cab_f->next) {
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
uint64_t max_size;
|
2018-12-03 12:40:13 -05:00
|
|
|
char *tmp_fname = NULL;
|
|
|
|
|
|
|
|
ret = cli_matchmeta(ctx, cab_f->filename, 0, cab_f->length, 0,
|
|
|
|
files, 0, NULL);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == CL_VIRUS) {
|
|
|
|
virus_num++;
|
|
|
|
if (!SCAN_ALLMATCHES)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->engine->maxscansize) {
|
|
|
|
if (ctx->scansize >= ctx->engine->maxscansize) {
|
|
|
|
ret = CL_CLEAN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
if (ctx->engine->maxfilesize > 0) {
|
|
|
|
// max filesize has been set
|
|
|
|
if ((ctx->engine->maxscansize > 0) &&
|
|
|
|
(ctx->scansize + ctx->engine->maxfilesize >= ctx->engine->maxscansize)) {
|
|
|
|
// ... but would exceed max scansize, shrink it.
|
|
|
|
max_size = ctx->engine->maxscansize - ctx->scansize;
|
|
|
|
} else {
|
|
|
|
// ... and will work
|
|
|
|
max_size = ctx->engine->maxfilesize;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// max filesize not specified
|
|
|
|
if ((ctx->engine->maxscansize > 0) &&
|
|
|
|
(ctx->scansize + UINT32_MAX >= ctx->engine->maxscansize)) {
|
|
|
|
// ... but UINT32_MAX would exceed max scansize, shrink it.
|
|
|
|
max_size = ctx->engine->maxscansize - ctx->scansize;
|
|
|
|
} else {
|
|
|
|
// ... use UINT32_MAX
|
|
|
|
max_size = UINT32_MAX;
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
2020-03-19 21:23:54 -04:00
|
|
|
tmp_fname = cli_gentemp(ctx->sub_tmpdir);
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!tmp_fname) {
|
|
|
|
ret = CL_EMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops_ex.max_size = max_size;
|
|
|
|
/* scan */
|
|
|
|
ret = cab_d->extract(cab_d, cab_f, tmp_fname);
|
|
|
|
if (ret)
|
|
|
|
/* Failed to extract. Try to scan what is there */
|
|
|
|
cli_dbgmsg("%s() failed to extract %d\n", __func__, ret);
|
|
|
|
|
2020-03-21 14:15:28 -04:00
|
|
|
ret = cli_magic_scan_file(tmp_fname, ctx, cab_f->filename);
|
2020-03-19 21:23:54 -04:00
|
|
|
if (CL_EOPEN == ret) {
|
|
|
|
ret = CL_CLEAN;
|
|
|
|
} else if (CL_VIRUS == ret) {
|
2018-12-03 12:40:13 -05:00
|
|
|
virus_num++;
|
2020-03-19 21:23:54 -04:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
if (!access(tmp_fname, R_OK) && cli_unlink(tmp_fname)) {
|
|
|
|
free(tmp_fname);
|
|
|
|
ret = CL_EUNLINK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tmp_fname);
|
|
|
|
files++;
|
|
|
|
if (ret == CL_VIRUS && SCAN_ALLMATCHES)
|
|
|
|
continue;
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-24 12:26:04 -04:00
|
|
|
|
|
|
|
out_close:
|
2018-12-03 12:40:13 -05:00
|
|
|
cab_d->close(cab_d, cab_h);
|
2016-03-24 12:26:04 -04:00
|
|
|
out_dest:
|
2018-12-03 12:40:13 -05:00
|
|
|
mspack_destroy_cab_decompressor(cab_d);
|
|
|
|
if (virus_num)
|
|
|
|
return CL_VIRUS;
|
|
|
|
return ret;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int cli_scanmschm(cli_ctx *ctx)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
struct mschm_decompressor *mschm_d;
|
|
|
|
struct mschmd_header *mschm_h;
|
|
|
|
struct mschmd_file *mschm_f;
|
|
|
|
int ret = CL_CLEAN; // Default CLEAN in case CHM contains no files.
|
|
|
|
int files;
|
|
|
|
int virus_num = 0;
|
|
|
|
struct mspack_name mspack_fmap = {
|
libclamav: Fix scan recursion tracking
Scan recursion is the process of identifying files embedded in other
files and then scanning them, recursively.
Internally this process is more complex than it may sound because a file
may have multiple layers of types before finding a new "file".
At present we treat the recursion count in the scanning context as an
index into both our fmap list AND our container list. These two lists
are conceptually a part of the same thing and should be unified.
But what's concerning is that the "recursion level" isn't actually
incremented or decremented at the same time that we add a layer to the
fmap or container lists but instead is more touchy-feely, increasing
when we find a new "file".
To account for this shadiness, the size of the fmap and container lists
has always been a little longer than our "max scan recursion" limit so
we don't accidentally overflow the fmap or container arrays (!).
I've implemented a single recursion-stack as an array, similar to before,
which includes a pointer to each fmap at each layer, along with the size
and type. Push and pop functions add and remove layers whenever a new
fmap is added. A boolean argument when pushing indicates if the new layer
represents a new buffer or new file (descriptor). A new buffer will reset
the "nested fmap level" (described below).
This commit also provides a solution for an issue where we detect
embedded files more than once during scan recursion.
For illustration, imagine a tarball named foo.tar.gz with this structure:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
But suppose baz.exe embeds a ZIP archive and a 7Z archive, like this:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| baz.exe | PE | 0 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| │ └── hello.txt | ASCII | 2 | 0 |
| └── sfx.7z | 7Z | 1 | 1 |
| └── world.txt | ASCII | 2 | 0 |
(A) If we scan for embedded files at any layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| ├── foo.tar | TAR | 1 | 0 |
| │ ├── bar.zip | ZIP | 2 | 1 |
| │ │ └── hola.txt | ASCII | 3 | 0 |
| │ ├── baz.exe | PE | 2 | 1 |
| │ │ ├── sfx.zip | ZIP | 3 | 1 |
| │ │ │ └── hello.txt | ASCII | 4 | 0 |
| │ │ └── sfx.7z | 7Z | 3 | 1 |
| │ │ └── world.txt | ASCII | 4 | 0 |
| │ ├── sfx.zip | ZIP | 2 | 1 |
| │ │ └── hello.txt | ASCII | 3 | 0 |
| │ └── sfx.7z | 7Z | 2 | 1 |
| │ └── world.txt | ASCII | 3 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| └── sfx.7z | 7Z | 1 | 1 |
(A) is bad because it scans content more than once.
Note that for the GZ layer, it may detect the ZIP and 7Z if the
signature hits on the compressed data, which it might, though
extracting the ZIP and 7Z will likely fail.
The reason the above doesn't happen now is that we restrict embedded
type scans for a bunch of archive formats to include GZ and TAR.
(B) If we scan for embedded files at the foo.tar layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| ├── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 2 | 1 |
| │ └── hello.txt | ASCII | 3 | 0 |
| └── sfx.7z | 7Z | 2 | 1 |
| └── world.txt | ASCII | 3 | 0 |
(B) is almost right. But we can achieve it easily enough only scanning for
embedded content in the current fmap when the "nested fmap level" is 0.
The upside is that it should safely detect all embedded content, even if
it may think the sfz.zip and sfx.7z are in foo.tar instead of in baz.exe.
The biggest risk I can think of affects ZIPs. SFXZIP detection
is identical to ZIP detection, which is why we don't allow SFXZIP to be
detected if insize of a ZIP. If we only allow embedded type scanning at
fmap-layer 0 in each buffer, this will fail to detect the embedded ZIP
if the bar.exe was not compressed in foo.zip and if non-compressed files
extracted from ZIPs aren't extracted as new buffers:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.zip | ZIP | 0 | 0 |
| └── bar.exe | PE | 1 | 1 |
| └── sfx.zip | ZIP | 2 | 2 |
Provided that we ensure all files extracted from zips are scanned in
new buffers, option (B) should be safe.
(C) If we scan for embedded files at the baz.exe layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 3 | 1 |
| │ └── hello.txt | ASCII | 4 | 0 |
| └── sfx.7z | 7Z | 3 | 1 |
| └── world.txt | ASCII | 4 | 0 |
(C) is right. But it's harder to achieve. For this example we can get it by
restricting 7ZSFX and ZIPSFX detection only when scanning an executable.
But that may mean losing detection of archives embedded elsewhere.
And we'd have to identify allowable container types for each possible
embedded type, which would be very difficult.
So this commit aims to solve the issue the (B)-way.
Note that in all situations, we still have to scan with file typing
enabled to determine if we need to reassign the current file type, such
as re-identifying a Bzip2 archive as a DMG that happens to be Bzip2-
compressed. Detection of DMG and a handful of other types rely on
finding data partway through or near the ned of a file before
reassigning the entire file as the new type.
Other fixes and considerations in this commit:
- The utf16 HTML parser has weak error handling, particularly with respect
to creating a nested fmap for scanning the ascii decoded file.
This commit cleans up the error handling and wraps the nested scan with
the recursion-stack push()/pop() for correct recursion tracking.
Before this commit, each container layer had a flag to indicate if the
container layer is valid.
We need something similar so that the cli_recursion_stack_get_*()
functions ignore normalized layers. Details...
Imagine an LDB signature for HTML content that specifies a ZIP
container. If the signature actually alerts on the normalized HTML and
you don't ignore normalized layers for the container check, it will
appear as though the alert is in an HTML container rather than a ZIP
container.
This commit accomplishes this with a boolean you set in the scan context
before scanning a new layer. Then when the new fmap is created, it will
use that flag to set similar flag for the layer. The context flag is
reset those that anything after this doesn't have that flag.
The flag allows the new recursion_stack_get() function to ignore
normalized layers when iterating the stack to return a layer at a
requested index, negative or positive.
Scanning normalized extracted/normalized javascript and VBA should also
use the 'layer is normalized' flag.
- This commit also fixes Heuristic.Broken.Executable alert for ELF files
to make sure that:
A) these only alert if cli_append_virus() returns CL_VIRUS (aka it
respects the FP check).
B) all broken-executable alerts for ELF only happen if the
SCAN_HEURISTIC_BROKEN option is enabled.
- This commit also cleans up the error handling in cli_magic_scan_dir().
This was needed so we could correctly apply the layer-is-normalized-flag
to all VBA macros extracted to a directory when scanning the directory.
- Also fix an issue where exceeding scan maximums wouldn't cause embedded
file detection scans to abort. Granted we don't actually want to abort
if max filesize or max recursion depth are exceeded... only if max
scansize, max files, and max scantime are exceeded.
Add 'abort_scan' flag to scan context, to protect against depending on
correct error propagation for fatal conditions. Instead, setting this
flag in the scan context should guarantee that a fatal condition deep in
scan recursion isn't lost which result in more stuff being scanned
instead of aborting. This shouldn't be necessary, but some status codes
like CL_ETIMEOUT never used to be fatal and it's easier to do this than
to verify every parser only returns CL_ETIMEOUT and other "fatal
status codes" in fatal conditions.
- Remove duplicate is_tar() prototype from filestypes.c and include
is_tar.h instead.
- Presently we create the fmap hash when creating the fmap.
This wastes a bit of CPU if the hash is never needed.
Now that we're creating fmap's for all embedded files discovered with
file type recognition scans, this is a much more frequent occurence and
really slows things down.
This commit fixes the issue by only creating fmap hashes as needed.
This should not only resolve the perfomance impact of creating fmap's
for all embedded files, but also should improve performance in general.
- Add allmatch check to the zip parser after the central-header meta
match. That way we don't multiple alerts with the same match except in
allmatch mode. Clean up error handling in the zip parser a tiny bit.
- Fixes to ensure that the scan limits such as scansize, filesize,
recursion depth, # of embedded files, and scantime are always reported
if AlertExceedsMax (--alert-exceeds-max) is enabled.
- Fixed an issue where non-fatal alerts for exceeding scan maximums may
mask signature matches later on. I changed it so these alerts use the
"possibly unwanted" alert-type and thus only alert if no other alerts
were found or if all-match or heuristic-precedence are enabled.
- Added the "Heuristics.Limits.Exceeded.*" events to the JSON metadata
when the --gen-json feature is enabled. These will show up once under
"ParseErrors" the first time a limit is exceeded. In the present
implementation, only one limits-exceeded events will be added, so as to
prevent a malicious or malformed sample from filling the JSON buffer
with millions of events and using a tonne of RAM.
2021-09-11 14:15:21 -07:00
|
|
|
.fmap = ctx->fmap,
|
2018-12-03 12:40:13 -05:00
|
|
|
};
|
|
|
|
struct mspack_system_ex ops_ex;
|
|
|
|
memset(&ops_ex, 0, sizeof(struct mspack_system_ex));
|
|
|
|
ops_ex.ops = mspack_sys_fmap_ops;
|
|
|
|
|
|
|
|
mschm_d = mspack_create_chm_decompressor(&ops_ex.ops);
|
|
|
|
if (!mschm_d) {
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
return CL_EUNPACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mschm_h = mschm_d->open(mschm_d, (char *)&mspack_fmap);
|
|
|
|
if (!mschm_h) {
|
|
|
|
ret = CL_EFORMAT;
|
|
|
|
cli_dbgmsg("%s() failed at %d\n", __func__, __LINE__);
|
|
|
|
goto out_dest;
|
|
|
|
}
|
|
|
|
files = 0;
|
|
|
|
for (mschm_f = mschm_h->files; mschm_f; mschm_f = mschm_f->next) {
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
uint64_t max_size;
|
2018-12-03 12:40:13 -05:00
|
|
|
char *tmp_fname;
|
|
|
|
|
|
|
|
ret = cli_matchmeta(ctx, mschm_f->filename, 0, mschm_f->length,
|
|
|
|
0, files, 0, NULL);
|
|
|
|
if (ret) {
|
|
|
|
if (ret == CL_VIRUS) {
|
|
|
|
virus_num++;
|
|
|
|
if (!SCAN_ALLMATCHES)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->engine->maxscansize) {
|
|
|
|
if (ctx->scansize >= ctx->engine->maxscansize) {
|
|
|
|
ret = CL_CLEAN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix ability to disable filesize limit with libclamav C API
You should be able to disable the maxfilesize limit by setting it to
zero. When "disabled", ClamAV should defer to inherent limitations, which
at this time is INT_MAX - 2 bytes.
This works okay for ClamScan and ClamD because our option parser
converts max-filesize=0 to 4294967295 (4GB). But it is presently broken
for other applications using the libclamav C API, like this:
```c
cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, 0);
```
The limit checks added for cl_scanmap_callback and cl_scanfile_callback
in 0.103.4 and 0.104.1 broke this ability because we forgot to check if
the `maxfilesize > 0` before enforcing it.
This commit adds that guard so you can disable by setting to `0`.
While working on this, I also found that the `max_size` variables in our
libmspack scanner code are using an `off_t` type, which is a SIGNED integer
that may be 32bit width even on some 64bit platforms, or may be a 64bit
width. AND the default `max_size` when `maxfilesize == 0` was being set to
UINT_MAX (0xffffffff), aka `-1` when `off_t` is 32bits.
This commit addresses this related issue by:
- changing the `max_size` to use `uint64_t`, like our other limits.
- verifying that `maxfilesize > 0` before using it.
- checking that using `UINT32_MAX` as a backup will not exceed the
max-scansize in the same way that we do with the maxfilesize.
2021-12-22 13:29:18 -08:00
|
|
|
if (ctx->engine->maxfilesize > 0) {
|
|
|
|
// max filesize has been set
|
|
|
|
if ((ctx->engine->maxscansize > 0) &&
|
|
|
|
(ctx->scansize + ctx->engine->maxfilesize >= ctx->engine->maxscansize)) {
|
|
|
|
// ... but would exceed max scansize, shrink it.
|
|
|
|
max_size = ctx->engine->maxscansize - ctx->scansize;
|
|
|
|
} else {
|
|
|
|
// ... and will work
|
|
|
|
max_size = ctx->engine->maxfilesize;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// max filesize not specified
|
|
|
|
if ((ctx->engine->maxscansize > 0) &&
|
|
|
|
(ctx->scansize + UINT32_MAX >= ctx->engine->maxscansize)) {
|
|
|
|
// ... but UINT32_MAX would exceed max scansize, shrink it.
|
|
|
|
max_size = ctx->engine->maxscansize - ctx->scansize;
|
|
|
|
} else {
|
|
|
|
// ... use UINT32_MAX
|
|
|
|
max_size = UINT32_MAX;
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
ops_ex.max_size = max_size;
|
|
|
|
|
2020-03-19 21:23:54 -04:00
|
|
|
tmp_fname = cli_gentemp(ctx->sub_tmpdir);
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!tmp_fname) {
|
|
|
|
ret = CL_EMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan */
|
|
|
|
ret = mschm_d->extract(mschm_d, mschm_f, tmp_fname);
|
|
|
|
if (ret)
|
|
|
|
/* Failed to extract. Try to scan what is there */
|
|
|
|
cli_dbgmsg("%s() failed to extract %d\n", __func__, ret);
|
|
|
|
|
2020-03-21 14:15:28 -04:00
|
|
|
ret = cli_magic_scan_file(tmp_fname, ctx, mschm_f->filename);
|
2020-03-19 21:23:54 -04:00
|
|
|
if (CL_EOPEN == ret) {
|
|
|
|
ret = CL_CLEAN;
|
|
|
|
} else if (CL_VIRUS == ret) {
|
2018-12-03 12:40:13 -05:00
|
|
|
virus_num++;
|
2020-03-19 21:23:54 -04:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
if (!access(tmp_fname, R_OK) && cli_unlink(tmp_fname)) {
|
|
|
|
free(tmp_fname);
|
|
|
|
ret = CL_EUNLINK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tmp_fname);
|
|
|
|
files++;
|
|
|
|
if (ret == CL_VIRUS && SCAN_ALLMATCHES)
|
|
|
|
continue;
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-24 12:26:04 -04:00
|
|
|
|
|
|
|
out_close:
|
2018-12-03 12:40:13 -05:00
|
|
|
mschm_d->close(mschm_d, mschm_h);
|
2016-03-24 12:26:04 -04:00
|
|
|
out_dest:
|
2018-12-03 12:40:13 -05:00
|
|
|
mspack_destroy_chm_decompressor(mschm_d);
|
|
|
|
if (virus_num)
|
|
|
|
return CL_VIRUS;
|
|
|
|
return ret;
|
2016-03-24 12:26:04 -04:00
|
|
|
}
|