~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/matcher-ncore.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2006 Tomasz Kojm <tkojm@clamav.net>
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
17
 *  MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#if HAVE_CONFIG_H
 
21
#include "clamav-config.h"
 
22
#endif
 
23
 
 
24
#ifdef HAVE_NCORE
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#ifdef  HAVE_UNISTD_H
 
29
#include <unistd.h>
 
30
#endif
 
31
#include <string.h>
 
32
#include <sys/types.h>
 
33
#include <sys/stat.h>
 
34
#include <fcntl.h>
 
35
#include <ctype.h>
 
36
#ifdef HAVE_NCORE
 
37
#include <dlfcn.h>
 
38
#endif
 
39
 
 
40
#include "clamav.h"
 
41
#include "matcher.h"
 
42
#include "cltypes.h"
 
43
#include "md5.h"
 
44
#include "readdb.h"
 
45
#include "str.h"
 
46
#include "matcher-ncore.h"
 
47
 
 
48
#define HWBUFFSIZE 32768
 
49
 
 
50
/* Globals */
 
51
static void *g_ncore_dllhandle = 0;
 
52
static const char *g_ncore_dllpath = "/usr/lib/libsn_sigscan.so";
 
53
 
 
54
/* Function pointer types */
 
55
typedef int (*sn_sigscan_initdb_t)(void **);
 
56
typedef int (*sn_sigscan_loaddb_t)(void *dbhandle, const char *filename,
 
57
        int devicenum, unsigned int *count);
 
58
typedef int (*sn_sigscan_load2dbs_t)(void *dbhandle, const char *baseFilename,
 
59
        const char *incrFilename, int devicenum, unsigned int *count);
 
60
typedef int (*sn_sigscan_closedb_t)(void *dbhandle);
 
61
typedef int (*sn_sigscan_createstream_t)(void *dbhandle,
 
62
        const uint32_t *dbMaskData, unsigned int dbMaskWords,
 
63
        void **streamhandle);
 
64
typedef int (*sn_sigscan_writestream_t)(void *streamhandle, const char *buffer,
 
65
        unsigned int len);
 
66
typedef int (*sn_sigscan_closestream_t)(void *streamhandle,
 
67
        void **resulthandle);
 
68
typedef int (*sn_sigscan_resultcount_t)(void *resulthandle);
 
69
typedef int (*sn_sigscan_resultget_name_t)(void *resulthandle,
 
70
        unsigned int index, const char **matchname);
 
71
typedef int (*sn_sigscan_resultget_startoffset_t)(void *resulthandle,
 
72
        unsigned int index, unsigned long long *startoffset);
 
73
typedef int (*sn_sigscan_resultget_endoffset_t)(void *resulthandle,
 
74
        unsigned int index, unsigned long long *endoffset);
 
75
typedef int (*sn_sigscan_resultget_targettype_t)(void *resulthandle,
 
76
        unsigned int index, int *targettype);
 
77
typedef int (*sn_sigscan_resultget_offsetstring_t)(void *resulthandle,
 
78
        unsigned int index, const char **offsetstring);
 
79
typedef int (*sn_sigscan_resultget_extradata_t)(void *resulthandle,
 
80
        unsigned int index, const char **optionalsigdata);
 
81
typedef int (*sn_sigscan_resultfree_t)(void *resulthandle);
 
82
typedef void (*sn_sigscan_error_function_t)(const char *msg);
 
83
typedef int (*sn_sigscan_seterrorlogger_t)(sn_sigscan_error_function_t errfn);
 
84
 
 
85
/* Function pointer values */
 
86
sn_sigscan_initdb_t sn_sigscan_initdb_f = 0;
 
87
sn_sigscan_loaddb_t sn_sigscan_loaddb_f = 0;
 
88
sn_sigscan_load2dbs_t sn_sigscan_load2dbs_f = 0;
 
89
sn_sigscan_closedb_t sn_sigscan_closedb_f = 0;
 
90
sn_sigscan_createstream_t sn_sigscan_createstream_f = 0;
 
91
sn_sigscan_writestream_t sn_sigscan_writestream_f = 0;
 
92
sn_sigscan_closestream_t sn_sigscan_closestream_f = 0;
 
93
sn_sigscan_resultcount_t sn_sigscan_resultcount_f = 0;
 
94
sn_sigscan_resultget_name_t sn_sigscan_resultget_name_f = 0;
 
95
sn_sigscan_resultget_startoffset_t sn_sigscan_resultget_startoffset_f = 0;
 
96
sn_sigscan_resultget_endoffset_t sn_sigscan_resultget_endoffset_f = 0;
 
97
sn_sigscan_resultget_targettype_t sn_sigscan_resultget_targettype_f = 0;
 
98
sn_sigscan_resultget_offsetstring_t sn_sigscan_resultget_offsetstring_f = 0;
 
99
sn_sigscan_resultget_extradata_t sn_sigscan_resultget_extradata_f = 0;
 
100
sn_sigscan_resultfree_t sn_sigscan_resultfree_f = 0;
 
101
sn_sigscan_seterrorlogger_t sn_sigscan_seterrorlogger_f = 0;
 
102
 
 
103
static int cli_ncore_dlinit()
 
104
{
 
105
    if(access(g_ncore_dllpath, R_OK) == -1) {
 
106
        cli_dbgmsg("cli_ncore_dlinit: Can't access %s\n", g_ncore_dllpath);
 
107
        return CL_ENCINIT;
 
108
    }
 
109
 
 
110
    g_ncore_dllhandle = dlopen(g_ncore_dllpath, RTLD_NOW | RTLD_LOCAL);
 
111
    if(!g_ncore_dllhandle) {
 
112
        cli_dbgmsg("cli_ncore_dlinit: dlopen() failed for %s\n", g_ncore_dllpath);
 
113
        return CL_ENCINIT;
 
114
    }
 
115
 
 
116
    /* get the symbols */
 
117
    sn_sigscan_initdb_f = (sn_sigscan_initdb_t)dlsym(g_ncore_dllhandle, "sn_sigscan_initdb");
 
118
    sn_sigscan_loaddb_f = (sn_sigscan_loaddb_t)dlsym(g_ncore_dllhandle, "sn_sigscan_loaddb");
 
119
    sn_sigscan_load2dbs_f = (sn_sigscan_load2dbs_t)dlsym(g_ncore_dllhandle, "sn_sigscan_load2dbs");
 
120
    sn_sigscan_closedb_f = (sn_sigscan_closedb_t)dlsym(g_ncore_dllhandle, "sn_sigscan_closedb");
 
121
    sn_sigscan_createstream_f = (sn_sigscan_createstream_t)dlsym(g_ncore_dllhandle, "sn_sigscan_createstream");
 
122
    sn_sigscan_writestream_f = (sn_sigscan_writestream_t)dlsym(g_ncore_dllhandle, "sn_sigscan_writestream");
 
123
    sn_sigscan_closestream_f = (sn_sigscan_closestream_t)dlsym(g_ncore_dllhandle, "sn_sigscan_closestream");
 
124
    sn_sigscan_resultcount_f = (sn_sigscan_resultcount_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultcount");
 
125
    sn_sigscan_resultget_name_f = (sn_sigscan_resultget_name_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_name");
 
126
    sn_sigscan_resultget_startoffset_f = (sn_sigscan_resultget_startoffset_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_startoffset");
 
127
    sn_sigscan_resultget_endoffset_f = (sn_sigscan_resultget_endoffset_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_endoffset");
 
128
    sn_sigscan_resultget_targettype_f = (sn_sigscan_resultget_targettype_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_targettype");
 
129
    sn_sigscan_resultget_offsetstring_f = (sn_sigscan_resultget_offsetstring_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_offsetstring");
 
130
    sn_sigscan_resultget_extradata_f = (sn_sigscan_resultget_extradata_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultget_extradata");
 
131
    sn_sigscan_resultfree_f = (sn_sigscan_resultfree_t)dlsym(g_ncore_dllhandle, "sn_sigscan_resultfree");
 
132
    sn_sigscan_seterrorlogger_f = (sn_sigscan_seterrorlogger_t)dlsym(g_ncore_dllhandle, "sn_sigscan_seterrorlogger");
 
133
 
 
134
    /* Check that we got all the symbols */
 
135
    if(sn_sigscan_initdb_f && sn_sigscan_loaddb_f && sn_sigscan_load2dbs_f &&
 
136
            sn_sigscan_closedb_f && sn_sigscan_createstream_f &&
 
137
            sn_sigscan_writestream_f && sn_sigscan_closestream_f &&
 
138
            sn_sigscan_resultcount_f && sn_sigscan_resultget_name_f &&
 
139
            sn_sigscan_resultget_startoffset_f &&
 
140
            sn_sigscan_resultget_endoffset_f &&
 
141
            sn_sigscan_resultget_targettype_f &&
 
142
            sn_sigscan_resultget_offsetstring_f &&
 
143
            sn_sigscan_resultget_extradata_f && sn_sigscan_resultfree_f &&
 
144
            sn_sigscan_seterrorlogger_f)
 
145
    {
 
146
        return CL_SUCCESS;
 
147
    }
 
148
 
 
149
    dlclose(g_ncore_dllhandle);
 
150
    g_ncore_dllhandle = 0;
 
151
    return CL_ENCINIT;
 
152
}
 
153
 
 
154
int cli_ncore_scanbuff(const char *buffer, unsigned int length, const char **virname, const struct cl_engine *engine, unsigned short ftype, unsigned int *targettab)
 
155
{
 
156
        void *streamhandle;
 
157
        void *resulthandle;
 
158
        static const uint32_t datamask[2] = { 0xffffffff, 0xffffffff };
 
159
        int count, hret, i;
 
160
        char *pt;
 
161
        int ret = CL_CLEAN;
 
162
 
 
163
 
 
164
    /* TODO: Setup proper data bitmask (need specs) */
 
165
    /* Create the hardware scanning stream */
 
166
    hret = (*sn_sigscan_createstream_f)(engine->ncdb, datamask, 2, &streamhandle);
 
167
    if(hret) {
 
168
        cli_errmsg("cli_ncore_scanbuff: can't create new hardware stream: %d\n", hret);
 
169
        return CL_ENCIO;
 
170
    }
 
171
 
 
172
    /* Write data to the hardware scanning stream */
 
173
    hret = (*sn_sigscan_writestream_f)(streamhandle, buffer, length);
 
174
    if(hret) {
 
175
        cli_errmsg("cli_ncore_scanbuff: can't write %u bytes to hardware stream: %d\n", length, hret);
 
176
        (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
 
177
        (*sn_sigscan_resultfree_f)(resulthandle);
 
178
        return CL_ENCIO;
 
179
    }
 
180
 
 
181
    /* Close the hardware scanning stream and collect the result */
 
182
    hret = (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
 
183
    if(hret) {
 
184
        cli_errmsg("cli_ncore_scanbuff: can't close hardware stream: %d\n", hret);
 
185
        return CL_ENCIO;
 
186
    }
 
187
 
 
188
    /* Iterate through the results */
 
189
    count = (*sn_sigscan_resultcount_f)(resulthandle);
 
190
    for(i = 0; i < count; i++) {
 
191
        const char *matchname = NULL, *offsetstring = NULL, *optionalsigdata = NULL;
 
192
        unsigned int targettype = 0;
 
193
 
 
194
        /* Acquire the name of the result */
 
195
        hret = (*sn_sigscan_resultget_name_f)(resulthandle, i, &matchname);
 
196
        if(hret) {
 
197
            cli_errmsg("cli_ncore_scanbuff: sn_sigscan_resultget_name failed for result %u: %d\n", i, hret);
 
198
            (*sn_sigscan_resultfree_f)(resulthandle);
 
199
            return CL_ENCIO;
 
200
        }
 
201
        if(!matchname) {
 
202
            cli_errmsg("cli_ncore_scanbuff: HW Result[%u]: Signature without name\n", i);
 
203
            (*sn_sigscan_resultfree_f)(resulthandle);
 
204
            return CL_EMALFDB;
 
205
        }
 
206
 
 
207
        /* Acquire the result file type and check that it is correct */
 
208
        hret = (*sn_sigscan_resultget_targettype_f)(resulthandle, i, &targettype);
 
209
        if(hret) {
 
210
            cli_errmsg("cli_ncore_scanbuff: sn_sigscan_resultget_targettype failed for result %u, signature %s: %d\n", i, matchname, hret);
 
211
            (*sn_sigscan_resultfree_f)(resulthandle);
 
212
            return CL_ENCIO;
 
213
        }
 
214
 
 
215
        if(targettype && targettab[targettype] != ftype) {
 
216
            cli_dbgmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Target type: %u, expected: %u\n", i, matchname, targettab[targettype], ftype);
 
217
            continue;
 
218
        }
 
219
 
 
220
        hret = (*sn_sigscan_resultget_offsetstring_f)(resulthandle, i, &offsetstring);
 
221
        if(hret) {
 
222
            cli_errmsg("cli_ncore_scanbuff: sn_sigscan_resultget_offsetstring failed for result %u, signature %s: %d\n", i, matchname, hret);
 
223
            (*sn_sigscan_resultfree_f)(resulthandle);
 
224
            return CL_ENCIO;
 
225
        }
 
226
        if(offsetstring) {
 
227
            cli_dbgmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Offset based signature not supported in buffer mode\n", i, matchname);
 
228
            continue;
 
229
        }
 
230
 
 
231
        hret = (*sn_sigscan_resultget_extradata_f)(resulthandle, i, &optionalsigdata);
 
232
        if(hret) {
 
233
            cli_errmsg("cli_ncore_scanbuff: sn_sigscan_resultget_extradata failed for result %u, signature %s: %d\n", i, matchname, hret);
 
234
            (*sn_sigscan_resultfree_f)(resulthandle);
 
235
            return CL_ENCIO;
 
236
        }
 
237
        if(optionalsigdata && strlen(optionalsigdata)) {
 
238
            pt = cli_strtok(optionalsigdata, 1, ":");
 
239
            if(pt) {
 
240
                if(!isdigit(*pt)) {
 
241
                    free(pt);
 
242
                    cli_errmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
243
                    (*sn_sigscan_resultfree_f)(resulthandle);
 
244
                    return CL_EMALFDB;
 
245
                }
 
246
 
 
247
                if((unsigned int) atoi(pt) < cl_retflevel()) {
 
248
                    cli_dbgmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Signature max flevel: %d, current: %d\n", i, matchname, atoi(pt), cl_retflevel());
 
249
                    free(pt);
 
250
                    continue;
 
251
                }
 
252
 
 
253
                free(pt);
 
254
                pt = cli_strtok(optionalsigdata, 0, ":");
 
255
                if(pt) {
 
256
                    if(!isdigit(*pt)) {
 
257
                        free(pt);
 
258
                        cli_errmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
259
                        (*sn_sigscan_resultfree_f)(resulthandle);
 
260
                        return CL_EMALFDB;
 
261
                    }
 
262
 
 
263
                    if((unsigned int) atoi(pt) > cl_retflevel()) {
 
264
                        cli_dbgmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Signature required flevel: %u, current: %u\n", i, matchname, atoi(pt), cl_retflevel());
 
265
                        free(pt);
 
266
                        continue;
 
267
                    }
 
268
                    free(pt);
 
269
                }
 
270
 
 
271
            } else {
 
272
                if(!isdigit(*optionalsigdata)) {
 
273
                    cli_errmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
274
                    (*sn_sigscan_resultfree_f)(resulthandle);
 
275
                    return CL_EMALFDB;
 
276
                }
 
277
 
 
278
                if((unsigned int) atoi(optionalsigdata) > cl_retflevel()) {
 
279
                    cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Signature required flevel: %u, current: %u\n", i, matchname, atoi(optionalsigdata), cl_retflevel());
 
280
                    continue;
 
281
                }
 
282
            }
 
283
        }
 
284
 
 
285
        /* Store the name of the match */
 
286
        *virname = matchname;
 
287
        ret = CL_VIRUS;
 
288
        break;
 
289
    }
 
290
 
 
291
    /* Clean up the result structure */
 
292
    hret = (*sn_sigscan_resultfree_f)(resulthandle);
 
293
    if(hret) {
 
294
        cli_errmsg("cli_ncore_scanbuff: can't free results: %d\n", ret);
 
295
        return CL_ENCIO;
 
296
    }
 
297
 
 
298
    return ret;
 
299
}
 
300
 
 
301
int cli_ncore_scandesc(int desc, cli_ctx *ctx, unsigned short ftype, int *cont, unsigned int *targettab, cli_md5_ctx *md5ctx)
 
302
{
 
303
        void *streamhandle;
 
304
        void *resulthandle;
 
305
        uint32_t datamask[2] = { 0xffffffff, 0xffffffff };
 
306
        struct cli_target_info info;
 
307
        int i, count, hret, bytes, ret = CL_CLEAN;
 
308
        off_t origoff;
 
309
        *cont = 0;
 
310
        char *buffer;
 
311
 
 
312
 
 
313
    /* TODO: Setup proper data bitmask (need specs) */
 
314
    /* Create the hardware scanning stream */
 
315
    hret = (*sn_sigscan_createstream_f)(ctx->engine->ncdb, datamask, 2, &streamhandle);
 
316
    if(hret) {
 
317
        cli_errmsg("cli_ncore_scandesc: can't create new hardware stream: %d\n", hret);
 
318
        return CL_ENCIO;
 
319
    }
 
320
 
 
321
    /* Obtain the initial offset */
 
322
    origoff = lseek(desc, 0, SEEK_CUR);
 
323
    if(origoff == -1) {
 
324
        cli_errmsg("cli_ncore_scandesc: lseek() failed for descriptor %d\n", desc);
 
325
        (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
 
326
        (*sn_sigscan_resultfree_f)(resulthandle);
 
327
        return CL_EIO;
 
328
    }
 
329
 
 
330
    buffer = (char *) cli_calloc(HWBUFFSIZE, sizeof(char));
 
331
    if(!buffer) {
 
332
        cli_dbgmsg("cli_ncore_scandesc: unable to cli_calloc(%u)\n", HWBUFFSIZE);
 
333
        (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
 
334
        (*sn_sigscan_resultfree_f)(resulthandle);
 
335
        return CL_EMEM;
 
336
    }
 
337
 
 
338
    /* Initialize the MD5 hasher */
 
339
    if(ctx->engine->md5_hlist)
 
340
        MD5_Init(md5ctx);
 
341
 
 
342
    /* Read and scan the data */
 
343
    while ((bytes = cli_readn(desc, buffer, HWBUFFSIZE)) > 0) {
 
344
        hret = (*sn_sigscan_writestream_f)(streamhandle, buffer, bytes);
 
345
        if(hret) {
 
346
            cli_errmsg("cli_ncore_scandesc: can't write to hardware stream: %d\n", hret);
 
347
            ret = CL_ENCIO;
 
348
            break;
 
349
        } else {
 
350
            if(ctx->scanned)
 
351
                *ctx->scanned += bytes / CL_COUNT_PRECISION;
 
352
 
 
353
            if(ctx->engine->md5_hlist)
 
354
                MD5_Update(md5ctx, buffer, bytes);
 
355
        }
 
356
    }
 
357
 
 
358
    free(buffer);
 
359
 
 
360
    /* Close the stream and get the result */
 
361
    hret = (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
 
362
    if(hret) {
 
363
        cli_errmsg("cli_ncore_scandesc: can't close hardware stream: %d\n", hret);
 
364
        return CL_ENCIO;
 
365
    }
 
366
 
 
367
    memset(&info, 0, sizeof(info));
 
368
 
 
369
    /* Iterate over the list of results */
 
370
    count = (*sn_sigscan_resultcount_f)(resulthandle);
 
371
    for(i = 0; i < count; i++) {
 
372
        const char *matchname = NULL, *offsetstring = NULL, *optionalsigdata = NULL;
 
373
        unsigned long long startoffset = 0;
 
374
        unsigned int targettype = 0;
 
375
        char *pt;
 
376
 
 
377
        /* Get the description of the match */
 
378
        hret = (*sn_sigscan_resultget_name_f)(resulthandle, i, &matchname);
 
379
        if(hret) {
 
380
            cli_errmsg("cli_ncore_scandesc: sn_sigscan_resultget_name failed for result %u: %d\n", i, hret);
 
381
            (*sn_sigscan_resultfree_f)(resulthandle);
 
382
            if(info.exeinfo.section)
 
383
                free(info.exeinfo.section);
 
384
            return CL_ENCIO;
 
385
        }
 
386
 
 
387
        if(!matchname) {
 
388
            cli_errmsg("cli_ncore_scandesc: HW Result[%u]: Signature without name\n", i);
 
389
            (*sn_sigscan_resultfree_f)(resulthandle);
 
390
            if(info.exeinfo.section)
 
391
                free(info.exeinfo.section);
 
392
            return CL_EMALFDB;
 
393
        }
 
394
 
 
395
        hret = (*sn_sigscan_resultget_targettype_f)(resulthandle, i, &targettype);
 
396
        if(hret) {
 
397
            cli_errmsg("cli_ncore_scandesc: sn_sigscan_resultget_targettype failed for result %d, signature %s: %d\n", i, matchname, hret);
 
398
            (*sn_sigscan_resultfree_f)(resulthandle);
 
399
            if(info.exeinfo.section)
 
400
                free(info.exeinfo.section);
 
401
            return CL_ENCIO;
 
402
        }
 
403
        if(targettype && targettab[targettype] != ftype) {
 
404
            cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Target type: %u, expected: %d\n", i, matchname, targettab[targettype], ftype);
 
405
            continue;
 
406
        }
 
407
 
 
408
        hret = (*sn_sigscan_resultget_offsetstring_f)(resulthandle, i, &offsetstring);
 
409
        if(hret) {
 
410
            cli_errmsg("cli_ncore_scandesc: sn_sigscan_resultget_offsetstring failed for result %u, signature %s: %d\n", i, matchname, hret);
 
411
            (*sn_sigscan_resultfree_f)(resulthandle);
 
412
            if(info.exeinfo.section)
 
413
                free(info.exeinfo.section);
 
414
            return CL_ENCIO;
 
415
        }
 
416
 
 
417
        hret = (*sn_sigscan_resultget_startoffset_f)(resulthandle, i, &startoffset);
 
418
        if(hret) {
 
419
            cli_errmsg("cli_ncore_scandesc: sn_sigscan_resultget_startoffset failed for result %u, signature %s: %d\n", i, matchname, hret);
 
420
            (*sn_sigscan_resultfree_f)(resulthandle);
 
421
            if(info.exeinfo.section)
 
422
                free(info.exeinfo.section);
 
423
            return CL_ENCIO;
 
424
        }
 
425
        if(offsetstring && strcmp(offsetstring, "*")) {
 
426
            off_t off = cli_caloff(offsetstring, &info, desc, ftype, &hret);
 
427
 
 
428
            if(hret == -1) {
 
429
                cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Bad offset in signature\n", i, matchname);
 
430
                (*sn_sigscan_resultfree_f)(resulthandle);
 
431
                if(info.exeinfo.section)
 
432
                    free(info.exeinfo.section);
 
433
                return CL_EMALFDB;
 
434
            }
 
435
            if(startoffset != (unsigned long long) off) {
 
436
                cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Virus offset: " "%Lu, expected: %Lu\n", i, matchname, startoffset, off);
 
437
                continue;
 
438
            }
 
439
        }
 
440
 
 
441
        hret = (*sn_sigscan_resultget_extradata_f)(resulthandle, i, &optionalsigdata);
 
442
        if(hret) {
 
443
            cli_errmsg("cli_ncore_scandesc: sn_sigscan_resultget_extradata failed for result %d, signature %s: %d\n", i, matchname, hret);
 
444
            (*sn_sigscan_resultfree_f)(resulthandle);
 
445
            if(info.exeinfo.section)
 
446
                free(info.exeinfo.section);
 
447
            return CL_ENCIO;
 
448
        }
 
449
 
 
450
        if(optionalsigdata && strlen(optionalsigdata)) {
 
451
            pt = cli_strtok(optionalsigdata, 1, ":");
 
452
            if(pt) {
 
453
                if(!isdigit(*pt)) {
 
454
                    free(pt);
 
455
                    cli_errmsg("cli_ncore_scandesc: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
456
                    (*sn_sigscan_resultfree_f)(resulthandle);
 
457
                    if(info.exeinfo.section)
 
458
                        free(info.exeinfo.section);
 
459
                    return CL_EMALFDB;
 
460
                }
 
461
 
 
462
                if((unsigned int) atoi(pt) < cl_retflevel()) {
 
463
                    cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Signature max flevel: %d, current: %d\n", i, matchname, atoi(pt), cl_retflevel());
 
464
                    free(pt);
 
465
                    continue;
 
466
                }
 
467
 
 
468
                free(pt);
 
469
 
 
470
                pt = cli_strtok(optionalsigdata, 0, ":");
 
471
                if(pt) {
 
472
                    if(!isdigit(*pt)) {
 
473
                        free(pt);
 
474
                        cli_errmsg("cli_ncore_scandesc: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
475
                        (*sn_sigscan_resultfree_f)(resulthandle);
 
476
                        if(info.exeinfo.section)
 
477
                            free(info.exeinfo.section);
 
478
                        return CL_EMALFDB;
 
479
                    }
 
480
 
 
481
                    if((unsigned int) atoi(pt) > cl_retflevel()) {
 
482
                        cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Signature required flevel: %d, current: %d\n", i, matchname, atoi(pt), cl_retflevel());
 
483
                        free(pt);
 
484
                        continue;
 
485
                    }
 
486
                    free(pt);
 
487
                }
 
488
            } else {
 
489
                if(!isdigit(*optionalsigdata)) {
 
490
                    cli_errmsg("cli_ncore_scandesc: HW Result[%u]: %s: Incorrect optional signature data: %s\n", i, matchname, optionalsigdata);
 
491
                    (*sn_sigscan_resultfree_f)(resulthandle);
 
492
                    if(info.exeinfo.section)
 
493
                        free(info.exeinfo.section);
 
494
                    return CL_EMALFDB;
 
495
                }
 
496
 
 
497
                if((unsigned int) atoi(optionalsigdata) > cl_retflevel()) {
 
498
                    cli_dbgmsg("cli_ncore_scandesc: HW Result[%u]: %s: Signature required flevel: %d, current: %d\n", i, matchname, atoi(optionalsigdata), cl_retflevel());
 
499
                    continue;
 
500
                }
 
501
            }
 
502
        }
 
503
 
 
504
        *ctx->virname = matchname;
 
505
        ret = CL_VIRUS;
 
506
        break;
 
507
    }
 
508
 
 
509
    if(info.exeinfo.section)
 
510
        free(info.exeinfo.section);
 
511
 
 
512
    hret = (*sn_sigscan_resultfree_f)(resulthandle);
 
513
    if(hret) {
 
514
        cli_errmsg("cli_ncore_scandesc: can't free results: %d\n", ret);
 
515
        return CL_ENCIO;
 
516
    }
 
517
 
 
518
    if(ctx->engine->md5_hlist) {
 
519
        unsigned char digest[16];
 
520
        struct cli_md5_node *md5_node;
 
521
        MD5_Final(digest, md5ctx);
 
522
 
 
523
        md5_node = cli_vermd5(digest, ctx->engine);
 
524
        if(md5_node) {
 
525
            struct stat sb;
 
526
            if(fstat(desc, &sb) == -1)
 
527
                return CL_EIO;
 
528
 
 
529
            if((unsigned int) sb.st_size != md5_node->size) {
 
530
                cli_warnmsg("Detected false positive MD5 match. Please report.\n");
 
531
            } else {
 
532
                if(md5_node->fp) {
 
533
                    cli_dbgmsg("Eliminated false positive match (fp sig: %s)\n", md5_node->virname);
 
534
                    ret = CL_CLEAN;
 
535
                } else {
 
536
                    if(ctx->virname)
 
537
                        *ctx->virname = md5_node->virname;
 
538
 
 
539
                    ret = CL_VIRUS;
 
540
                }
 
541
            }
 
542
        }
 
543
    }
 
544
 
 
545
    if(ret == CL_VIRUS || (ftype != CL_TYPE_UNKNOWN_TEXT && ftype != CL_TYPE_UNKNOWN_DATA))
 
546
        return ret;
 
547
 
 
548
    if(lseek(desc, origoff, SEEK_SET) == -1) {
 
549
        cli_errmsg("cli_ncore_scandesc: lseek() failed for descriptor %d\n", desc);
 
550
        return CL_EIO;
 
551
    }
 
552
 
 
553
    *cont = 1;
 
554
    return ret;
 
555
}
 
556
 
 
557
int cli_ncore_load(const char *filename, struct cl_engine **engine, unsigned int *signo, unsigned int options)
 
558
{
 
559
        int ret = 0;
 
560
        unsigned int newsigs = 0;
 
561
 
 
562
 
 
563
    if((ret = cli_initengine(engine, options))) {
 
564
        cl_free(*engine);
 
565
        return ret;
 
566
    }
 
567
 
 
568
    if((ret = cli_ncore_dlinit())) {
 
569
        cl_free(*engine);
 
570
        return ret;
 
571
    }
 
572
 
 
573
    ret = (*sn_sigscan_initdb_f)(&(*engine)->ncdb);
 
574
    if(ret) {
 
575
        cli_errmsg("cli_ncore_load: error initializing the matcher: %d\n", ret);
 
576
        cl_free(*engine);
 
577
        return CL_ENCINIT;
 
578
    }
 
579
 
 
580
    (*engine)->ncore = 1;
 
581
 
 
582
    ret = (*sn_sigscan_loaddb_f)((*engine)->ncdb, filename, 0, &newsigs);
 
583
    if(ret) {
 
584
        cli_errmsg("cli_ncore_load: can't load hardware database: %d\n", ret);
 
585
        cl_free(*engine);
 
586
        return CL_ENCLOAD;
 
587
    }
 
588
 
 
589
    *signo += newsigs;
 
590
    return CL_SUCCESS;
 
591
}
 
592
 
 
593
void cli_ncore_unload(struct cl_engine *engine)
 
594
{
 
595
        int ret;
 
596
 
 
597
    ret = (*sn_sigscan_closedb_f)(engine->ncdb);
 
598
    if(ret)
 
599
        cli_errmsg("cl_free: can't close hardware database: %d\n", ret);
 
600
}
 
601
#endif