Browse code

Improve bounds checking

git-svn: trunk@3289

Trog authored on 2007/10/09 20:35:35
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Oct  9 11:36:44 BST 2007 (trog)
2
+-----------------------------------
3
+  * libclamav/unarj.c: Improve bounds checking.
4
+
1 5
 Tue Oct  9 08:47:44 BST 2007 (njh)
2 6
 ----------------------------------
3 7
   * clamav-milter:	Fix bug introducted in revision 3277.
... ...
@@ -428,6 +428,10 @@ static uint16_t decode_p(arj_decode_t *decode_data)
428 428
 	if (j >= NP) {
429 429
 		mask = 1 << 7;
430 430
 		do {
431
+			if (j >= (2 * NC - 1)) {
432
+				cli_errmsg("ERROR: bounds exceeded\n");
433
+				return 0;
434
+			}
431 435
 			if (decode_data->bit_buf & mask) {
432 436
 				j = decode_data->right[j];
433 437
 			} else {
... ...
@@ -478,12 +482,16 @@ static int decode(int fd, arj_metadata_t *metadata)
478 478
 			if ((i = out_ptr - i - 1) < 0) {
479 479
 				i += DDICSIZ;
480 480
 			}
481
+			if ((i >= DDICSIZ) || (i < 0)) {
482
+				cli_warnmsg("UNARJ: bounds exceeded - probably a corrupted file.\n");
483
+				break;
484
+			}
481 485
 			if (out_ptr > i && out_ptr < DDICSIZ - MAXMATCH - 1) {
482
-				while (--j >= 0) {
486
+				while ((--j >= 0) && (i < DDICSIZ)) {
483 487
 					decode_data.text[out_ptr++] = decode_data.text[i++];
484 488
 				}
485 489
 			} else {
486
-				while (--j >= 0) {
490
+				while (--j >= 0) {				
487 491
 					decode_data.text[out_ptr] = decode_data.text[i];
488 492
 					if (++out_ptr >= DDICSIZ) {
489 493
 						out_ptr = 0;
... ...
@@ -589,6 +597,10 @@ static int decode_f(int fd, arj_metadata_t *metadata)
589 589
 			if ((i = out_ptr - pos - 1) < 0) {
590 590
 				i += DDICSIZ;
591 591
 			}
592
+			if ((i >= DDICSIZ) || (i < 0)) {
593
+				cli_warnmsg("UNARJ: bounds exceeded - probably a corrupted file.\n");
594
+				break;
595
+			}
592 596
 			while (j-- > 0) {
593 597
 				decode_data.text[out_ptr] = decode_data.text[i];
594 598
 				if (++out_ptr >= DDICSIZ) {