Browse code

minor improvements for unspin

git-svn: trunk@3367

aCaB authored on 2007/11/12 10:05:41
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Nov 12 01:09:13 CET 2007 (acab)
2
+-----------------------------------
3
+  * libclamav/spin.c: Minor improvements
4
+
1 5
 Thu Nov  8 14:29:00 GMT 2007 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/vba_extract.c:	Removed more unused code
... ...
@@ -194,9 +194,9 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
194 194
 
195 195
   cli_dbgmsg("spin: Key8 is %x, Len is %x\n", key8, len);
196 196
 
197
-  if (!CLI_ISCONTAINED(spinned, sections[sectcnt].rsz, ep, len+0x1fe5-1)) {
197
+  if (!CLI_ISCONTAINED(spinned, sections[sectcnt].rsz, ep, len+0x1fe5-1) || !CLI_ISCONTAINED(spinned, sections[sectcnt].rsz, ep+0x3217, 4)) {
198 198
     free(spinned);
199
-    cli_dbgmsg("spin: len out of bounds, giving up\n");
199
+    cli_dbgmsg("spin: len or key out of bounds, giving up\n");
200 200
     return 1;
201 201
   }
202 202
 
... ...
@@ -212,12 +212,6 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
212 212
     curr--;
213 213
   }
214 214
 
215
-  if (!CLI_ISCONTAINED(spinned, sections[sectcnt].rsz, ep+0x3217, 4)) {
216
-    free(spinned);
217
-    cli_dbgmsg("spin: key out of bounds, giving up\n");
218
-    return 1;
219
-  }
220
-
221 215
   curr = ep+0x26eb;
222 216
   key32 = cli_readint32(curr);
223 217
   if ( (len = cli_readint32(curr+5)) != 0x5a0) {
... ...
@@ -267,7 +261,7 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
267 267
       char *ptr = src + sections[j].raw;
268 268
       uint32_t keydup = key32;
269 269
       
270
-      if (!CLI_ISCONTAINED(src, ssize, ptr, size)) {
270
+      if (!CLI_ISCONTAINED(src, (unsigned int)ssize, ptr, size)) {
271 271
 	cli_dbgmsg("spin: sect %d out of file, giving up\n", j);
272 272
 	return 1; /* FIXME: Already checked in pe.c? */
273 273
       }
... ...
@@ -299,7 +293,7 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
299 299
   cli_dbgmsg("spin: Key is %x, Len is %x\n", key32, len);
300 300
   curr = ep+0x28d3;
301 301
 
302
-  if (!CLI_ISCONTAINED(src, ssize, curr, len)) { /* always true but i may decide to remove the previous check */
302
+  if (!CLI_ISCONTAINED(src, (unsigned int)ssize, curr, len)) { /* always true but i may decide to remove the previous check */
303 303
     cli_dbgmsg("spin: key out of bounds, giving up\n");
304 304
     return 1;
305 305
   }
... ...
@@ -324,7 +318,7 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
324 324
   cli_dbgmsg("spin: POLY1 len is %x\n", len);
325 325
   curr+=0xf; /* POLY1 */
326 326
   emu = ep+0x6d4;
327
-  if (!CLI_ISCONTAINED(src, ssize, emu, len)) {
327
+  if (!CLI_ISCONTAINED(src, (unsigned int)ssize, emu, len)) {
328 328
     cli_dbgmsg("spin: poly1 out of bounds\n");
329 329
     return 1;
330 330
   }
... ...
@@ -372,41 +366,34 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
372 372
   bitmap = cli_readint32(ep+0x3061);
373 373
   bitman = bitmap;
374 374
 
375
-  if(ctx->limits && ctx->limits->maxfilesize) {
376
-    unsigned long int filesize = 0;
377
-    
378
-    for (j=0; j<sectcnt; j++) {
379
-      if (bitmap&1) {
380
-	if ( filesize > ctx->limits->maxfilesize || sections[j].vsz > ctx->limits->maxfilesize - filesize ) return 2;
381
-	filesize += sections[j].vsz;
382
-      }
383
-      bitmap>>=1;
384
-    }
385
-    
386
-    bitmap = bitman;
387
-  }
388
-
389 375
   cli_dbgmsg("spin: Compression bitmap is %x\n", bitmap);
390 376
   if ( (sects= (char **) cli_malloc(sectcnt*sizeof(char *))) == NULL )
391 377
     return 1;
392 378
 
393
-  len = 0;
379
+  key8 = 0;
394 380
   for (j=0; j<sectcnt; j++) {
381
+    uint32_t thissize = (bitmap&1) ? sections[j].vsz : sections[j].rsz;
382
+    if(ctx->limits && ctx->limits->maxfilesize && ((unsigned long int)thissize > ctx->limits->maxfilesize || (unsigned long int)blobsz > ctx->limits->maxfilesize - thissize)) {
383
+      key8++;
384
+      cli_dbgmsg("spin: section %d size exceeded (%u, %lu)\n", j, thissize, ctx->limits->maxfilesize);
385
+      break;
386
+    }
395 387
     if (bitmap&1) {
396
-       if ( (sects[j] = (char *) cli_malloc(sections[j].vsz) ) == NULL ) {
397
-	 cli_dbgmsg("spin: malloc(%d) failed\n", sections[j].vsz);
398
-	 len = 1;
399
-	 break;
400
-       }
401
-       blobsz+=sections[j].vsz;
402
-       memset(sects[j], 0, sections[j].vsz);
403
-       cli_dbgmsg("spin: Growing sect%d: was %x will be %x\n", j, sections[j].rsz, sections[j].vsz);
404
-       if ( cli_unfsg(src + sections[j].raw, sects[j], sections[j].rsz, sections[j].vsz, NULL, NULL) == -1 ) {
405
-	 len++;
406
-         cli_dbgmsg("spin: Unpack failure\n");
407
-       }
388
+      if ( (sects[j] = (char *) cli_calloc(thissize, sizeof(char)) ) == NULL ) {
389
+	cli_dbgmsg("spin: malloc(%d) failed\n", thissize);
390
+	key8++;
391
+	break;
392
+      }
393
+      blobsz+=thissize;
394
+      cli_dbgmsg("spin: Growing sect%d: was %x will be %x\n", j, sections[j].rsz, thissize);
395
+      if ( cli_unfsg(src + sections[j].raw, sects[j], sections[j].rsz, thissize, NULL, NULL) == -1 ) {
396
+	key8++;
397
+	j++;
398
+	cli_dbgmsg("spin: Unpack failure\n");
399
+	break;
400
+      }
408 401
     } else {
409
-      blobsz+=sections[j].rsz;
402
+      blobsz+=thissize;
410 403
       sects[j] = src + sections[j].raw;
411 404
       cli_dbgmsg("spin: Not growing sect%d\n", j);
412 405
     }
... ...
@@ -415,12 +402,12 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
415 415
   
416 416
   cli_dbgmsg("spin: decompression complete\n");
417 417
  
418
-  if ( len ) {
418
+  if ( key8 ) {
419 419
     int t;
420 420
     for (t=0 ; t<j ; t++) {
421 421
       if (bitman&1)
422 422
 	free(sects[t]);
423
-      bitman = bitman >>1 & 0x7fffffff;
423
+      bitman = bitman >>1;
424 424
     }
425 425
     free(sects);
426 426
     return 1;
... ...
@@ -498,7 +485,7 @@ int unspin(char *src, int ssize, struct cli_exe_section *sections, int sectcnt,
498 498
   cli_dbgmsg ("spin: free bitmap is %x\n", bitman);
499 499
   for (j=0; j<sectcnt; j++) {
500 500
     if (bitmap&1) free(sects[j]);
501
-    bitman = bitman >>1 & 0x7fffffff;
501
+    bitman = bitman >>1;
502 502
   }
503 503
   free(sects);
504 504
   return 1; /* :( */