libclamav/blob.c
e3aaff8e
 /*
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
2023340a
  *
  *  Authors: Nigel Horne
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
2023340a
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
2023340a
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
e3aaff8e
 
0e5a0129
 #include <stdio.h>
e3aaff8e
 #include <stdlib.h>
 #include <string.h>
0e5a0129
 #include <errno.h>
55e7a299
 #include <fcntl.h>
2ecbd98a
 #include <sys/types.h>
 #include <sys/stat.h>
0e5a0129
 
288057e9
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h> /* for NAME_MAX */
bc6bbeff
 #endif
0e5a0129
 
288057e9
 #ifdef C_DARWIN
e3aaff8e
 #include <sys/types.h>
 #endif
0e5a0129
 
288057e9
 #ifdef HAVE_UNISTD_H
09d26b44
 #include <unistd.h>
 #endif
 
0f7f7682
 #include "others.h"
e3aaff8e
 #include "mbox.h"
0f7f7682
 #include "matcher.h"
a585329e
 #include "scanners.h"
c7543866
 #include "filetypes.h"
e3aaff8e
 
 #include <assert.h>
 
7cd9337a
 /* Scheduled for rewrite in 0.94 (bb#804). Disabling for now */
86e209d6
 /* #define	MAX_SCAN_SIZE	20*1024	/\* */
 /* 				 * The performance benefit of scanning */
 /* 				 * early disappears on medium and */
 /* 				 * large sized files */
 /* 				 *\/ */
01ff5174
 
288057e9
 static const char *blobGetFilename(const blob *b);
18682e48
 
e3aaff8e
 blob *
 blobCreate(void)
 {
288057e9
 #ifdef CL_DEBUG
     blob *b = (blob *)cli_calloc(1, sizeof(blob));
     if (b)
         b->magic = BLOBCLASS;
     cli_dbgmsg("blobCreate\n");
     return b;
e3aaff8e
 #else
288057e9
     return (blob *)cli_calloc(1, sizeof(blob));
e3aaff8e
 #endif
 }
 
288057e9
 void blobDestroy(blob *b)
e3aaff8e
 {
288057e9
 #ifdef CL_DEBUG
     cli_dbgmsg("blobDestroy %d\n", b->magic);
0ae75a8d
 #else
288057e9
     cli_dbgmsg("blobDestroy\n");
0ae75a8d
 #endif
 
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
e3aaff8e
 
288057e9
     if (b->name)
         free(b->name);
     if (b->data)
         free(b->data);
 #ifdef CL_DEBUG
     b->magic = INVALIDCLASS;
e3aaff8e
 #endif
288057e9
     free(b);
e3aaff8e
 }
 
288057e9
 void blobArrayDestroy(blob *blobList[], int n)
e3aaff8e
 {
288057e9
     assert(blobList != NULL);
 
     while (--n >= 0) {
         cli_dbgmsg("blobArrayDestroy: %d\n", n);
         if (blobList[n]) {
             blobDestroy(blobList[n]);
             blobList[n] = NULL;
         }
     }
e3aaff8e
 }
 
9fe789f8
 /*
  * No longer needed to be growable, so turn into a normal memory area which
  * the caller must free. The passed blob is destroyed
  */
 void *
 blobToMem(blob *b)
 {
288057e9
     void *ret;
9fe789f8
 
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
9fe789f8
 
288057e9
     if (!b->isClosed)
         blobClose(b);
     if (b->name)
         free(b->name);
 #ifdef CL_DEBUG
     b->magic = INVALIDCLASS;
9fe789f8
 #endif
288057e9
     ret = (void *)b->data;
     free(b);
9fe789f8
 
288057e9
     return ret;
9fe789f8
 }
 
78e302e1
 /*ARGSUSED*/
288057e9
 void blobSetFilename(blob *b, const char *dir, const char *filename)
e3aaff8e
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
288057e9
     assert(filename != NULL);
e3aaff8e
 
cd94be7a
     UNUSEDPARAM(dir);
 
288057e9
     cli_dbgmsg("blobSetFilename: %s\n", filename);
bb3fdd1b
 
288057e9
     if (b->name)
         free(b->name);
e3aaff8e
 
288057e9
     b->name = cli_strdup(filename);
e3aaff8e
 
288057e9
     if (b->name)
         sanitiseName(b->name);
e3aaff8e
 }
 
18682e48
 static const char *
e3aaff8e
 blobGetFilename(const blob *b)
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
e3aaff8e
 
288057e9
     return b->name;
e3aaff8e
 }
 
ee1ecd46
 /*
  * Returns <0 for failure
  */
288057e9
 int blobAddData(blob *b, const unsigned char *data, size_t len)
e3aaff8e
 {
288057e9
 #if HAVE_CLI_GETPAGESIZE
     static int pagesize;
     int growth;
767f16ab
 #endif
 
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
288057e9
     assert(data != NULL);
e3aaff8e
 
288057e9
     if (len == 0)
         return 0;
e3aaff8e
 
288057e9
     if (b->isClosed) {
         /*
8ef734d4
 		 * Should be cli_dbgmsg, but I want to see them for now,
 		 * and cli_dbgmsg doesn't support debug levels
 		 */
288057e9
         cli_warnmsg("Reopening closed blob\n");
         b->isClosed = 0;
     }
     /*
767f16ab
 	 * The payoff here is between reducing the number of calls to
 	 * malloc/realloc and not overallocating memory. A lot of machines
 	 * are more tight with memory than one may imagine which is why
 	 * we don't just allocate a *huge* amount and be done with it. Closing
 	 * the blob helps because that reclaims memory. If you know the maximum
 	 * size of a blob before you start adding data, use blobGrow() that's
 	 * the most optimum
 	 */
288057e9
 #if HAVE_CLI_GETPAGESIZE
     if (pagesize == 0) {
         pagesize = cli_getpagesize();
         if (pagesize == 0)
             pagesize = 4096;
     }
     growth = pagesize;
     if (len >= (size_t)pagesize)
         growth = ((len / pagesize) + 1) * pagesize;
 
     /*cli_dbgmsg("blobGrow: b->size %lu, b->len %lu, len %lu, growth = %u\n",
2673dc74
 		b->size, b->len, len, growth);*/
e7aa5e3d
 
288057e9
     if (b->data == NULL) {
         assert(b->len == 0);
         assert(b->size == 0);
767f16ab
 
288057e9
         b->size = growth;
         b->data = cli_malloc(growth);
     } else if (b->size < b->len + (off_t)len) {
         unsigned char *p = cli_realloc(b->data, b->size + growth);
767f16ab
 
288057e9
         if (p == NULL)
             return -1;
767f16ab
 
288057e9
         b->size += growth;
         b->data = p;
     }
767f16ab
 #else
288057e9
     if (b->data == NULL) {
         assert(b->len == 0);
         assert(b->size == 0);
c81143fc
 
288057e9
         b->size = (off_t)len * 4;
         b->data = cli_malloc(b->size);
     } else if (b->size < b->len + (off_t)len) {
         unsigned char *p = cli_realloc(b->data, b->size + (len * 4));
fbb3b454
 
288057e9
         if (p == NULL)
             return -1;
fbb3b454
 
288057e9
         b->size += (off_t)len * 4;
         b->data = p;
     }
767f16ab
 #endif
e3aaff8e
 
288057e9
     if (b->data) {
         memcpy(&b->data[b->len], data, len);
         b->len += (off_t)len;
     }
     return 0;
e3aaff8e
 }
 
2a4b5c6e
 unsigned char *
e3aaff8e
 blobGetData(const blob *b)
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
e3aaff8e
 
288057e9
     if (b->len == 0)
         return NULL;
     return b->data;
e3aaff8e
 }
 
bc6bbeff
 size_t
e3aaff8e
 blobGetDataSize(const blob *b)
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
e3aaff8e
 
288057e9
     return b->len;
e3aaff8e
 }
8ef734d4
 
288057e9
 void blobClose(blob *b)
8ef734d4
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
4b34df42
 
288057e9
     if (b->isClosed) {
         cli_warnmsg("Attempt to close a previously closed blob\n");
         return;
     }
767f16ab
 
288057e9
     /*
e2875303
 	 * Nothing more is going to be added to this blob. If it'll save more
 	 * than a trivial amount (say 64 bytes) of memory, shrink the allocation
 	 */
288057e9
     if ((b->size - b->len) >= 64) {
         if (b->len == 0) { /* Not likely */
             free(b->data);
             b->data = NULL;
             cli_dbgmsg("blobClose: recovered all %lu bytes\n",
                        (unsigned long)b->size);
             b->size = 0;
         } else {
             unsigned char *ptr = cli_realloc(b->data, b->len);
 
             if (ptr == NULL)
                 return;
 
             cli_dbgmsg("blobClose: recovered %lu bytes from %lu\n",
                        (unsigned long)(b->size - b->len),
                        (unsigned long)b->size);
             b->size = b->len;
             b->data = ptr;
         }
     }
     b->isClosed = 1;
8ef734d4
 }
 
 /*
  * Returns 0 if the blobs are the same
  */
288057e9
 int blobcmp(const blob *b1, const blob *b2)
8ef734d4
 {
288057e9
     size_t s1, s2;
8ef734d4
 
288057e9
     assert(b1 != NULL);
     assert(b2 != NULL);
8ef734d4
 
288057e9
     if (b1 == b2)
         return 0;
8ef734d4
 
288057e9
     s1 = blobGetDataSize(b1);
     s2 = blobGetDataSize(b2);
8ef734d4
 
288057e9
     if (s1 != s2)
         return 1;
8ef734d4
 
288057e9
     if ((s1 == 0) && (s2 == 0))
         return 0;
767f16ab
 
288057e9
     return memcmp(blobGetData(b1), blobGetData(b2), s1);
8ef734d4
 }
c81143fc
 
826864d6
 /*
  * Return clamav return code
  */
288057e9
 int blobGrow(blob *b, size_t len)
c81143fc
 {
288057e9
     assert(b != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(b->magic == BLOBCLASS);
694e7882
 #endif
c81143fc
 
288057e9
     if (len == 0)
         return CL_SUCCESS;
c81143fc
 
288057e9
     if (b->isClosed) {
         /*
c81143fc
 		 * Should be cli_dbgmsg, but I want to see them for now,
 		 * and cli_dbgmsg doesn't support debug levels
 		 */
288057e9
         cli_warnmsg("Growing closed blob\n");
         b->isClosed = 0;
     }
     if (b->data == NULL) {
         assert(b->len == 0);
         assert(b->size == 0);
 
         b->data = cli_malloc(len);
         if (b->data)
             b->size = (off_t)len;
     } else {
         unsigned char *ptr = cli_realloc(b->data, b->size + len);
 
         if (ptr) {
             b->size += (off_t)len;
             b->data = ptr;
         }
     }
 
     return (b->data) ? CL_SUCCESS : CL_EMEM;
c81143fc
 }
0e5a0129
 
 fileblob *
 fileblobCreate(void)
 {
288057e9
 #ifdef CL_DEBUG
     fileblob *fb = (fileblob *)cli_calloc(1, sizeof(fileblob));
     if (fb)
         fb->b.magic = BLOBCLASS;
     cli_dbgmsg("blobCreate\n");
     return fb;
0e5a0129
 #else
288057e9
     return (fileblob *)cli_calloc(1, sizeof(fileblob));
0e5a0129
 #endif
 }
 
a585329e
 /*
  * Returns CL_CLEAN or CL_VIRUS. Destroys the fileblob and removes the file
  * if possible
  */
288057e9
 int fileblobScanAndDestroy(fileblob *fb)
a585329e
 {
288057e9
     switch (fileblobScan(fb)) {
         case CL_VIRUS:
             fileblobDestructiveDestroy(fb);
             return CL_VIRUS;
         case CL_BREAK:
             fileblobDestructiveDestroy(fb);
             return CL_CLEAN;
         default:
             fileblobDestroy(fb);
             return CL_CLEAN;
     }
a585329e
 }
 
 /*
  * Destroy the fileblob, and remove the file associated with it
  */
288057e9
 void fileblobDestructiveDestroy(fileblob *fb)
a585329e
 {
288057e9
     if (fb->fp && fb->fullname) {
         fclose(fb->fp);
         cli_dbgmsg("fileblobDestructiveDestroy: %s\n", fb->fullname);
         if (!fb->ctx || !fb->ctx->engine->keeptmp)
             cli_unlink(fb->fullname);
         free(fb->fullname);
         fb->fp       = NULL;
         fb->fullname = NULL;
     }
     if (fb->b.name) {
         free(fb->b.name);
         fb->b.name = NULL;
     }
     fileblobDestroy(fb);
a585329e
 }
 
 /*
  * Destroy the fileblob, and remove the file associated with it if that file is
  * empty
  */
288057e9
 void fileblobDestroy(fileblob *fb)
0e5a0129
 {
288057e9
     assert(fb != NULL);
694e7882
 #ifdef CL_DEBUG
288057e9
     assert(fb->b.magic == BLOBCLASS);
694e7882
 #endif
288057e9
 
     if (fb->b.name && fb->fp) {
         fclose(fb->fp);
         if (fb->fullname) {
             cli_dbgmsg("fileblobDestroy: %s\n", fb->fullname);
             if (!fb->isNotEmpty) {
                 cli_dbgmsg("fileblobDestroy: not saving empty file\n");
                 cli_unlink(fb->fullname);
             }
         }
         free(fb->b.name);
 
         assert(fb->b.data == NULL);
     } else if (fb->b.data) {
         free(fb->b.data);
         if (fb->b.name) {
             cli_errmsg("fileblobDestroy: %s not saved: report to https://bugzilla.clamav.net\n",
                        (fb->fullname) ? fb->fullname : fb->b.name);
             free(fb->b.name);
         } else
             cli_errmsg("fileblobDestroy: file not saved (%lu bytes): report to https://bugzilla.clamav.net\n",
                        (unsigned long)fb->b.len);
     }
     if (fb->fullname)
         free(fb->fullname);
 #ifdef CL_DEBUG
     fb->b.magic = INVALIDCLASS;
bd601898
 #endif
288057e9
     free(fb);
0e5a0129
 }
 
288057e9
 void fileblobPartialSet(fileblob *fb, const char *fullname, const char *arg)
4270f93b
 {
cd94be7a
     UNUSEDPARAM(arg);
 
288057e9
     if (fb->b.name)
         return;
 
     assert(fullname != NULL);
 
     cli_dbgmsg("fileblobPartialSet: saving to %s\n", fullname);
 
     fb->fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, 0600);
     if (fb->fd < 0) {
         cli_errmsg("fileblobPartialSet: unable to create file: %s\n", fullname);
         return;
     }
     fb->fp = fdopen(fb->fd, "wb");
 
     if (fb->fp == NULL) {
         cli_errmsg("fileblobSetFilename: fdopen failed\n");
         close(fb->fd);
         return;
     }
     blobSetFilename(&fb->b, fb->ctx ? fb->ctx->engine->tmpdir : NULL, fullname);
     if (fb->b.data)
         if (fileblobAddData(fb, fb->b.data, fb->b.len) == 0) {
             free(fb->b.data);
             fb->b.data = NULL;
             fb->b.len = fb->b.size = 0;
             fb->isNotEmpty         = 1;
         }
     fb->fullname = cli_strdup(fullname);
4270f93b
 }
 
288057e9
 void fileblobSetFilename(fileblob *fb, const char *dir, const char *filename)
0e5a0129
 {
288057e9
     char *fullname;
0e5a0129
 
288057e9
     if (fb->b.name)
         return;
0e5a0129
 
288057e9
     assert(filename != NULL);
     assert(dir != NULL);
815e712f
 
288057e9
     blobSetFilename(&fb->b, dir, filename);
0e5a0129
 
288057e9
     /*
0e5a0129
 	 * Reload the filename, it may be different from the one we've
 	 * asked for, e.g. '/'s taken out
 	 */
288057e9
     filename = blobGetFilename(&fb->b);
 
     assert(filename != NULL);
 
     if (cli_gentempfd(dir, &fullname, &fb->fd) != CL_SUCCESS) return;
 
     cli_dbgmsg("fileblobSetFilename: file %s saved to %s\n", filename, fullname);
 
     fb->fp = fdopen(fb->fd, "wb");
 
     if (fb->fp == NULL) {
         cli_errmsg("fileblobSetFilename: fdopen failed\n");
         close(fb->fd);
         free(fullname);
         return;
     }
     if (fb->b.data)
         if (fileblobAddData(fb, fb->b.data, fb->b.len) == 0) {
             free(fb->b.data);
             fb->b.data = NULL;
             fb->b.len = fb->b.size = 0;
             fb->isNotEmpty         = 1;
         }
     fb->fullname = fullname;
0e5a0129
 }
 
288057e9
 int fileblobAddData(fileblob *fb, const unsigned char *data, size_t len)
0e5a0129
 {
288057e9
     if (len == 0)
         return 0;
 
     assert(data != NULL);
 
     if (fb->fp) {
 #if defined(MAX_SCAN_SIZE) && (MAX_SCAN_SIZE > 0)
         const cli_ctx *ctx = fb->ctx;
 
         if (fb->isInfected) /* pretend all was written */
             return 0;
         if (ctx) {
             int do_scan = 1;
 
             if (cli_checklimits("fileblobAddData", ctx, fb->bytes_scanned, 0, 0) != CL_CLEAN)
                 do_scan = 0;
 
             if (fb->bytes_scanned > MAX_SCAN_SIZE)
                 do_scan = 0;
             if (do_scan) {
                 if (ctx->scanned)
                     *ctx->scanned += (unsigned long)len / CL_COUNT_PRECISION;
                 fb->bytes_scanned += (unsigned long)len;
 
                 if ((len > 5) && cli_updatelimits(ctx, len) == CL_CLEAN && (cli_scanbuff(data, (unsigned int)len, 0, ctx->virname, ctx->engine, CL_TYPE_BINARY_DATA, NULL) == CL_VIRUS)) {
                     cli_dbgmsg("fileblobAddData: found %s\n", cli_get_last_virus_str(ctx->virname));
                     fb->isInfected = 1;
                 }
             }
         }
01ff5174
 #endif
0f7f7682
 
288057e9
         if (fwrite(data, len, 1, fb->fp) != 1) {
             cli_errmsg("fileblobAddData: Can't write %lu bytes to temporary file %s\n",
                        (unsigned long)len, fb->b.name);
             return -1;
         }
         fb->isNotEmpty = 1;
         return 0;
     }
     return blobAddData(&(fb->b), data, len);
0e5a0129
 }
 
 const char *
 fileblobGetFilename(const fileblob *fb)
 {
288057e9
     return blobGetFilename(&(fb->b));
0e5a0129
 }
bb3fdd1b
 
288057e9
 void fileblobSetCTX(fileblob *fb, cli_ctx *ctx)
a603478f
 {
288057e9
     fb->ctx = ctx;
a603478f
 }
 
a585329e
 /*
  * Performs a full scan on the fileblob, returning ClamAV status:
  *	CL_BREAK means clean
  *	CL_CLEAN means unknown
  *	CL_VIRUS means infected
  */
288057e9
 int fileblobScan(const fileblob *fb)
a585329e
 {
288057e9
     int rc;
     cli_ctx *ctx = fb->ctx;
     STATBUF sb;
     int virus_found = 0;
 
     if (fb->isInfected)
         return CL_VIRUS;
     if (fb->fp == NULL || fb->fullname == NULL) {
         /* shouldn't happen, scan called before fileblobSetFilename */
         cli_warnmsg("fileblobScan, fullname == NULL\n");
         return CL_ENULLARG; /* there is no CL_UNKNOWN */
     }
     if (fb->ctx == NULL) {
         /* fileblobSetCTX hasn't been called */
         cli_dbgmsg("fileblobScan, ctx == NULL\n");
         return CL_CLEAN; /* there is no CL_UNKNOWN */
     }
 
     fflush(fb->fp);
     lseek(fb->fd, 0, SEEK_SET);
     FSTAT(fb->fd, &sb);
     if (cli_matchmeta(fb->ctx, fb->b.name, sb.st_size, sb.st_size, 0, 0, 0, NULL) == CL_VIRUS) {
         if (!SCAN_ALLMATCHES)
             return CL_VIRUS;
         virus_found = 1;
     }
 
     rc = cli_magic_scandesc(fb->fd, fb->fullname, fb->ctx);
     if (rc == CL_VIRUS || virus_found != 0) {
         cli_dbgmsg("%s is infected\n", fb->fullname);
         return CL_VIRUS;
     }
     cli_dbgmsg("%s is clean\n", fb->fullname);
     return CL_BREAK;
a585329e
 }
 
 /*
  * Doesn't perform a full scan just lets the caller know if something suspicious has
  * been seen yet
  */
288057e9
 int fileblobInfected(const fileblob *fb)
a603478f
 {
288057e9
     return fb->isInfected;
a603478f
 }
11b50569
 
 /*
  * Different operating systems allow different characters in their filenames
  * FIXME: What does QNX want? There is no #ifdef C_QNX, but if there were
  * it may be best to treat it like MSDOS
  */
288057e9
 void sanitiseName(char *name)
11b50569
 {
288057e9
     char c;
     while ((c = *name)) {
         if (c != '.' && c != '_' && (c > 'z' || c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a')))
             *name = '_';
         name++;
     }
11b50569
 }