Temporary fix for stack smashing in mailinfo
Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alex Riesen committed
Aug 30, 2007 at 23:48 UTC
8dabdfcc1bca58884eb161ab1d9b9416da1090c8
1 file changed
+50
-35
builtin-mailinfo.c
+50
-35
@@ -287,12 +287,12 @@ static void cleanup_space(char *buf)
287
}
288
}
289
290
-static void decode_header(char *it);
290
+static void decode_header(char *it, unsigned itsize);
291
static char *header[MAX_HDR_PARSED] = {
292
"From","Subject","Date",
293
};
294
295
-static int check_header(char *line, char **hdr_data, int overwrite)
295
+static int check_header(char *line, unsigned linesize, char **hdr_data, int overwrite)
296
{
297
int i;
298
@@ -305,7 +305,7 @@ static int check_header(char *line, char **hdr_data, int overwrite)
305
/* Unwrap inline B and Q encoding, and optionally
306
* normalize the meta information to utf8.
307
*/
308
- decode_header(line + len + 2);
308
+ decode_header(line + len + 2, linesize - len - 2);
309
hdr_data[i] = xmalloc(1000 * sizeof(char));
310
if (! handle_header(line, hdr_data[i], len + 2)) {
311
return 1;
@@ -316,14 +316,14 @@ static int check_header(char *line, char **hdr_data, int overwrite)
316
/* Content stuff */
317
if (!strncasecmp(line, "Content-Type", 12) &&
318
line[12] == ':' && isspace(line[12 + 1])) {
319
- decode_header(line + 12 + 2);
319
+ decode_header(line + 12 + 2, linesize - 12 - 2);
320
if (! handle_content_type(line)) {
321
return 1;
322
}
323
}
324
if (!strncasecmp(line, "Content-Transfer-Encoding", 25) &&
325
line[25] == ':' && isspace(line[25 + 1])) {
326
- decode_header(line + 25 + 2);
326
+ decode_header(line + 25 + 2, linesize - 25 - 2);
327
if (! handle_content_transfer_encoding(line)) {
328
return 1;
329
}
@@ -432,10 +432,15 @@ static int read_one_header_line(char *line, int sz, FILE *in)
432
return 1;
433
}
434
435
-static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
435
+static int decode_q_segment(char *in, char *ot, unsigned otsize, char *ep, int rfc2047)
436
{
437
+ char *otend = ot + otsize;
438
int c;
439
while ((c = *in++) != 0 && (in <= ep)) {
440
+ if (ot == otend) {
441
+ *--ot = '\0';
442
+ return -1;
443
+ }
444
if (c == '=') {
445
int d = *in++;
446
if (d == '\n' || !d)
@@ -451,12 +456,17 @@ static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
456
return 0;
457
}
458
454
-static int decode_b_segment(char *in, char *ot, char *ep)
459
+static int decode_b_segment(char *in, char *ot, unsigned otsize, char *ep)
460
{
461
/* Decode in..ep, possibly in-place to ot */
462
int c, pos = 0, acc = 0;
463
+ char *otend = ot + otsize;
464
465
while ((c = *in++) != 0 && (in <= ep)) {
466
+ if (ot == otend) {
467
+ *--ot = '\0';
468
+ return -1;
469
+ }
470
if (c == '+')
471
c = 62;
472
else if (c == '/')
@@ -518,7 +528,7 @@ static const char *guess_charset(const char *line, const char *target_charset)
528
return "latin1";
529
}
530
521
-static void convert_to_utf8(char *line, const char *charset)
531
+static void convert_to_utf8(char *line, unsigned linesize, const char *charset)
532
{
533
char *out;
534
@@ -534,11 +544,11 @@ static void convert_to_utf8(char *line, const char *charset)
544
if (!out)
545
die("cannot convert from %s to %s\n",
546
charset, metainfo_charset);
537
- strcpy(line, out);
547
+ strlcpy(line, out, linesize);
548
free(out);
549
}
550
541
-static int decode_header_bq(char *it)
551
+static int decode_header_bq(char *it, unsigned itsize)
552
{
553
char *in, *out, *ep, *cp, *sp;
554
char outbuf[1000];
@@ -578,56 +588,60 @@ static int decode_header_bq(char *it)
588
default:
589
return rfc2047; /* no munging */
590
case 'b':
581
- sz = decode_b_segment(cp + 3, piecebuf, ep);
591
+ sz = decode_b_segment(cp + 3, piecebuf, sizeof(piecebuf), ep);
592
break;
593
case 'q':
584
- sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
594
+ sz = decode_q_segment(cp + 3, piecebuf, sizeof(piecebuf), ep, 1);
595
break;
596
}
597
if (sz < 0)
598
return rfc2047;
599
if (metainfo_charset)
590
- convert_to_utf8(piecebuf, charset_q);
600
+ convert_to_utf8(piecebuf, sizeof(piecebuf), charset_q);
601
+
602
+ sz = strlen(piecebuf);
603
+ if (outbuf + sizeof(outbuf) <= out + sz)
604
+ return rfc2047; /* no munging */
605
strcpy(out, piecebuf);
592
- out += strlen(out);
606
+ out += sz;
607
in = ep + 2;
608
}
609
strcpy(out, in);
596
- strcpy(it, outbuf);
610
+ strlcpy(it, outbuf, itsize);
611
return rfc2047;
612
}
613
600
-static void decode_header(char *it)
614
+static void decode_header(char *it, unsigned itsize)
615
{
616
603
- if (decode_header_bq(it))
617
+ if (decode_header_bq(it, itsize))
618
return;
619
/* otherwise "it" is a straight copy of the input.
620
* This can be binary guck but there is no charset specified.
621
*/
622
if (metainfo_charset)
609
- convert_to_utf8(it, "");
623
+ convert_to_utf8(it, itsize, "");
624
}
625
612
-static void decode_transfer_encoding(char *line)
626
+static void decode_transfer_encoding(char *line, unsigned linesize)
627
{
628
char *ep;
629
630
switch (transfer_encoding) {
631
case TE_QP:
632
ep = line + strlen(line);
619
- decode_q_segment(line, line, ep, 0);
633
+ decode_q_segment(line, line, linesize, ep, 0);
634
break;
635
case TE_BASE64:
636
ep = line + strlen(line);
623
- decode_b_segment(line, line, ep);
637
+ decode_b_segment(line, line, linesize, ep);
638
break;
639
case TE_DONTCARE:
640
break;
641
}
642
}
643
630
-static int handle_filter(char *line);
644
+static int handle_filter(char *line, unsigned linesize);
645
646
static int find_boundary(void)
647
{
@@ -655,7 +669,7 @@ again:
669
"can't recover\n");
670
exit(1);
671
}
658
- handle_filter(newline);
672
+ handle_filter(newline, sizeof(newline));
673
674
/* skip to the next boundary */
675
if (!find_boundary())
@@ -670,7 +684,7 @@ again:
684
685
/* slurp in this section's info */
686
while (read_one_header_line(line, sizeof(line), fin))
673
- check_header(line, p_hdr_data, 0);
687
+ check_header(line, sizeof(line), p_hdr_data, 0);
688
689
/* eat the blank line after section info */
690
return (fgets(line, sizeof(line), fin) != NULL);
@@ -709,9 +723,10 @@ static inline int patchbreak(const char *line)
723
}
724
725
712
-static int handle_commit_msg(char *line)
726
+static int handle_commit_msg(char *line, unsigned linesize)
727
{
728
static int still_looking = 1;
729
+ char *endline = line + linesize;
730
731
if (!cmitmsg)
732
return 0;
@@ -726,13 +741,13 @@ static int handle_commit_msg(char *line)
741
if (!*cp)
742
return 0;
743
}
729
- if ((still_looking = check_header(cp, s_hdr_data, 0)) != 0)
744
+ if ((still_looking = check_header(cp, endline - cp, s_hdr_data, 0)) != 0)
745
return 0;
746
}
747
748
/* normalize the log message to UTF-8. */
749
if (metainfo_charset)
735
- convert_to_utf8(line, charset);
750
+ convert_to_utf8(line, endline - line, charset);
751
752
if (patchbreak(line)) {
753
fclose(cmitmsg);
@@ -751,7 +766,7 @@ static int handle_patch(char *line)
766
return 0;
767
}
768
754
-static int handle_filter(char *line)
769
+static int handle_filter(char *line, unsigned linesize)
770
{
771
static int filter = 0;
772
@@ -760,7 +775,7 @@ static int handle_filter(char *line)
775
*/
776
switch (filter) {
777
case 0:
763
- if (!handle_commit_msg(line))
778
+ if (!handle_commit_msg(line, linesize))
779
break;
780
filter++;
781
case 1:
@@ -792,14 +807,14 @@ static void handle_body(void)
807
/* flush any leftover */
808
if ((transfer_encoding == TE_BASE64) &&
809
(np != newline)) {
795
- handle_filter(newline);
810
+ handle_filter(newline, sizeof(newline));
811
}
812
if (!handle_boundary())
813
return;
814
}
815
816
/* Unwrap transfer encoding */
802
- decode_transfer_encoding(line);
817
+ decode_transfer_encoding(line, sizeof(line));
818
819
switch (transfer_encoding) {
820
case TE_BASE64:
@@ -808,7 +823,7 @@ static void handle_body(void)
823
824
/* binary data most likely doesn't have newlines */
825
if (message_type != TYPE_TEXT) {
811
- rc = handle_filter(line);
826
+ rc = handle_filter(line, sizeof(newline));
827
break;
828
}
829
@@ -825,7 +840,7 @@ static void handle_body(void)
840
/* should be sitting on a new line */
841
*(++np) = 0;
842
op++;
828
- rc = handle_filter(newline);
843
+ rc = handle_filter(newline, sizeof(newline));
844
np = newline;
845
}
846
} while (*op != 0);
@@ -835,7 +850,7 @@ static void handle_body(void)
850
break;
851
}
852
default:
838
- rc = handle_filter(line);
853
+ rc = handle_filter(line, sizeof(newline));
854
}
855
if (rc)
856
/* nothing left to filter */
@@ -922,7 +937,7 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
937
938
/* process the email header */
939
while (read_one_header_line(line, sizeof(line), fin))
925
- check_header(line, p_hdr_data, 1);
940
+ check_header(line, sizeof(line), p_hdr_data, 1);
941
942
handle_body();
943
handle_info();