Browse code

hardfail on unlink failure

git-svn: trunk@3775

aCaB authored on 2008/04/09 02:45:05
Showing 13 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Apr  8 19:04:50 CEST 2008 (acab)
2
+------------------------------------
3
+  * libclamav: hardfail on unlink failure
4
+
1 5
 Mon Apr  7 22:38:27 CEST 2008 (tk)
2 6
 ----------------------------------
3 7
   * libclamav/msexpand.c: fix big-endian support
... ...
@@ -375,11 +375,13 @@ static int ea05(int desc, cli_ctx *ctx, char *tmpd) {
375 375
     lseek(i, 0, SEEK_SET);
376 376
     if(cli_magic_scandesc(i, ctx) == CL_VIRUS) {
377 377
       close(i);
378
-      if(!cli_leavetemps_flag) cli_unlink(tempfile);
378
+      if(!cli_leavetemps_flag)
379
+        if(cli_unlink(tempfile)) return CL_EIO;
379 380
       return CL_VIRUS;
380 381
     }
381 382
     close(i);
382
-    if(!cli_leavetemps_flag) cli_unlink(tempfile);
383
+    if(!cli_leavetemps_flag) 
384
+      if (cli_unlink(tempfile)) return CL_EIO;
383 385
   }
384 386
   return ret;
385 387
 }
... ...
@@ -883,11 +885,13 @@ static int ea06(int desc, cli_ctx *ctx, char *tmpd) {
883 883
     lseek(i, 0, SEEK_SET);
884 884
     if(cli_magic_scandesc(i, ctx) == CL_VIRUS) {
885 885
       close(i);
886
-      if(!cli_leavetemps_flag) cli_unlink(tempfile);
886
+      if(!cli_leavetemps_flag) 
887
+        if (cli_unlink(tempfile)) return CL_EIO;
887 888
       return CL_VIRUS;
888 889
     }
889 890
     close(i);
890
-    if(!cli_leavetemps_flag) cli_unlink(tempfile);
891
+    if(!cli_leavetemps_flag)
892
+      if (cli_unlink(tempfile)) return CL_EIO;
891 893
   }
892 894
   return ret;
893 895
 }
... ...
@@ -766,7 +766,7 @@ static int chm_decompress_stream(int fd, chm_metadata_t *metadata, const char *d
766 766
 	lzx_free(stream);
767 767
 	
768 768
 	/* Delete the file */
769
-	cli_unlink(filename);
769
+	if(cli_unlink(filename)) retval = -1;
770 770
 	retval = tmpfd;
771 771
 	
772 772
 abort:
... ...
@@ -1242,7 +1242,7 @@ cli_parse_mbox(const char *dir, int desc, cli_ctx *ctx)
1242 1242
 		/* empty message */
1243 1243
 		fclose(fd);
1244 1244
 #ifdef	SAVE_TMP
1245
-		cli_unlink(tmpfilename);
1245
+		if (cli_unlink(tmpfilename)) return CL_EIO;
1246 1246
 #endif
1247 1247
 		return CL_CLEAN;
1248 1248
 	}
... ...
@@ -1260,7 +1260,7 @@ cli_parse_mbox(const char *dir, int desc, cli_ctx *ctx)
1260 1260
 #endif
1261 1261
 			fclose(fd);
1262 1262
 #ifdef	SAVE_TMP
1263
-			cli_unlink(tmpfilename);
1263
+			if (cli_unlink(tmpfilename)) return CL_EIO;
1264 1264
 #endif
1265 1265
 			return CL_EMEM;
1266 1266
 		}
... ...
@@ -1321,7 +1321,7 @@ cli_parse_mbox(const char *dir, int desc, cli_ctx *ctx)
1321 1321
 			signal(SIGSEGV, segv);
1322 1322
 #endif
1323 1323
 #ifdef	SAVE_TMP
1324
-			cli_unlink(tmpfilename);
1324
+			if (cli_unlink(tmpfilename)) return CL_EIO;
1325 1325
 #endif
1326 1326
 			return CL_EMEM;
1327 1327
 		}
... ...
@@ -1479,7 +1479,7 @@ cli_parse_mbox(const char *dir, int desc, cli_ctx *ctx)
1479 1479
 #endif
1480 1480
 
1481 1481
 #ifdef	SAVE_TMP
1482
-	cli_unlink(tmpfilename);
1482
+	if (cli_unlink(tmpfilename)) return CL_EIO;
1483 1483
 #endif
1484 1484
 	return retcode;
1485 1485
 }
... ...
@@ -3850,8 +3850,16 @@ rfc1341(message *m, const char *dir)
3850 3850
 							continue;
3851 3851
 						if(stat(fullname, &statb) < 0)
3852 3852
 							continue;
3853
-						if(now - statb.st_mtime > (time_t)(7 * 24 * 3600))
3854
-							cli_unlink(fullname);
3853
+						if(now - statb.st_mtime > (time_t)(7 * 24 * 3600)) {
3854
+							if (cli_unlink(fullname)) {
3855
+								cli_unlink(outname);
3856
+								fclose(fout);
3857
+								free(id);
3858
+								free(number);
3859
+								closedir(dd);
3860
+								return -1;
3861
+							}
3862
+						}
3855 3863
 						continue;
3856 3864
 					}
3857 3865
 
... ...
@@ -3890,9 +3898,17 @@ rfc1341(message *m, const char *dir)
3890 3890
 						}
3891 3891
 					fclose(fin);
3892 3892
 
3893
-					/* don't cli_unlink if leave temps */
3894
-					if(!cli_leavetemps_flag)
3895
-						cli_unlink(fullname);
3893
+					/* don't unlink if leave temps */
3894
+					if(!cli_leavetemps_flag) {
3895
+						if(cli_unlink(fullname)) {
3896
+							fclose(fout);
3897
+							cli_unlink(outname);
3898
+							free(id);
3899
+							free(number);
3900
+							closedir(dd);
3901
+							return -1;
3902
+						}
3903
+					}
3896 3904
 					break;
3897 3905
 				}
3898 3906
 				rewinddir(dd);
... ...
@@ -4393,7 +4409,7 @@ getURL(struct arg *arg)
4393 4393
 					if(location) {
4394 4394
 						char *end;
4395 4395
 
4396
-						cli_unlink(fout);
4396
+						if (cli_unlink(fout)) return NULL;
4397 4397
 						location += 11;
4398 4398
 						end = location;
4399 4399
 						while(*end && (*end != '\n'))
... ...
@@ -571,7 +571,7 @@ int cli_scannulsft(int desc, cli_ctx *ctx, off_t offset) {
571 571
 	    ret=cli_magic_scandesc(nsist.ofd, ctx);
572 572
 	  close(nsist.ofd);
573 573
 	  if(!cli_leavetemps_flag)
574
-	    cli_unlink(nsist.ofn);
574
+	    if(cli_unlink(nsist.ofn)) ret = CL_EIO;
575 575
 	} else if(ret == CL_EMAXSIZE) {
576 576
 	    ret = nsist.solid ? CL_BREAK : CL_SUCCESS;
577 577
 	}
... ...
@@ -562,9 +562,13 @@ int cli_gentempfd(const char *dir, char **name, int *fd)
562 562
 /* Function: unlink
563 563
         unlink() with error checking
564 564
 */
565
-void cli_unlink(const char *pathname)
565
+int cli_unlink(const char *pathname)
566 566
 {
567
-	if (unlink(pathname)) cli_warnmsg("cli_unlink: failure - %s\n", strerror(errno));
567
+	if (unlink(pathname)==-1) {
568
+	    cli_warnmsg("cli_unlink: failure - %s\n", strerror(errno));
569
+	    return 1;
570
+	}
571
+	return 0;
568 572
 }
569 573
 
570 574
 #ifdef	C_WINDOWS
... ...
@@ -592,7 +596,7 @@ cli_rmdirs(const char *name)
592 592
     }
593 593
 
594 594
     if(!S_ISDIR(statb.st_mode)) {
595
-	cli_unlink(name);
595
+	if(cli_unlink(name)) return -1;
596 596
 	return 0;
597 597
     }
598 598
 
... ...
@@ -700,8 +704,13 @@ int cli_rmdirs(const char *dirname)
700 700
 					return -1;
701 701
 				    }
702 702
 				}
703
-			    } else
704
-				cli_unlink(path); 
703
+			    } else {
704
+				if(cli_unlink(path)) {
705
+				    free(path);
706
+				    closedir(dd);
707
+				    return -1;
708
+				}
709
+			    }
705 710
 			}
706 711
 			free(path);
707 712
 		    }
... ...
@@ -259,7 +259,7 @@ int cli_rmdirs(const char *dirname);
259 259
 unsigned char *cli_md5digest(int desc);
260 260
 char *cli_md5stream(FILE *fs, unsigned char *digcpy);
261 261
 char *cli_md5file(const char *filename);
262
-void cli_unlink(const char *pathname);
262
+int cli_unlink(const char *pathname);
263 263
 int cli_readn(int fd, void *buff, unsigned int count);
264 264
 int cli_writen(int fd, const void *buff, unsigned int count);
265 265
 char *cli_gentemp(const char *dir);
... ...
@@ -395,7 +395,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
395 395
 		if(streamend <= streamstart) {
396 396
 			close(fout);
397 397
 			cli_dbgmsg("cli_pdf: Empty stream\n");
398
-			cli_unlink(fullname);
398
+			if (cli_unlink(fullname)) {
399
+				rc = CL_EIO;
400
+				break;
401
+			}
399 402
 			continue;
400 403
 		}
401 404
 		calculated_streamlen = (int)(streamend - streamstart);
... ...
@@ -421,7 +424,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
421 421
 
422 422
 			if(ret != CL_CLEAN) {
423 423
 				close(fout);
424
-				cli_unlink(fullname);
424
+				if (cli_unlink(fullname)) {
425
+					rc = CL_EIO;
426
+					break;
427
+				}
425 428
 				continue;
426 429
 			}
427 430
 
... ...
@@ -429,7 +435,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
429 429
 
430 430
 			if(tmpbuf == NULL) {
431 431
 				close(fout);
432
-				cli_unlink(fullname);
432
+				if (cli_unlink(fullname)) {
433
+					rc = CL_EIO;
434
+					break;
435
+				}
433 436
 				continue;
434 437
 			}
435 438
 
... ...
@@ -438,7 +447,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
438 438
 			if(ret == -1) {
439 439
 				free(tmpbuf);
440 440
 				close(fout);
441
-				cli_unlink(fullname);
441
+				if (cli_unlink(fullname)) {
442
+					rc = CL_EIO;
443
+					break;
444
+				}
442 445
 				continue;
443 446
 			}
444 447
 			if(ret) {
... ...
@@ -450,7 +462,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
450 450
 				if(t == NULL) {
451 451
 					free(tmpbuf);
452 452
 					close(fout);
453
-					cli_unlink(fullname);
453
+					if (cli_unlink(fullname)) {
454
+						rc = CL_EIO;
455
+						break;
456
+					}
454 457
 					continue;
455 458
 				}
456 459
 				tmpbuf = t;
... ...
@@ -492,7 +507,10 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
492 492
 				if(tableFind(md5table, md5str) >= 0) {
493 493
 					cli_dbgmsg("cli_pdf: not scanning duplicate embedded file '%s'\n", fullname);
494 494
 					close(fout);
495
-					cli_unlink(fullname);
495
+					if (cli_unlink(fullname)) {
496
+						rc = CL_EIO;
497
+						break;
498
+					}
496 499
 					continue;
497 500
 				} else
498 501
 					tableInsert(md5table, md5str, 1);
... ...
@@ -502,7 +520,8 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx)
502 502
 			rc = cli_magic_scandesc(fout, ctx);
503 503
 		}
504 504
 		close(fout);
505
-		if(!cli_leavetemps_flag) cli_unlink(fullname);
505
+		if(!cli_leavetemps_flag)
506
+			if (cli_unlink(fullname)) rc = CL_EIO;
506 507
 		if(rc != CL_CLEAN) break;
507 508
 	}
508 509
 
... ...
@@ -652,7 +671,10 @@ flatedecode(unsigned char *buf, off_t len, int fout, cli_ctx *ctx)
652 652
 	}
653 653
 			
654 654
 #ifdef	SAVE_TMP
655
-	cli_unlink(tmpfilename);
655
+	if (cli_unlink(tmpfilename)) {
656
+		inflateEnd(&stream);
657
+		return CL_EIO;
658
+	}
656 659
 #endif
657 660
 	inflateEnd(&stream);
658 661
 	return CL_CLEAN;
... ...
@@ -99,13 +99,23 @@ if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) { \
99 99
     return CL_EIO; \
100 100
 }
101 101
 
102
-#define CLI_TMPUNLK() if(!cli_leavetemps_flag) cli_unlink(tempfile)
102
+#define CLI_TMPUNLK() if(!cli_leavetemps_flag) { \
103
+    if (cli_unlink(tempfile)) { \
104
+	free(tempfile); \
105
+	return CL_EIO; \
106
+    } \
107
+}
103 108
 
104 109
 #define FSGCASE(NAME,FREESEC) \
105 110
     case 0: /* Unpacked and NOT rebuilt */ \
106 111
 	cli_dbgmsg(NAME": Successfully decompressed\n"); \
107 112
 	close(ndesc); \
108
-	cli_unlink(tempfile); \
113
+	if (cli_unlink(tempfile)) { \
114
+	    free(exe_sections); \
115
+	    free(tempfile); \
116
+	    FREESEC; \
117
+	    return CL_EIO; \
118
+	} \
109 119
 	free(tempfile); \
110 120
 	FREESEC; \
111 121
 	found = 0; \
... ...
@@ -116,7 +126,11 @@ if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) { \
116 116
     case 2: \
117 117
 	free(spinned); \
118 118
 	close(ndesc); \
119
-	cli_unlink(tempfile); \
119
+	if (cli_unlink(tempfile)) { \
120
+	    free(exe_sections); \
121
+	    free(tempfile); \
122
+	    return CL_EIO; \
123
+	} \
120 124
 	cli_dbgmsg("PESpin: Size exceeded\n"); \
121 125
 	free(tempfile); \
122 126
 	break; \
... ...
@@ -148,7 +162,12 @@ FSGSTUFF; \
148 148
     default: \
149 149
 	cli_dbgmsg(NAME": Unpacking failed\n"); \
150 150
 	close(ndesc); \
151
-	cli_unlink(tempfile); \
151
+	if (cli_unlink(tempfile)) { \
152
+	    free(exe_sections); \
153
+	    free(tempfile); \
154
+	    cli_multifree FREEME; \
155
+	    return CL_EIO; \
156
+	} \
152 157
 	cli_multifree FREEME; \
153 158
         free(tempfile); \
154 159
     }
... ...
@@ -252,7 +252,7 @@ static int decode_and_scan(struct rtf_object_data* data, cli_ctx* ctx)
252 252
 	data->fd = -1;
253 253
 	if(data->name) {
254 254
 		if(!cli_leavetemps_flag)
255
-			cli_unlink(data->name);
255
+			if(cli_unlink(data->name)) ret = CL_EIO;
256 256
 		free(data->name);
257 257
 		data->name = NULL;
258 258
 	}
... ...
@@ -325,7 +325,7 @@ static int cli_scanrar(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
325 325
 	    rc = cli_magic_scandesc(rar_state.ofd,ctx);
326 326
 	    close(rar_state.ofd);
327 327
 	    if(!cli_leavetemps_flag) 
328
-		cli_unlink(rar_state.filename);
328
+		if (cli_unlink(rar_state.filename)) ret = CL_EIO;
329 329
 	    if(rc == CL_VIRUS ) {
330 330
 		cli_dbgmsg("RAR: infected with %s\n",*ctx->virname);
331 331
 		ret = CL_VIRUS;
... ...
@@ -466,9 +466,12 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
466 466
 	cli_dbgmsg("GZip: Unable to malloc %u bytes.\n", FILEBUFF);
467 467
 	gzclose(gd);
468 468
 	close(fd);
469
-	if(!cli_leavetemps_flag)
470
-	    cli_unlink(tmpname);
471
-	free(tmpname);	
469
+	if(!cli_leavetemps_flag) {
470
+	    if(cli_unlink(tmpname)) {
471
+	    	free(tmpname);
472
+		return CL_EIO;
473
+	    }
474
+	}
472 475
 	return CL_EMEM;
473 476
     }
474 477
 
... ...
@@ -481,8 +484,14 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
481 481
 	if(cli_writen(fd, buff, bytes) != bytes) {
482 482
 	    cli_dbgmsg("GZip: Can't write to file.\n");
483 483
 	    close(fd);
484
-	    if(!cli_leavetemps_flag)
485
-		cli_unlink(tmpname);
484
+	    if(!cli_leavetemps_flag) {
485
+	    	if (cli_unlink(tmpname)) {
486
+		    free(tmpname);
487
+		    gzclose(gd);
488
+		    free(buff);
489
+		    return CL_EIO;
490
+		}
491
+	    }
486 492
 	    free(tmpname);	
487 493
 	    gzclose(gd);
488 494
 	    free(buff);
... ...
@@ -496,7 +505,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
496 496
     if(ret == CL_VIRUS) {
497 497
 	close(fd);
498 498
 	if(!cli_leavetemps_flag)
499
-	    cli_unlink(tmpname);
499
+	    if (cli_unlink(tmpname)) ret = CL_EIO;
500 500
 	free(tmpname);	
501 501
 	return ret;
502 502
     }
... ...
@@ -505,14 +514,18 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
505 505
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
506 506
 	cli_dbgmsg("GZip: Infected with %s\n", *ctx->virname);
507 507
 	close(fd);
508
-	if(!cli_leavetemps_flag)
509
-	    cli_unlink(tmpname);
508
+	if(!cli_leavetemps_flag) {
509
+	    if (cli_unlink(tmpname)) {
510
+	    	free(tmpname);
511
+		return CL_EIO;
512
+	    }
513
+	}
510 514
 	free(tmpname);	
511 515
 	return CL_VIRUS;
512 516
     }
513 517
     close(fd);
514 518
     if(!cli_leavetemps_flag)
515
-	cli_unlink(tmpname);
519
+	if (cli_unlink(tmpname)) ret = CL_EIO;
516 520
     free(tmpname);	
517 521
 
518 522
     return ret;
... ...
@@ -562,8 +575,14 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
562 562
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
563 563
 	cli_dbgmsg("Bzip: Unable to malloc %u bytes.\n", FILEBUFF);
564 564
 	close(fd);
565
-	if(!cli_leavetemps_flag)
566
-	    cli_unlink(tmpname);
565
+	if(!cli_leavetemps_flag) {
566
+	    if (cli_unlink(tmpname)) {
567
+	    	free(tmpname);
568
+		fclose(fs);
569
+		BZ2_bzReadClose(&bzerror, bfd);
570
+		return CL_EIO;
571
+	    }
572
+	}
567 573
 	free(tmpname);	
568 574
 	fclose(fs);
569 575
 	BZ2_bzReadClose(&bzerror, bfd);
... ...
@@ -580,8 +599,14 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
580 580
 	    cli_dbgmsg("Bzip: Can't write to file.\n");
581 581
 	    BZ2_bzReadClose(&bzerror, bfd);
582 582
 	    close(fd);
583
-	    if(!cli_leavetemps_flag)
584
-		cli_unlink(tmpname);
583
+	    if(!cli_leavetemps_flag) {
584
+		if (cli_unlink(tmpname)) {
585
+		    free(tmpname);
586
+		    free(buff);
587
+		    fclose(fs);
588
+		    return CL_EIO;
589
+		}
590
+	    }
585 591
 	    free(tmpname);	
586 592
 	    free(buff);
587 593
 	    fclose(fs);
... ...
@@ -595,7 +620,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
595 595
     if(ret == CL_VIRUS) {
596 596
 	close(fd);
597 597
 	if(!cli_leavetemps_flag)
598
-	    cli_unlink(tmpname);
598
+	    if (cli_unlink(tmpname)) ret = CL_EIO;
599 599
 	free(tmpname);	
600 600
 	fclose(fs);
601 601
 	return ret;
... ...
@@ -607,7 +632,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
607 607
     }
608 608
     close(fd);
609 609
     if(!cli_leavetemps_flag)
610
-	cli_unlink(tmpname);
610
+	if (cli_unlink(tmpname)) ret = CL_EIO;
611 611
     free(tmpname);	
612 612
     fclose(fs);
613 613
 
... ...
@@ -634,7 +659,7 @@ static int cli_scanszdd(int desc, cli_ctx *ctx)
634 634
     if(ret != CL_SUCCESS) { /* CL_VIRUS or some error */
635 635
 	close(ofd);
636 636
 	if(!cli_leavetemps_flag)
637
-	    cli_unlink(tmpname);
637
+	    if (cli_unlink(tmpname)) ret = CL_EIO;
638 638
 	free(tmpname);	
639 639
 	return ret;
640 640
     }
... ...
@@ -644,7 +669,7 @@ static int cli_scanszdd(int desc, cli_ctx *ctx)
644 644
     ret = cli_magic_scandesc(ofd, ctx);
645 645
     close(ofd);
646 646
     if(!cli_leavetemps_flag)
647
-	cli_unlink(tmpname);
647
+	if (cli_unlink(tmpname)) ret = CL_EIO;
648 648
     free(tmpname);	
649 649
 
650 650
     return ret;
... ...
@@ -680,8 +705,13 @@ static int cli_scanmscab(int desc, cli_ctx *ctx, off_t sfx_offset)
680 680
 	else
681 681
 	    ret = cli_scanfile(tempname, ctx);
682 682
 
683
-	if(!cli_leavetemps_flag)
684
-	    cli_unlink(tempname);
683
+	if(!cli_leavetemps_flag) {
684
+	    if (cli_unlink(tempname)) {
685
+	    	free(tempname);
686
+		ret = CL_EIO;
687
+		break;
688
+	    }
689
+	}
685 690
 	free(tempname);
686 691
 	if(ret == CL_VIRUS)
687 692
 	    break;
... ...
@@ -1043,7 +1073,7 @@ static int cli_scanhtml_utf16(int desc, cli_ctx *ctx)
1043 1043
     close(fd);
1044 1044
 
1045 1045
     if(!cli_leavetemps_flag)
1046
-	cli_unlink(tempname);
1046
+	if (cli_unlink(tempname)) ret = CL_EIO;
1047 1047
     else
1048 1048
 	cli_dbgmsg("cli_scanhtml_utf16: Decoded HTML data saved in %s\n", tempname);
1049 1049
     free(tempname);
... ...
@@ -1343,7 +1373,7 @@ static int cli_scancryptff(int desc, cli_ctx *ctx)
1343 1343
     if(cli_leavetemps_flag)
1344 1344
 	cli_dbgmsg("CryptFF: Decompressed data saved in %s\n", tempfile);
1345 1345
     else
1346
-	cli_unlink(tempfile);
1346
+	if (cli_unlink(tempfile)) ret = CL_EIO;
1347 1347
 
1348 1348
     free(tempfile);
1349 1349
     return ret;
... ...
@@ -1488,8 +1518,12 @@ static int cli_scanembpe(int desc, cli_ctx *ctx)
1488 1488
 	if(cli_writen(fd, buff, bytes) != bytes) {
1489 1489
 	    cli_dbgmsg("cli_scanembpe: Can't write to temporary file\n");
1490 1490
 	    close(fd);
1491
-	    if(!cli_leavetemps_flag)
1492
-		cli_unlink(tmpname);
1491
+	    if(!cli_leavetemps_flag) {
1492
+		if (cli_unlink(tmpname)) {
1493
+		    free(tmpname);
1494
+		    return CL_EIO;
1495
+		}
1496
+	    }
1493 1497
 	    free(tmpname);	
1494 1498
 	    return CL_EIO;
1495 1499
 	}
... ...
@@ -1500,16 +1534,24 @@ static int cli_scanembpe(int desc, cli_ctx *ctx)
1500 1500
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS) {
1501 1501
 	cli_dbgmsg("cli_scanembpe: Infected with %s\n", *ctx->virname);
1502 1502
 	close(fd);
1503
-	if(!cli_leavetemps_flag)
1504
-	    cli_unlink(tmpname);
1503
+	if(!cli_leavetemps_flag) {
1504
+	    if (cli_unlink(tmpname)) {
1505
+	    	free(tmpname);
1506
+		return CL_EIO;
1507
+	    }
1508
+	}
1505 1509
 	free(tmpname);	
1506 1510
 	return CL_VIRUS;
1507 1511
     }
1508 1512
     ctx->recursion--;
1509 1513
 
1510 1514
     close(fd);
1511
-    if(!cli_leavetemps_flag)
1512
-	cli_unlink(tmpname);
1515
+    if(!cli_leavetemps_flag) {
1516
+	if (cli_unlink(tmpname)) {
1517
+	    free(tmpname);
1518
+	    return CL_EIO;
1519
+	}
1520
+    }
1513 1521
     free(tmpname);
1514 1522
 
1515 1523
     /* intentionally ignore possible errors from cli_magic_scandesc */
... ...
@@ -94,7 +94,7 @@ cli_untar(const char *dir, int desc, unsigned int posix, cli_ctx *ctx)
94 94
 				ret = cli_magic_scandesc(fout, ctx);
95 95
 				close(fout);
96 96
 				if (!cli_leavetemps_flag)
97
-					cli_unlink(fullname);
97
+					if (cli_unlink(fullname)) return CL_EIO;
98 98
 				if (ret==CL_VIRUS)
99 99
 					return CL_VIRUS;
100 100
 				fout = -1;
... ...
@@ -216,7 +216,7 @@ cli_untar(const char *dir, int desc, unsigned int posix, cli_ctx *ctx)
216 216
 		ret = cli_magic_scandesc(fout, ctx);
217 217
 		close(fout);
218 218
 		if (!cli_leavetemps_flag)
219
-			cli_unlink(fullname);
219
+			if (cli_unlink(fullname)) return CL_EIO;
220 220
 		if (ret==CL_VIRUS)
221 221
 			return CL_VIRUS;
222 222
 	}
... ...
@@ -284,13 +284,15 @@ static int unz(uint8_t *src, uint32_t csize, uint32_t usize, uint16_t method, ui
284 284
     lseek(of, 0, SEEK_SET);
285 285
     ret = cli_magic_scandesc(of, ctx);
286 286
     close(of);
287
-    if(!cli_leavetemps_flag) cli_unlink(tempfile);
287
+    if(!cli_leavetemps_flag)
288
+      if(cli_unlink(tempfile)) ret = CL_EIO;
288 289
     if(!tmpd) free(tempfile);
289 290
     return ret;
290 291
   }
291 292
 
292 293
   close(of);
293
-  if(!cli_leavetemps_flag) cli_unlink(tempfile);
294
+  if(!cli_leavetemps_flag)
295
+    if(cli_unlink(tempfile)) ret = CL_EIO;
294 296
   if(!tmpd) free(tempfile);
295 297
   cli_dbgmsg("cli_unzip: extraction failed\n");
296 298
   return ret;