~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to libclamav/nsis/nulsft.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <unistd.h>
32
32
#endif
33
33
 
 
34
#include <openssl/ssl.h>
 
35
#include <openssl/err.h>
 
36
#include "libclamav/crypto.h"
 
37
 
34
38
#include "others.h"
35
39
#include "cltypes.h"
36
40
#include "nsis_bzlib.h"
55
59
struct nsis_st {
56
60
  size_t curpos;
57
61
  int ofd;
 
62
  int opened;
58
63
  off_t off;
59
64
  off_t fullsz;
60
65
  char *dir;
185
190
static int nsis_unpack_next(struct nsis_st *n, cli_ctx *ctx) {
186
191
  const unsigned char *ibuf;
187
192
  uint32_t size, loops;
188
 
  int ret, gotsome=0;
 
193
  int ret, gotsome=0, opened=0;
189
194
  unsigned char obuf[BUFSIZ];
190
195
 
191
196
  if (n->eof) {
202
207
    snprintf(n->ofn, 1023, "%s"PATHSEP"headers", n->dir);
203
208
 
204
209
  n->fno++;
205
 
 
206
 
  if ((n->ofd=open(n->ofn, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600))==-1) {
207
 
    cli_errmsg("NSIS: unable to create output file %s - aborting.", n->ofn);
208
 
    return CL_ECREAT;
209
 
  }
 
210
  n->opened = 0;
210
211
 
211
212
  if (!n->solid) {
212
213
    if (fmap_readn(n->map, &size, n->curpos, 4)!=4) {
213
214
      cli_dbgmsg("NSIS: reached EOF - extraction complete\n");
214
 
      close(n->ofd);
215
215
      return CL_BREAK;
216
216
    }
217
217
    n->curpos += 4;
218
218
    if (n->asz==4) {
219
219
      cli_dbgmsg("NSIS: reached CRC - extraction complete\n");
220
 
      close(n->ofd);
221
220
      return CL_BREAK;
222
221
    }
223
222
    loops = EC32(size);
227
226
    }
228
227
    if (n->asz <4 || size > n->asz-4) {
229
228
      cli_dbgmsg("NSIS: next file is outside the archive\n");
230
 
      close(n->ofd);
231
229
      return CL_BREAK;
232
230
    }
233
231
 
234
232
    n->asz -= size+4;
235
233
 
236
234
    if ((ret=cli_checklimits("NSIS", ctx, size, 0, 0))!=CL_CLEAN) {
237
 
      close(n->ofd);
238
235
      n->curpos += size;
239
236
      return ret;
240
237
    }
241
238
    if (!(ibuf = fmap_need_off_once(n->map, n->curpos, size))) {
242
239
      cli_dbgmsg("NSIS: cannot read %u bytes"__AT__"\n", size);
243
 
      close(n->ofd);
244
240
      return CL_EREAD;
245
241
    }
 
242
  if ((n->ofd=open(n->ofn, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600))==-1) {
 
243
    cli_errmsg("NSIS: unable to create output file %s - aborting.", n->ofn);
 
244
    return CL_ECREAT;
 
245
  }
 
246
  n->opened = 1;
246
247
    n->curpos += size;
247
248
    if (loops==size) {
 
249
 
248
250
      if (cli_writen(n->ofd, ibuf, size) != (ssize_t) size) {
249
251
        cli_dbgmsg("NSIS: cannot write output file"__AT__"\n");
250
252
        close(n->ofd);
317
319
    if (!n->freeme) {
318
320
      if ((ret=nsis_init(n))!=CL_SUCCESS) {
319
321
        cli_dbgmsg("NSIS: decompressor init failed\n");
320
 
        close(n->ofd);
321
322
        return ret;
322
323
      }
323
324
      if(!(n->freeme = fmap_need_off_once(n->map, n->curpos, n->asz))) {
324
325
        cli_dbgmsg("NSIS: cannot read %u bytes"__AT__"\n", n->asz);
325
 
        close(n->ofd);
326
326
        return CL_EREAD;
327
327
      }
328
328
      n->nsis.next_in = (void*)n->freeme;
331
331
 
332
332
    if (n->nsis.avail_in<=4) {
333
333
      cli_dbgmsg("NSIS: extraction complete\n");
334
 
      close(n->ofd);
335
334
      return CL_BREAK;
336
335
    }
337
336
    n->nsis.next_out = obuf;
349
348
 
350
349
    if (ret != CL_SUCCESS) {
351
350
      cli_dbgmsg("NSIS: bad stream"__AT__"\n");
352
 
      close(n->ofd);
353
351
      return CL_EFORMAT;
354
352
    }
355
353
 
356
354
    size=cli_readint32(obuf);
357
355
    if ((ret=cli_checklimits("NSIS", ctx, size, 0, 0))!=CL_CLEAN) {
358
 
      close(n->ofd);
359
356
      return ret;
360
357
    }
361
358
 
 
359
    if (size == 0) {
 
360
        cli_dbgmsg("NSIS: Empty file found.\n");
 
361
        return CL_SUCCESS;
 
362
    }
 
363
 
362
364
    n->nsis.next_out = obuf;
363
365
    n->nsis.avail_out = MIN(BUFSIZ,size);
364
366
    loops = 0;
365
367
 
 
368
      if ((n->ofd=open(n->ofn, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600))==-1) {
 
369
        cli_errmsg("NSIS: unable to create output file %s - aborting.", n->ofn);
 
370
        return CL_ECREAT;
 
371
      }
 
372
      n->opened = 1;
 
373
 
366
374
    while (size && (ret=nsis_decomp(n))==CL_SUCCESS) {
367
375
      unsigned int wsz;
368
376
      if ((wsz = n->nsis.next_out - obuf)) {
510
518
 
511
519
    do {
512
520
        ret = cli_nsis_unpack(&nsist, ctx);
 
521
        if (ret == CL_SUCCESS && nsist.opened == 0) {
 
522
            /* Don't scan a non-existent file */
 
523
            continue;
 
524
        }
513
525
        if (ret == CL_SUCCESS) {
514
526
          cli_dbgmsg("NSIS: Successully extracted file #%u\n", nsist.fno);
515
527
          if (lseek(nsist.ofd, 0, SEEK_SET) == -1) {