~ubuntu-branches/ubuntu/wily/clamav/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/0007-fix-ssize_t-size_t-off_t-printf-modifier.patch/libclamav/matcher.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Sebastian Andrzej Siewior, Andreas Cadhalpun, Scott Kitterman, Javier Fernández-Sanguino
  • Date: 2015-01-28 00:25:13 UTC
  • mfrom: (0.48.14 sid)
  • Revision ID: package-import@ubuntu.com-20150128002513-lil2oi74cooy4lzr
Tags: 0.98.6+dfsg-1
[ Sebastian Andrzej Siewior ]
* update "fix-ssize_t-size_t-off_t-printf-modifier", include of misc.h was
  missing but was pulled in via the systemd patch.
* Don't leak return codes from libmspack to clamav API. (Closes: #774686).

[ Andreas Cadhalpun ]
* Add patch to avoid emitting incremental progress messages when not
  outputting to a terminal. (Closes: #767350)
* Update lintian-overrides for unused-file-paragraph-in-dep5-copyright.
* clamav-base.postinst: always chown /var/log/clamav and /var/lib/clamav
  to clamav:clamav, not only on fresh installations. (Closes: #775400)
* Adapt the clamav-daemon and clamav-freshclam logrotate scripts,
  so that they correctly work under systemd.
* Move the PidFile variable from the clamd/freshclam configuration files
  to the init scripts. This makes the init scripts more robust against
  misconfiguration and avoids error messages with systemd. (Closes: #767353)
* debian/copyright: drop files from Files-Excluded only present in github
  tarballs
* Drop Workaround-a-bug-in-libc-on-Hurd.patch, because hurd got fixed.
  (see #752237)
* debian/rules: Remove useless --with-system-tommath --without-included-ltdl
  configure options.

[ Scott Kitterman ]
* Stop stripping llvm when repacking the tarball as the system llvm on some
  releases is too old to use
* New upstream bugfix release
  - Library shared object revisions.
  - Includes a patch from Sebastian Andrzej Siewior making ClamAV pid files
    compatible with systemd.
  - Fix a heap out of bounds condition with crafted Yoda's crypter files.
    This issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted mew packer files. This
    issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted upx packer files. This
    issue was discovered by Kevin Szkudlapski of Quarkslab.
  - Fix a heap out of bounds condition with crafted upack packer files. This
    issue was discovered by Sebastian Andrzej Siewior. CVE-2014-9328.
  - Compensate a crash due to incorrect compiler optimization when handling
    crafted petite packer files. This issue was discovered by Sebastian
    Andrzej Siewior.
* Update lintian override for embedded zlib to match new so version

[ Javier Fernández-Sanguino ]
* Updated Spanish Debconf template translation (Closes: #773563)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2007-2013 Sourcefire, Inc.
 
3
 *
 
4
 *  Authors: Tomasz Kojm
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License version 2 as
 
8
 *  published by the Free Software Foundation.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
18
 *  MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#if HAVE_CONFIG_H
 
22
#include "clamav-config.h"
 
23
#endif
 
24
 
 
25
#include <string.h>
 
26
#include <ctype.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#ifdef  HAVE_UNISTD_H
 
30
#include <unistd.h>
 
31
#endif
 
32
 
 
33
#include "clamav.h"
 
34
#include "others.h"
 
35
#include "matcher-ac.h"
 
36
#include "matcher-bm.h"
 
37
#include "filetypes.h"
 
38
#include "matcher.h"
 
39
#include "pe.h"
 
40
#include "elf.h"
 
41
#include "execs.h"
 
42
#include "special.h"
 
43
#include "scanners.h"
 
44
#include "str.h"
 
45
#include "cltypes.h"
 
46
#include "default.h"
 
47
#include "macho.h"
 
48
#include "fmap.h"
 
49
#include "pe_icons.h"
 
50
#include "regex/regex.h"
 
51
#include "filtering.h"
 
52
#include "perflogging.h"
 
53
#include "bytecode_priv.h"
 
54
#include "bytecode_api_impl.h"
 
55
 
 
56
#ifdef CLI_PERF_LOGGING
 
57
 
 
58
static inline void PERF_LOG_FILTER(int32_t pos, int32_t length, int8_t trie)
 
59
{
 
60
    cli_perf_log_add(RAW_BYTES_SCANNED, length);
 
61
    cli_perf_log_add(FILTER_BYTES_SCANNED, length - pos);
 
62
    cli_perf_log_count2(TRIE_SCANNED, trie, length - pos);
 
63
}
 
64
 
 
65
static inline int PERF_LOG_TRIES(int8_t acmode, int8_t bm_called, int32_t length)
 
66
{
 
67
    if (bm_called)
 
68
        cli_perf_log_add(BM_SCANNED, length);
 
69
    if (acmode)
 
70
        cli_perf_log_add(AC_SCANNED, length);
 
71
    return 0;
 
72
}
 
73
 
 
74
#else
 
75
static inline void PERF_LOG_FILTER(int32_t pos, uint32_t length, int8_t trie) {
 
76
    UNUSEDPARAM(pos);
 
77
    UNUSEDPARAM(length);
 
78
    UNUSEDPARAM(trie);
 
79
}
 
80
 
 
81
static inline int PERF_LOG_TRIES(int8_t acmode, int8_t bm_called, int32_t length) {
 
82
    UNUSEDPARAM(acmode);
 
83
    UNUSEDPARAM(bm_called);
 
84
    UNUSEDPARAM(length);
 
85
 
 
86
    return 0;
 
87
}
 
88
#endif
 
89
 
 
90
static inline int matcher_run(const struct cli_matcher *root,
 
91
                              const unsigned char *buffer, uint32_t length,
 
92
                              const char **virname, struct cli_ac_data *mdata,
 
93
                              uint32_t offset,
 
94
                              const struct cli_target_info *tinfo,
 
95
                              cli_file_t ftype,
 
96
                              struct cli_matched_type **ftoffset,
 
97
                              unsigned int acmode,
 
98
                              struct cli_ac_result **acres,
 
99
                              fmap_t *map,
 
100
                              struct cli_bm_off *offdata,
 
101
                              uint32_t *viroffset,
 
102
                              cli_ctx *ctx)
 
103
{
 
104
    int ret;
 
105
    int32_t pos = 0;
 
106
    struct filter_match_info info;
 
107
    uint32_t orig_length, orig_offset;
 
108
    const unsigned char* orig_buffer;
 
109
    unsigned int viruses_found = 0;
 
110
 
 
111
    UNUSEDPARAM(map);
 
112
 
 
113
    if (root->filter) {
 
114
        if(filter_search_ext(root->filter, buffer, length, &info) == -1) {
 
115
            /*  for safety always scan last maxpatlen bytes */
 
116
            pos = length - root->maxpatlen - 1;
 
117
            if (pos < 0) pos = 0;
 
118
            PERF_LOG_FILTER(pos, length, root->type);
 
119
        } else {
 
120
            /* must not cut buffer for 64[4-4]6161, because we must be able to check
 
121
             * 64! */
 
122
            pos = info.first_match - root->maxpatlen - 1;
 
123
            if (pos < 0) pos = 0;
 
124
            PERF_LOG_FILTER(pos, length, root->type);
 
125
        }
 
126
    } else {
 
127
        PERF_LOG_FILTER(0, length, root->type);
 
128
    }
 
129
 
 
130
    orig_length = length;
 
131
    orig_buffer = buffer;
 
132
    orig_offset = offset;
 
133
    length -= pos;
 
134
    buffer += pos;
 
135
    offset += pos;
 
136
    if (!root->ac_only) {
 
137
        PERF_LOG_TRIES(0, 1, length);
 
138
        if (root->bm_offmode) {
 
139
            /* Don't use prefiltering for BM offset mode, since BM keeps tracks
 
140
             * of offsets itself, and doesn't work if we skip chunks of input
 
141
             * data */
 
142
            ret = cli_bm_scanbuff(orig_buffer, orig_length, virname, NULL, root, orig_offset, tinfo, offdata, viroffset);
 
143
        } else {
 
144
            ret = cli_bm_scanbuff(buffer, length, virname, NULL, root, offset, tinfo, offdata, viroffset);
 
145
        }
 
146
        if (ret == CL_VIRUS) {
 
147
            if (ctx) {
 
148
                cli_append_virus(ctx, *virname);
 
149
                if (SCAN_ALL)
 
150
                    viruses_found++;
 
151
                else
 
152
                    return ret;
 
153
            }
 
154
        }
 
155
    }
 
156
    PERF_LOG_TRIES(acmode, 0, length);
 
157
    ret = cli_ac_scanbuff(buffer, length, virname, NULL, acres, root, mdata, offset, ftype, ftoffset, acmode, ctx);
 
158
 
 
159
    if (ctx && !SCAN_ALL && ret == CL_VIRUS)
 
160
        cli_append_virus(ctx, *virname);
 
161
    if (ctx && SCAN_ALL && viruses_found)
 
162
        return CL_VIRUS;
 
163
 
 
164
    return ret;
 
165
}
 
166
 
 
167
int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, cli_ctx *ctx, cli_file_t ftype, struct cli_ac_data **acdata)
 
168
{
 
169
        int ret = CL_CLEAN;
 
170
        unsigned int i = 0, j = 0, viruses_found = 0;
 
171
        struct cli_ac_data mdata;
 
172
        struct cli_matcher *groot, *troot = NULL;
 
173
        const char *virname = NULL;
 
174
        const struct cl_engine *engine=ctx->engine;
 
175
 
 
176
    if(!engine) {
 
177
        cli_errmsg("cli_scanbuff: engine == NULL\n");
 
178
        return CL_ENULLARG;
 
179
    }
 
180
 
 
181
    groot = engine->root[0]; /* generic signatures */
 
182
 
 
183
    if(ftype) {
 
184
        for(i = 1; i < CLI_MTARGETS; i++) {
 
185
            for (j = 0; j < cli_mtargets[i].target_count; ++j) {
 
186
                if(cli_mtargets[i].target[j] == ftype) {
 
187
                    troot = ctx->engine->root[i];
 
188
                    break;
 
189
                }
 
190
            }
 
191
            if (troot) break;
 
192
        }
 
193
    }
 
194
 
 
195
    if(troot) {
 
196
 
 
197
        if(!acdata && (ret = cli_ac_initdata(&mdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
 
198
            return ret;
 
199
 
 
200
        ret = matcher_run(troot, buffer, length, &virname, acdata ? (acdata[0]): (&mdata), offset, NULL, ftype, NULL, AC_SCAN_VIR, NULL, *ctx->fmap, NULL, NULL, ctx);
 
201
 
 
202
        if(!acdata)
 
203
            cli_ac_freedata(&mdata);
 
204
 
 
205
        if(ret == CL_EMEM)
 
206
            return ret;
 
207
        if(ret == CL_VIRUS) {
 
208
            viruses_found = 1;
 
209
            if(ctx && !SCAN_ALL) {
 
210
                return ret;
 
211
            }
 
212
        }
 
213
    }
 
214
 
 
215
    virname = NULL;
 
216
 
 
217
    if(!acdata && (ret = cli_ac_initdata(&mdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
 
218
        return ret;
 
219
 
 
220
    ret = matcher_run(groot, buffer, length, &virname, acdata ? (acdata[1]): (&mdata), offset, NULL, ftype, NULL, AC_SCAN_VIR, NULL, *ctx->fmap, NULL, NULL, ctx);
 
221
 
 
222
    if(!acdata)
 
223
        cli_ac_freedata(&mdata);
 
224
 
 
225
    if(viruses_found)
 
226
        return CL_VIRUS;
 
227
    return ret;
 
228
}
 
229
 
 
230
/*
 
231
 * offdata[0]: type
 
232
 * offdata[1]: offset value
 
233
 * offdata[2]: max shift
 
234
 * offdata[3]: section number
 
235
 */
 
236
int cli_caloff(const char *offstr, const struct cli_target_info *info, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max)
 
237
{
 
238
        char offcpy[65];
 
239
        unsigned int n, val;
 
240
        char *pt;
 
241
 
 
242
    if(!info) { /* decode offset string */
 
243
        if(!offstr) {
 
244
            cli_errmsg("cli_caloff: offstr == NULL\n");
 
245
            return CL_ENULLARG;
 
246
        }
 
247
 
 
248
        if(!strcmp(offstr, "*")) {
 
249
            offdata[0] = *offset_max = *offset_min = CLI_OFF_ANY;
 
250
            return CL_SUCCESS;
 
251
        }
 
252
 
 
253
        if(strlen(offstr) > 64) {
 
254
            cli_errmsg("cli_caloff: Offset string too long\n");
 
255
            return CL_EMALFDB;
 
256
        }
 
257
        strcpy(offcpy, offstr);
 
258
 
 
259
        if((pt = strchr(offcpy, ','))) {
 
260
            if(!cli_isnumber(pt + 1)) {
 
261
                cli_errmsg("cli_caloff: Invalid offset shift value\n");
 
262
                return CL_EMALFDB;
 
263
            }
 
264
            offdata[2] = atoi(pt + 1);
 
265
            *pt = 0;
 
266
        } else {
 
267
            offdata[2] = 0;
 
268
        }
 
269
 
 
270
        *offset_max = *offset_min = CLI_OFF_NONE;
 
271
 
 
272
        if(!strncmp(offcpy, "EP+", 3) || !strncmp(offcpy, "EP-", 3)) {
 
273
            if(offcpy[2] == '+')
 
274
                offdata[0] = CLI_OFF_EP_PLUS;
 
275
            else
 
276
                offdata[0] = CLI_OFF_EP_MINUS;
 
277
 
 
278
            if(!cli_isnumber(&offcpy[3])) {
 
279
                cli_errmsg("cli_caloff: Invalid offset value\n");
 
280
                return CL_EMALFDB;
 
281
            }
 
282
            offdata[1] = atoi(&offcpy[3]);
 
283
 
 
284
        } else if(offcpy[0] == 'S') {
 
285
            if(offcpy[1] == 'E') {
 
286
                if(!cli_isnumber(&offcpy[2])) {
 
287
                    cli_errmsg("cli_caloff: Invalid section number\n");
 
288
                    return CL_EMALFDB;
 
289
                }
 
290
                offdata[0] = CLI_OFF_SE;
 
291
                offdata[3] = atoi(&offcpy[2]);
 
292
 
 
293
            } else if(!strncmp(offstr, "SL+", 3)) {
 
294
                offdata[0] = CLI_OFF_SL_PLUS;
 
295
                if(!cli_isnumber(&offcpy[3])) {
 
296
                    cli_errmsg("cli_caloff: Invalid offset value\n");
 
297
                    return CL_EMALFDB;
 
298
                }
 
299
                offdata[1] = atoi(&offcpy[3]);
 
300
 
 
301
            } else if(sscanf(offcpy, "S%u+%u", &n, &val) == 2) {
 
302
                offdata[0] = CLI_OFF_SX_PLUS;
 
303
                offdata[1] = val;
 
304
                offdata[3] = n;
 
305
            } else {
 
306
                cli_errmsg("cli_caloff: Invalid offset string\n");
 
307
                return CL_EMALFDB;
 
308
            }
 
309
 
 
310
        } else if(!strncmp(offcpy, "EOF-", 4)) {
 
311
            offdata[0] = CLI_OFF_EOF_MINUS;
 
312
            if(!cli_isnumber(&offcpy[4])) {
 
313
                cli_errmsg("cli_caloff: Invalid offset value\n");
 
314
                return CL_EMALFDB;
 
315
            }
 
316
            offdata[1] = atoi(&offcpy[4]);
 
317
        } else if(!strncmp(offcpy, "VI", 2)) {
 
318
            /* versioninfo */
 
319
            offdata[0] = CLI_OFF_VERSION;
 
320
        } else if (strchr(offcpy, '$')) {
 
321
            if (sscanf(offcpy, "$%u$", &n) != 1) {
 
322
                cli_errmsg("cli_caloff: Invalid macro($) in offset: %s\n", offcpy);
 
323
                return CL_EMALFDB;
 
324
            }
 
325
            if (n >= 32) {
 
326
                cli_errmsg("cli_caloff: at most 32 macro groups supported\n");
 
327
                return CL_EMALFDB;
 
328
            }
 
329
            offdata[0] = CLI_OFF_MACRO;
 
330
            offdata[1] = n;
 
331
        } else {
 
332
            offdata[0] = CLI_OFF_ABSOLUTE;
 
333
            if(!cli_isnumber(offcpy)) {
 
334
                cli_errmsg("cli_caloff: Invalid offset value\n");
 
335
                return CL_EMALFDB;
 
336
            }
 
337
            *offset_min = offdata[1] = atoi(offcpy);
 
338
            *offset_max = *offset_min + offdata[2];
 
339
        }
 
340
 
 
341
        if(offdata[0] != CLI_OFF_ANY && offdata[0] != CLI_OFF_ABSOLUTE &&
 
342
           offdata[0] != CLI_OFF_EOF_MINUS && offdata[0] != CLI_OFF_MACRO) {
 
343
            if(target != 1 && target != 6 && target != 9) {
 
344
                cli_errmsg("cli_caloff: Invalid offset type for target %u\n", target);
 
345
                return CL_EMALFDB;
 
346
            }
 
347
        }
 
348
 
 
349
    } else {
 
350
        /* calculate relative offsets */
 
351
        *offset_min = CLI_OFF_NONE;
 
352
        if(offset_max)
 
353
            *offset_max = CLI_OFF_NONE;
 
354
        if(info->status == -1)
 
355
            return CL_SUCCESS;
 
356
 
 
357
        switch(offdata[0]) {
 
358
            case CLI_OFF_EOF_MINUS:
 
359
                *offset_min = info->fsize - offdata[1];
 
360
                break;
 
361
 
 
362
            case CLI_OFF_EP_PLUS:
 
363
                *offset_min = info->exeinfo.ep + offdata[1];
 
364
                break;
 
365
 
 
366
            case CLI_OFF_EP_MINUS:
 
367
                *offset_min = info->exeinfo.ep - offdata[1];
 
368
                break;
 
369
 
 
370
            case CLI_OFF_SL_PLUS:
 
371
                *offset_min = info->exeinfo.section[info->exeinfo.nsections - 1].raw + offdata[1];
 
372
                break;
 
373
 
 
374
            case CLI_OFF_SX_PLUS:
 
375
                if(offdata[3] >= info->exeinfo.nsections)
 
376
                    *offset_min = CLI_OFF_NONE;
 
377
                else
 
378
                    *offset_min = info->exeinfo.section[offdata[3]].raw + offdata[1];
 
379
                break;
 
380
 
 
381
            case CLI_OFF_SE:
 
382
                if(offdata[3] >= info->exeinfo.nsections) {
 
383
                    *offset_min = CLI_OFF_NONE;
 
384
                } else {
 
385
                    *offset_min = info->exeinfo.section[offdata[3]].raw;
 
386
            if (offset_max)
 
387
                        *offset_max = *offset_min + info->exeinfo.section[offdata[3]].rsz + offdata[2];
 
388
                }
 
389
                break;
 
390
 
 
391
            case CLI_OFF_VERSION:
 
392
        if (offset_max)
 
393
                *offset_min = *offset_max = CLI_OFF_ANY;
 
394
                break;
 
395
            default:
 
396
                cli_errmsg("cli_caloff: Not a relative offset (type: %u)\n", offdata[0]);
 
397
                return CL_EARG;
 
398
        }
 
399
 
 
400
        if(offset_max && *offset_max == CLI_OFF_NONE && *offset_min != CLI_OFF_NONE)
 
401
            *offset_max = *offset_min + offdata[2];
 
402
    }
 
403
 
 
404
    return CL_SUCCESS;
 
405
}
 
406
 
 
407
void cli_targetinfo(struct cli_target_info *info, unsigned int target, fmap_t *map)
 
408
{
 
409
        int (*einfo)(fmap_t *, struct cli_exe_info *) = NULL;
 
410
 
 
411
 
 
412
    memset(info, 0, sizeof(struct cli_target_info));
 
413
    info->fsize = map->len;
 
414
    cli_hashset_init_noalloc(&info->exeinfo.vinfo);
 
415
 
 
416
    if(target == 1)
 
417
        einfo = cli_peheader;
 
418
    else if(target == 6)
 
419
        einfo = cli_elfheader;
 
420
    else if(target == 9)
 
421
        einfo = cli_machoheader;
 
422
    else return;
 
423
 
 
424
    if(einfo(map, &info->exeinfo))
 
425
        info->status = -1;
 
426
    else
 
427
        info->status = 1;
 
428
}
 
429
 
 
430
int cli_checkfp(unsigned char *digest, size_t size, cli_ctx *ctx)
 
431
{
 
432
    char md5[33];
 
433
    unsigned int i;
 
434
    const char *virname=NULL;
 
435
    fmap_t *map;
 
436
    const char *ptr;
 
437
    uint8_t shash1[SHA1_HASH_SIZE*2+1];
 
438
    uint8_t shash256[SHA256_HASH_SIZE*2+1];
 
439
    int have_sha1, have_sha256, do_dsig_check = 1;
 
440
    stats_section_t sections;
 
441
 
 
442
    if(cli_hm_scan(digest, size, &virname, ctx->engine->hm_fp, CLI_HASH_MD5) == CL_VIRUS) {
 
443
        cli_dbgmsg("cli_checkfp(md5): Found false positive detection (fp sig: %s), size: %d\n", virname, (int)size);
 
444
        return CL_CLEAN;
 
445
    }
 
446
    else if(cli_hm_scan_wild(digest, &virname, ctx->engine->hm_fp, CLI_HASH_MD5) == CL_VIRUS) {
 
447
        cli_dbgmsg("cli_checkfp(md5): Found false positive detection (fp sig: %s), size: *\n", virname);
 
448
        return CL_CLEAN;
 
449
    }
 
450
 
 
451
    if(cli_debug_flag || ctx->engine->cb_hash) {
 
452
        for(i = 0; i < 16; i++)
 
453
            sprintf(md5 + i * 2, "%02x", digest[i]);
 
454
        md5[32] = 0;
 
455
        cli_dbgmsg("FP SIGNATURE: %s:%u:%s\n", md5, (unsigned int) size,
 
456
               cli_get_last_virus(ctx) ? cli_get_last_virus(ctx) : "Name");
 
457
    }
 
458
 
 
459
    if(cli_get_last_virus(ctx))
 
460
        do_dsig_check = strncmp("W32S.", cli_get_last_virus(ctx), 5);
 
461
 
 
462
    map = *ctx->fmap;
 
463
    have_sha1 = cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA1, size)
 
464
     || cli_hm_have_wild(ctx->engine->hm_fp, CLI_HASH_SHA1)
 
465
     || (cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA1, 1) && do_dsig_check);
 
466
    have_sha256 = cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA256, size)
 
467
     || cli_hm_have_wild(ctx->engine->hm_fp, CLI_HASH_SHA256);
 
468
    if(have_sha1 || have_sha256) {
 
469
        if((ptr = fmap_need_off_once(map, 0, size))) {
 
470
            if(have_sha1) {
 
471
                cl_sha1(ptr, size, &shash1[SHA1_HASH_SIZE], NULL);
 
472
 
 
473
                if(cli_hm_scan(&shash1[SHA1_HASH_SIZE], size, &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
 
474
                    cli_dbgmsg("cli_checkfp(sha1): Found false positive detection (fp sig: %s)\n", virname);
 
475
                    return CL_CLEAN;
 
476
                }
 
477
                if(cli_hm_scan_wild(&shash1[SHA1_HASH_SIZE], &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
 
478
                    cli_dbgmsg("cli_checkfp(sha1): Found false positive detection (fp sig: %s)\n", virname);
 
479
                    return CL_CLEAN;
 
480
                }
 
481
                if(do_dsig_check && cli_hm_scan(&shash1[SHA1_HASH_SIZE], 1, &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
 
482
                    cli_dbgmsg("cli_checkfp(sha1): Found false positive detection via catalog file\n");
 
483
                    return CL_CLEAN;
 
484
                }
 
485
            }
 
486
 
 
487
            if(have_sha256) {
 
488
                cl_sha256(ptr, size, &shash256[SHA256_HASH_SIZE], NULL);
 
489
 
 
490
                if(cli_hm_scan(&shash256[SHA256_HASH_SIZE], size, &virname, ctx->engine->hm_fp, CLI_HASH_SHA256) == CL_VIRUS) {
 
491
                    cli_dbgmsg("cli_checkfp(sha256): Found false positive detection (fp sig: %s)\n", virname);
 
492
                    return CL_CLEAN;
 
493
                }
 
494
                if(cli_hm_scan_wild(&shash256[SHA256_HASH_SIZE], &virname, ctx->engine->hm_fp, CLI_HASH_SHA256) == CL_VIRUS) {
 
495
                    cli_dbgmsg("cli_checkfp(sha256): Found false positive detection (fp sig: %s)\n", virname);
 
496
                    return CL_CLEAN;
 
497
                }
 
498
            }
 
499
        }
 
500
    }
 
501
 
 
502
#ifdef HAVE__INTERNAL__SHA_COLLECT
 
503
    if((ctx->options & CL_SCAN_INTERNAL_COLLECT_SHA) && ctx->sha_collect>0) {
 
504
        if((ptr = fmap_need_off_once(map, 0, size))) {
 
505
            if(!have_sha256)
 
506
                cl_sha256(ptr, size, shash256+SHA256_HASH_SIZE, NULL);
 
507
 
 
508
            for(i=0; i<SHA256_HASH_SIZE; i++)
 
509
                sprintf((char *)shash256+i*2, "%02x", shash256[SHA256_HASH_SIZE+i]);
 
510
 
 
511
            if(!have_sha1)
 
512
                cl_sha1(ptr, size, shash1+SHA1_HASH_SIZE);
 
513
 
 
514
            for(i=0; i<SHA1_HASH_SIZE; i++)
 
515
                sprintf((char *)shash1+i*2, "%02x", shash1[SHA1_HASH_SIZE+i]);
 
516
 
 
517
            cli_errmsg("COLLECT:%s:%s:%u:%s:%s\n", shash256, shash1, size, cli_get_last_virus(ctx), ctx->entry_filename);
 
518
        } else
 
519
            cli_errmsg("can't compute sha\n!");
 
520
 
 
521
        ctx->sha_collect = -1;
 
522
    }
 
523
#endif
 
524
 
 
525
    memset(&sections, 0x00, sizeof(stats_section_t));
 
526
    if(do_dsig_check || ctx->engine->cb_stats_add_sample) {
 
527
        uint32_t flags = (do_dsig_check ? CL_CHECKFP_PE_FLAG_AUTHENTICODE : 0);
 
528
        if (!(ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_PE_STATS) && !(ctx->engine->dconf->stats & (DCONF_STATS_DISABLED | DCONF_STATS_PE_SECTION_DISABLED)))
 
529
            flags |= CL_CHECKFP_PE_FLAG_STATS;
 
530
 
 
531
        switch(cli_checkfp_pe(ctx, shash1, &sections, flags)) {
 
532
        case CL_CLEAN:
 
533
            cli_dbgmsg("cli_checkfp(pe): PE file whitelisted due to valid embedded digital signature\n");
 
534
            return CL_CLEAN;
 
535
        case CL_VIRUS:
 
536
            if(cli_hm_scan(shash1, 2, &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
 
537
                cli_dbgmsg("cli_checkfp(pe): PE file whitelisted by catalog file\n");
 
538
 
 
539
                return CL_CLEAN;
 
540
            }
 
541
        }
 
542
    }
 
543
 
 
544
    if (ctx->engine->cb_hash)
 
545
        ctx->engine->cb_hash(fmap_fd(*ctx->fmap), size, (const unsigned char *)md5, cli_get_last_virus(ctx), ctx->cb_ctx);
 
546
 
 
547
    if (ctx->engine->cb_stats_add_sample)
 
548
        ctx->engine->cb_stats_add_sample(cli_get_last_virus(ctx), digest, size, &sections, ctx->engine->stats_data);
 
549
 
 
550
    if (sections.sections)
 
551
        free(sections.sections);
 
552
 
 
553
    return CL_VIRUS;
 
554
}
 
555
 
 
556
static int matchicon(cli_ctx *ctx, struct cli_exe_info *exeinfo, const char *grp1, const char *grp2)
 
557
{
 
558
    icon_groupset iconset;
 
559
 
 
560
    if(!ctx ||
 
561
       !ctx->engine ||
 
562
       !ctx->engine->iconcheck ||
 
563
       !ctx->engine->iconcheck->group_counts[0] ||
 
564
       !ctx->engine->iconcheck->group_counts[1] ||
 
565
       !exeinfo->res_addr
 
566
    ) return CL_CLEAN;
 
567
 
 
568
    if (!(ctx->dconf->pe & PE_CONF_MATCHICON))
 
569
        return CL_CLEAN;
 
570
 
 
571
    cli_icongroupset_init(&iconset);
 
572
    cli_icongroupset_add(grp1 ? grp1 : "*", &iconset, 0, ctx);
 
573
    cli_icongroupset_add(grp2 ? grp2 : "*", &iconset, 1, ctx);
 
574
    return cli_scanicon(&iconset, exeinfo->res_addr, ctx, exeinfo->section, exeinfo->nsections, exeinfo->hdr_size);
 
575
}
 
576
 
 
577
int32_t cli_bcapi_matchicon(struct cli_bc_ctx *ctx , const uint8_t* grp1, int32_t grp1len,
 
578
                            const uint8_t* grp2, int32_t grp2len)
 
579
{
 
580
    int ret;
 
581
    char group1[128], group2[128];
 
582
    const char **oldvirname;
 
583
    struct cli_exe_info info;
 
584
 
 
585
    if (!ctx->hooks.pedata->ep) {
 
586
        cli_dbgmsg("bytecode: matchicon only works with PE files\n");
 
587
        return -1;
 
588
    }
 
589
    if ((size_t) grp1len > sizeof(group1)-1 ||
 
590
        (size_t) grp2len > sizeof(group2)-1)
 
591
        return -1;
 
592
    oldvirname = ((cli_ctx*)ctx->ctx)->virname;
 
593
    ((cli_ctx*)ctx->ctx)->virname = NULL;
 
594
    memcpy(group1, grp1, grp1len);
 
595
    memcpy(group2, grp2, grp2len);
 
596
    group1[grp1len] = 0;
 
597
    group2[grp2len] = 0;
 
598
    memset(&info, 0, sizeof(info));
 
599
    if (ctx->bc->kind == BC_PE_UNPACKER || ctx->bc->kind == BC_PE_ALL) {
 
600
        if(le16_to_host(ctx->hooks.pedata->file_hdr.Characteristics) & 0x2000 ||
 
601
           !ctx->hooks.pedata->dirs[2].Size)
 
602
            info.res_addr = 0;
 
603
        else
 
604
            info.res_addr = le32_to_host(ctx->hooks.pedata->dirs[2].VirtualAddress);
 
605
    } else
 
606
        info.res_addr = ctx->resaddr; /* from target_info */
 
607
    info.section = (struct cli_exe_section*)ctx->sections;
 
608
    info.nsections = ctx->hooks.pedata->nsections;
 
609
    info.hdr_size = ctx->hooks.pedata->hdr_size;
 
610
    cli_dbgmsg("bytecode matchicon %s %s\n", group1, group2);
 
611
    ret = matchicon(ctx->ctx, &info, group1[0] ? group1 : NULL,
 
612
                    group2[0] ? group2 : NULL);
 
613
    ((cli_ctx*)ctx->ctx)->virname = oldvirname;
 
614
    return ret;
 
615
}
 
616
 
 
617
 
 
618
int cli_scandesc(int desc, cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode, struct cli_ac_result **acres)
 
619
{
 
620
    int ret = CL_EMEM, empty;
 
621
    fmap_t *map = *ctx->fmap;
 
622
 
 
623
    if((*ctx->fmap = fmap_check_empty(desc, 0, 0, &empty))) {
 
624
        ret = cli_fmap_scandesc(ctx, ftype, ftonly, ftoffset, acmode, acres, NULL);
 
625
        map->dont_cache_flag = (*ctx->fmap)->dont_cache_flag;
 
626
        funmap(*ctx->fmap);
 
627
    }
 
628
    *ctx->fmap = map;
 
629
    if(empty)
 
630
        return CL_CLEAN;
 
631
    return ret;
 
632
}
 
633
 
 
634
int cli_lsig_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *acdata, struct cli_target_info *target_info, const char *hash)
 
635
{
 
636
        unsigned int i, evalcnt;
 
637
        uint64_t evalids;
 
638
        fmap_t *map = *ctx->fmap;
 
639
        unsigned int viruses_found = 0;
 
640
 
 
641
    for(i = 0; i < root->ac_lsigs; i++) {
 
642
        evalcnt = 0;
 
643
        evalids = 0;
 
644
        cli_ac_chkmacro(root, acdata, i);
 
645
        if(cli_ac_chklsig(root->ac_lsigtable[i]->logic, root->ac_lsigtable[i]->logic + strlen(root->ac_lsigtable[i]->logic), acdata->lsigcnt[i], &evalcnt, &evalids, 0) == 1) {
 
646
            if(root->ac_lsigtable[i]->tdb.container && root->ac_lsigtable[i]->tdb.container[0] != ctx->container_type)
 
647
                continue;
 
648
            if(root->ac_lsigtable[i]->tdb.filesize && (root->ac_lsigtable[i]->tdb.filesize[0] > map->len || root->ac_lsigtable[i]->tdb.filesize[1] < map->len))
 
649
                continue;
 
650
 
 
651
            if(root->ac_lsigtable[i]->tdb.ep || root->ac_lsigtable[i]->tdb.nos) {
 
652
                if(!target_info || target_info->status != 1)
 
653
                    continue;
 
654
                if(root->ac_lsigtable[i]->tdb.ep && (root->ac_lsigtable[i]->tdb.ep[0] > target_info->exeinfo.ep || root->ac_lsigtable[i]->tdb.ep[1] < target_info->exeinfo.ep))
 
655
                    continue;
 
656
                if(root->ac_lsigtable[i]->tdb.nos && (root->ac_lsigtable[i]->tdb.nos[0] > target_info->exeinfo.nsections || root->ac_lsigtable[i]->tdb.nos[1] < target_info->exeinfo.nsections))
 
657
                    continue;
 
658
            }
 
659
 
 
660
            if(hash && root->ac_lsigtable[i]->tdb.handlertype) {
 
661
                if(memcmp(ctx->handlertype_hash, hash, 16)) {
 
662
                    ctx->recursion++;
 
663
                    memcpy(ctx->handlertype_hash, hash, 16);
 
664
                    if(cli_magic_scandesc_type(ctx, root->ac_lsigtable[i]->tdb.handlertype[0]) == CL_VIRUS) {
 
665
                        ctx->recursion--;
 
666
                        if (SCAN_ALL) {
 
667
                            viruses_found++;
 
668
                            continue;
 
669
                        }
 
670
                        return CL_VIRUS;
 
671
                    }
 
672
                    ctx->recursion--;
 
673
                    continue;
 
674
                }
 
675
            }
 
676
 
 
677
            if(root->ac_lsigtable[i]->tdb.icongrp1 || root->ac_lsigtable[i]->tdb.icongrp2) {
 
678
                if(!target_info || target_info->status != 1)
 
679
                    continue;
 
680
                if(matchicon(ctx, &target_info->exeinfo, root->ac_lsigtable[i]->tdb.icongrp1, root->ac_lsigtable[i]->tdb.icongrp2) == CL_VIRUS) {
 
681
                    if(!root->ac_lsigtable[i]->bc_idx) {
 
682
                        cli_append_virus(ctx, root->ac_lsigtable[i]->virname);
 
683
                        if (SCAN_ALL) {
 
684
                            viruses_found++;
 
685
                            continue;
 
686
                        }
 
687
                        return CL_VIRUS;
 
688
                    } else if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, acdata->lsigcnt[i], acdata->lsigsuboff_first[i], map) == CL_VIRUS) {
 
689
                        if (SCAN_ALL) {
 
690
                            viruses_found++;
 
691
                            continue;
 
692
                        }
 
693
                        return CL_VIRUS;
 
694
                    }
 
695
                }
 
696
                continue;
 
697
            }
 
698
            if(!root->ac_lsigtable[i]->bc_idx) {
 
699
                cli_append_virus(ctx, root->ac_lsigtable[i]->virname);
 
700
                if (SCAN_ALL) {
 
701
                    viruses_found++;
 
702
                    continue;
 
703
                }
 
704
                return CL_VIRUS;
 
705
            }
 
706
            if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, acdata->lsigcnt[i], acdata->lsigsuboff_first[i], map) == CL_VIRUS) {
 
707
                if (SCAN_ALL) {
 
708
                    viruses_found++;
 
709
                    continue;
 
710
                }
 
711
                return CL_VIRUS;
 
712
            }
 
713
        }
 
714
    }
 
715
    if (SCAN_ALL && viruses_found)
 
716
        return CL_VIRUS;
 
717
    return CL_CLEAN;
 
718
}
 
719
 
 
720
int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode, struct cli_ac_result **acres, unsigned char *refhash)
 
721
{
 
722
    const unsigned char *buff;
 
723
    int ret = CL_CLEAN, type = CL_CLEAN, bytes, compute_hash[CLI_HASH_AVAIL_TYPES];
 
724
    unsigned int i = 0, j = 0, bm_offmode = 0;
 
725
    uint32_t maxpatlen, offset = 0;
 
726
    struct cli_ac_data gdata, tdata;
 
727
    struct cli_bm_off toff;
 
728
    unsigned char digest[CLI_HASH_AVAIL_TYPES][32];
 
729
    struct cli_matcher *groot = NULL, *troot = NULL;
 
730
    struct cli_target_info info;
 
731
    fmap_t *map = *ctx->fmap;
 
732
    struct cli_matcher *hdb, *fp;
 
733
    const char *virname = NULL;
 
734
    uint32_t viroffset = 0;
 
735
    uint32_t viruses_found = 0;
 
736
    void *md5ctx, *sha1ctx, *sha256ctx;
 
737
 
 
738
    if(!ctx->engine) {
 
739
        cli_errmsg("cli_scandesc: engine == NULL\n");
 
740
        return CL_ENULLARG;
 
741
    }
 
742
 
 
743
    md5ctx = cl_hash_init("md5");
 
744
    if (!(md5ctx))
 
745
        return CL_EMEM;
 
746
 
 
747
    sha1ctx = cl_hash_init("sha1");
 
748
    if (!(sha1ctx)) {
 
749
        cl_hash_destroy(md5ctx);
 
750
        return CL_EMEM;
 
751
    }
 
752
 
 
753
    sha256ctx = cl_hash_init("sha256");
 
754
    if (!(sha256ctx)) {
 
755
        cl_hash_destroy(md5ctx);
 
756
        cl_hash_destroy(sha1ctx);
 
757
        return CL_EMEM;
 
758
    }
 
759
 
 
760
    if(!ftonly)
 
761
        groot = ctx->engine->root[0]; /* generic signatures */
 
762
 
 
763
    if(ftype) {
 
764
        for(i = 1; i < CLI_MTARGETS; i++) {
 
765
            for (j = 0; j < cli_mtargets[i].target_count; ++j) {
 
766
                if(cli_mtargets[i].target[j] == ftype) {
 
767
                    troot = ctx->engine->root[i];
 
768
                    break;
 
769
                }
 
770
            }
 
771
            if (troot) break;
 
772
        }
 
773
    }
 
774
 
 
775
    if(ftonly) {
 
776
        if(!troot) {
 
777
            cl_hash_destroy(md5ctx);
 
778
            cl_hash_destroy(sha1ctx);
 
779
            cl_hash_destroy(sha256ctx);
 
780
            return CL_CLEAN;
 
781
        }
 
782
 
 
783
        maxpatlen = troot->maxpatlen;
 
784
    } else {
 
785
        if(troot)
 
786
            maxpatlen = MAX(troot->maxpatlen, groot->maxpatlen);
 
787
        else
 
788
            maxpatlen = groot->maxpatlen;
 
789
    }
 
790
 
 
791
    cli_targetinfo(&info, i, map);
 
792
 
 
793
    if(!ftonly) {
 
794
        if((ret = cli_ac_initdata(&gdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)) || (ret = cli_ac_caloff(groot, &gdata, &info))) {
 
795
            if(info.exeinfo.section)
 
796
                free(info.exeinfo.section);
 
797
 
 
798
            cli_hashset_destroy(&info.exeinfo.vinfo);
 
799
            cl_hash_destroy(md5ctx);
 
800
            cl_hash_destroy(sha1ctx);
 
801
            cl_hash_destroy(sha256ctx);
 
802
            return ret;
 
803
        }
 
804
    }
 
805
 
 
806
    if(troot) {
 
807
        if((ret = cli_ac_initdata(&tdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)) || (ret = cli_ac_caloff(troot, &tdata, &info))) {
 
808
            if(!ftonly)
 
809
                cli_ac_freedata(&gdata);
 
810
            if(info.exeinfo.section)
 
811
                free(info.exeinfo.section);
 
812
 
 
813
            cli_hashset_destroy(&info.exeinfo.vinfo);
 
814
            cl_hash_destroy(md5ctx);
 
815
            cl_hash_destroy(sha1ctx);
 
816
            cl_hash_destroy(sha256ctx);
 
817
            return ret;
 
818
        }
 
819
        if(troot->bm_offmode) {
 
820
            if(map->len >= CLI_DEFAULT_BM_OFFMODE_FSIZE) {
 
821
                if((ret = cli_bm_initoff(troot, &toff, &info))) {
 
822
                    if(!ftonly)
 
823
                        cli_ac_freedata(&gdata);
 
824
 
 
825
                    cli_ac_freedata(&tdata);
 
826
                    if(info.exeinfo.section)
 
827
                        free(info.exeinfo.section);
 
828
 
 
829
                    cli_hashset_destroy(&info.exeinfo.vinfo);
 
830
                    cl_hash_destroy(md5ctx);
 
831
                    cl_hash_destroy(sha1ctx);
 
832
                    cl_hash_destroy(sha256ctx);
 
833
                    return ret;
 
834
                }
 
835
 
 
836
                bm_offmode = 1;
 
837
            }
 
838
        }
 
839
    }
 
840
 
 
841
    hdb = ctx->engine->hm_hdb;
 
842
    fp = ctx->engine->hm_fp;
 
843
 
 
844
    if(!ftonly && hdb) {
 
845
        if(!refhash) {
 
846
            if(cli_hm_have_size(hdb, CLI_HASH_MD5, map->len) || cli_hm_have_size(fp, CLI_HASH_MD5, map->len)) {
 
847
                compute_hash[CLI_HASH_MD5] = 1;
 
848
            } else {
 
849
                compute_hash[CLI_HASH_MD5] = 0;
 
850
            }
 
851
        } else {
 
852
            compute_hash[CLI_HASH_MD5] = 0;
 
853
            memcpy(digest[CLI_HASH_MD5], refhash, 16);
 
854
        }
 
855
 
 
856
        if(cli_hm_have_size(hdb, CLI_HASH_SHA1, map->len) || cli_hm_have_wild(hdb, CLI_HASH_SHA1)
 
857
            || cli_hm_have_size(fp, CLI_HASH_SHA1, map->len) || cli_hm_have_wild(fp, CLI_HASH_SHA1) ) {
 
858
            compute_hash[CLI_HASH_SHA1] = 1;
 
859
        } else {
 
860
            compute_hash[CLI_HASH_SHA1] = 0;
 
861
        }
 
862
 
 
863
        if(cli_hm_have_size(hdb, CLI_HASH_SHA256, map->len) || cli_hm_have_wild(hdb, CLI_HASH_SHA256)
 
864
            || cli_hm_have_size(fp, CLI_HASH_SHA256, map->len) || cli_hm_have_wild(fp, CLI_HASH_SHA256)) {
 
865
            compute_hash[CLI_HASH_SHA256] = 1;
 
866
        } else {
 
867
            compute_hash[CLI_HASH_SHA256] = 0;
 
868
        }
 
869
    }
 
870
 
 
871
    while(offset < map->len) {
 
872
        bytes = MIN(map->len - offset, SCANBUFF);
 
873
        if(!(buff = fmap_need_off_once(map, offset, bytes)))
 
874
            break;
 
875
        if(ctx->scanned)
 
876
            *ctx->scanned += bytes / CL_COUNT_PRECISION;
 
877
 
 
878
        if(troot) {
 
879
                virname = NULL;
 
880
                viroffset = 0;
 
881
                ret = matcher_run(troot, buff, bytes, &virname, &tdata, offset, &info, ftype, ftoffset, acmode, acres, map, bm_offmode ? &toff : NULL, &viroffset, ctx);
 
882
 
 
883
            if (virname) {
 
884
                /* virname already appended by matcher_run */
 
885
                viruses_found = 1;
 
886
            }
 
887
            if((ret == CL_VIRUS && !SCAN_ALL) || ret == CL_EMEM) {
 
888
                if(!ftonly)
 
889
                    cli_ac_freedata(&gdata);
 
890
 
 
891
                cli_ac_freedata(&tdata);
 
892
                if(bm_offmode)
 
893
                    cli_bm_freeoff(&toff);
 
894
 
 
895
                if(info.exeinfo.section)
 
896
                    free(info.exeinfo.section);
 
897
 
 
898
                cli_hashset_destroy(&info.exeinfo.vinfo);
 
899
                cl_hash_destroy(md5ctx);
 
900
                cl_hash_destroy(sha1ctx);
 
901
                cl_hash_destroy(sha256ctx);
 
902
                return ret;
 
903
            }
 
904
        }
 
905
 
 
906
        if(!ftonly) {
 
907
            virname = NULL;
 
908
            viroffset = 0;
 
909
            ret = matcher_run(groot, buff, bytes, &virname, &gdata, offset, &info, ftype, ftoffset, acmode, acres, map, NULL, &viroffset, ctx);
 
910
 
 
911
            if (virname) {
 
912
                /* virname already appended by matcher_run */
 
913
                viruses_found = 1;
 
914
            }
 
915
            if((ret == CL_VIRUS && !SCAN_ALL) || ret == CL_EMEM) {
 
916
                cli_ac_freedata(&gdata);
 
917
                if(troot) {
 
918
                    cli_ac_freedata(&tdata);
 
919
                    if(bm_offmode)
 
920
                        cli_bm_freeoff(&toff);
 
921
                }
 
922
 
 
923
                if(info.exeinfo.section)
 
924
                    free(info.exeinfo.section);
 
925
 
 
926
                cli_hashset_destroy(&info.exeinfo.vinfo);
 
927
                cl_hash_destroy(md5ctx);
 
928
                cl_hash_destroy(sha1ctx);
 
929
                cl_hash_destroy(sha256ctx);
 
930
                return ret;
 
931
            } else if((acmode & AC_SCAN_FT) && ret >= CL_TYPENO) {
 
932
                if(ret > type)
 
933
                    type = ret;
 
934
            }
 
935
 
 
936
            if(hdb) {
 
937
                const void *data = buff + maxpatlen * (offset!=0);
 
938
                uint32_t data_len = bytes - maxpatlen * (offset!=0);
 
939
 
 
940
                if(compute_hash[CLI_HASH_MD5])
 
941
                    cl_update_hash(md5ctx, (void *)data, data_len);
 
942
                if(compute_hash[CLI_HASH_SHA1])
 
943
                    cl_update_hash(sha1ctx, (void *)data, data_len);
 
944
                if(compute_hash[CLI_HASH_SHA256])
 
945
                    cl_update_hash(sha256ctx, (void *)data, data_len);
 
946
            }
 
947
        }
 
948
 
 
949
        if(SCAN_ALL && viroffset) {
 
950
            offset = viroffset;
 
951
            continue;
 
952
        }
 
953
 
 
954
        if(bytes < SCANBUFF)
 
955
            break;
 
956
 
 
957
        offset += bytes - maxpatlen;
 
958
    }
 
959
 
 
960
    if(!ftonly && hdb) {
 
961
        enum CLI_HASH_TYPE hashtype, hashtype2;
 
962
 
 
963
        if(compute_hash[CLI_HASH_MD5]) {
 
964
            cl_finish_hash(md5ctx, digest[CLI_HASH_MD5]);
 
965
            md5ctx = NULL;
 
966
        }
 
967
        if(refhash)
 
968
            compute_hash[CLI_HASH_MD5] = 1;
 
969
        if(compute_hash[CLI_HASH_SHA1]) {
 
970
            cl_finish_hash(sha1ctx, digest[CLI_HASH_SHA1]);
 
971
            sha1ctx = NULL;
 
972
        }
 
973
        if(compute_hash[CLI_HASH_SHA256]) {
 
974
            cl_finish_hash(sha256ctx, digest[CLI_HASH_SHA256]);
 
975
            sha256ctx = NULL;
 
976
        }
 
977
 
 
978
        virname = NULL;
 
979
        for(hashtype = CLI_HASH_MD5; hashtype < CLI_HASH_AVAIL_TYPES; hashtype++) {
 
980
            const char * virname_w = NULL;
 
981
            int found = 0;
 
982
 
 
983
            /* If no hash, skip to next type */
 
984
            if(!compute_hash[hashtype])
 
985
                continue;
 
986
 
 
987
            /* Do hash scan */
 
988
            if((ret = cli_hm_scan(digest[hashtype], map->len, &virname, hdb, hashtype)) == CL_VIRUS) {
 
989
                found += 1;
 
990
            }
 
991
            if(!found || SCAN_ALL) {
 
992
                if ((ret = cli_hm_scan_wild(digest[hashtype], &virname_w, hdb, hashtype)) == CL_VIRUS)
 
993
                    found += 2;
 
994
            }
 
995
 
 
996
            /* If found, do immediate hash-only FP check */
 
997
            if (found && fp) {
 
998
                for(hashtype2 = CLI_HASH_MD5; hashtype2 < CLI_HASH_AVAIL_TYPES; hashtype2++) {
 
999
                    if(!compute_hash[hashtype2])
 
1000
                        continue;
 
1001
                    if(cli_hm_scan(digest[hashtype2], map->len, NULL, fp, hashtype2) == CL_VIRUS) {
 
1002
                        found = 0;
 
1003
                        ret = CL_CLEAN;
 
1004
                        break;
 
1005
                    }
 
1006
                    else if(cli_hm_scan_wild(digest[hashtype2], NULL, fp, hashtype2) == CL_VIRUS) {
 
1007
                        found = 0;
 
1008
                        ret = CL_CLEAN;
 
1009
                        break;
 
1010
                    }
 
1011
                }
 
1012
            }
 
1013
 
 
1014
            /* If matched size-based hash ... */
 
1015
            if (found % 2) {
 
1016
                viruses_found = 1;
 
1017
                cli_append_virus(ctx, virname);
 
1018
                if (!SCAN_ALL)
 
1019
                    break;
 
1020
                virname = NULL;
 
1021
            }
 
1022
            /* If matched size-agnostic hash ... */
 
1023
            if (found > 1) {
 
1024
                viruses_found = 1;
 
1025
                cli_append_virus(ctx, virname_w);
 
1026
 
 
1027
                if (!SCAN_ALL)
 
1028
                    break;
 
1029
            }
 
1030
        }
 
1031
    }
 
1032
 
 
1033
    cl_hash_destroy(md5ctx);
 
1034
    cl_hash_destroy(sha1ctx);
 
1035
    cl_hash_destroy(sha256ctx);
 
1036
 
 
1037
    if(troot) {
 
1038
        if(ret != CL_VIRUS || SCAN_ALL)
 
1039
            ret = cli_lsig_eval(ctx, troot, &tdata, &info, (const char *)refhash);
 
1040
        if (ret == CL_VIRUS)
 
1041
            viruses_found++;
 
1042
 
 
1043
        cli_ac_freedata(&tdata);
 
1044
        if(bm_offmode)
 
1045
            cli_bm_freeoff(&toff);
 
1046
    }
 
1047
 
 
1048
    if(groot) {
 
1049
        if(ret != CL_VIRUS || SCAN_ALL)
 
1050
            ret = cli_lsig_eval(ctx, groot, &gdata, &info, (const char *)refhash);
 
1051
        cli_ac_freedata(&gdata);
 
1052
    }
 
1053
 
 
1054
    if(info.exeinfo.section)
 
1055
        free(info.exeinfo.section);
 
1056
 
 
1057
    cli_hashset_destroy(&info.exeinfo.vinfo);
 
1058
 
 
1059
    if (SCAN_ALL && viruses_found)
 
1060
        return CL_VIRUS;
 
1061
    if(ret == CL_VIRUS)
 
1062
        return CL_VIRUS;
 
1063
 
 
1064
    return (acmode & AC_SCAN_FT) ? type : CL_CLEAN;
 
1065
}
 
1066
 
 
1067
int cli_matchmeta(cli_ctx *ctx, const char *fname, size_t fsizec, size_t fsizer, int encrypted, unsigned int filepos, int res1, void *res2)
 
1068
{
 
1069
        const struct cli_cdb *cdb;
 
1070
        unsigned int viruses_found = 0;
 
1071
 
 
1072
    cli_dbgmsg("CDBNAME:%s:%lu:%s:%lu:%lu:%d:%u:%u:%p\n",
 
1073
               cli_ftname(ctx->container_type), fsizec, fname, fsizec, fsizer, encrypted, filepos, res1, res2);
 
1074
 
 
1075
    if (ctx->engine && ctx->engine->cb_meta)
 
1076
        if (ctx->engine->cb_meta(cli_ftname(ctx->container_type), fsizec, fname, fsizer, encrypted, filepos, ctx->cb_ctx) == CL_VIRUS) {
 
1077
            cli_dbgmsg("inner file blacklisted by callback: %s\n", fname);
 
1078
 
 
1079
            cli_append_virus(ctx, "Detected.By.Callback");
 
1080
            viruses_found++;
 
1081
            if(!SCAN_ALL)
 
1082
                return CL_VIRUS;
 
1083
        }
 
1084
 
 
1085
    if(!ctx->engine || !(cdb = ctx->engine->cdb))
 
1086
        return CL_CLEAN;
 
1087
 
 
1088
    do {
 
1089
        if(cdb->ctype != CL_TYPE_ANY && cdb->ctype != ctx->container_type)
 
1090
            continue;
 
1091
 
 
1092
        if(cdb->encrypted != 2 && cdb->encrypted != encrypted)
 
1093
            continue;
 
1094
 
 
1095
        if(cdb->res1 && (cdb->ctype == CL_TYPE_ZIP || cdb->ctype == CL_TYPE_RAR) && cdb->res1 != res1)
 
1096
            continue;
 
1097
 
 
1098
#define CDBRANGE(field, val)                                                \
 
1099
        if(field[0] != CLI_OFF_ANY) {                                       \
 
1100
            if(field[0] == field[1] && field[0] != val)                     \
 
1101
                continue;                                                   \
 
1102
            else if(field[0] != field[1] && ((field[0] && field[0] > val) ||\
 
1103
              (field[1] && field[1] < val)))                                \
 
1104
                continue;                                                   \
 
1105
        }
 
1106
 
 
1107
        CDBRANGE(cdb->csize, ctx->container_size);
 
1108
        CDBRANGE(cdb->fsizec, fsizec);
 
1109
        CDBRANGE(cdb->fsizer, fsizer);
 
1110
        CDBRANGE(cdb->filepos, filepos);
 
1111
 
 
1112
        if(cdb->name.re_magic && (!fname || cli_regexec(&cdb->name, fname, 0, NULL, 0) == REG_NOMATCH))
 
1113
            continue;
 
1114
 
 
1115
        cli_append_virus(ctx, cdb->virname);
 
1116
        viruses_found++;
 
1117
        if(!SCAN_ALL)
 
1118
            return CL_VIRUS;
 
1119
 
 
1120
    } while((cdb = cdb->next));
 
1121
 
 
1122
    if (SCAN_ALL && viruses_found)
 
1123
        return CL_VIRUS;
 
1124
    return CL_CLEAN;
 
1125
}