Browse code

fmap in magicscan

aCaB authored on 2009/08/31 02:14:49
Showing 2 changed files
... ...
@@ -38,6 +38,7 @@
38 38
 #include "clamav.h"
39 39
 #include "dconf.h"
40 40
 #include "filetypes.h"
41
+#include "fmap.h"
41 42
 #include "libclamunrar_iface/unrar_iface.h"
42 43
 #include "regex/regex.h"
43 44
 
... ...
@@ -109,6 +110,7 @@ typedef struct {
109 109
     unsigned int found_possibly_unwanted;
110 110
     cli_file_t container_type; /* FIXME: to be made into a stack or array - see bb#1579 & bb#1293 */
111 111
     struct cli_dconf *dconf;
112
+    struct F_MAP **fmap;
112 113
 } cli_ctx;
113 114
 
114 115
 struct cl_engine {
... ...
@@ -40,14 +40,6 @@
40 40
 #include <netinet/in.h>
41 41
 #endif
42 42
 
43
-#if HAVE_MMAP
44
-#if HAVE_SYS_MMAN_H
45
-#include <sys/mman.h>
46
-#else /* HAVE_SYS_MMAN_H */
47
-#undef HAVE_MMAP
48
-#endif
49
-#endif
50
-
51 43
 #ifndef	O_BINARY
52 44
 #define	O_BINARY	0
53 45
 #endif
... ...
@@ -96,6 +88,7 @@
96 96
 #include "macho.h"
97 97
 #include "ishield.h"
98 98
 #include "7z.h"
99
+#include "fmap.h"
99 100
 
100 101
 #ifdef HAVE_BZLIB_H
101 102
 #include <bzlib.h>
... ...
@@ -1890,6 +1883,12 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
1890 1890
     if(cli_updatelimits(ctx, sb.st_size)!=CL_CLEAN)
1891 1891
         return CL_CLEAN;
1892 1892
 
1893
+    ctx->fmap++;
1894
+    if(!(*ctx->fmap = fmap(desc, 0, sb.st_size))) {
1895
+	cli_errmsg("CRITICAL: fmap() failed\n");
1896
+	return CL_EMEM;
1897
+    }
1898
+
1893 1899
     if(!ctx->options || (ctx->recursion == ctx->engine->maxreclevel)) { /* raw mode (stdin, etc.) or last level of recursion */
1894 1900
 	if(ctx->recursion == ctx->engine->maxreclevel)
1895 1901
 	    cli_dbgmsg("cli_magic_scandesc: Hit recursion limit, only scanning raw file\n");
... ...
@@ -1897,21 +1896,28 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
1897 1897
 	    cli_dbgmsg("Raw mode: No support for special files\n");
1898 1898
 	if((ret = cli_scandesc(desc, ctx, 0, 0, NULL, AC_SCAN_VIR)) == CL_VIRUS)
1899 1899
 	    cli_dbgmsg("%s found in descriptor %d\n", *ctx->virname, desc);
1900
+	fmunmap(*ctx->fmap);
1901
+	ctx->fmap--; 
1900 1902
 	return ret;
1901 1903
     }
1902 1904
 
1903
-    lseek(desc, 0, SEEK_SET);
1904
-    type = cli_filetype2(desc, ctx->engine);
1905
+    lseek(desc, 0, SEEK_SET); /* FIXMEFMAP: remove ? */
1906
+    type = cli_filetype2(desc, ctx->engine); /* FIXMEFMAP: port to fmap */
1905 1907
     if(type == CL_TYPE_ERROR) {
1906 1908
 	cli_dbgmsg("cli_magic_scandesc: cli_filetype2 returned CL_TYPE_ERROR\n");
1909
+	fmunmap(*ctx->fmap);
1910
+	ctx->fmap--; 
1907 1911
 	return CL_EREAD;
1908 1912
     }
1909
-    lseek(desc, 0, SEEK_SET);
1913
+    lseek(desc, 0, SEEK_SET); /* FIXMEFMAP: remove ? */
1910 1914
 
1911 1915
     if(type != CL_TYPE_IGNORED && ctx->engine->sdb) {
1912
-	if((ret = cli_scanraw(desc, ctx, type, 0, &dettype)) == CL_VIRUS)
1916
+	if((ret = cli_scanraw(desc, ctx, type, 0, &dettype)) == CL_VIRUS) {
1917
+	    fmunmap(*ctx->fmap);
1918
+	    ctx->fmap--; 
1913 1919
 	    return CL_VIRUS;
1914
-	lseek(desc, 0, SEEK_SET);
1920
+	}
1921
+	lseek(desc, 0, SEEK_SET); /* FIXMEFMAP: remove ? */
1915 1922
     }
1916 1923
 
1917 1924
     ctx->container_type = 0;
... ...
@@ -2113,8 +2119,11 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
2113 2113
     ctx->recursion--;
2114 2114
     ctx->container_type = current_container;
2115 2115
 
2116
-    if(ret == CL_VIRUS)
2116
+    if(ret == CL_VIRUS) {
2117
+	fmunmap(*ctx->fmap);
2118
+	ctx->fmap--; 
2117 2119
 	return CL_VIRUS;
2120
+    }
2118 2121
 
2119 2122
     if(type == CL_TYPE_ZIP && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ZIP)) {
2120 2123
 	if(sb.st_size > 1048576) {
... ...
@@ -2125,8 +2134,11 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
2125 2125
 
2126 2126
     /* CL_TYPE_HTML: raw HTML files are not scanned, unless safety measure activated via DCONF */
2127 2127
     if(type != CL_TYPE_IGNORED && (type != CL_TYPE_HTML || !(DCONF_DOC & DOC_CONF_HTML_SKIPRAW)) && !ctx->engine->sdb) {
2128
-	if(cli_scanraw(desc, ctx, type, typercg, &dettype) == CL_VIRUS)
2128
+	if(cli_scanraw(desc, ctx, type, typercg, &dettype) == CL_VIRUS) {
2129
+	    fmunmap(*ctx->fmap);
2130
+	    ctx->fmap--; 
2129 2131
 	    return CL_VIRUS;
2132
+	}
2130 2133
     }
2131 2134
 
2132 2135
     ctx->recursion++;
... ...
@@ -2153,6 +2165,8 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
2153 2153
 	    break;
2154 2154
     }
2155 2155
     ctx->recursion--;
2156
+    fmunmap(*ctx->fmap);
2157
+    ctx->fmap--;
2156 2158
 
2157 2159
     switch(ret) {
2158 2160
 	case CL_EFORMAT:
... ...
@@ -2179,8 +2193,15 @@ int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, cons
2179 2179
     ctx.found_possibly_unwanted = 0;
2180 2180
     ctx.container_type = 0;
2181 2181
     ctx.dconf = (struct cli_dconf *) engine->dconf;
2182
+    ctx.fmap = cli_calloc(sizeof(struct F_MAP *), ctx.engine->maxreclevel + 1);
2183
+    if(!ctx.fmap)
2184
+	return CL_EMEM;
2185
+    ctx.fmap--;
2182 2186
 
2183 2187
     rc = cli_magic_scandesc(desc, &ctx);
2188
+
2189
+    ctx.fmap++;
2190
+    free(ctx.fmap);
2184 2191
     if(rc == CL_CLEAN && ctx.found_possibly_unwanted)
2185 2192
     	rc = CL_VIRUS;
2186 2193
     return rc;