Browse code

bb#5343

David Raynor authored on 2012/06/23 05:55:29
Showing 18 changed files
... ...
@@ -116,6 +116,7 @@ static void MT_decrypt(uint8_t *buf, unsigned int size, uint32_t seed) {
116 116
   for(i=1; i<624; i++)
117 117
     mt[i] = i+0x6c078965*((mt[i-1]>>30)^mt[i-1]);
118 118
   MT.items = 1;
119
+  MT.next = MT.mt;
119 120
 
120 121
   while(size--)
121 122
     *buf++ ^= MT_getnext(&MT);
... ...
@@ -513,6 +513,7 @@ static inline char *readData(const unsigned char *p, unsigned *off, unsigned len
513 513
 	if (UNLIKELY((v0&0xf0) != 0x60 || (v1&0xf0) != 0x60)) {
514 514
 	    cli_errmsg("Invalid data part: %c%c\n", v0, v1);
515 515
 	    *ok = 0;
516
+	    free(dat);
516 517
 	    return 0;
517 518
 	}
518 519
 	*q++ = (v0&0xf) | ((v1&0xf) << 4);
... ...
@@ -884,8 +885,10 @@ static int parseApis(struct cli_bc *bc, unsigned char *buffer)
884 884
 	}
885 885
 	/* don't need the name anymore */
886 886
 	free(name);
887
-	if (!ok)
887
+	if (!ok) {
888
+	    free(apity2ty); /* free temporary map */
888 889
 	    return CL_EMALFDB;
890
+	}
889 891
 
890 892
 	/* APIcall is valid */
891 893
 	cli_bitset_set(bc->uses_apis, id);
... ...
@@ -127,11 +127,13 @@ static int detect_SELinux(void)
127 127
 	return 0;
128 128
 
129 129
     f = fopen("/selinux/enforce", "r");
130
-    if (f && fscanf(f, "%d", &enforce) == 1) {
131
-	if (enforce == 1)
132
-	    selinux = 2;
133
-	if (enforce == -1)
134
-	    selinux = 0;
130
+    if (f) {
131
+	if (fscanf(f, "%d", &enforce) == 1) {
132
+	    if (enforce == 1)
133
+		selinux = 2;
134
+	    if (enforce == -1)
135
+		selinux = 0;
136
+	}
135 137
 	fclose(f);
136 138
     }
137 139
     return selinux;
... ...
@@ -2503,6 +2503,7 @@ void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx)
2503 2503
 #endif
2504 2504
 	if (!lines->buffer) {
2505 2505
 	    errs() << "Unable to open file '" << path << "'\n";
2506
+	    delete lines;
2506 2507
 	    return ;
2507 2508
 	}
2508 2509
 	LinePrinter.files[path] = lines;
... ...
@@ -46,15 +46,28 @@
46 46
 
47 47
 #define TAR_BLOCKSIZE 512
48 48
 
49
+static void cli_untgz_cleanup(char *path, gzFile infile, FILE *outfile, int fdd)
50
+{
51
+    cli_dbgmsg("in cli_untgz_cleanup()\n");
52
+    if (path != NULL)
53
+        free (path);
54
+    if (infile != NULL) 
55
+        gzclose (infile);
56
+    if (outfile != NULL)
57
+        fclose(outfile);
58
+    if (fdd > -1)
59
+        close(fdd);
60
+}
61
+
49 62
 static int cli_untgz(int fd, const char *destdir)
50 63
 {
51 64
 	char *path, osize[13], name[101], type;
52 65
 	char block[TAR_BLOCKSIZE];
53
-	int nbytes, nread, nwritten, in_block = 0, fdd;
66
+	int nbytes, nread, nwritten, in_block = 0, fdd = -1;
54 67
 	unsigned int size, pathlen = strlen(destdir) + 100 + 5;
55 68
 	FILE *outfile = NULL;
56 69
 	struct stat foo;
57
-	gzFile infile;
70
+	gzFile infile = NULL;
58 71
 
59 72
 
60 73
     cli_dbgmsg("in cli_untgz()\n");
... ...
@@ -74,7 +87,7 @@ static int cli_untgz(int fd, const char *destdir)
74 74
     path = (char *) cli_calloc(sizeof(char), pathlen);
75 75
     if(!path) {
76 76
 	cli_errmsg("cli_untgz: Can't allocate memory for path\n");
77
-	gzclose(infile);
77
+	cli_untgz_cleanup(NULL, infile, NULL, fdd);
78 78
 	return -1;
79 79
     }
80 80
 
... ...
@@ -87,8 +100,7 @@ static int cli_untgz(int fd, const char *destdir)
87 87
 
88 88
 	if(nread != TAR_BLOCKSIZE) {
89 89
 	    cli_errmsg("cli_untgz: Incomplete block read\n");
90
-	    free(path);
91
-	    gzclose(infile);
90
+	    cli_untgz_cleanup(path, infile, outfile, fdd);
92 91
 	    return -1;
93 92
 	}
94 93
 
... ...
@@ -101,8 +113,7 @@ static int cli_untgz(int fd, const char *destdir)
101 101
 
102 102
 	    if(strchr(name, '/')) {
103 103
 		cli_errmsg("cli_untgz: Slash separators are not allowed in CVD\n");
104
-		free(path);
105
-	        gzclose(infile);
104
+		cli_untgz_cleanup(path, infile, outfile, fdd);
106 105
 		return -1;
107 106
 	    }
108 107
 
... ...
@@ -116,13 +127,11 @@ static int cli_untgz(int fd, const char *destdir)
116 116
 		    break;
117 117
 		case '5':
118 118
 		    cli_errmsg("cli_untgz: Directories are not supported in CVD\n");
119
-		    free(path);
120
-	            gzclose(infile);
119
+		    cli_untgz_cleanup(path, infile, outfile, fdd);
121 120
 		    return -1;
122 121
 		default:
123 122
 		    cli_errmsg("cli_untgz: Unknown type flag '%c'\n", type);
124
-		    free(path);
125
-	            gzclose(infile);
123
+		    cli_untgz_cleanup(path, infile, outfile, fdd);
126 124
 		    return -1;
127 125
 	    }
128 126
 	    in_block = 1;
... ...
@@ -130,8 +139,7 @@ static int cli_untgz(int fd, const char *destdir)
130 130
 	    if(outfile) {
131 131
 		if(fclose(outfile)) {
132 132
 		    cli_errmsg("cli_untgz: Cannot close file %s\n", path);
133
-		    free(path);
134
-	            gzclose(infile);
133
+		    cli_untgz_cleanup(path, infile, outfile, fdd);
135 134
 		    return -1;
136 135
 		}
137 136
 		outfile = NULL;
... ...
@@ -139,8 +147,7 @@ static int cli_untgz(int fd, const char *destdir)
139 139
 
140 140
 	    if(!(outfile = fopen(path, "wb"))) {
141 141
 		cli_errmsg("cli_untgz: Cannot create file %s\n", path);
142
-		free(path);
143
-	        gzclose(infile);
142
+		cli_untgz_cleanup(path, infile, outfile, fdd);
144 143
 		return -1;
145 144
 	    }
146 145
 
... ...
@@ -149,9 +156,7 @@ static int cli_untgz(int fd, const char *destdir)
149 149
 
150 150
 	    if((sscanf(osize, "%o", &size)) == 0) {
151 151
 		cli_errmsg("cli_untgz: Invalid size in header\n");
152
-		free(path);
153
-	        gzclose(infile);
154
-		fclose(outfile);
152
+		cli_untgz_cleanup(path, infile, outfile, fdd);
155 153
 		return -1;
156 154
 	    }
157 155
 
... ...
@@ -161,8 +166,7 @@ static int cli_untgz(int fd, const char *destdir)
161 161
 
162 162
 	    if(nwritten != nbytes) {
163 163
 		cli_errmsg("cli_untgz: Wrote %d instead of %d (%s)\n", nwritten, nbytes, path);
164
-		free(path);
165
-	        gzclose(infile);
164
+		cli_untgz_cleanup(path, infile, outfile, fdd);
166 165
 		return -1;
167 166
 	    }
168 167
 
... ...
@@ -172,14 +176,29 @@ static int cli_untgz(int fd, const char *destdir)
172 172
 	}
173 173
     }
174 174
 
175
-    if(outfile)
176
-	fclose(outfile);
177
-
178
-    gzclose(infile);
179
-    free(path);
175
+    cli_untgz_cleanup(path, infile, outfile, fdd);
180 176
     return 0;
181 177
 }
182 178
 
179
+static void cli_tgzload_cleanup(int comp, struct cli_dbio *dbio, int fdd)
180
+{
181
+    cli_dbgmsg("in cli_tgzload_cleanup()\n");
182
+    if(comp) {
183
+        gzclose(dbio->gzs);
184
+        dbio->gzs = NULL;
185
+    }
186
+    else {
187
+        fclose(dbio->fs);
188
+        dbio->fs = NULL;
189
+    }
190
+    if(dbio->buf != NULL) {
191
+        free(dbio->buf);
192
+        dbio->buf = NULL;
193
+    }
194
+    if(fdd > -1)
195
+        close(fdd);
196
+}
197
+
183 198
 static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, unsigned int options, struct cli_dbio *dbio, struct cli_dbinfo *dbinfo)
184 199
 {
185 200
 	char osize[13], name[101];
... ...
@@ -190,12 +209,6 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
190 190
 	struct cli_dbinfo *db;
191 191
 	unsigned char hash[32];
192 192
 
193
-#define CLOSE_DBIO	    \
194
-    if(compr)		    \
195
-	gzclose(dbio->gzs);  \
196
-    else		    \
197
-	fclose(dbio->fs)
198
-
199 193
     cli_dbgmsg("in cli_tgzload()\n");
200 194
 
201 195
     lseek(fd, 512, SEEK_SET);
... ...
@@ -215,12 +228,16 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
215 215
     if(compr) {
216 216
 	if((dbio->gzs = gzdopen(fdd, "rb")) == NULL) {
217 217
 	    cli_errmsg("cli_tgzload: Can't gzdopen() descriptor %d, errno = %d\n", fdd, errno);
218
+	    if (fdd > -1)
219
+		close(fdd);
218 220
 	    return CL_EOPEN;
219 221
 	}
220 222
 	dbio->fs = NULL;
221 223
     } else {
222 224
 	if((dbio->fs = fdopen(fdd, "rb")) == NULL) {
223 225
 	    cli_errmsg("cli_tgzload: Can't fdopen() descriptor %d, errno = %d\n", fdd, errno);
226
+	    if (fdd > -1)
227
+		close(fdd);
224 228
 	    return CL_EOPEN;
225 229
 	}
226 230
 	dbio->gzs = NULL;
... ...
@@ -230,7 +247,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
230 230
     dbio->buf = cli_malloc(dbio->bufsize);
231 231
     if(!dbio->buf) {
232 232
 	cli_errmsg("cli_tgzload: Can't allocate memory for dbio->buf\n");
233
-	CLOSE_DBIO;
233
+	cli_tgzload_cleanup(compr, dbio, fdd);
234 234
 	return CL_EMALFDB;
235 235
     }
236 236
     dbio->bufpt = NULL;
... ...
@@ -249,8 +266,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
249 249
 
250 250
 	if(nread != TAR_BLOCKSIZE) {
251 251
 	    cli_errmsg("cli_tgzload: Incomplete block read\n");
252
-	    free(dbio->buf);
253
-	    CLOSE_DBIO;
252
+	    cli_tgzload_cleanup(compr, dbio, fdd);
254 253
 	    return CL_EMALFDB;
255 254
 	}
256 255
 
... ...
@@ -262,8 +278,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
262 262
 
263 263
 	if(strchr(name, '/')) {
264 264
 	    cli_errmsg("cli_tgzload: Slash separators are not allowed in CVD\n");
265
-	    free(dbio->buf);
266
-	    CLOSE_DBIO;
265
+	    cli_tgzload_cleanup(compr, dbio, fdd);
267 266
 	    return CL_EMALFDB;
268 267
 	}
269 268
 
... ...
@@ -275,13 +290,11 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
275 275
 		break;
276 276
 	    case '5':
277 277
 		cli_errmsg("cli_tgzload: Directories are not supported in CVD\n");
278
-		free(dbio->buf);
279
-		CLOSE_DBIO;
278
+		cli_tgzload_cleanup(compr, dbio, fdd);
280 279
 		return CL_EMALFDB;
281 280
 	    default:
282 281
 		cli_errmsg("cli_tgzload: Unknown type flag '%c'\n", type);
283
-		free(dbio->buf);
284
-		CLOSE_DBIO;
282
+		cli_tgzload_cleanup(compr, dbio, fdd);
285 283
 		return CL_EMALFDB;
286 284
 	}
287 285
 
... ...
@@ -290,8 +303,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
290 290
 
291 291
 	if((sscanf(osize, "%o", &size)) == 0) {
292 292
 	    cli_errmsg("cli_tgzload: Invalid size in header\n");
293
-	    free(dbio->buf);
294
-	    CLOSE_DBIO;
293
+	    cli_tgzload_cleanup(compr, dbio, fdd);
295 294
 	    return CL_EMALFDB;
296 295
 	}
297 296
 	dbio->size = size;
... ...
@@ -311,13 +323,11 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
311 311
 	    ret = cli_load(name, engine, signo, options, dbio);
312 312
 	    if(ret) {
313 313
 		cli_errmsg("cli_tgzload: Can't load %s\n", name);
314
-		free(dbio->buf);
315
-		CLOSE_DBIO;
314
+		cli_tgzload_cleanup(compr, dbio, fdd);
316 315
 		return CL_EMALFDB;
317 316
 	    }
318 317
 	    if(!dbinfo) {
319
-		free(dbio->buf);
320
-		CLOSE_DBIO;
318
+		cli_tgzload_cleanup(compr, dbio, fdd);
321 319
 		return CL_SUCCESS;
322 320
 	    } else {
323 321
 		db = dbinfo;
... ...
@@ -325,22 +335,19 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
325 325
 		    db = db->next;
326 326
 		if(!db) {
327 327
 		    cli_errmsg("cli_tgzload: File %s not found in .info\n", name);
328
-		    free(dbio->buf);
329
-		    CLOSE_DBIO;
328
+		    cli_tgzload_cleanup(compr, dbio, fdd);
330 329
 		    return CL_EMALFDB;
331 330
 		}
332 331
 		if(dbio->bread) {
333 332
 		    if(db->size != dbio->bread) {
334 333
 			cli_errmsg("cli_tgzload: File %s not correctly loaded\n", name);
335
-			free(dbio->buf);
336
-			CLOSE_DBIO;
334
+			cli_tgzload_cleanup(compr, dbio, fdd);
337 335
 			return CL_EMALFDB;
338 336
 		    }
339 337
 		    sha256_final(&dbio->sha256ctx, hash);
340 338
 		    if(memcmp(db->hash, hash, 32)) {
341 339
 			cli_errmsg("cli_tgzload: Invalid checksum for file %s\n", name);
342
-			free(dbio->buf);
343
-			CLOSE_DBIO;
340
+			cli_tgzload_cleanup(compr, dbio, fdd);
344 341
 			return CL_EMALFDB;
345 342
 		    }
346 343
 		}
... ...
@@ -360,8 +367,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
360 360
 	}
361 361
     }
362 362
 
363
-    free(dbio->buf);
364
-    CLOSE_DBIO;
363
+    cli_tgzload_cleanup(compr, dbio, fdd);
365 364
     return CL_SUCCESS;
366 365
 }
367 366
 
... ...
@@ -334,6 +334,9 @@ int dlp_is_valid_ssn(const unsigned char *buffer, int length, int format)
334 334
                  return 0;
335 335
              }       
336 336
              break;
337
+        default:
338
+	    cli_dbgmsg("dlp_is_valid_ssn: unknown format type %d \n", format);
339
+	    return 0;
337 340
     }
338 341
         
339 342
     /* start validating */
... ...
@@ -689,6 +689,8 @@ static iconv_t iconv_open_cached(const char* fromcode)
689 689
 			if(!cache->tab) {
690 690
 				cli_dbgmsg(MODULE_NAME "!Out of mem in iconv-pool\n");
691 691
 				errno = ENOMEM;
692
+				/* Close descriptor before returning -1 */
693
+				iconv_close (iconv_struct);
692 694
 				return (iconv_t)-1;
693 695
 			}
694 696
 		}
... ...
@@ -355,6 +355,7 @@ static int cli_hashtab_grow(struct cli_hashtable *s)
355 355
 			}
356 356
 			else {
357 357
 				cli_errmsg("hashtab.c: Impossible - unable to rehash table");
358
+				free (htable);
358 359
 				return CL_EMEM;/* this means we didn't find enough room for all elements in the new table, should never happen */ 
359 360
 			}
360 361
 		}
... ...
@@ -1722,8 +1722,8 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
1722 1722
 	free(hexcpy);
1723 1723
 
1724 1724
 	if(error) {
1725
+	    free(hex);
1725 1726
 	    if(new->special) {
1726
-		free(hex);
1727 1727
 		mpool_ac_free_special(root->mempool, new);
1728 1728
 	    }
1729 1729
 	    mpool_free(root->mempool, new);
... ...
@@ -2955,6 +2955,7 @@ rfc1341(message *m, const char *dir)
2955 2955
 						cli_errmsg("Can't open '%s' for reading", fullname);
2956 2956
 						fclose(fout);
2957 2957
 						cli_unlink(outname);
2958
+						free(md5_hex);
2958 2959
 						free(id);
2959 2960
 						free(number);
2960 2961
 						closedir(dd);
... ...
@@ -2567,8 +2567,10 @@ push(LINK1 *top, const char *string)
2567 2567
 
2568 2568
 	if((element = (LINK1)cli_malloc(sizeof(ELEMENT1))) == NULL)
2569 2569
 		return OUT_OF_MEMORY;
2570
-	if((element->d1 = cli_strdup(string)) == NULL)
2570
+	if((element->d1 = cli_strdup(string)) == NULL) {
2571
+		free (element);
2571 2572
 		return OUT_OF_MEMORY;
2573
+	}
2572 2574
 	element->next = *top;
2573 2575
 	*top = element;
2574 2576
 
... ...
@@ -800,6 +800,8 @@ int unmew11(char *src, int off, int ssize, int dsize, uint32_t base, uint32_t va
800 800
 		if (!CLI_ISCONTAINED(src, size_sum, lesi, loc_ss) || !CLI_ISCONTAINED(src, size_sum, ledi, loc_ds))
801 801
 		{
802 802
 			cli_dbgmsg("Possibly programmer error or hand-crafted PE file, report to clamav team\n");
803
+			if (section != NULL)
804
+				 free(section);
803 805
 			return -1;
804 806
 		}
805 807
 		if (unmew(lesi, ledi, loc_ss, loc_ds, &f1, &f2))
... ...
@@ -1162,6 +1162,11 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
1162 1162
 		memcpy(&tdb->str[cnt], pt, strlen(pt));
1163 1163
 		tdb->str[tdb->cnt[CLI_TDB_STR] - 1] = 0;
1164 1164
 		break;
1165
+
1166
+	    default:
1167
+		/* All known TDB types handled above, skip unknown */
1168
+		cli_dbgmsg("lsigattribs: Unknown attribute type '%u'\n", apt->type);
1169
+		return 1; /* +1 = skip */
1165 1170
 	}
1166 1171
     }
1167 1172
 
... ...
@@ -646,8 +646,10 @@ static int add_pattern_suffix(void *cbdata, const char *suffix, size_t suffix_le
646 646
 		size_t n = matcher->suffix_cnt++;
647 647
 		el = cli_hashtab_insert(&matcher->suffix_hash, suffix, suffix_len, n);
648 648
 		matcher->suffix_regexes = cli_realloc(matcher->suffix_regexes, (n+1)*sizeof(*matcher->suffix_regexes));
649
-		if(!matcher->suffix_regexes)
649
+		if(!matcher->suffix_regexes) {
650
+			free (regex);
650 651
 			return CL_EMEM;
652
+		}
651 653
 		matcher->suffix_regexes[n].tail = regex;
652 654
 		matcher->suffix_regexes[n].head = regex;
653 655
 		if (suffix[0] == '/' && suffix[1] == '\0')
... ...
@@ -109,8 +109,10 @@ static struct node *dup_node(struct node *p)
109 109
 			break;
110 110
 		case leaf_class:
111 111
 			d->u.leaf_class_bitmap = cli_malloc(32);
112
-			if(!d->u.leaf_class_bitmap)
112
+			if(!d->u.leaf_class_bitmap) {
113
+				free(d);
113 114
 				return NULL;
115
+			}
114 116
 			memcpy(d->u.leaf_class_bitmap, p->u.leaf_class_bitmap, 32);
115 117
 			break;
116 118
 		default:
... ...
@@ -423,6 +423,7 @@ static int cli_scangzip_with_zib_from_the_80s(cli_ctx *ctx, unsigned char *buff)
423 423
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
424 424
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
425 425
 	gzclose(gz);
426
+	close(fd);
426 427
 	return ret;
427 428
     }
428 429
     
... ...
@@ -388,6 +388,7 @@ cli_vba_readdir(const char *dir, struct uniq *U, uint32_t which)
388 388
 		if(ptr == NULL) break;
389 389
 		if (!(vba_project->colls[i]=uniq_get(U, ptr, strlen(ptr), &hash))) {
390 390
 			cli_dbgmsg("vba_readdir: cannot find project %s (%s)\n", ptr, hash);
391
+			free(ptr);
391 392
 			break;
392 393
 		}
393 394
 		cli_dbgmsg("vba_readdir: project name: %s (%s)\n", ptr, hash);
... ...
@@ -1058,7 +1059,8 @@ cli_wm_readdir(int fd)
1058 1058
 
1059 1059
 	end_offset = fib.macro_offset + fib.macro_len;
1060 1060
 	done = FALSE;
1061
-	memset(&macro_info, '\0', sizeof(macro_info));
1061
+	macro_info.entries = NULL;
1062
+	macro_info.count = 0;
1062 1063
 
1063 1064
 	while((lseek(fd, 0, SEEK_CUR) < end_offset) && !done) {
1064 1065
 		if (cli_readn(fd, &info_id, 1) != 1) {
... ...
@@ -910,6 +910,7 @@ int cdiff_apply(int fd, unsigned short mode)
910 910
 		logg("!cdiff_apply: Premature EOF at line %d\n", lines + 1);
911 911
 		cdiff_ctx_free(&ctx);
912 912
 		gzclose(gzh);
913
+		close(desc);
913 914
 		free(line);
914 915
 		free(lbuf);
915 916
 		return -1;