Browse code

bb11420 - fix preclass/cache interaction.

Steven Morgan authored on 2015/11/05 04:46:46
Showing 2 changed files
... ...
@@ -896,22 +896,11 @@ void cache_remove(unsigned char *md5, size_t size, const struct cl_engine *engin
896 896
     return;
897 897
 }
898 898
 
899
-/* Hashes a file onto the provided buffer and looks it up the cache.
900
-   Returns CL_VIRUS if found, CL_CLEAN if not FIXME or a recoverable error,
901
-   and returns CL_EREAD if unrecoverable */
902
-int cache_check(unsigned char *hash, cli_ctx *ctx) {
899
+int cache_get_MD5(unsigned char *hash, cli_ctx *ctx)
900
+{
903 901
     fmap_t *map;
904 902
     size_t todo, at = 0;
905 903
     void *hashctx;
906
-    int ret;
907
-
908
-    if(!ctx || !ctx->engine || !ctx->engine->cache)
909
-       return CL_VIRUS;
910
-
911
-    if (ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
912
-        cli_dbgmsg("cache_check: Caching disabled. Returning CL_VIRUS.\n");
913
-        return CL_VIRUS;
914
-    }
915 904
 
916 905
     map = *ctx->fmap;
917 906
     todo = map->len;
... ...
@@ -920,7 +909,6 @@ int cache_check(unsigned char *hash, cli_ctx *ctx) {
920 920
     if (!(hashctx))
921 921
         return CL_VIRUS;
922 922
 
923
-
924 923
     while(todo) {
925 924
         const void *buf;
926 925
         size_t readme = todo < FILEBUFF ? todo : FILEBUFF;
... ...
@@ -942,6 +930,29 @@ int cache_check(unsigned char *hash, cli_ctx *ctx) {
942 942
 
943 943
     cl_finish_hash(hashctx, hash);
944 944
 
945
+    return CL_CLEAN;
946
+}
947
+
948
+/* Hashes a file onto the provided buffer and looks it up the cache.
949
+   Returns CL_VIRUS if found, CL_CLEAN if not FIXME or a recoverable error,
950
+   and returns CL_EREAD if unrecoverable */
951
+int cache_check(unsigned char *hash, cli_ctx *ctx) {
952
+    fmap_t *map;
953
+    int ret;
954
+
955
+    if(!ctx || !ctx->engine || !ctx->engine->cache)
956
+       return CL_VIRUS;
957
+
958
+    if (ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
959
+        cli_dbgmsg("cache_check: Caching disabled. Returning CL_VIRUS.\n");
960
+        return CL_VIRUS;
961
+    }
962
+
963
+    ret = cache_get_MD5(hash, ctx);
964
+    if (ret != CL_CLEAN)
965
+        return ret;
966
+        
967
+    map = *ctx->fmap;
945 968
     ret = cache_lookup_hash(hash, map->len, ctx->engine->cache, ctx->recursion);
946 969
     cli_dbgmsg("cache_check: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x is %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], (ret == CL_VIRUS) ? "negative" : "positive");
947 970
     return ret;
... ...
@@ -2565,7 +2565,8 @@ static int magic_scandesc_cleanup(cli_ctx *ctx, cli_file_t type, unsigned char *
2565 2565
     }
2566 2566
     if (cb_retcode == CL_CLEAN && cache_clean) {
2567 2567
         perf_start(ctx, PERFT_CACHE);
2568
-        cache_add(hash, hashed_size, ctx);
2568
+        if (!(SCAN_PROPERTIES))
2569
+            cache_add(hash, hashed_size, ctx);
2569 2570
         perf_stop(ctx, PERFT_CACHE);
2570 2571
     }
2571 2572
     return retcode;
... ...
@@ -2738,17 +2739,27 @@ static int magic_scandesc(cli_ctx *ctx, cli_file_t type)
2738 2738
     }
2739 2739
 
2740 2740
     perf_start(ctx, PERFT_CACHE);
2741
-    res = cache_check(hash, ctx);
2741
+    if (!(SCAN_PROPERTIES))
2742
+        res = cache_check(hash, ctx);
2742 2743
 
2743 2744
 #if HAVE_JSON
2744 2745
     if (SCAN_PROPERTIES /* ctx.options & CL_SCAN_FILE_PROPERTIES && ctx->wrkproperty != NULL */) {
2745 2746
         char hashstr[33];
2746
-        snprintf(hashstr, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
2747
+        ret = cache_get_MD5(hash, ctx);
2748
+        if (ret != CL_SUCCESS) {
2749
+            early_ret_from_magicscan(ret);
2750
+        }
2751
+        snprintf(hashstr, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
2752
+                 hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
2753
+                 hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
2747 2754
 
2748 2755
         ret = cli_jsonstr(ctx->wrkproperty, "FileMD5", hashstr);
2756
+        if (ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE)
2757
+            memset(hash, 0, sizeof(hash));
2749 2758
         if (ret != CL_SUCCESS) {
2750 2759
             early_ret_from_magicscan(ret);
2751 2760
         }
2761
+        res = CL_VIRUS;
2752 2762
     }
2753 2763
 #endif
2754 2764