~ubuntu-branches/ubuntu/trusty/clamav/trusty-proposed

« back to all changes in this revision

Viewing changes to libclamav/7z_iface.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-02-01 11:06:17 UTC
  • mfrom: (0.35.37 sid)
  • Revision ID: package-import@ubuntu.com-20140201110617-33h2xxk09dep0ui4
Tags: 0.98.1+dfsg-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes
  - Add autopkgtest

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2011 Sourcefire, Inc.
 
3
 *
 
4
 *  Authors: aCaB
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License version 2 as
 
8
 *  published by the Free Software Foundation.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
18
 *  MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
 
 
22
#include "7z_iface.h"
 
23
#include "lzma_iface.h"
 
24
#include "scanners.h"
 
25
#include "others.h"
 
26
#include "fmap.h"
 
27
 
 
28
#include "7z/7z.h"
 
29
#include "7z/7zAlloc.h"
 
30
#include "7z/7zFile.h"
 
31
 
 
32
 
 
33
static ISzAlloc allocImp = { __lzma_wrap_alloc, __lzma_wrap_free}, allocTempImp = { __lzma_wrap_alloc, __lzma_wrap_free};
 
34
 
 
35
static SRes FileInStream_fmap_Read(void *pp, void *buf, size_t *size) {
 
36
    CFileInStream *p = (CFileInStream *)pp;
 
37
    int read_sz;
 
38
 
 
39
    if (*size == 0)
 
40
        return 0;
 
41
 
 
42
    read_sz = fmap_readn(p->file.fmap, buf, p->s.curpos, *size);
 
43
    if(read_sz < 0) {
 
44
        *size = 0;
 
45
        return SZ_ERROR_READ;
 
46
    }
 
47
 
 
48
    p->s.curpos += read_sz;
 
49
 
 
50
    *size = read_sz;
 
51
    return SZ_OK;
 
52
}
 
53
 
 
54
static SRes FileInStream_fmap_Seek(void *pp, Int64 *pos, ESzSeek origin) {
 
55
    CFileInStream *p = (CFileInStream *)pp;
 
56
 
 
57
    switch (origin) {
 
58
    case SZ_SEEK_SET:
 
59
        p->s.curpos = *pos;
 
60
        break;
 
61
    case SZ_SEEK_CUR:
 
62
        p->s.curpos += *pos;
 
63
        *pos = p->s.curpos;
 
64
        break;
 
65
    case SZ_SEEK_END:
 
66
        p->s.curpos = p->file.fmap->len + *pos;
 
67
        *pos = p->s.curpos;
 
68
        break;
 
69
    default:
 
70
        return 1;
 
71
    }
 
72
    return 0;
 
73
}
 
74
 
 
75
#define UTFBUFSZ 256
 
76
int cli_7unz (cli_ctx *ctx, size_t offset) {
 
77
    CFileInStream archiveStream;
 
78
    CLookToRead lookStream;
 
79
    CSzArEx db;
 
80
    SRes res;
 
81
    UInt16 utf16buf[UTFBUFSZ], *utf16name = utf16buf;
 
82
    int namelen = UTFBUFSZ, found = CL_CLEAN;
 
83
    Int64 begin_of_archive = offset;
 
84
    UInt32 viruses_found = 0;
 
85
 
 
86
    /* Replacement for 
 
87
       FileInStream_CreateVTable(&archiveStream); */
 
88
    archiveStream.s.Read = FileInStream_fmap_Read;
 
89
    archiveStream.s.Seek = FileInStream_fmap_Seek;
 
90
    archiveStream.s.curpos = 0;
 
91
    archiveStream.file.fmap = *ctx->fmap;
 
92
 
 
93
    LookToRead_CreateVTable(&lookStream, False);
 
94
  
 
95
    if(archiveStream.s.Seek(&archiveStream.s, &begin_of_archive, SZ_SEEK_SET) != 0)
 
96
        return CL_CLEAN;
 
97
 
 
98
    lookStream.realStream = &archiveStream.s;
 
99
    LookToRead_Init(&lookStream);
 
100
 
 
101
    SzArEx_Init(&db);
 
102
    res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
 
103
    if(res == SZ_OK) {
 
104
        UInt32 i, blockIndex = 0xFFFFFFFF;
 
105
        Byte *outBuffer = 0;
 
106
        size_t outBufferSize = 0;
 
107
        unsigned int encrypted = 0;
 
108
 
 
109
        for (i = 0; i < db.db.NumFiles; i++) {
 
110
            size_t offset = 0;
 
111
            size_t outSizeProcessed = 0;
 
112
            const CSzFileItem *f = db.db.Files + i;
 
113
            char *name;
 
114
            size_t j;
 
115
            int newnamelen, fd;
 
116
 
 
117
            if((found = cli_checklimits("7unz", ctx, 0, 0, 0)))
 
118
                break;
 
119
 
 
120
            if (f->IsDir)
 
121
                continue;
 
122
 
 
123
            if(cli_checklimits("7unz", ctx, f->Size, 0, 0))
 
124
                continue;
 
125
 
 
126
            if (!db.FileNameOffsets)
 
127
                newnamelen = 0; /* no filename */
 
128
            else {
 
129
                newnamelen = SzArEx_GetFileNameUtf16(&db, i, NULL);
 
130
                if (newnamelen > namelen) {
 
131
                    if(namelen > UTFBUFSZ)
 
132
                        free(utf16name);
 
133
                    utf16name = cli_malloc(newnamelen*2);
 
134
                    if(!utf16name) {
 
135
                        found = CL_EMEM;
 
136
                        break;
 
137
                    }
 
138
                    namelen = newnamelen;
 
139
                }
 
140
                SzArEx_GetFileNameUtf16(&db, i, utf16name);
 
141
            }
 
142
 
 
143
            name = (char *)utf16name;
 
144
            for(j=0; j<newnamelen; j++) /* FIXME */
 
145
                name[j] = utf16name[j];
 
146
            name[j] = 0;
 
147
            cli_dbgmsg("cli_7unz: extracting %s\n", name);
 
148
 
 
149
            res = SzArEx_Extract(&db, &lookStream.s, i, &blockIndex, &outBuffer, &outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp);
 
150
            if(res == SZ_ERROR_ENCRYPTED) {
 
151
                encrypted = 1;
 
152
                if(DETECT_ENCRYPTED) {
 
153
                    cli_dbgmsg("cli_7unz: Encrypted files found in archive.\n");
 
154
                    cli_append_virus(ctx, "Heuristics.Encrypted.7Zip");
 
155
                    viruses_found++;
 
156
                    if(!SCAN_ALL) {
 
157
                        found = CL_VIRUS;
 
158
                        break;
 
159
                    }
 
160
                }
 
161
            }
 
162
            if(cli_matchmeta(ctx, name, 0, f->Size, encrypted, i, f->CrcDefined ? f->Crc : 0, NULL)) {
 
163
                found = CL_VIRUS;
 
164
                viruses_found++;
 
165
                if (!SCAN_ALL)
 
166
                    break;
 
167
            }
 
168
            if (res != SZ_OK)
 
169
                cli_dbgmsg("cli_unz: extraction failed with %d\n", res);
 
170
            else {
 
171
                if((found = cli_gentempfd(ctx->engine->tmpdir, &name, &fd)))
 
172
                    break;
 
173
                    
 
174
                cli_dbgmsg("cli_7unz: Saving to %s\n", name);
 
175
                if(cli_writen(fd, outBuffer + offset, outSizeProcessed) != outSizeProcessed)
 
176
                    found = CL_EWRITE;
 
177
                else
 
178
                    if ((found = cli_magic_scandesc(fd, ctx)) == CL_VIRUS)
 
179
                        viruses_found++;
 
180
                close(fd);
 
181
                if(!ctx->engine->keeptmp && cli_unlink(name))
 
182
                    found = CL_EUNLINK;
 
183
 
 
184
                free(name);
 
185
                if(found != CL_CLEAN)
 
186
                    if (!(SCAN_ALL && found == CL_VIRUS))
 
187
                        break;
 
188
            }
 
189
        }
 
190
        IAlloc_Free(&allocImp, outBuffer);
 
191
    }
 
192
    SzArEx_Free(&db, &allocImp);
 
193
    if(namelen > UTFBUFSZ)
 
194
        free(utf16name);
 
195
 
 
196
    if (res == SZ_OK)
 
197
        cli_dbgmsg("cli_7unz: completed successfully\n");
 
198
    else if (res == SZ_ERROR_UNSUPPORTED)
 
199
        cli_dbgmsg("cli_7unz: unsupported\n");
 
200
    else if (res == SZ_ERROR_MEM)
 
201
        cli_dbgmsg("cli_7unz: oom\n");
 
202
    else if (res == SZ_ERROR_CRC)
 
203
        cli_dbgmsg("cli_7unz: crc mismatch\n");
 
204
    else
 
205
        cli_dbgmsg("cli_7unz: error %d\n", res);
 
206
 
 
207
    if (SCAN_ALL && viruses_found)
 
208
        return CL_VIRUS;
 
209
    return found;
 
210
}