~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/cpio.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2009 Sourcefire, Inc.
 
3
 *
 
4
 *  Authors: Tomasz Kojm <tkojm@clamav.net>
 
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
#if HAVE_CONFIG_H
 
22
#include "clamav-config.h"
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <ctype.h>
 
28
#ifdef HAVE_UNISTD_H
 
29
#include <unistd.h>
 
30
#endif
 
31
#include <sys/types.h>
 
32
#include <sys/stat.h>
 
33
#include <fcntl.h>
 
34
 
 
35
#include "cltypes.h"
 
36
#include "others.h"
 
37
#include "cpio.h"
 
38
#include "scanners.h"
 
39
#include "matcher.h"
 
40
 
 
41
struct cpio_hdr_old {
 
42
    uint16_t magic;
 
43
    uint16_t dev;
 
44
    uint16_t ino;
 
45
    uint16_t mode;
 
46
    uint16_t uid;
 
47
    uint16_t gid;
 
48
    uint16_t nlink;
 
49
    uint16_t rdev;
 
50
    uint16_t mtime[2];
 
51
    uint16_t namesize;
 
52
    uint16_t filesize[2];
 
53
};
 
54
 
 
55
struct cpio_hdr_odc {
 
56
    char magic[6];
 
57
    char dev[6];
 
58
    char ino[6];
 
59
    char mode[6];
 
60
    char uid[6];
 
61
    char gid[6];
 
62
    char nlink[6];
 
63
    char rdev[6];
 
64
    char mtime[11];
 
65
    char namesize[6];
 
66
    char filesize[11];
 
67
};
 
68
 
 
69
struct cpio_hdr_newc {
 
70
    char magic[6];
 
71
    char ino[8];
 
72
    char mode[8];
 
73
    char uid[8];
 
74
    char gid[8];
 
75
    char nlink[8];
 
76
    char mtime[8];
 
77
    char filesize[8];
 
78
    char devmajor[8];
 
79
    char devminor[8];
 
80
    char rdevmajor[8];
 
81
    char rdevminor[8];
 
82
    char namesize[8];
 
83
    char check[8];
 
84
};
 
85
 
 
86
#define EC16(v, conv)   (conv ? cbswap16(v) : v)
 
87
 
 
88
static void sanitname(char *name)
 
89
{
 
90
    while(*name) {
 
91
        if(!isascii(*name) || strchr("%\\\t\n\r", *name))
 
92
            *name = '_';
 
93
        name++;
 
94
    }
 
95
}
 
96
 
 
97
int cli_scancpio_old(int fd, cli_ctx *ctx)
 
98
{
 
99
        struct cpio_hdr_old hdr_old;
 
100
        char name[513];
 
101
        unsigned int file = 0, trailer = 0;
 
102
        uint32_t filesize, namesize, hdr_namesize;
 
103
        int ret, conv;
 
104
        off_t pos;
 
105
 
 
106
 
 
107
    while(read(fd, &hdr_old, sizeof(hdr_old)) == sizeof(hdr_old)) {
 
108
        if(!hdr_old.magic && trailer)
 
109
            return CL_SUCCESS;
 
110
 
 
111
        if(hdr_old.magic == 070707) {
 
112
            conv = 0;
 
113
        } else if(hdr_old.magic == 0143561) {
 
114
            conv = 1;
 
115
        } else {
 
116
            cli_dbgmsg("cli_scancpio_old: Invalid magic number\n");
 
117
            return CL_EFORMAT;
 
118
        }
 
119
 
 
120
        cli_dbgmsg("CPIO: -- File %u --\n", ++file);
 
121
 
 
122
        if(hdr_old.namesize) {
 
123
            hdr_namesize = EC16(hdr_old.namesize, conv);
 
124
            namesize = MIN(sizeof(name), hdr_namesize);
 
125
            if(read(fd, name, namesize) != namesize) {
 
126
                cli_dbgmsg("cli_scancpio_old: Can't read file name\n");
 
127
                return CL_EFORMAT;
 
128
            }
 
129
            name[namesize - 1] = 0;
 
130
            sanitname(name);
 
131
            cli_dbgmsg("CPIO: Name: %s\n", name);
 
132
            if(!strcmp(name, "TRAILER!!!"))
 
133
                trailer = 1;
 
134
 
 
135
            if(namesize < hdr_namesize) {
 
136
                if(hdr_namesize % 2)
 
137
                    hdr_namesize++;
 
138
                lseek(fd, hdr_namesize - namesize, SEEK_CUR);
 
139
            } else if(hdr_namesize % 2)
 
140
                lseek(fd, 1, SEEK_CUR);
 
141
        }
 
142
        filesize = (uint32_t) (EC16(hdr_old.filesize[0], conv) << 16 | EC16(hdr_old.filesize[1], conv));
 
143
        cli_dbgmsg("CPIO: Filesize: %u\n", filesize);
 
144
        if(!filesize)
 
145
            continue;
 
146
 
 
147
        if(cli_matchmeta(ctx, name, filesize, filesize, 0, file, 0, NULL) == CL_VIRUS)
 
148
            return CL_VIRUS;
 
149
 
 
150
        pos = lseek(fd, 0, SEEK_CUR);
 
151
 
 
152
        if((EC16(hdr_old.mode, conv) & 0170000) != 0100000) {
 
153
            cli_dbgmsg("CPIO: Not a regular file, skipping\n");
 
154
        } else {
 
155
            ret = cli_checklimits("cli_scancpio_old", ctx, filesize, 0, 0);
 
156
            if(ret == CL_EMAXFILES) {
 
157
                return ret;
 
158
            } else if(ret == CL_SUCCESS) {
 
159
                ret = cli_dumpscan(fd, 0, filesize, ctx);
 
160
                if(ret == CL_VIRUS)
 
161
                    return ret;
 
162
            }
 
163
        }
 
164
        if(filesize % 2)
 
165
            filesize++;
 
166
 
 
167
        lseek(fd, pos + filesize, SEEK_SET);
 
168
    }
 
169
 
 
170
    return CL_CLEAN;
 
171
}
 
172
 
 
173
int cli_scancpio_odc(int fd, cli_ctx *ctx)
 
174
{
 
175
        struct cpio_hdr_odc hdr_odc;
 
176
        char name[513], buff[12];
 
177
        unsigned int file = 0, trailer = 0;
 
178
        uint32_t filesize, namesize, hdr_namesize;
 
179
        int ret;
 
180
        off_t pos;
 
181
 
 
182
 
 
183
    while(read(fd, &hdr_odc, sizeof(hdr_odc)) == sizeof(hdr_odc)) {
 
184
 
 
185
        if(!hdr_odc.magic[0] && trailer)
 
186
            return CL_SUCCESS;
 
187
 
 
188
        if(strncmp(hdr_odc.magic, "070707", 6)) {
 
189
            cli_dbgmsg("cli_scancpio_odc: Invalid magic string\n");
 
190
            return CL_EFORMAT;
 
191
        }
 
192
 
 
193
        cli_dbgmsg("CPIO: -- File %u --\n", ++file);
 
194
 
 
195
        strncpy(buff, hdr_odc.namesize, 6);
 
196
        buff[6] = 0;
 
197
        if(sscanf(buff, "%o", &hdr_namesize) != 1) {
 
198
            cli_dbgmsg("cli_scancpio_odc: Can't convert name size\n");
 
199
            return CL_EFORMAT;
 
200
        }
 
201
        if(hdr_namesize) {
 
202
            namesize = MIN(sizeof(name), hdr_namesize);
 
203
            if(read(fd, name, namesize) != namesize) {
 
204
                cli_dbgmsg("cli_scancpio_odc: Can't read file name\n");
 
205
                return CL_EFORMAT;
 
206
            }
 
207
            name[namesize - 1] = 0;
 
208
            sanitname(name);
 
209
            cli_dbgmsg("CPIO: Name: %s\n", name);
 
210
            if(!strcmp(name, "TRAILER!!!"))
 
211
                trailer = 1;
 
212
 
 
213
            if(namesize < hdr_namesize)
 
214
                lseek(fd, hdr_namesize - namesize, SEEK_CUR);
 
215
        }
 
216
 
 
217
        strncpy(buff, hdr_odc.filesize, 11);
 
218
        buff[11] = 0;
 
219
        if(sscanf(buff, "%o", &filesize) != 1) {
 
220
            cli_dbgmsg("cli_scancpio_odc: Can't convert file size\n");
 
221
            return CL_EFORMAT;
 
222
        }
 
223
        cli_dbgmsg("CPIO: Filesize: %u\n", filesize);
 
224
        if(!filesize)
 
225
            continue;
 
226
 
 
227
        if(cli_matchmeta(ctx, name, filesize, filesize, 0, file, 0, NULL) == CL_VIRUS)
 
228
            return CL_VIRUS;
 
229
 
 
230
        pos = lseek(fd, 0, SEEK_CUR);
 
231
 
 
232
        ret = cli_checklimits("cli_scancpio_odc", ctx, filesize, 0, 0);
 
233
        if(ret == CL_EMAXFILES) {
 
234
            return ret;
 
235
        } else if(ret == CL_SUCCESS) {
 
236
            ret = cli_dumpscan(fd, 0, filesize, ctx);
 
237
            if(ret == CL_VIRUS)
 
238
                return ret;
 
239
        }
 
240
 
 
241
        lseek(fd, pos + filesize, SEEK_SET);
 
242
    }
 
243
 
 
244
    return CL_CLEAN;
 
245
}
 
246
 
 
247
int cli_scancpio_newc(int fd, cli_ctx *ctx, int crc)
 
248
{
 
249
        struct cpio_hdr_newc hdr_newc;
 
250
        char name[513], buff[9];
 
251
        unsigned int file = 0, trailer = 0;
 
252
        uint32_t filesize, namesize, hdr_namesize, pad;
 
253
        int ret;
 
254
        off_t pos;
 
255
 
 
256
 
 
257
    while(read(fd, &hdr_newc, sizeof(hdr_newc)) == sizeof(hdr_newc)) {
 
258
 
 
259
        if(!hdr_newc.magic[0] && trailer)
 
260
            return CL_SUCCESS;
 
261
 
 
262
        if((!crc && strncmp(hdr_newc.magic, "070701", 6)) || (crc && strncmp(hdr_newc.magic, "070702", 6))) {
 
263
            cli_dbgmsg("cli_scancpio_newc: Invalid magic string\n");
 
264
            return CL_EFORMAT;
 
265
        }
 
266
 
 
267
        cli_dbgmsg("CPIO: -- File %u --\n", ++file);
 
268
 
 
269
        strncpy(buff, hdr_newc.namesize, 8);
 
270
        buff[8] = 0;
 
271
        if(sscanf(buff, "%x", &hdr_namesize) != 1) {
 
272
            cli_dbgmsg("cli_scancpio_newc: Can't convert name size\n");
 
273
            return CL_EFORMAT;
 
274
        }
 
275
        if(hdr_namesize) {
 
276
            namesize = MIN(sizeof(name), hdr_namesize);
 
277
            if(read(fd, name, namesize) != namesize) {
 
278
                cli_dbgmsg("cli_scancpio_newc: Can't read file name\n");
 
279
                return CL_EFORMAT;
 
280
            }
 
281
            name[namesize - 1] = 0;
 
282
            sanitname(name);
 
283
            cli_dbgmsg("CPIO: Name: %s\n", name);
 
284
            if(!strcmp(name, "TRAILER!!!"))
 
285
                trailer = 1;
 
286
 
 
287
            pad = (4 - (sizeof(hdr_newc) + hdr_namesize) % 4) % 4;
 
288
            if(namesize < hdr_namesize) {
 
289
                if(pad)
 
290
                    hdr_namesize += pad;
 
291
                lseek(fd, hdr_namesize - namesize, SEEK_CUR);
 
292
            } else if(pad)
 
293
                lseek(fd, pad, SEEK_CUR);
 
294
        }
 
295
 
 
296
        strncpy(buff, hdr_newc.filesize, 8);
 
297
        buff[8] = 0;
 
298
        if(sscanf(buff, "%x", &filesize) != 1) {
 
299
            cli_dbgmsg("cli_scancpio_newc: Can't convert file size\n");
 
300
            return CL_EFORMAT;
 
301
        }
 
302
        cli_dbgmsg("CPIO: Filesize: %u\n", filesize);
 
303
        if(!filesize)
 
304
            continue;
 
305
 
 
306
        if(cli_matchmeta(ctx, name, filesize, filesize, 0, file, 0, NULL) == CL_VIRUS)
 
307
            return CL_VIRUS;
 
308
 
 
309
        pos = lseek(fd, 0, SEEK_CUR);
 
310
 
 
311
        ret = cli_checklimits("cli_scancpio_newc", ctx, filesize, 0, 0);
 
312
        if(ret == CL_EMAXFILES) {
 
313
            return ret;
 
314
        } else if(ret == CL_SUCCESS) {
 
315
            ret = cli_dumpscan(fd, 0, filesize, ctx);
 
316
            if(ret == CL_VIRUS)
 
317
                return ret;
 
318
        }
 
319
 
 
320
        if((pad = filesize % 4))
 
321
            filesize += (4 - pad);
 
322
 
 
323
        lseek(fd, pos + filesize, SEEK_SET);
 
324
    }
 
325
 
 
326
    return CL_CLEAN;
 
327
}