Commit 1917d2ee 1917d2ee74d54f841f09daa2171298ed1a716dfe by Sergey Poznyakoff

(util_finish): Fix potential vulnerability (IDEF0954).

1 parent 9c1f689b
...@@ -199,13 +199,14 @@ util_msgset (char *s, size_t ** set, int *n, int isuid) ...@@ -199,13 +199,14 @@ util_msgset (char *s, size_t ** set, int *n, int isuid)
199 { 199 {
200 errno = 0; 200 errno = 0;
201 val = strtoul (s, &s, 10); 201 val = strtoul (s, &s, 10);
202 if (val == ULONG_MAX && errno == ERANGE) 202 if ((val == ULONG_MAX && errno == ERANGE) || val > max)
203 { 203 {
204 if (*set) 204 if (*set)
205 free (*set); 205 free (*set);
206 *n = 0; 206 *n = 0;
207 return EINVAL; 207 return EINVAL;
208 } 208 }
209
209 if (low) 210 if (low)
210 { 211 {
211 /* Reverse it. */ 212 /* Reverse it. */
...@@ -400,26 +401,40 @@ util_out (int rc, const char *format, ...) ...@@ -400,26 +401,40 @@ util_out (int rc, const char *format, ...)
400 int 401 int
401 util_finish (struct imap4d_command *command, int rc, const char *format, ...) 402 util_finish (struct imap4d_command *command, int rc, const char *format, ...)
402 { 403 {
403 char *tempbuf = NULL; 404 size_t size;
404 char *buf = NULL; 405 char *buf = NULL;
406 char *tempbuf = NULL;
405 int new_state; 407 int new_state;
406 int status = 0; 408 int status = 0;
407 va_list ap; 409 va_list ap;
408 410 char *sc = sc2string (rc);
409 asprintf (&tempbuf, "%s %s%s %s\r\n", command->tag, sc2string (rc), 411
410 command->name, format);
411 va_start (ap, format); 412 va_start (ap, format);
412 vasprintf (&buf, tempbuf, ap); 413 vasprintf (&tempbuf, format, ap);
413 va_end (ap); 414 va_end (ap);
415 if (!tempbuf)
416 imap4d_bye (ERR_NO_MEM);
417
418 size = strlen (command->tag) + 1 +
419 strlen (sc) + strlen (command->name) + 1 +
420 strlen (tempbuf) + 1;
421 buf = malloc (size);
414 if (!buf) 422 if (!buf)
415 imap4d_bye (ERR_NO_MEM); 423 imap4d_bye (ERR_NO_MEM);
424 strcpy (buf, command->tag);
425 strcat (buf, " ");
426 strcat (buf, sc);
427 strcat (buf, command->name);
428 strcat (buf, " ");
429 strcat (buf, tempbuf);
430 free (tempbuf);
416 431
417 if (daemon_param.transcript) 432 if (daemon_param.transcript)
418 syslog (LOG_DEBUG, "sent: %s", buf); 433 syslog (LOG_DEBUG, "sent: %s\r\n", buf);
419 434
420 status = stream_sequential_write (ostream, buf, strlen (buf)); 435 stream_sequential_write (ostream, buf, strlen (buf));
421 free (buf); 436 free (buf);
422 free (tempbuf); 437 stream_sequential_write (ostream, "\r\n", 2);
423 438
424 /* Reset the state. */ 439 /* Reset the state. */
425 if (rc == RESP_OK) 440 if (rc == RESP_OK)
......