~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to libclamav/readdb.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2007-2012 Sourcefire, Inc.
 
2
 *  Copyright (C) 2007-2014 Cisco Systems, Inc.
3
3
 *
4
4
 *  Authors: Tomasz Kojm
5
5
 *
39
39
#include <zlib.h>
40
40
#include <errno.h>
41
41
 
 
42
#include <openssl/ssl.h>
 
43
#include <openssl/err.h>
 
44
#include "libclamav/crypto.h"
 
45
 
42
46
#include "clamav.h"
43
47
#include "cvd.h"
44
48
#ifdef  HAVE_STRINGS_H
56
60
#include "readdb.h"
57
61
#include "cltypes.h"
58
62
#include "default.h"
59
 
#include "md5.h"
60
 
#include "sha256.h"
61
63
#include "dsig.h"
62
64
#include "asn1.h"
63
65
 
77
79
#include "bytecode_api.h"
78
80
#include "bytecode_priv.h"
79
81
#include "cache.h"
 
82
#include "openioc.h"
80
83
#ifdef CL_THREAD_SAFE
81
84
#  include <pthread.h>
82
85
static pthread_mutex_t cli_ref_mutex = PTHREAD_MUTEX_INITIALIZER;
418
421
                dbio->bufpt = dbio->buf;
419
422
                dbio->size -= bread;
420
423
                dbio->bread += bread;
421
 
                sha256_update(&dbio->sha256ctx, dbio->readpt, bread);
 
424
        if (dbio->hashctx)
 
425
            cl_update_hash(dbio->hashctx, dbio->readpt, bread);
422
426
            }
423
427
            if(dbio->chkonly && dbio->bufpt) {
424
428
                dbio->bufpt = NULL;
475
479
        bs = strlen(buff);
476
480
        dbio->size -= bs;
477
481
        dbio->bread += bs;
478
 
        sha256_update(&dbio->sha256ctx, buff, bs);
 
482
    if (dbio->hashctx)
 
483
        cl_update_hash(dbio->hashctx, buff, bs);
479
484
        return pt;
480
485
    }
481
486
}
482
487
 
483
488
static int cli_chkign(const struct cli_matcher *ignored, const char *signame, const char *entry)
484
489
{
485
 
        const char *md5_expected = NULL;
486
 
        cli_md5_ctx md5ctx;
487
 
        unsigned char digest[16];
 
490
    const char *md5_expected = NULL;
 
491
    unsigned char digest[16];
488
492
 
489
493
    if(!ignored || !signame || !entry)
490
 
        return 0;
 
494
        return 0;
491
495
 
492
496
    if(cli_bm_scanbuff((const unsigned char *) signame, strlen(signame), &md5_expected, NULL, ignored, 0, NULL, NULL,NULL) == CL_VIRUS) {
493
 
        if(md5_expected) {
494
 
            cli_md5_init(&md5ctx);
495
 
            cli_md5_update(&md5ctx, entry, strlen(entry));
496
 
            cli_md5_final(digest, &md5ctx);
497
 
            if(memcmp(digest, (const unsigned char *) md5_expected, 16))
498
 
                return 0;
499
 
        }
500
 
        cli_dbgmsg("Ignoring signature %s\n", signame);
501
 
        return 1;
 
497
        if(md5_expected) {
 
498
            cl_hash_data("md5", entry, strlen(entry), digest, NULL);
 
499
            if(memcmp(digest, (const unsigned char *) md5_expected, 16))
 
500
                return 0;
 
501
        }
 
502
 
 
503
        cli_dbgmsg("Ignoring signature %s\n", signame);
 
504
        return 1;
502
505
    }
503
506
 
504
507
    return 0;
1227
1230
    mpool_free(x.mempool, x.macro_ptids);\
1228
1231
  } while(0);
1229
1232
 
 
1233
/*     0         1        2      3        4        5    ... (max 66)
 
1234
 * VirusName:Attributes:Logic:SubSig1[:SubSig2[:SubSig3 ... ]]
 
1235
 * NOTE: Maximum of 64 subsignatures (last would be token 66)
 
1236
 */
1230
1237
#define LDB_TOKENS 67
1231
1238
static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsigned int options, const char *dbname, unsigned int line, unsigned int *sigs, unsigned bc_idx, const char *buffer_cpy, int *skip)
1232
1239
{
1580
1587
    return CL_SUCCESS;
1581
1588
}
1582
1589
 
 
1590
/*     0       1      2     3        4            5          6      7
 
1591
 * MagicType:Offset:HexSig:Name:RequiredType:DetectedType[:MinFL[:MaxFL]]
 
1592
 */
1583
1593
#define FTM_TOKENS 8
1584
1594
static int cli_loadftm(FILE *fs, struct cl_engine *engine, unsigned int options, unsigned int internal, struct cli_dbio *dbio)
1585
1595
{
1724
1734
        unsigned char hash[32];
1725
1735
        struct cli_dbinfo *last = NULL, *new;
1726
1736
        int ret = CL_SUCCESS, dsig = 0;
1727
 
        SHA256_CTX ctx;
 
1737
    void *ctx;
1728
1738
 
1729
1739
 
1730
1740
    if(!dbio) {
1731
1741
        cli_errmsg("cli_loadinfo: .info files can only be loaded from within database container files\n");
1732
1742
        return CL_EMALFDB;
1733
1743
    }
1734
 
    sha256_init(&ctx);
 
1744
 
 
1745
    ctx = cl_hash_init("sha256");
 
1746
    if (!(ctx))
 
1747
        return CL_EMALFDB;
 
1748
 
1735
1749
    while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
1736
1750
        line++;
1737
1751
        if(!(options & CL_DB_UNSIGNED) && !strncmp(buffer, "DSIG:", 5)) {
1738
1752
            dsig = 1;
1739
 
            sha256_final(&ctx, hash);
 
1753
            cl_finish_hash(ctx, hash);
1740
1754
            if(cli_versig2(hash, buffer + 5, INFO_NSTR, INFO_ESTR) != CL_SUCCESS) {
1741
1755
                cli_errmsg("cli_loadinfo: Incorrect digital signature\n");
1742
1756
                ret = CL_EMALFDB;
1754
1768
            buffer[len + 1] = 0;
1755
1769
        }
1756
1770
    }
1757
 
        sha256_update(&ctx, buffer, strlen(buffer));
 
1771
        cl_update_hash(ctx, buffer, strlen(buffer));
1758
1772
        cli_chomp(buffer);
1759
1773
        if(!strncmp("ClamAV-VDB:", buffer, 11)) {
1760
1774
            if(engine->dbinfo) { /* shouldn't be initialized at this point */
2588
2602
    return 0;
2589
2603
}
2590
2604
 
 
2605
static int cli_loadopenioc(FILE *fs, const char *dbname, struct cl_engine *engine, unsigned int options)
 
2606
{
 
2607
    int rc;
 
2608
    rc = openioc_parse(dbname, fileno(fs), engine, options);
 
2609
    if (rc != CL_SUCCESS)
 
2610
        return CL_EMALFDB;
 
2611
    return rc;
 
2612
}
 
2613
 
2591
2614
static int cli_loaddbdir(const char *dirname, struct cl_engine *engine, unsigned int *signo, unsigned int options);
2592
2615
 
2593
2616
int cli_load(const char *filename, struct cl_engine *engine, unsigned int *signo, unsigned int options, struct cli_dbio *dbio)
2716
2739
        ret = cli_loadcdb(fs, engine, signo, options, dbio);
2717
2740
    } else if(cli_strbcasestr(dbname, ".cat")) {
2718
2741
        ret = cli_loadmscat(fs, dbname, engine, options, dbio);
 
2742
    } else if(cli_strbcasestr(dbname, ".ioc")) {
 
2743
        ret = cli_loadopenioc(fs, dbname, engine, options);
2719
2744
    } else {
2720
2745
        cli_dbgmsg("cli_load: unknown extension - assuming old database format\n");
2721
2746
        ret = cli_loaddb(fs, engine, signo, options, dbio, dbname);
2922
2947
    }
2923
2948
    closedir(dd);
2924
2949
    if(ret == CL_EOPEN)
2925
 
        cli_errmsg("cli_loaddb(): No supported database files found in %s\n", dirname);
 
2950
        cli_errmsg("cli_loaddbdir(): No supported database files found in %s\n", dirname);
2926
2951
 
2927
2952
    return ret;
2928
2953
}
3237
3262
        return CL_SUCCESS;
3238
3263
    }
3239
3264
 
 
3265
    if (engine->cb_stats_submit)
 
3266
        engine->cb_stats_submit(engine, engine->stats_data);
 
3267
 
3240
3268
#ifdef CL_THREAD_SAFE
 
3269
    if (engine->stats_data) {
 
3270
        cli_intel_t *intel = (cli_intel_t *)(engine->stats_data);
 
3271
 
 
3272
        pthread_mutex_destroy(&(intel->mutex));
 
3273
    }
 
3274
 
3241
3275
    pthread_mutex_unlock(&cli_ref_mutex);
3242
3276
#endif
 
3277
    if (engine->stats_data)
 
3278
        free(engine->stats_data);
 
3279
 
3243
3280
    if(engine->root) {
3244
3281
        for(i = 0; i < CLI_MTARGETS; i++) {
3245
3282
            if((root = engine->root[i])) {
3297
3334
        mpool_free(engine->mempool, pt);
3298
3335
    }
3299
3336
 
3300
 
    if(engine->dconf->bytecode & BYTECODE_ENGINE_MASK) {
3301
 
        if (engine->bcs.all_bcs)
3302
 
            for(i=0;i<engine->bcs.count;i++)
3303
 
                cli_bytecode_destroy(&engine->bcs.all_bcs[i]);
3304
 
        cli_bytecode_done(&engine->bcs);
3305
 
        free(engine->bcs.all_bcs);
3306
 
        for (i=0;i<_BC_LAST_HOOK - _BC_START_HOOKS;i++) {
3307
 
            free (engine->hooks[i]);
3308
 
        }
 
3337
    if(engine->dconf) {
 
3338
        if(engine->dconf->bytecode & BYTECODE_ENGINE_MASK) {
 
3339
            if (engine->bcs.all_bcs)
 
3340
                for(i=0;i<engine->bcs.count;i++)
 
3341
                    cli_bytecode_destroy(&engine->bcs.all_bcs[i]);
 
3342
            cli_bytecode_done(&engine->bcs);
 
3343
            free(engine->bcs.all_bcs);
 
3344
            for (i=0;i<_BC_LAST_HOOK - _BC_START_HOOKS;i++) {
 
3345
                free (engine->hooks[i]);
 
3346
            }
 
3347
        }
 
3348
 
 
3349
        if(engine->dconf->phishing & PHISHING_CONF_ENGINE)
 
3350
            phishing_done(engine);
 
3351
 
 
3352
        mpool_free(engine->mempool, engine->dconf);
3309
3353
    }
3310
 
    if(engine->dconf->phishing & PHISHING_CONF_ENGINE)
3311
 
        phishing_done(engine);
3312
 
    if(engine->dconf)
3313
 
        mpool_free(engine->mempool, engine->dconf);
3314
3354
 
3315
3355
    if(engine->pua_cats)
3316
3356
        mpool_free(engine->mempool, engine->pua_cats);