git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@134 77e5149b-7576-45b1-b177-96237e5ba77b
Tomasz Kojm authored on 2003/12/02 04:28:40... | ... |
@@ -1,3 +1,12 @@ |
1 |
+Mon Dec 1 20:26:45 CET 2003 (tk) |
|
2 |
+--------------------------------- |
|
3 |
+ * A bunch of patches from Thomas Lamy <Thomas.Lamy*in-online.net>: |
|
4 |
+ + fixed on error descriptor leak in cli_untgz() |
|
5 |
+ + added missing mpz_clear() in cli_versig() |
|
6 |
+ + more error messages in scanners.c and others.c |
|
7 |
+ + properly free file stream in cli_scanbzip() |
|
8 |
+ + clamd: clean up resources on exit (added freecfg() and logg_close()) |
|
9 |
+ |
|
1 | 10 |
Sun Nov 30 06:13:28 GMT 2003 (njh) |
2 | 11 |
---------------------------------- |
3 | 12 |
* clamav-milter: Added --quarantine-dir |
... | ... |
@@ -191,6 +191,35 @@ struct cfgstruct *parsecfg(const char *cfgfile) |
191 | 191 |
return copt; |
192 | 192 |
} |
193 | 193 |
|
194 |
+void freecfg(struct cfgstruct *copt) |
|
195 |
+{ |
|
196 |
+ struct cfgstruct *handler; |
|
197 |
+ struct cfgstruct *arg; |
|
198 |
+ |
|
199 |
+ while (copt) { |
|
200 |
+ arg = copt->nextarg; |
|
201 |
+ while (arg) { |
|
202 |
+ if(arg->strarg) { |
|
203 |
+ free(arg->optname); |
|
204 |
+ free(arg->strarg); |
|
205 |
+ handler = arg; |
|
206 |
+ arg=arg->nextarg; |
|
207 |
+ free(handler); |
|
208 |
+ } |
|
209 |
+ } |
|
210 |
+ if(copt->optname) { |
|
211 |
+ free(copt->optname); |
|
212 |
+ } |
|
213 |
+ if(copt->strarg) { |
|
214 |
+ free(copt->strarg); |
|
215 |
+ } |
|
216 |
+ handler = copt; |
|
217 |
+ copt = copt->next; |
|
218 |
+ free(handler); |
|
219 |
+ } |
|
220 |
+ return; |
|
221 |
+} |
|
222 |
+ |
|
194 | 223 |
char *tok(const char *line, int field) |
195 | 224 |
{ |
196 | 225 |
int length, counter = 0, i, j = 0, k; |
... | ... |
@@ -229,7 +258,7 @@ char *tok(const char *line, int field) |
229 | 229 |
return realloc(buffer, strlen(buffer) + 1); |
230 | 230 |
} |
231 | 231 |
|
232 |
-struct cfgstruct *regcfg(struct cfgstruct *copt, const char *optname, const char *strarg, int numarg) |
|
232 |
+struct cfgstruct *regcfg(struct cfgstruct *copt, char *optname, char *strarg, int numarg) |
|
233 | 233 |
{ |
234 | 234 |
struct cfgstruct *newnode, *pt; |
235 | 235 |
|
... | ... |
@@ -37,8 +37,8 @@ struct cfgoption { |
37 | 37 |
}; |
38 | 38 |
|
39 | 39 |
struct cfgstruct { |
40 |
- const char *optname; |
|
41 |
- const char *strarg; |
|
40 |
+ char *optname; |
|
41 |
+ char *strarg; |
|
42 | 42 |
int numarg; |
43 | 43 |
struct cfgstruct *nextarg; |
44 | 44 |
struct cfgstruct *next; |
... | ... |
@@ -47,9 +47,11 @@ struct cfgstruct { |
47 | 47 |
|
48 | 48 |
struct cfgstruct *parsecfg(const char *cfgfile); |
49 | 49 |
|
50 |
-struct cfgstruct *regcfg(struct cfgstruct *copt, const char *optname, const char *strarg, int numarg); |
|
50 |
+struct cfgstruct *regcfg(struct cfgstruct *copt, char *optname, char *strarg, int numarg); |
|
51 | 51 |
|
52 | 52 |
struct cfgstruct *cfgopt(const struct cfgstruct *copt, const char *optname); |
53 | 53 |
char *tok(const char *line, int field); |
54 | 54 |
|
55 |
+void freecfg(struct cfgstruct *copt); |
|
56 |
+ |
|
55 | 57 |
#endif |
... | ... |
@@ -203,6 +203,11 @@ void clamd(struct optstruct *opt) |
203 | 203 |
else |
204 | 204 |
ret = localserver(opt, copt, root); |
205 | 205 |
|
206 |
+ cli_dbgmsg("*server ended; result=%d\n", ret); |
|
207 |
+ logg_close(); |
|
208 |
+ freecfg(copt); |
|
209 |
+ cli_dbgmsg("*free() copt\n"); |
|
210 |
+ |
|
206 | 211 |
} |
207 | 212 |
|
208 | 213 |
void help(void) |
... | ... |
@@ -36,6 +36,8 @@ |
36 | 36 |
pthread_mutex_t logg_mutex = PTHREAD_MUTEX_INITIALIZER; |
37 | 37 |
pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER; |
38 | 38 |
|
39 |
+FILE *log_fd = NULL; |
|
40 |
+ |
|
39 | 41 |
int mdprintf(int desc, const char *str, ...) |
40 | 42 |
{ |
41 | 43 |
va_list args; |
... | ... |
@@ -50,23 +52,37 @@ int mdprintf(int desc, const char *str, ...) |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
|
53 |
+void logg_close(void) { |
|
54 |
+ pthread_mutex_lock(&logg_mutex); |
|
55 |
+ if (log_fd) { |
|
56 |
+ fclose(log_fd); |
|
57 |
+ } |
|
58 |
+ pthread_mutex_unlock(&logg_mutex); |
|
59 |
+#if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX) |
|
60 |
+ if(use_syslog) { |
|
61 |
+ closelog(); |
|
62 |
+ } |
|
63 |
+#endif |
|
64 |
+} |
|
65 |
+ |
|
66 |
+ |
|
53 | 67 |
int logg(const char *str, ...) |
54 | 68 |
{ |
55 | 69 |
va_list args; |
56 |
- static FILE *fd = NULL; |
|
57 | 70 |
struct flock fl; |
58 | 71 |
char *pt, *timestr; |
59 | 72 |
time_t currtime; |
60 | 73 |
struct stat sb; |
61 | 74 |
mode_t old_umask; |
62 | 75 |
|
76 |
+ |
|
63 | 77 |
if(logfile) { |
64 | 78 |
|
65 | 79 |
pthread_mutex_lock(&logg_mutex); |
66 | 80 |
|
67 |
- if(!fd) { |
|
81 |
+ if(!log_fd) { |
|
68 | 82 |
old_umask = umask(0036); |
69 |
- if((fd = fopen(logfile, "a")) == NULL) { |
|
83 |
+ if((log_fd = fopen(logfile, "a")) == NULL) { |
|
70 | 84 |
umask(old_umask); |
71 | 85 |
pthread_mutex_unlock(&logg_mutex); |
72 | 86 |
return -1; |
... | ... |
@@ -75,7 +91,7 @@ int logg(const char *str, ...) |
75 | 75 |
if(loglock) { |
76 | 76 |
memset(&fl, 0, sizeof(fl)); |
77 | 77 |
fl.l_type = F_WRLCK; |
78 |
- if(fcntl(fileno(fd), F_SETLK, &fl) == -1) { |
|
78 |
+ if(fcntl(fileno(log_fd), F_SETLK, &fl) == -1) { |
|
79 | 79 |
pthread_mutex_unlock(&logg_mutex); |
80 | 80 |
return -1; |
81 | 81 |
} |
... | ... |
@@ -90,7 +106,7 @@ int logg(const char *str, ...) |
90 | 90 |
pt = ctime(&currtime); |
91 | 91 |
timestr = mcalloc(strlen(pt), sizeof(char)); |
92 | 92 |
strncpy(timestr, pt, strlen(pt) - 1); |
93 |
- fprintf(fd, "%s -> ", timestr); |
|
93 |
+ fprintf(log_fd, "%s -> ", timestr); |
|
94 | 94 |
free(timestr); |
95 | 95 |
} |
96 | 96 |
|
... | ... |
@@ -99,9 +115,9 @@ int logg(const char *str, ...) |
99 | 99 |
if(stat(logfile, &sb) != -1) { |
100 | 100 |
if(sb.st_size > logsize) { |
101 | 101 |
logfile = NULL; |
102 |
- fprintf(fd, "Log size = %d, maximal = %d\n", (int) sb.st_size, logsize); |
|
103 |
- fprintf(fd, "LOGGING DISABLED (Maximal log file size exceeded).\n"); |
|
104 |
- fclose(fd); |
|
102 |
+ fprintf(log_fd, "Log size = %d, maximal = %d\n", (int) sb.st_size, logsize); |
|
103 |
+ fprintf(log_fd, "LOGGING DISABLED (Maximal log file size exceeded).\n"); |
|
104 |
+ fclose(log_fd); |
|
105 | 105 |
pthread_mutex_unlock(&logg_mutex); |
106 | 106 |
return 0; |
107 | 107 |
} |
... | ... |
@@ -111,19 +127,19 @@ int logg(const char *str, ...) |
111 | 111 |
va_start(args, str); |
112 | 112 |
|
113 | 113 |
if(*str == '!') { |
114 |
- fprintf(fd, "ERROR: "); |
|
115 |
- vfprintf(fd, ++str, args); |
|
114 |
+ fprintf(log_fd, "ERROR: "); |
|
115 |
+ vfprintf(log_fd, ++str, args); |
|
116 | 116 |
} else if(*str == '^') { |
117 |
- fprintf(fd, "WARNING: "); |
|
118 |
- vfprintf(fd, ++str, args); |
|
117 |
+ fprintf(log_fd, "WARNING: "); |
|
118 |
+ vfprintf(log_fd, ++str, args); |
|
119 | 119 |
} else if(*str == '*') { |
120 | 120 |
if(logverbose) |
121 |
- vfprintf(fd, ++str, args); |
|
122 |
- } else vfprintf(fd, str, args); |
|
121 |
+ vfprintf(log_fd, ++str, args); |
|
122 |
+ } else vfprintf(log_fd, str, args); |
|
123 | 123 |
|
124 | 124 |
va_end(args); |
125 | 125 |
|
126 |
- fflush(fd); |
|
126 |
+ fflush(log_fd); |
|
127 | 127 |
pthread_mutex_unlock(&logg_mutex); |
128 | 128 |
} |
129 | 129 |
|
... | ... |
@@ -155,7 +155,7 @@ int acceptloop_proc(int socketd, struct cl_node *root, const struct cfgstruct *c |
155 | 155 |
|
156 | 156 |
if(cfgopt(copt, "ArchiveLimitMemoryUsage")) { |
157 | 157 |
limits.archivememlim = 1; |
158 |
- logg("Archive: Limited memory usage.\n", limits.maxfiles); |
|
158 |
+ logg("Archive: Limited memory usage.\n"); |
|
159 | 159 |
} else |
160 | 160 |
limits.archivememlim = 0; |
161 | 161 |
|
... | ... |
@@ -84,8 +84,12 @@ void *threadscanner(void *arg) |
84 | 84 |
scan(buff + strlen(CMD2) + 1, NULL, tharg->root, NULL, options, tharg->copt, ths[tharg->sid].desc, 0); |
85 | 85 |
|
86 | 86 |
} else if(!strncmp(buff, CMD3, strlen(CMD3))) { /* QUIT */ |
87 |
- if(!progexit) |
|
88 |
- kill(progpid, SIGTERM); |
|
87 |
+ if(!progexit) { |
|
88 |
+ /* was: kill(progpid, SIGTERM); |
|
89 |
+ * Now we break out of the loop to clean up resources |
|
90 |
+ * thomas@in-online.net 20031201 */ |
|
91 |
+ progexit=1; |
|
92 |
+ } |
|
89 | 93 |
|
90 | 94 |
} else if(!strncmp(buff, CMD4, strlen(CMD4))) { /* RELOAD */ |
91 | 95 |
mdprintf(ths[tharg->sid].desc, "RELOADING\n"); |
... | ... |
@@ -230,6 +234,9 @@ void *threadwatcher(void *arg) |
230 | 230 |
logg("Pid file removed.\n"); |
231 | 231 |
} |
232 | 232 |
|
233 |
+ logg("*Freeing stat structure.\n"); |
|
234 |
+ cl_statfree(&dbstat); |
|
235 |
+ |
|
233 | 236 |
progexit = 2; |
234 | 237 |
logg("*Exit level %d, ThreadWatcher termination.\n", progexit); |
235 | 238 |
return NULL; |
... | ... |
@@ -374,6 +381,7 @@ int acceptloop_th(int socketd, struct cl_node *root, const struct cfgstruct *cop |
374 | 374 |
size_t stacksize; |
375 | 375 |
#endif |
376 | 376 |
|
377 |
+ memset (&sigact, 0, sizeof(struct sigaction)); |
|
377 | 378 |
|
378 | 379 |
/* save the PID */ |
379 | 380 |
if((cpt = cfgopt(copt, "PidFile"))) { |
... | ... |
@@ -440,7 +448,7 @@ int acceptloop_th(int socketd, struct cl_node *root, const struct cfgstruct *cop |
440 | 440 |
|
441 | 441 |
if(cfgopt(copt, "ArchiveLimitMemoryUsage")) { |
442 | 442 |
limits.archivememlim = 1; |
443 |
- logg("Archive: Limited memory usage.\n", limits.maxfiles); |
|
443 |
+ logg("Archive: Limited memory usage.\n"); |
|
444 | 444 |
} else |
445 | 445 |
limits.archivememlim = 0; |
446 | 446 |
} |
... | ... |
@@ -540,7 +548,7 @@ int acceptloop_th(int socketd, struct cl_node *root, const struct cfgstruct *cop |
540 | 540 |
pthread_attr_setstacksize(&thattr, stacksize + SCANBUFF + 64 * 1024); |
541 | 541 |
#endif |
542 | 542 |
|
543 |
- while(1) { |
|
543 |
+ while(progexit != 2) { |
|
544 | 544 |
|
545 | 545 |
/* find a free session */ |
546 | 546 |
for(i = 0; ; i++) { |
... | ... |
@@ -604,6 +612,7 @@ int acceptloop_th(int socketd, struct cl_node *root, const struct cfgstruct *cop |
604 | 604 |
ths[i].active = 0; |
605 | 605 |
} |
606 | 606 |
} |
607 |
+ free(ths); |
|
607 | 608 |
} |
608 | 609 |
|
609 | 610 |
void sighandler_th(int sig) |
... | ... |
@@ -154,7 +154,7 @@ void freshclam(struct optstruct *opt) |
154 | 154 |
if(ret > 1) |
155 | 155 |
system(getargl(opt, "on-error-execute")); |
156 | 156 |
|
157 |
- mexit(ret); |
|
157 |
+ exit(ret); |
|
158 | 158 |
|
159 | 159 |
} |
160 | 160 |
|
... | ... |
@@ -171,6 +171,7 @@ int download(struct optstruct *opt) |
171 | 171 |
int mirror_used = 0; |
172 | 172 |
struct sigaction sigalrm; |
173 | 173 |
|
174 |
+ memset(&sigalrm, 0, sizeof(struct sigaction)); |
|
174 | 175 |
sigalrm.sa_handler = d_timeout; |
175 | 176 |
sigaction(SIGALRM, &sigalrm, NULL); |
176 | 177 |
|
... | ... |
@@ -62,6 +62,7 @@ int cli_untgz(int fd, const char *destdir) |
62 | 62 |
if(nread != TAR_BLOCKSIZE) { |
63 | 63 |
cli_errmsg("Incomplete block read.\n"); |
64 | 64 |
free(fullname); |
65 |
+ gzclose(infile); |
|
65 | 66 |
return -1; |
66 | 67 |
} |
67 | 68 |
|
... | ... |
@@ -84,10 +85,12 @@ int cli_untgz(int fd, const char *destdir) |
84 | 84 |
case '5': |
85 | 85 |
cli_errmsg("Directories in CVD are not supported.\n"); |
86 | 86 |
free(fullname); |
87 |
+ gzclose(infile); |
|
87 | 88 |
return -1; |
88 | 89 |
default: |
89 | 90 |
cli_errmsg("Unknown type flag %c.\n",type); |
90 | 91 |
free(fullname); |
92 |
+ gzclose(infile); |
|
91 | 93 |
return -1; |
92 | 94 |
} |
93 | 95 |
|
... | ... |
@@ -97,6 +100,7 @@ int cli_untgz(int fd, const char *destdir) |
97 | 97 |
if(fclose(outfile)) { |
98 | 98 |
cli_errmsg("Cannot close file %s.\n", fullname); |
99 | 99 |
free(fullname); |
100 |
+ gzclose(infile); |
|
100 | 101 |
return -1; |
101 | 102 |
} |
102 | 103 |
outfile = NULL; |
... | ... |
@@ -105,6 +109,7 @@ int cli_untgz(int fd, const char *destdir) |
105 | 105 |
if(!(outfile = fopen(fullname, "wb"))) { |
106 | 106 |
cli_errmsg("Cannot create file %s.\n", fullname); |
107 | 107 |
free(fullname); |
108 |
+ gzclose(infile); |
|
108 | 109 |
return -1; |
109 | 110 |
} |
110 | 111 |
|
... | ... |
@@ -116,6 +121,7 @@ int cli_untgz(int fd, const char *destdir) |
116 | 116 |
if(size < 0) { |
117 | 117 |
cli_errmsg("Invalid size in header.\n"); |
118 | 118 |
free(fullname); |
119 |
+ gzclose(infile); |
|
119 | 120 |
return -1; |
120 | 121 |
} |
121 | 122 |
|
... | ... |
@@ -126,6 +132,7 @@ int cli_untgz(int fd, const char *destdir) |
126 | 126 |
if(nwritten != nbytes) { |
127 | 127 |
cli_errmsg("Wrote %d instead of %d (%s).\n", nwritten, nbytes, fullname); |
128 | 128 |
free(fullname); |
129 |
+ gzclose(infile); |
|
129 | 130 |
return -1; |
130 | 131 |
} |
131 | 132 |
|
... | ... |
@@ -138,6 +145,8 @@ int cli_untgz(int fd, const char *destdir) |
138 | 138 |
if(outfile) |
139 | 139 |
fclose(outfile); |
140 | 140 |
|
141 |
+ gzclose(infile); |
|
142 |
+ free(fullname); |
|
141 | 143 |
return 0; |
142 | 144 |
} |
143 | 145 |
|
... | ... |
@@ -224,8 +233,9 @@ struct cl_cvd *cl_cvdhead(const char *file) |
224 | 224 |
return NULL; |
225 | 225 |
} |
226 | 226 |
|
227 |
- if(fread(head, 1, 512, fd) != 512) { |
|
228 |
- cli_dbgmsg("Can't read CVD head from stream\n"); |
|
227 |
+ if((i=fread(head, 1, 512, fd)) != 512) { |
|
228 |
+ cli_dbgmsg("Short read (%d) while reading CVD head from %s\n", i, file); |
|
229 |
+ fclose(fd); |
|
229 | 230 |
return NULL; |
230 | 231 |
} |
231 | 232 |
|
... | ... |
@@ -270,18 +280,22 @@ int cli_cvdverify(FILE *fd) |
270 | 270 |
|
271 | 271 |
if(strncmp(md5, cvd->md5, 32)) { |
272 | 272 |
cli_dbgmsg("MD5 verification error.\n"); |
273 |
+ free(md5); |
|
274 |
+ cl_cvdfree(cvd); |
|
273 | 275 |
return CL_EMD5; |
274 | 276 |
} |
275 | 277 |
|
276 | 278 |
#ifdef HAVE_GMP |
277 | 279 |
if(cli_versig(md5, cvd->dsig)) { |
278 | 280 |
cli_dbgmsg("Digital signature verification error.\n"); |
281 |
+ free(md5); |
|
282 |
+ cl_cvdfree(cvd); |
|
279 | 283 |
return CL_EDSIG; |
280 | 284 |
} |
281 | 285 |
#endif |
282 | 286 |
|
283 | 287 |
free(md5); |
284 |
- free(cvd); |
|
288 |
+ cl_cvdfree(cvd); |
|
285 | 289 |
return 0; |
286 | 290 |
} |
287 | 291 |
|
... | ... |
@@ -107,8 +107,11 @@ int cli_versig(const char *md5, const char *dsig) |
107 | 107 |
mpz_init_set_str(n, cli_nstr, 10); |
108 | 108 |
mpz_init_set_str(e, cli_estr, 10); |
109 | 109 |
|
110 |
- if(!(pt = cli_decodesig(dsig, 16, e, n))) |
|
110 |
+ if(!(pt = cli_decodesig(dsig, 16, e, n))) { |
|
111 |
+ mpz_clear(n); |
|
112 |
+ mpz_clear(e); |
|
111 | 113 |
return CL_EDSIG; |
114 |
+ } |
|
112 | 115 |
|
113 | 116 |
pt2 = cl_str2hex(pt, 16); |
114 | 117 |
free(pt); |
... | ... |
@@ -118,10 +121,14 @@ int cli_versig(const char *md5, const char *dsig) |
118 | 118 |
if(strncmp(md5, pt2, 32)) { |
119 | 119 |
cli_dbgmsg("Signature doesn't match.\n"); |
120 | 120 |
free(pt2); |
121 |
+ mpz_clear(n); |
|
122 |
+ mpz_clear(e); |
|
121 | 123 |
return CL_EDSIG; |
122 | 124 |
} |
123 | 125 |
|
124 | 126 |
free(pt2); |
127 |
+ mpz_clear(n); |
|
128 |
+ mpz_clear(e); |
|
125 | 129 |
|
126 | 130 |
cli_dbgmsg("Digital signature is correct.\n"); |
127 | 131 |
return 0; |
... | ... |
@@ -48,11 +48,17 @@ int cli_addpatt(struct cl_node *root, struct cli_patt *pattern) |
48 | 48 |
|
49 | 49 |
if(!next) { |
50 | 50 |
next = (struct cl_node *) cli_calloc(1, sizeof(struct cl_node)); |
51 |
- if(!next) |
|
51 |
+ if(!next) { |
|
52 |
+ cli_dbgmsg("Unable to allocate pattern node (%d)\n", sizeof(struct cl_node)); |
|
52 | 53 |
return CL_EMEM; |
54 |
+ } |
|
53 | 55 |
|
54 | 56 |
root->nodes++; |
55 | 57 |
root->nodetable = (struct cl_node **) realloc(root->nodetable, (root->nodes) * sizeof(struct cl_node *)); |
58 |
+ if (root->nodetable == NULL) { |
|
59 |
+ cli_dbgmsg("Unable to realloc nodetable (%d)\n", (root->nodes) * sizeof(struct cl_node *)); |
|
60 |
+ return CL_EMEM; |
|
61 |
+ } |
|
56 | 62 |
root->nodetable[root->nodes - 1] = next; |
57 | 63 |
|
58 | 64 |
pos->trans[((unsigned char) pattern->pattern[i]) & 0xff] = next; |
... | ... |
@@ -74,6 +80,10 @@ void cli_enqueue(struct nodelist **bfs, struct cl_node *n) |
74 | 74 |
struct nodelist *new; |
75 | 75 |
|
76 | 76 |
new = (struct nodelist *) cli_calloc(1, sizeof(struct nodelist)); |
77 |
+ if (new == NULL) { |
|
78 |
+ cli_dbgmsg("Unable to allocate node list (%d)\n", sizeof(struct nodelist)); |
|
79 |
+ return CL_EMEM; |
|
80 |
+ } |
|
77 | 81 |
|
78 | 82 |
new->next = *bfs; |
79 | 83 |
new->node = n; |
... | ... |
@@ -181,7 +191,11 @@ int cl_scanbuff(const char *buffer, unsigned int length, char **virname, const s |
181 | 181 |
|
182 | 182 |
current = (struct cl_node *) root; |
183 | 183 |
|
184 |
- partcnt = (int *) cli_calloc(root->partsigs + 1, sizeof(int)); |
|
184 |
+ if ((partcnt = (int *) cli_calloc(root->partsigs + 1, sizeof(int))) == NULL) { |
|
185 |
+ cli_dbgmsg("cl_scanbuff(): unable to calloc(%d, %d)\n", root->partsigs + 1, sizeof(int)); |
|
186 |
+ return CL_EMEM; |
|
187 |
+ } |
|
188 |
+ |
|
185 | 189 |
|
186 | 190 |
for(i = 0; i < length; i++) { |
187 | 191 |
current = current->trans[(unsigned char) buffer[i] & 0xff]; |
... | ... |
@@ -93,6 +93,7 @@ int cl_loaddb(const char *filename, struct cl_node **root, int *virnum) |
93 | 93 |
if(!(buffer = (char *) cli_malloc(FILEBUFF))) |
94 | 94 |
return CL_EMEM; |
95 | 95 |
|
96 |
+ memset(buffer, 0, FILEBUFF); |
|
96 | 97 |
/* check for CVD file */ |
97 | 98 |
fgets(buffer, 12, fd); |
98 | 99 |
rewind(fd); |
... | ... |
@@ -328,8 +329,10 @@ int cl_statfree(struct cl_stat *dbstat) |
328 | 328 |
free(dbstat->stattab); |
329 | 329 |
dbstat->stattab = NULL; |
330 | 330 |
dbstat->no = 0; |
331 |
- if(dbstat->dir) |
|
331 |
+ if(dbstat->dir) { |
|
332 | 332 |
free(dbstat->dir); |
333 |
+ dbstat->dir = NULL; |
|
334 |
+ } |
|
333 | 335 |
} else { |
334 | 336 |
cli_errmsg("cl_statfree(): Null argument passed.\n"); |
335 | 337 |
return CL_ENULLARG; |
... | ... |
@@ -71,11 +71,12 @@ cl_node *root) |
71 | 71 |
char *buffer, *buff, *endbl, *pt; |
72 | 72 |
int bytes, buffsize, length; |
73 | 73 |
|
74 |
- |
|
75 | 74 |
/* prepare the buffer */ |
76 | 75 |
buffsize = root->maxpatlen + SCANBUFF; |
77 |
- if(!(buffer = (char *) cli_calloc(buffsize, sizeof(char)))) |
|
76 |
+ if(!(buffer = (char *) cli_calloc(buffsize, sizeof(char)))) { |
|
77 |
+ cli_dbgmsg("cli_scandesc(): unable to malloc(%d)\n", buffsize); |
|
78 | 78 |
return CL_EMEM; |
79 |
+ } |
|
79 | 80 |
|
80 | 81 |
buff = buffer; |
81 | 82 |
buff += root->maxpatlen; /* pointer to read data block */ |
... | ... |
@@ -257,6 +258,7 @@ int cli_scanzip(int desc, char **virname, long int *scanned, const struct cl_nod |
257 | 257 |
fstat(desc, &source); |
258 | 258 |
|
259 | 259 |
if(!(buff = (char *) cli_malloc(FILEBUFF))) { |
260 |
+ cli_dbgmsg("cli_scanzip(): unable to malloc(%d)\n", FILEBUFF); |
|
260 | 261 |
zzip_dir_close(zdir); |
261 | 262 |
return CL_EMEM; |
262 | 263 |
} |
... | ... |
@@ -401,6 +403,7 @@ int cli_scangzip(int desc, char **virname, long int *scanned, const struct cl_no |
401 | 401 |
fd = fileno(tmp); |
402 | 402 |
|
403 | 403 |
if(!(buff = (char *) cli_malloc(FILEBUFF))) { |
404 |
+ cli_dbgmsg("cli_scangzip(): unable to malloc(%d)\n", FILEBUFF); |
|
404 | 405 |
gzclose(gd); |
405 | 406 |
return CL_EMEM; |
406 | 407 |
} |
... | ... |
@@ -462,7 +465,7 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no |
462 | 462 |
BZFILE *bfd; |
463 | 463 |
|
464 | 464 |
|
465 |
- if((fs = fdopen(desc, "rb")) == NULL) { |
|
465 |
+ if((fs = fdopen(dup(desc), "rb")) == NULL) { |
|
466 | 466 |
cli_errmsg("Can't fdopen() descriptor %d.\n", desc); |
467 | 467 |
return CL_EBZIP; |
468 | 468 |
} |
... | ... |
@@ -473,18 +476,25 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no |
473 | 473 |
|
474 | 474 |
if((bfd = BZ2_bzReadOpen(&bzerror, fs, memlim, 0, NULL, 0)) == NULL) { |
475 | 475 |
cli_dbgmsg("Can't initialize bzip2 library (descriptor %d).\n", desc); |
476 |
+ fclose(fs); |
|
476 | 477 |
return CL_EBZIP; |
477 | 478 |
} |
478 | 479 |
|
479 | 480 |
if((tmp = tmpfile()) == NULL) { |
480 | 481 |
cli_dbgmsg("Can't generate tmpfile().\n"); |
481 | 482 |
BZ2_bzReadClose(&bzerror, bfd); |
483 |
+ fclose(fs); |
|
482 | 484 |
return CL_ETMPFILE; |
483 | 485 |
} |
484 | 486 |
fd = fileno(tmp); |
485 | 487 |
|
486 |
- if(!(buff = (char *) cli_malloc(FILEBUFF))) |
|
488 |
+ if(!(buff = (char *) malloc(FILEBUFF))) { |
|
489 |
+ cli_dbgmsg("cli_scanbzip(): unable to malloc(%d)\n", FILEBUFF); |
|
490 |
+ fclose(tmp); |
|
491 |
+ fclose(fs); |
|
492 |
+ BZ2_bzReadClose(&bzerror, bfd); |
|
487 | 493 |
return CL_EMEM; |
494 |
+ } |
|
488 | 495 |
|
489 | 496 |
while((bytes = BZ2_bzRead(&bzerror, bfd, buff, FILEBUFF)) > 0) { |
490 | 497 |
size += bytes; |
... | ... |
@@ -501,6 +511,7 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no |
501 | 501 |
BZ2_bzReadClose(&bzerror, bfd); |
502 | 502 |
fclose(tmp); |
503 | 503 |
free(buff); |
504 |
+ fclose(fs); |
|
504 | 505 |
return CL_EGZIP; |
505 | 506 |
} |
506 | 507 |
} |
... | ... |
@@ -510,16 +521,16 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no |
510 | 510 |
if(fsync(fd) == -1) { |
511 | 511 |
cli_dbgmsg("fsync() failed for descriptor %d\n", fd); |
512 | 512 |
fclose(tmp); |
513 |
+ fclose(fs); |
|
513 | 514 |
return CL_EFSYNC; |
514 | 515 |
} |
515 | 516 |
|
516 | 517 |
lseek(fd, 0, SEEK_SET); |
517 | 518 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, reclev)) == CL_VIRUS ) { |
518 | 519 |
cli_dbgmsg("Bzip2 -> Found %s virus.\n", *virname); |
519 |
- fclose(tmp); |
|
520 |
- return CL_VIRUS; |
|
521 | 520 |
} |
522 | 521 |
fclose(tmp); |
522 |
+ fclose(fs); |
|
523 | 523 |
|
524 | 524 |
return ret; |
525 | 525 |
} |
... | ... |
@@ -663,29 +674,36 @@ int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct |
663 | 663 |
#ifdef CL_THREAD_SAFE |
664 | 664 |
/* this check protects against recursive deadlock */ |
665 | 665 |
if(!DISABLE_RAR && SCAN_ARCHIVE && !cli_scanrar_inuse && !strncmp(magic, RAR_MAGIC_STR, strlen(RAR_MAGIC_STR))) { |
666 |
+ cli_dbgmsg("Recognized rar file.\n"); |
|
666 | 667 |
ret = cli_scanrar(desc, virname, scanned, root, limits, options, reclev); |
667 | 668 |
} |
668 | 669 |
#else |
669 | 670 |
if(!DISABLE_RAR && SCAN_ARCHIVE && !strncmp(magic, RAR_MAGIC_STR, strlen(RAR_MAGIC_STR))) { |
671 |
+ cli_dbgmsg("Recognized rar file.\n"); |
|
670 | 672 |
ret = cli_scanrar(desc, virname, scanned, root, limits, options, reclev); |
671 | 673 |
} |
672 | 674 |
#endif |
673 | 675 |
#ifdef HAVE_ZLIB_H |
674 | 676 |
else if(SCAN_ARCHIVE && !strncmp(magic, ZIP_MAGIC_STR, strlen(ZIP_MAGIC_STR))) { |
677 |
+ cli_dbgmsg("Recognized zip file.\n"); |
|
675 | 678 |
ret = cli_scanzip(desc, virname, scanned, root, limits, options, reclev); |
676 | 679 |
} else if(SCAN_ARCHIVE && !strncmp(magic, GZIP_MAGIC_STR, strlen(GZIP_MAGIC_STR))) { |
680 |
+ cli_dbgmsg("Recognized gzip file.\n"); |
|
677 | 681 |
ret = cli_scangzip(desc, virname, scanned, root, limits, options, reclev); |
678 | 682 |
} |
679 | 683 |
#endif |
680 | 684 |
#ifdef HAVE_BZLIB_H |
681 | 685 |
else if(SCAN_ARCHIVE && !strncmp(magic, BZIP_MAGIC_STR, strlen(BZIP_MAGIC_STR))) { |
686 |
+ cli_dbgmsg("Recognized bzip file.\n"); |
|
682 | 687 |
ret = cli_scanbzip(desc, virname, scanned, root, limits, options, reclev); |
683 | 688 |
} |
684 | 689 |
#endif |
685 | 690 |
else if(SCAN_MAIL && !strncmp(magic, MAIL_MAGIC_STR, strlen(MAIL_MAGIC_STR))) { |
691 |
+ cli_dbgmsg("Recognized mail file.\n"); |
|
686 | 692 |
ret = cli_scanmail(desc, virname, scanned, root, limits, options, reclev); |
687 | 693 |
} |
688 | 694 |
else if(SCAN_MAIL && !strncmp(magic, RAWMAIL_MAGIC_STR, strlen(RAWMAIL_MAGIC_STR))) { |
695 |
+ cli_dbgmsg("Recognized raw mail file.\n"); |
|
689 | 696 |
ret = cli_scanmail(desc, virname, scanned, root, limits, options, reclev); |
690 | 697 |
} else if(SCAN_MAIL && !strncmp(magic, MAILDIR_MAGIC_STR, strlen(MAILDIR_MAGIC_STR))) { |
691 | 698 |
cli_dbgmsg("Recognized Maildir mail file.\n"); |