libclamav/matcher.c
e3aaff8e
 /*
33872a43
  *  Copyright (C) 2007-2009 Sourcefire, Inc.
4addba22
  *
2023340a
  *  Authors: Tomasz Kojm
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <string.h>
7ec67e94
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
b58fdfc2
 #ifdef	HAVE_UNISTD_H
7ec67e94
 #include <unistd.h>
b58fdfc2
 #endif
e3aaff8e
 
 #include "clamav.h"
 #include "others.h"
8000d078
 #include "matcher-ac.h"
 #include "matcher-bm.h"
 #include "md5.h"
888f5794
 #include "filetypes.h"
b68d11d2
 #include "matcher.h"
7ec67e94
 #include "pe.h"
01302683
 #include "elf.h"
 #include "execs.h"
c3a3be2d
 #include "special.h"
c9c463fe
 #include "str.h"
bedc58de
 #include "cltypes.h"
589d8d8e
 #include "default.h"
8af7ccd0
 #include "macho.h"
53721687
 
e06afe8e
 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)
8000d078
 {
bedc58de
 	int ret = CL_CLEAN;
 	unsigned int i;
4e9ab8ed
 	struct cli_ac_data mdata;
5612732c
 	struct cli_matcher *groot, *troot = NULL;
2ac2095a
 	const char **virname=ctx->virname;
 	const struct cl_engine *engine=ctx->engine;
e3aaff8e
 
5612732c
     if(!engine) {
 	cli_errmsg("cli_scanbuff: engine == NULL\n");
 	return CL_ENULLARG;
     }
 
     groot = engine->root[0]; /* generic signatures */
 
     if(ftype) {
4addba22
 	for(i = 1; i < CLI_MTARGETS; i++) {
 	    if(cli_mtargets[i].target == ftype) {
f46d2cc7
 		troot = engine->root[i];
5612732c
 		break;
 	    }
 	}
     }
 
     if(troot) {
 
aca9ea82
 	if(!acdata && (ret = cli_ac_initdata(&mdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
4e9ab8ed
 	    return ret;
5612732c
 
006f5fe6
 	if(troot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, troot, offset, -1, NULL)) != CL_VIRUS)
33872a43
 	    ret = cli_ac_scanbuff(buffer, length, virname, NULL, NULL, troot, acdata ? (acdata[0]) : (&mdata), offset, ftype, NULL, AC_SCAN_VIR, NULL);
5612732c
 
01386195
 	if(!acdata)
 	    cli_ac_freedata(&mdata);
5612732c
 
 	if(ret == CL_VIRUS)
 	    return ret;
     }
 
aca9ea82
     if(!acdata && (ret = cli_ac_initdata(&mdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
4e9ab8ed
 	return ret;
e3aaff8e
 
006f5fe6
     if(groot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, groot, offset, -1, NULL)) != CL_VIRUS)
33872a43
 	ret = cli_ac_scanbuff(buffer, length, virname, NULL, NULL, groot, acdata ? (acdata[1]) : (&mdata), offset, ftype, NULL, AC_SCAN_VIR, NULL);
4e9ab8ed
 
01386195
     if(!acdata)
 	cli_ac_freedata(&mdata);
8000d078
 
     return ret;
e3aaff8e
 }
 
33872a43
 /*
  * offdata[0]: type
  * offdata[1]: offset value
  * offdata[2]: max shift
  * offdata[3]: section number
  */
 int cli_caloff(const char *offstr, struct cli_target_info *info, int fd, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max)
7ec67e94
 {
01302683
 	int (*einfo)(int, struct cli_exe_info *) = NULL;
33872a43
 	char offcpy[65];
bda5598b
 	unsigned int n, val;
33872a43
 	char *pt;
 	off_t pos;
 	struct stat sb;
841161e0
 
e06afe8e
 
33872a43
     if(!info) { /* decode offset string */
 	if(!offstr) {
 	    cli_errmsg("cli_caloff: offstr == NULL\n");
 	    return CL_ENULLARG;
 	}
e06afe8e
 
33872a43
 	if(!strcmp(offstr, "*")) {
 	    offdata[0] = *offset_max = *offset_min = CLI_OFF_ANY;
 	    return CL_SUCCESS;
 	}
e06afe8e
 
33872a43
 	if(strlen(offstr) > 64) {
 	    cli_errmsg("cli_caloff: Offset string too long\n");
 	    return CL_EMALFDB;
 	}
 	strcpy(offcpy, offstr);
841161e0
 
33872a43
 	if((pt = strchr(offcpy, ','))) {
 	    if(!cli_isnumber(pt + 1)) {
 		cli_errmsg("cli_caloff: Invalid offset shift value\n");
 		return CL_EMALFDB;
 	    }
 	    offdata[2] = atoi(pt + 1);
 	    *pt = 0;
 	} else {
 	    offdata[2] = 0;
 	}
841161e0
 
33872a43
 	*offset_max = *offset_min = CLI_OFF_NONE;
841161e0
 
33872a43
 	if(!strncmp(offcpy, "EP+", 3) || !strncmp(offcpy, "EP-", 3)) {
 	    if(offcpy[2] == '+')
 		offdata[0] = CLI_OFF_EP_PLUS;
 	    else
 		offdata[0] = CLI_OFF_EP_MINUS;
841161e0
 
33872a43
 	    if(!cli_isnumber(&offcpy[3])) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
 	    }
 	    offdata[1] = atoi(&offcpy[3]);
 
 	} else if(offcpy[0] == 'S') {
 	    if(!strncmp(offstr, "SL+", 3)) {
 		offdata[0] = CLI_OFF_SL_PLUS;
 		if(!cli_isnumber(&offcpy[3])) {
 		    cli_errmsg("cli_caloff: Invalid offset value\n");
 		    return CL_EMALFDB;
841161e0
 		}
33872a43
 		offdata[1] = atoi(&offcpy[3]);
 
 	    } else if(sscanf(offcpy, "S%u+%u", &n, &val) == 2) {
 		offdata[0] = CLI_OFF_SX_PLUS;
 		offdata[1] = val;
 		offdata[3] = n;
 	    } else {
 		cli_errmsg("cli_caloff: Invalid offset string\n");
 		return CL_EMALFDB;
 	    }
841161e0
 
33872a43
 	} else if(!strncmp(offcpy, "EOF-", 4)) {
 	    offdata[0] = CLI_OFF_EOF_MINUS;
 	    if(!cli_isnumber(&offcpy[4])) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
841161e0
 	    }
33872a43
 	    offdata[1] = atoi(&offcpy[4]);
 	} else {
 	    offdata[0] = CLI_OFF_ABSOLUTE;
 	    if(!cli_isnumber(offcpy)) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
 	    }
 	    *offset_min = offdata[1] = atoi(offcpy);
 	    *offset_max = *offset_min + offdata[2];
841161e0
 	}
4d1136fe
 
33872a43
 	if(offdata[0] != CLI_OFF_ANY && offdata[0] != CLI_OFF_ABSOLUTE && offdata[0] != CLI_OFF_EOF_MINUS) {
 	    if(target != 1 && target != 6 && target != 9) {
 		cli_errmsg("cli_caloff: Invalid offset type for target %u\n", target);
 		return CL_EMALFDB;
 	    }
 	}
7ec67e94
 
33872a43
     } else {
 	/* calculate relative offsets */
 	if(info->status == -1) {
006f5fe6
 	    *offset_min = CLI_OFF_NONE;
 	    if(offset_max)
 		*offset_max = CLI_OFF_NONE;
33872a43
 	    return CL_SUCCESS;
 	}
7ec67e94
 
33872a43
 	if((offdata[0] == CLI_OFF_EOF_MINUS)) {
 	    if(!info->fsize) {
 		if(fstat(fd, &sb) == -1) {
 		    cli_errmsg("cli_caloff: fstat(%d) failed\n", fd);
 		    return CL_ESTAT;
 		}
 		info->fsize = sb.st_size;
399bd596
 	    }
 
33872a43
 	} else if(!info->status) {
 	    if(target == 1)
 		einfo = cli_peheader;
 	    else if(target == 6)
 		einfo = cli_elfheader;
 	    else if(target == 9)
 		einfo = cli_machoheader;
399bd596
 
33872a43
 	    if(!einfo) {
 		cli_errmsg("cli_caloff: Invalid offset/filetype\n");
 		return CL_EMALFDB;
399bd596
 	    }
 
33872a43
 	    if((pos = lseek(fd, 0, SEEK_CUR)) == -1) {
 		cli_errmsg("cli_caloff: lseek(%d) failed\n", fd);
 		return CL_ESEEK;
399bd596
 	    }
 
33872a43
 	    lseek(fd, 0, SEEK_SET);
 	    if(einfo(fd, &info->exeinfo)) {
 		/* einfo *may* fail */
 		lseek(fd, pos, SEEK_SET);
 		info->status = -1;
006f5fe6
 		*offset_min = CLI_OFF_NONE;
 		if(offset_max)
 		    *offset_max = CLI_OFF_NONE;
33872a43
 		return CL_SUCCESS;
 	    }
 	    lseek(fd, pos, SEEK_SET);
 	    info->status = 1;
7ec67e94
 	}
 
33872a43
 	switch(offdata[0]) {
 	    case CLI_OFF_EOF_MINUS:
 		*offset_min = info->fsize - offdata[1];
 		break;
399bd596
 
33872a43
 	    case CLI_OFF_EP_PLUS:
 		*offset_min = info->exeinfo.ep + offdata[1];
 		break;
7ec67e94
 
33872a43
 	    case CLI_OFF_EP_MINUS:
 		*offset_min = info->exeinfo.ep - offdata[1];
 		break;
 
 	    case CLI_OFF_SL_PLUS:
 		*offset_min = info->exeinfo.section[info->exeinfo.nsections - 1].raw + offdata[1];
 		break;
 
 	    case CLI_OFF_SX_PLUS:
 		if(offdata[3] >= info->exeinfo.nsections)
006f5fe6
 		    *offset_min = CLI_OFF_NONE;
33872a43
 		else
 		    *offset_min = info->exeinfo.section[offdata[3]].raw + offdata[1];
 		break;
 
 	    default:
 		cli_errmsg("cli_caloff: Not a relative offset (type: %u)\n", offdata[0]);
 		return CL_EARG;
841161e0
 	}
7ec67e94
 
006f5fe6
 	if(offset_max && *offset_min != CLI_OFF_NONE)
33872a43
 	    *offset_max = *offset_min + offdata[2];
7ec67e94
     }
 
33872a43
     return CL_SUCCESS;
7ec67e94
 }
 
e4101980
 int cli_checkfp(int fd, cli_ctx *ctx)
db65451b
 {
1f089aa7
 	unsigned char *digest;
2b459819
 	const char *virname;
e4101980
 	off_t pos;
db65451b
 
 
e4101980
     if((pos = lseek(fd, 0, SEEK_CUR)) == -1) {
 	cli_errmsg("cli_checkfp(): lseek() failed\n");
 	return 0;
     }
 
     lseek(fd, 0, SEEK_SET);
 
     if(ctx->engine->md5_fp) {
db65451b
 	if(!(digest = cli_md5digest(fd))) {
 	    cli_errmsg("cli_checkfp(): Can't generate MD5 checksum\n");
e4101980
 	    lseek(fd, pos, SEEK_SET);
db65451b
 	    return 0;
 	}
 
006f5fe6
 	if(cli_bm_scanbuff(digest, 16, &virname, ctx->engine->md5_fp, 0, -1, NULL) == CL_VIRUS) {
e4101980
 	    cli_dbgmsg("cli_checkfp(): Found false positive detection (fp sig: %s)\n", virname);
2b459819
 	    free(digest);
e4101980
 	    lseek(fd, pos, SEEK_SET);
2b459819
 	    return 1;
db65451b
 	}
 	free(digest);
     }
 
e4101980
     lseek(fd, pos, SEEK_SET);
db65451b
     return 0;
 }
 
6038397e
 int cli_scandesc(int desc, cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode)
e3aaff8e
 {
d38d6dad
         unsigned char *buffer, *buff, *endbl, *upt;
c7aeeb46
 	int ret = CL_CLEAN, type = CL_CLEAN, bytes;
6d3c5bec
 	unsigned int i, evalcnt, bm_offmode = 0;
bedc58de
 	uint32_t buffersize, length, maxpatlen, shift = 0, offset = 0;
ee8bd2fb
 	uint64_t evalids;
4e9ab8ed
 	struct cli_ac_data gdata, tdata;
006f5fe6
 	struct cli_bm_off toff;
2a9e6ac8
 	cli_md5_ctx md5ctx;
335d1663
 	unsigned char digest[16];
73218de2
 	struct cli_matcher *groot = NULL, *troot = NULL;
6d3c5bec
 	struct stat sb;
888f5794
 
 
3c91998b
     if(!ctx->engine) {
5612732c
 	cli_errmsg("cli_scandesc: engine == NULL\n");
8000d078
 	return CL_ENULLARG;
4048c4f6
     }
 
73218de2
     if(!ftonly)
 	groot = ctx->engine->root[0]; /* generic signatures */
5612732c
 
     if(ftype) {
4addba22
 	for(i = 1; i < CLI_MTARGETS; i++) {
 	    if(cli_mtargets[i].target == ftype) {
f46d2cc7
 		troot = ctx->engine->root[i];
5612732c
 		break;
 	    }
 	}
     }
 
73218de2
     if(ftonly) {
 	if(!troot)
 	    return CL_CLEAN;
 
 	maxpatlen = troot->maxpatlen;
     } else {
 	if(troot)
 	    maxpatlen = MAX(troot->maxpatlen, groot->maxpatlen);
 	else
 	    maxpatlen = groot->maxpatlen;
     }
5612732c
 
8000d078
     /* prepare the buffer */
0710165d
     buffersize = maxpatlen + SCANBUFF;
4e9ab8ed
     if(!(buffer = (unsigned char *) cli_calloc(buffersize, sizeof(unsigned char)))) {
841161e0
 	cli_dbgmsg("cli_scandesc(): unable to cli_calloc(%u)\n", buffersize);
8000d078
 	return CL_EMEM;
     }
888f5794
 
33872a43
     if(!ftonly)
aca9ea82
 	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, desc)))
33872a43
 	    return ret;
e3aaff8e
 
5612732c
     if(troot) {
aca9ea82
 	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, desc))) {
33872a43
 	    if(!ftonly)
 		cli_ac_freedata(&gdata);
4e9ab8ed
 	    return ret;
33872a43
 	}
6d3c5bec
 	if(troot->bm_offmode) {
 	    if(fstat(desc, &sb) != -1) {
 		if(sb.st_size >= CLI_DEFAULT_BM_OFFMODE_FSIZE) {
 		    if((ret = cli_bm_initoff(troot, &toff, desc))) {
 			if(!ftonly)
 			    cli_ac_freedata(&gdata);
 			cli_ac_freedata(&tdata);
 			return ret;
 		    }
 		    bm_offmode = 1;
 		}
 	    }
006f5fe6
 	}
5612732c
     }
 
2b459819
     if(!ftonly && ctx->engine->md5_hdb)
2a9e6ac8
 	cli_md5_init(&md5ctx);
e3aaff8e
 
8000d078
     buff = buffer;
5612732c
     buff += maxpatlen; /* pointer to read data block */
     endbl = buff + SCANBUFF - maxpatlen; /* pointer to the last block
4e9ab8ed
 					  * length of maxpatlen
 					  */
e3aaff8e
 
4e9ab8ed
     upt = buff;
0710165d
     while((bytes = cli_readn(desc, buff + shift, SCANBUFF - shift)) > 0) {
e3aaff8e
 
3c91998b
 	if(ctx->scanned)
 	    *ctx->scanned += bytes / CL_COUNT_PRECISION;
e3aaff8e
 
0710165d
 	length = shift + bytes;
4e9ab8ed
 	if(upt == buffer)
0710165d
 	    length += maxpatlen;
4048c4f6
 
5612732c
 	if(troot) {
6d3c5bec
 	    if(troot->ac_only || (ret = cli_bm_scanbuff(upt, length, ctx->virname, troot, offset, desc, bm_offmode ? &toff : NULL)) != CL_VIRUS)
33872a43
 		ret = cli_ac_scanbuff(upt, length, ctx->virname, NULL, NULL, troot, &tdata, offset, ftype, ftoffset, acmode, NULL);
83fa5305
 
 	    if(ret == CL_VIRUS) {
5612732c
 		free(buffer);
73218de2
 		if(!ftonly)
 		    cli_ac_freedata(&gdata);
4e9ab8ed
 		cli_ac_freedata(&tdata);
6d3c5bec
 		if(bm_offmode)
0c190b52
 		    cli_bm_freeoff(&toff, troot);
5612732c
 
e4101980
 		if(cli_checkfp(desc, ctx))
5612732c
 		    return CL_CLEAN;
 		else
 		    return CL_VIRUS;
 	    }
 	}
db65451b
 
73218de2
 	if(!ftonly) {
006f5fe6
 	    if(groot->ac_only || (ret = cli_bm_scanbuff(upt, length, ctx->virname, groot, offset, desc, NULL)) != CL_VIRUS)
33872a43
 		ret = cli_ac_scanbuff(upt, length, ctx->virname, NULL, NULL, groot, &gdata, offset, ftype, ftoffset, acmode, NULL);
83fa5305
 
73218de2
 	    if(ret == CL_VIRUS) {
 		free(buffer);
 		cli_ac_freedata(&gdata);
006f5fe6
 		if(troot) {
73218de2
 		    cli_ac_freedata(&tdata);
6d3c5bec
 		    if(bm_offmode)
0c190b52
 			cli_bm_freeoff(&toff, troot);
006f5fe6
 		}
e4101980
 		if(cli_checkfp(desc, ctx))
73218de2
 		    return CL_CLEAN;
 		else
 		    return CL_VIRUS;
e3aaff8e
 
6038397e
 	    } else if((acmode & AC_SCAN_FT) && ret >= CL_TYPENO) {
73218de2
 		if(ret > type)
 		    type = ret;
 	    }
e3aaff8e
 
2b459819
 	    if(ctx->engine->md5_hdb)
2a9e6ac8
 		cli_md5_update(&md5ctx, buff + shift, bytes);
73218de2
 	}
0710165d
 
 	if(bytes + shift == SCANBUFF) {
5612732c
 	    memmove(buffer, endbl, maxpatlen);
0710165d
 	    offset += SCANBUFF;
fc8e8ca2
 
4e9ab8ed
 	    if(upt == buff) {
 		upt = buffer;
0710165d
 		offset -= maxpatlen;
102ef209
 	    }
0710165d
 
 	    shift = 0;
 
 	} else {
 	    shift += bytes;
102ef209
 	}
8000d078
     }
084ee140
 
8000d078
     free(buffer);
c7aeeb46
 
     if(troot) {
 	for(i = 0; i < troot->ac_lsigs; i++) {
 	    evalcnt = 0;
ee8bd2fb
 	    evalids = 0;
 	    if(cli_ac_chklsig(troot->ac_lsigtable[i]->logic, troot->ac_lsigtable[i]->logic + strlen(troot->ac_lsigtable[i]->logic), tdata.lsigcnt[i], &evalcnt, &evalids, 0) == 1) {
d38d6dad
 		if (!troot->ac_lsigtable[i]->bc) {
 		    if(ctx->virname)
 			*ctx->virname = troot->ac_lsigtable[i]->virname;
 		    ret = CL_VIRUS;
 		    break;
 		}
 		if (cli_bytecode_runlsig(&ctx->engine->bcs, troot->ac_lsigtable[i]->bc, ctx->virname, tdata.lsigcnt[i], desc) == CL_VIRUS) {
 		    ret = CL_VIRUS;
 		    break;
 		}
c7aeeb46
 	    }
 	}
4e9ab8ed
 	cli_ac_freedata(&tdata);
6d3c5bec
 	if(bm_offmode)
0c190b52
 	    cli_bm_freeoff(&toff, troot);
c7aeeb46
     }
 
     if(groot) {
 	if(ret != CL_VIRUS) for(i = 0; i < groot->ac_lsigs; i++) {
 	    evalcnt = 0;
ee8bd2fb
 	    evalids = 0;
 	    if(cli_ac_chklsig(groot->ac_lsigtable[i]->logic, groot->ac_lsigtable[i]->logic + strlen(groot->ac_lsigtable[i]->logic), gdata.lsigcnt[i], &evalcnt, &evalids, 0) == 1) {
d38d6dad
 		if (!groot->ac_lsigtable[i]->bc) {
 		    if(ctx->virname)
 			*ctx->virname = groot->ac_lsigtable[i]->virname;
 		    ret = CL_VIRUS;
 		    break;
 		}
 		if (cli_bytecode_runlsig(&ctx->engine->bcs, groot->ac_lsigtable[i]->bc, ctx->virname, gdata.lsigcnt[i], desc) == CL_VIRUS) {
 		    ret = CL_VIRUS;
 		    break;
 		}
c7aeeb46
 	    }
 	}
 	cli_ac_freedata(&gdata);
     }
 
6b273dfd
     if(ret == CL_VIRUS) {
 	lseek(desc, 0, SEEK_SET);
e4101980
 	if(cli_checkfp(desc, ctx))
6b273dfd
 	    return CL_CLEAN;
 	else
 	    return CL_VIRUS;
     }
fc8e8ca2
 
2b459819
     if(!ftonly && ctx->engine->md5_hdb) {
2a9e6ac8
 	cli_md5_final(digest, &md5ctx);
006f5fe6
 	if(cli_bm_scanbuff(digest, 16, ctx->virname, ctx->engine->md5_hdb, 0, -1, NULL) == CL_VIRUS && (cli_bm_scanbuff(digest, 16, NULL, ctx->engine->md5_fp, 0, -1, NULL) != CL_VIRUS))
2b459819
 	    return CL_VIRUS;
fc8e8ca2
     }
 
6038397e
     return (acmode & AC_SCAN_FT) ? type : CL_CLEAN;
fc8e8ca2
 }