~ubuntu-branches/ubuntu/raring/clamav/raring

« back to all changes in this revision

Viewing changes to libclamav/unzip.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran
  • Date: 2008-09-05 17:25:34 UTC
  • mfrom: (0.35.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080905172534-yi3f8fkye1o7u1r3
* New upstream version (closes: #497662, #497773)
  - lots of new options for clamd.conf
  - fixes CVEs CVE-2008-3912, CVE-2008-3913, CVE-2008-3914, and
    CVE-2008-1389
* No longer supports --unzip option, so typo is gone (closes: #496276)
* Translations:
  - sv (thanks Martin Bagge <brother@bsnet.se>) (closes: #491760)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "clamav-config.h"
25
25
#endif
26
26
 
27
 
#if HAVE_MMAP
28
27
#include <sys/types.h>
29
28
#include <sys/stat.h>
30
29
#include <fcntl.h>
35
34
#include <string.h>
36
35
#endif
37
36
#include <stdlib.h>
 
37
 
 
38
#if HAVE_MMAP
38
39
#ifdef HAVE_SYS_MMAN_H
39
40
#include <sys/mman.h>
40
41
#endif
 
42
#endif /* HAVE_MMAP */
 
43
 
41
44
#include <stdio.h>
42
45
 
43
46
#include <zlib.h>
64
67
  return inflateInit2(a, b);
65
68
}
66
69
 
 
70
static inline void destroy_map(void *map, size_t fsize) {
 
71
#if HAVE_MMAP
 
72
  munmap(map, fsize);
 
73
#else
 
74
  free(map);
 
75
#endif
 
76
}
 
77
 
 
78
 
67
79
static int unz(uint8_t *src, uint32_t csize, uint32_t usize, uint16_t method, uint16_t flags, unsigned int *fu, cli_ctx *ctx, char *tmpd) {
68
80
  char name[1024], obuf[BUFSIZ];
69
81
  char *tempfile = name;
178
190
 
179
191
#if HAVE_BZLIB_H
180
192
#ifdef NOBZ2PREFIX
 
193
#define BZ2_bzDecompress bzDecompress
 
194
#define BZ2_bzDecompressEnd bzDecompressEnd
181
195
#define BZ2_bzDecompressInit bzDecompressInit
182
 
#define BZ2_bzDecompressEnd bzDecompressEnd
183
 
#define BZ2_bzDecompress bzDecompress
184
196
#endif
185
197
 
186
198
  case ALG_BZIP2: {
290
302
    lseek(of, 0, SEEK_SET);
291
303
    ret = cli_magic_scandesc(of, ctx);
292
304
    close(of);
293
 
    if(!cli_leavetemps_flag) unlink(tempfile);
 
305
    if(!cli_leavetemps_flag)
 
306
      if(cli_unlink(tempfile)) ret = CL_EIO;
294
307
    if(!tmpd) free(tempfile);
295
308
    return ret;
296
309
  }
297
310
 
298
311
  close(of);
299
 
  if(!cli_leavetemps_flag) unlink(tempfile);
 
312
  if(!cli_leavetemps_flag)
 
313
    if(cli_unlink(tempfile)) ret = CL_EIO;
300
314
  if(!tmpd) free(tempfile);
301
315
  cli_dbgmsg("cli_unzip: extraction failed\n");
302
316
  return ret;
333
347
  zip+=LH_flen;
334
348
  zsize-=LH_flen;
335
349
 
336
 
  cli_dbgmsg("cli_unzip: lh - ZMDNAME:%d:%s:%u:%u:%u:%u:%u:%u\n", ((LH_flags & F_ENCR)==0), name, LH_usize, LH_csize, LH_crc32, LH_method, fc, ctx->recursion);
 
350
  cli_dbgmsg("cli_unzip: lh - ZMDNAME:%d:%s:%u:%u:%x:%u:%u:%u\n", ((LH_flags & F_ENCR)!=0), name, LH_usize, LH_csize, LH_crc32, LH_method, fc, ctx->recursion);
337
351
  /* ZMDfmt virname:encrypted(0-1):filename(exact|*):usize(exact|*):csize(exact|*):crc32(exact|*):method(exact|*):fileno(exact|*):maxdepth(exact|*) */
338
352
 
339
353
  while(meta &&
345
359
         (meta->method>0 && meta->method != LH_method) ||
346
360
         (meta->fileno   && meta->fileno != fc ) ||
347
361
         (meta->maxdepth && ctx->recursion > meta->maxdepth) ||
348
 
         (meta->filename && strcmp(name, meta->filename)) /* TODO: use a regex */
 
362
         (meta->filename && !cli_matchregex(name, meta->filename))
349
363
         )
350
364
        ) meta = meta->next;
351
365
  if(meta) {
478
492
    cli_dbgmsg("cli_unzip: file too short\n");
479
493
    return CL_CLEAN;
480
494
  }
 
495
 
 
496
#if HAVE_MMAP
481
497
  if ((map = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, f, 0))==MAP_FAILED) {
482
498
    cli_dbgmsg("cli_unzip: mmap failed\n");
483
499
    return CL_EMEM;
484
500
  }
 
501
#else
 
502
  if(fsize > CLI_MAX_ALLOCATION) {
 
503
    cli_warnmsg("cli_unzip: unzip support not compiled in and file is too big\n");
 
504
    return CL_CLEAN;
 
505
  }
 
506
  lseek(f, 0, SEEK_SET);
 
507
  if(!(map = cli_malloc(fsize)))
 
508
    return CL_EMEM;
 
509
  if(cli_readn(f, map, fsize)!=fsize) {
 
510
    free(map);
 
511
    return CL_EIO;
 
512
  }
 
513
#endif
485
514
 
486
515
  if (!(tmpd = cli_gentemp(NULL))) {
487
 
    munmap(map, fsize);
 
516
    destroy_map(map, fsize);
488
517
    return CL_ETMPDIR;
489
518
  }
490
519
  if (mkdir(tmpd, 0700)) {
491
520
    cli_dbgmsg("cli_unzip: Can't create temporary directory %s\n", tmpd);
492
 
    munmap(map, fsize);
 
521
    destroy_map(map, fsize);
493
522
    free(tmpd);
494
523
    return CL_ETMPDIR;
495
524
  }
525
554
    }
526
555
  }
527
556
 
528
 
  munmap(map, fsize);
 
557
  destroy_map(map, fsize);
529
558
  if (!cli_leavetemps_flag) cli_rmdirs(tmpd);
530
559
  free(tmpd);
531
560
 
554
583
    return CL_CLEAN;
555
584
  }
556
585
 
 
586
#if HAVE_MMAP
557
587
  if ((map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0))==MAP_FAILED) {
558
588
    cli_dbgmsg("cli_unzip: mmap() failed\n");
559
589
    return CL_EMEM;
560
590
  }
561
 
 
 
591
#else
 
592
  if(st.st_size > CLI_MAX_ALLOCATION) {
 
593
    cli_warnmsg("cli_unzip: unzip support not compiled in and file is too big\n");
 
594
    return CL_CLEAN;
 
595
  }
 
596
  lseek(f, 0, SEEK_SET);
 
597
  if(!(map = cli_malloc(st.st_size)))
 
598
    return CL_EMEM;
 
599
  if(cli_readn(f, map, st.st_size)!=st.st_size) {
 
600
    free(map);
 
601
    return CL_EIO;
 
602
  }
 
603
#endif
562
604
  lhdr(&map[lhoffl], fsize, &fu, 0, NULL, &ret, ctx, NULL);
563
605
 
564
 
  munmap(map, st.st_size);
 
606
  destroy_map(map, st.st_size);
565
607
  return ret;
566
608
}
567
 
 
568
 
#else /* HAVE_MMAP */
569
 
 
570
 
#include "others.h"
571
 
#include "clamav.h"
572
 
int cli_unzip(int f, cli_ctx *ctx) {
573
 
  cli_warnmsg("cli_unzip: unzip support not compiled in\n");
574
 
  return CL_CLEAN;
575
 
}
576
 
 
577
 
#endif /* HAVE_MMAP */