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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Copyright (C) 2009 Sourcefire, Inc.
 *
 *  Authors: aCaB
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

/* 7zip scanner */

#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "others.h"
#include "lzma_iface.h"
#include "scanners.h"
#include "matcher.h"
#include "7z/7zFile.h"
#include "7z/7zCrc.h"
#include "7z/Archive/7z/7zIn.h"
#include "7z/Archive/7z/7zExtract.h"

static ISzAlloc allocImp = { __lzma_wrap_alloc, __lzma_wrap_free}, allocTempImp = { __lzma_wrap_alloc, __lzma_wrap_free};

int cli_7unz (int fd, cli_ctx *ctx) {
    CFileInStream archiveStream;
    CLookToRead lookStream;
    CSzArEx db;
    UInt32 blockIndex = 0xFFFFFFFF;
    unsigned char *buf = NULL;
    size_t bufsz = 0;
    UInt32 i;
    int dupfd, ret = CL_CLEAN;
    unsigned int fu=0;

    if((dupfd = dup(fd)) == -1) {
	cli_errmsg("cli_7unz: dup() failed\n");
	return CL_EDUP;
    }
    FileInStream_CreateVTable(&archiveStream);
    archiveStream.file.file = fdopen(dupfd, "rb");
    if(!archiveStream.file.file) {
	cli_errmsg("cli_7unz: fdopen() failed\n");
	return CL_EOPEN;
    }
    LookToRead_CreateVTable(&lookStream, False);
    lookStream.realStream = &archiveStream.s;
    LookToRead_Init(&lookStream);

    SzArEx_Init(&db);
    if(SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp) != SZ_OK) {
	SzArEx_Free(&db, &allocImp);
	cli_dbgmsg("cli_7unz: possibly damaged archive\n");
	fclose(archiveStream.file.file);
	return CL_CLEAN;
    }
    for (i = 0; i < db.db.NumFiles; i++) {
	CSzFileItem *f = db.db.Files + i;
        size_t offset;
        size_t usize;

	if(f->IsDir || !f->Size) continue;
	if(ctx->engine->maxfilesize && f->Size > ctx->engine->maxfilesize) {
	    cli_dbgmsg("cli_7unz: skipping stream due to size limits (%llu vs %llu)\n", (long long)f->Size, (long long)ctx->engine->maxfilesize);
	    continue;
	}
	if(cli_matchmeta(ctx, f->Name, 0, f->Size, 0, i + 1, 0, NULL) == CL_VIRUS) {
	    ret = CL_VIRUS;
	    break;
	}
	if (ctx->engine->maxfiles && fu>=ctx->engine->maxfiles) {
	    cli_dbgmsg("cli_7unz: Files limit reached (max: %u)\n", ctx->engine->maxfiles);
	    ret=CL_EMAXFILES;
	    break;
	}
	cli_dbgmsg("cli_7unz: Extracting file %s\n", f->Name);
	if(SzAr_Extract(&db, &lookStream.s, i, &blockIndex, &buf, &bufsz, &offset, &usize, &allocImp, &allocTempImp) == SZ_OK) {
	    char *fname;
	    int ofd;

	    if(!usize) {
		cli_dbgmsg("cli_7unz: stream uncompressed to an empty file\n");
		continue;
	    }
	    fu++;
	    if(!(fname = cli_gentemp(ctx->engine->tmpdir))) {
		ret = CL_EMEM;
		break;
	    }
	    if((ofd = open(fname, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRUSR|S_IWUSR)) < 0) {
		cli_errmsg("cli_7unz: failed to create file %s\n", fname);
		free(fname);
		ret = CL_ECREAT;
		break;
	    }
	    if(cli_writen(ofd, buf, usize) <= 0) {
		close(ofd);
		if(cli_unlink(fname)) ret = CL_EUNLINK;
		else ret = CL_EWRITE;
		free(fname);
		break;
	    }
	    cli_dbgmsg("cli_7unz: extracted to %s\n", fname);
	    lseek(ofd, 0, SEEK_SET);
	    ret = cli_magic_scandesc(ofd, ctx);
	    close(ofd);
	    if(!ctx->engine->keeptmp)
		if(cli_unlink(fname)) ret = CL_EUNLINK;
	    free(fname);
	    if(ret == CL_EUNLINK || ret == CL_VIRUS)
		break;
	} else {
	    cli_dbgmsg("cli_7unz: decompression failed\n");
	}
    }
    if(buf) free(buf);
    SzArEx_Free(&db, &allocImp);
    fclose(archiveStream.file.file);
    return ret;
}