@matheusbsbs / matheus-bagliano / commits / 8960844a78
Fork 0

check patch_delta bounds more carefully

Let's avoid going south with invalid delta data. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 7, 2006 at 15:26 UTC 8960844a7890b1ac6ad5f8abf58a2a20923dde6d
3 files changed +28 -9
delta.h
+3 -2
@@ -16,7 +16,8 @@ extern void *patch_delta(void *src_buf, unsigned long src_size,
16 * This must be called twice on the delta data buffer, first to get the
17 * expected reference buffer size, and again to get the result buffer size.
18 */
19 -static inline unsigned long get_delta_hdr_size(const unsigned char **datap)
19 +static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
20 + const unsigned char *top)
21 {
22 const unsigned char *data = *datap;
23 unsigned char cmd;
@@ -26,7 +27,7 @@ static inline unsigned long get_delta_hdr_size(const unsigned char **datap)
27 cmd = *data++;
28 size |= (cmd & ~0x80) << i;
29 i += 7;
29 - } while (cmd & 0x80);
30 + } while (cmd & 0x80 && data < top);
31 *datap = data;
32 return size;
33 }
patch-delta.c
+21 -5
@@ -28,12 +28,12 @@ void *patch_delta(void *src_buf, unsigned long src_size,
28 top = delta_buf + delta_size;
29
30 /* make sure the orig file size matches what we expect */
31 - size = get_delta_hdr_size(&data);
31 + size = get_delta_hdr_size(&data, top);
32 if (size != src_size)
33 return NULL;
34
35 /* now the result size */
36 - size = get_delta_hdr_size(&data);
36 + size = get_delta_hdr_size(&data, top);
37 dst_buf = malloc(size + 1);
38 if (!dst_buf)
39 return NULL;
@@ -52,21 +52,37 @@ void *patch_delta(void *src_buf, unsigned long src_size,
52 if (cmd & 0x20) cp_size |= (*data++ << 8);
53 if (cmd & 0x40) cp_size |= (*data++ << 16);
54 if (cp_size == 0) cp_size = 0x10000;
55 + if (cp_off + cp_size < cp_size ||
56 + cp_off + cp_size > src_size ||
57 + cp_size > size)
58 + goto bad;
59 memcpy(out, src_buf + cp_off, cp_size);
60 out += cp_size;
57 - } else {
61 + size -= cp_size;
62 + } else if (cmd) {
63 + if (cmd > size)
64 + goto bad;
65 memcpy(out, data, cmd);
66 out += cmd;
67 data += cmd;
68 + size -= cmd;
69 + } else {
70 + /*
71 + * cmd == 0 is reserved for future encoding
72 + * extensions. In the mean time we must fail when
73 + * encountering them (might be data corruption).
74 + */
75 + goto bad;
76 }
77 }
78
79 /* sanity check */
65 - if (data != top || out - dst_buf != size) {
80 + if (data != top || size != 0) {
81 + bad:
82 free(dst_buf);
83 return NULL;
84 }
85
70 - *dst_size = size;
86 + *dst_size = out - dst_buf;
87 return dst_buf;
88 }
sha1_file.c
+4 -2
@@ -804,10 +804,12 @@ static int packed_delta_info(unsigned char *base_sha1,
804 * the result size.
805 */
806 data = delta_head;
807 - get_delta_hdr_size(&data); /* ignore base size */
807 +
808 + /* ignore base size */
809 + get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
810
811 /* Read the result size */
810 - result_size = get_delta_hdr_size(&data);
812 + result_size = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
813 *sizep = result_size;
814 }
815 return 0;