2
* Copyright (C) 2006 Tomasz Kojm <tkojm@clamav.net>
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.
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.
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,
21
#include "clamav-config.h"
32
#include <sys/types.h>
46
#include "matcher-ncore.h"
48
#define HWBUFFSIZE 32768
51
static void *g_ncore_dllhandle = 0;
52
static const char *g_ncore_dllpath = "/usr/lib/libsn_sigscan.so";
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,
64
typedef int (*sn_sigscan_writestream_t)(void *streamhandle, const char *buffer,
66
typedef int (*sn_sigscan_closestream_t)(void *streamhandle,
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);
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;
103
static int cli_ncore_dlinit()
105
if(access(g_ncore_dllpath, R_OK) == -1) {
106
cli_dbgmsg("cli_ncore_dlinit: Can't access %s\n", g_ncore_dllpath);
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);
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");
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)
149
dlclose(g_ncore_dllhandle);
150
g_ncore_dllhandle = 0;
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)
158
static const uint32_t datamask[2] = { 0xffffffff, 0xffffffff };
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);
168
cli_errmsg("cli_ncore_scanbuff: can't create new hardware stream: %d\n", hret);
172
/* Write data to the hardware scanning stream */
173
hret = (*sn_sigscan_writestream_f)(streamhandle, buffer, length);
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);
181
/* Close the hardware scanning stream and collect the result */
182
hret = (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
184
cli_errmsg("cli_ncore_scanbuff: can't close hardware stream: %d\n", hret);
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;
194
/* Acquire the name of the result */
195
hret = (*sn_sigscan_resultget_name_f)(resulthandle, i, &matchname);
197
cli_errmsg("cli_ncore_scanbuff: sn_sigscan_resultget_name failed for result %u: %d\n", i, hret);
198
(*sn_sigscan_resultfree_f)(resulthandle);
202
cli_errmsg("cli_ncore_scanbuff: HW Result[%u]: Signature without name\n", i);
203
(*sn_sigscan_resultfree_f)(resulthandle);
207
/* Acquire the result file type and check that it is correct */
208
hret = (*sn_sigscan_resultget_targettype_f)(resulthandle, i, &targettype);
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);
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);
220
hret = (*sn_sigscan_resultget_offsetstring_f)(resulthandle, i, &offsetstring);
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);
227
cli_dbgmsg("cli_ncore_scanbuff: HW Result[%u]: %s: Offset based signature not supported in buffer mode\n", i, matchname);
231
hret = (*sn_sigscan_resultget_extradata_f)(resulthandle, i, &optionalsigdata);
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);
237
if(optionalsigdata && strlen(optionalsigdata)) {
238
pt = cli_strtok(optionalsigdata, 1, ":");
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);
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());
254
pt = cli_strtok(optionalsigdata, 0, ":");
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);
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());
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);
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());
285
/* Store the name of the match */
286
*virname = matchname;
291
/* Clean up the result structure */
292
hret = (*sn_sigscan_resultfree_f)(resulthandle);
294
cli_errmsg("cli_ncore_scanbuff: can't free results: %d\n", ret);
301
int cli_ncore_scandesc(int desc, cli_ctx *ctx, unsigned short ftype, int *cont, unsigned int *targettab, cli_md5_ctx *md5ctx)
305
uint32_t datamask[2] = { 0xffffffff, 0xffffffff };
306
struct cli_target_info info;
307
int i, count, hret, bytes, ret = CL_CLEAN;
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);
317
cli_errmsg("cli_ncore_scandesc: can't create new hardware stream: %d\n", hret);
321
/* Obtain the initial offset */
322
origoff = lseek(desc, 0, SEEK_CUR);
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);
330
buffer = (char *) cli_calloc(HWBUFFSIZE, sizeof(char));
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);
338
/* Initialize the MD5 hasher */
339
if(ctx->engine->md5_hlist)
342
/* Read and scan the data */
343
while ((bytes = cli_readn(desc, buffer, HWBUFFSIZE)) > 0) {
344
hret = (*sn_sigscan_writestream_f)(streamhandle, buffer, bytes);
346
cli_errmsg("cli_ncore_scandesc: can't write to hardware stream: %d\n", hret);
351
*ctx->scanned += bytes / CL_COUNT_PRECISION;
353
if(ctx->engine->md5_hlist)
354
MD5_Update(md5ctx, buffer, bytes);
360
/* Close the stream and get the result */
361
hret = (*sn_sigscan_closestream_f)(streamhandle, &resulthandle);
363
cli_errmsg("cli_ncore_scandesc: can't close hardware stream: %d\n", hret);
367
memset(&info, 0, sizeof(info));
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;
377
/* Get the description of the match */
378
hret = (*sn_sigscan_resultget_name_f)(resulthandle, i, &matchname);
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);
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);
395
hret = (*sn_sigscan_resultget_targettype_f)(resulthandle, i, &targettype);
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);
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);
408
hret = (*sn_sigscan_resultget_offsetstring_f)(resulthandle, i, &offsetstring);
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);
417
hret = (*sn_sigscan_resultget_startoffset_f)(resulthandle, i, &startoffset);
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);
425
if(offsetstring && strcmp(offsetstring, "*")) {
426
off_t off = cli_caloff(offsetstring, &info, desc, ftype, &hret);
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);
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);
441
hret = (*sn_sigscan_resultget_extradata_f)(resulthandle, i, &optionalsigdata);
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);
450
if(optionalsigdata && strlen(optionalsigdata)) {
451
pt = cli_strtok(optionalsigdata, 1, ":");
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);
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());
470
pt = cli_strtok(optionalsigdata, 0, ":");
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);
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());
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);
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());
504
*ctx->virname = matchname;
509
if(info.exeinfo.section)
510
free(info.exeinfo.section);
512
hret = (*sn_sigscan_resultfree_f)(resulthandle);
514
cli_errmsg("cli_ncore_scandesc: can't free results: %d\n", ret);
518
if(ctx->engine->md5_hlist) {
519
unsigned char digest[16];
520
struct cli_md5_node *md5_node;
521
MD5_Final(digest, md5ctx);
523
md5_node = cli_vermd5(digest, ctx->engine);
526
if(fstat(desc, &sb) == -1)
529
if((unsigned int) sb.st_size != md5_node->size) {
530
cli_warnmsg("Detected false positive MD5 match. Please report.\n");
533
cli_dbgmsg("Eliminated false positive match (fp sig: %s)\n", md5_node->virname);
537
*ctx->virname = md5_node->virname;
545
if(ret == CL_VIRUS || (ftype != CL_TYPE_UNKNOWN_TEXT && ftype != CL_TYPE_UNKNOWN_DATA))
548
if(lseek(desc, origoff, SEEK_SET) == -1) {
549
cli_errmsg("cli_ncore_scandesc: lseek() failed for descriptor %d\n", desc);
557
int cli_ncore_load(const char *filename, struct cl_engine **engine, unsigned int *signo, unsigned int options)
560
unsigned int newsigs = 0;
563
if((ret = cli_initengine(engine, options))) {
568
if((ret = cli_ncore_dlinit())) {
573
ret = (*sn_sigscan_initdb_f)(&(*engine)->ncdb);
575
cli_errmsg("cli_ncore_load: error initializing the matcher: %d\n", ret);
580
(*engine)->ncore = 1;
582
ret = (*sn_sigscan_loaddb_f)((*engine)->ncdb, filename, 0, &newsigs);
584
cli_errmsg("cli_ncore_load: can't load hardware database: %d\n", ret);
593
void cli_ncore_unload(struct cl_engine *engine)
597
ret = (*sn_sigscan_closedb_f)(engine->ncdb);
599
cli_errmsg("cl_free: can't close hardware database: %d\n", ret);