Browse code

libclamav: pass array with initial matches to bytecode (bb#2397)

Tomasz Kojm authored on 2010/12/15 00:08:43
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Dec 14 16:07:42 CET 2010 (tk)
2
+---------------------------------
3
+ * libclamav: pass array with initial matches to bytecode (bb#2397)
4
+
1 5
 Tue Dec 14 12:20:34 CET 2010 (tk)
2 6
 ---------------------------------
3 7
  * libclamav/msexpand.c: fix name clash on HP-UX (bb#2398)
... ...
@@ -946,35 +946,47 @@ int cli_ac_initdata(struct cli_ac_data *data, uint32_t partsigs, uint32_t lsigs,
946 946
 	    data->lsigcnt[i] = data->lsigcnt[0] + 64 * i;
947 947
 
948 948
 	/* subsig offsets */
949
-	data->lsigsuboff = (uint32_t **) cli_malloc(lsigs * sizeof(uint32_t *));
950
-	if(!data->lsigsuboff) {
949
+	data->lsigsuboff_last = (uint32_t **) cli_malloc(lsigs * sizeof(uint32_t *));
950
+	data->lsigsuboff_first = (uint32_t **) cli_malloc(lsigs * sizeof(uint32_t *));
951
+	if(!data->lsigsuboff_last || !data->lsigsuboff_first) {
952
+	    free(data->lsigsuboff_last);
953
+	    free(data->lsigsuboff_first);
951 954
 	    free(data->lsigcnt[0]);
952 955
 	    free(data->lsigcnt);
953 956
 	    if(partsigs)
954 957
 		free(data->offmatrix);
955 958
 	    if(reloffsigs)
956 959
 		free(data->offset);
957
-	    cli_errmsg("cli_ac_init: Can't allocate memory for data->lsigsuboff\n");
960
+	    cli_errmsg("cli_ac_init: Can't allocate memory for data->lsigsuboff_(last|first)\n");
958 961
 	    return CL_EMEM;
959 962
 	}
960
-	data->lsigsuboff[0] = (uint32_t *) cli_calloc(lsigs * 64, sizeof(uint32_t));
961
-	if(!data->lsigsuboff[0]) {
962
-	    free(data->lsigsuboff);
963
+	data->lsigsuboff_last[0] = (uint32_t *) cli_calloc(lsigs * 64, sizeof(uint32_t));
964
+	data->lsigsuboff_first[0] = (uint32_t *) cli_calloc(lsigs * 64, sizeof(uint32_t));
965
+	if(!data->lsigsuboff_last[0] || !data->lsigsuboff_first[0]) {
966
+	    free(data->lsigsuboff_last[0]);
967
+	    free(data->lsigsuboff_first[0]);
968
+	    free(data->lsigsuboff_last);
969
+	    free(data->lsigsuboff_first);
963 970
 	    free(data->lsigcnt[0]);
964 971
 	    free(data->lsigcnt);
965 972
 	    if(partsigs)
966 973
 		free(data->offmatrix);
967 974
 	    if(reloffsigs)
968 975
 		free(data->offset);
969
-	    cli_errmsg("cli_ac_init: Can't allocate memory for data->lsigsuboff[0]\n");
976
+	    cli_errmsg("cli_ac_init: Can't allocate memory for data->lsigsuboff_(last|first)[0]\n");
970 977
 	    return CL_EMEM;
971 978
 	}
972
-	for(j = 0; j < 64; j++)
973
-	    data->lsigsuboff[0][j] = CLI_OFF_NONE;
979
+	for(j = 0; j < 64; j++) {
980
+	    data->lsigsuboff_last[0][j] = CLI_OFF_NONE;
981
+	    data->lsigsuboff_first[0][j] = CLI_OFF_NONE;
982
+	}
974 983
 	for(i = 1; i < lsigs; i++) {
975
-	    data->lsigsuboff[i] = data->lsigsuboff[0] + 64 * i;
976
-	    for(j = 0; j < 64; j++)
977
-		data->lsigsuboff[i][j] = CLI_OFF_NONE;
984
+	    data->lsigsuboff_last[i] = data->lsigsuboff_last[0] + 64 * i;
985
+	    data->lsigsuboff_first[i] = data->lsigsuboff_first[0] + 64 * i;
986
+	    for(j = 0; j < 64; j++) {
987
+		data->lsigsuboff_last[i][j] = CLI_OFF_NONE;
988
+		data->lsigsuboff_first[i][j] = CLI_OFF_NONE;
989
+	    }
978 990
 	}
979 991
     }
980 992
     for (i=0;i<32;i++)
... ...
@@ -1027,8 +1039,10 @@ void cli_ac_freedata(struct cli_ac_data *data)
1027 1027
     if(data && data->lsigs) {
1028 1028
 	free(data->lsigcnt[0]);
1029 1029
 	free(data->lsigcnt);
1030
-	free(data->lsigsuboff[0]);
1031
-	free(data->lsigsuboff);
1030
+	free(data->lsigsuboff_last[0]);
1031
+	free(data->lsigsuboff_last);
1032
+	free(data->lsigsuboff_first[0]);
1033
+	free(data->lsigsuboff_first);
1032 1034
 	data->lsigs = 0;
1033 1035
     }
1034 1036
 
... ...
@@ -1075,11 +1089,13 @@ static inline void lsig_sub_matched(const struct cli_matcher *root, struct cli_a
1075 1075
 	const struct cli_lsig_tdb *tdb = &root->ac_lsigtable[lsigid1]->tdb;
1076 1076
 
1077 1077
     if(realoff != CLI_OFF_NONE) {
1078
-	if(mdata->lsigsuboff[lsigid1][lsigid2] != CLI_OFF_NONE && ((!partial && realoff <= mdata->lsigsuboff[lsigid1][lsigid2]) || (partial && realoff < mdata->lsigsuboff[lsigid1][lsigid2])))
1078
+	if(mdata->lsigsuboff_first[lsigid1][lsigid2] == CLI_OFF_NONE)
1079
+	    mdata->lsigsuboff_first[lsigid1][lsigid2] = realoff;
1080
+	if(mdata->lsigsuboff_last[lsigid1][lsigid2] != CLI_OFF_NONE && ((!partial && realoff <= mdata->lsigsuboff_last[lsigid1][lsigid2]) || (partial && realoff < mdata->lsigsuboff_last[lsigid1][lsigid2])))
1079 1081
 	    return;
1080 1082
 	mdata->lsigcnt[lsigid1][lsigid2]++;
1081 1083
 	if(mdata->lsigcnt[lsigid1][lsigid2] <= 1 || !tdb->macro_ptids || !tdb->macro_ptids[lsigid2])
1082
-	    mdata->lsigsuboff[lsigid1][lsigid2] = realoff;
1084
+	    mdata->lsigsuboff_last[lsigid1][lsigid2] = realoff;
1083 1085
     }
1084 1086
 
1085 1087
     if (mdata->lsigcnt[lsigid1][lsigid2] > 1) {
... ...
@@ -1098,7 +1114,7 @@ static inline void lsig_sub_matched(const struct cli_matcher *root, struct cli_a
1098 1098
 	/* start of last macro match */
1099 1099
 	last_macro_match = mdata->macro_lastmatch[macropt->sigid];
1100 1100
 	/* start of previous lsig subsig match */
1101
-	last_macroprev_match = mdata->lsigsuboff[lsigid1][lsigid2];
1101
+	last_macroprev_match = mdata->lsigsuboff_last[lsigid1][lsigid2];
1102 1102
 	if (last_macro_match != CLI_OFF_NONE)
1103 1103
 	    cli_dbgmsg("Checking macro match: %u + (%u - %u) == %u\n",
1104 1104
 		       last_macroprev_match, smin, smax, last_macro_match);
... ...
@@ -1108,11 +1124,11 @@ static inline void lsig_sub_matched(const struct cli_matcher *root, struct cli_a
1108 1108
 	    cli_dbgmsg("Canceled false lsig macro match\n");
1109 1109
 	    /* Previous match was false - cancel it */
1110 1110
 	    mdata->lsigcnt[lsigid1][lsigid2]--;
1111
-	    mdata->lsigsuboff[lsigid1][lsigid2] = realoff;
1111
+	    mdata->lsigsuboff_last[lsigid1][lsigid2] = realoff;
1112 1112
 	} else {
1113 1113
 	    /* mark the macro sig itself matched */
1114 1114
 	    mdata->lsigcnt[lsigid1][lsigid2+1]++;
1115
-	    mdata->lsigsuboff[lsigid1][lsigid2+1] = last_macro_match;
1115
+	    mdata->lsigsuboff_last[lsigid1][lsigid2+1] = last_macro_match;
1116 1116
 	}
1117 1117
     }
1118 1118
 }
... ...
@@ -37,7 +37,7 @@ struct cli_ac_data {
37 37
     int32_t ***offmatrix;
38 38
     uint32_t partsigs, lsigs, reloffsigs;
39 39
     uint32_t **lsigcnt;
40
-    uint32_t **lsigsuboff;
40
+    uint32_t **lsigsuboff_last, **lsigsuboff_first;
41 41
     uint32_t *offset;
42 42
     uint32_t macro_lastmatch[32];
43 43
     /** Hashset for versioninfo matching */
... ...
@@ -545,7 +545,7 @@ int cli_lsig_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *ac
545 545
 			if(ctx->virname)
546 546
 			    *ctx->virname = root->ac_lsigtable[i]->virname;
547 547
 			return CL_VIRUS;
548
-		    } else if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, ctx->virname, acdata->lsigcnt[i], acdata->lsigsuboff[i], map) == CL_VIRUS) {
548
+		    } else if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, ctx->virname, acdata->lsigcnt[i], acdata->lsigsuboff_first[i], map) == CL_VIRUS) {
549 549
 			return CL_VIRUS;
550 550
 		    }
551 551
 		}
... ...
@@ -556,7 +556,7 @@ int cli_lsig_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *ac
556 556
 		    *ctx->virname = root->ac_lsigtable[i]->virname;
557 557
 		return CL_VIRUS;
558 558
 	    }
559
-	    if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, ctx->virname, acdata->lsigcnt[i], acdata->lsigsuboff[i], map) == CL_VIRUS) {
559
+	    if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, root->ac_lsigtable[i]->bc_idx, ctx->virname, acdata->lsigcnt[i], acdata->lsigsuboff_first[i], map) == CL_VIRUS) {
560 560
 		return CL_VIRUS;
561 561
 	    }
562 562
 	}