aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Tarreau <w@1wt.eu>2014-09-27 06:31:37 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-09-28 05:08:01 -0400
commit72cf90124e87d975d0b2114d930808c58b4c05e4 (patch)
treee258cacbf3bc33acf934d6b79e5d5b60248abb5f
parentaf958a38a60c7ca3d8a39c918c1baa2ff7b6b233 (diff)
lzo: check for length overrun in variable length encoding.
This fix ensures that we never meet an integer overflow while adding 255 while parsing a variable length encoding. It works differently from commit 206a81c ("lzo: properly check for overruns") because instead of ensuring that we don't overrun the input, which is tricky to guarantee due to many assumptions in the code, it simply checks that the cumulated number of 255 read cannot overflow by bounding this number. The MAX_255_COUNT is the maximum number of times we can add 255 to a base count without overflowing an integer. The multiply will overflow when multiplying 255 by more than MAXINT/255. The sum will overflow earlier depending on the base count. Since the base count is taken from a u8 and a few bits, it is safe to assume that it will always be lower than or equal to 2*255, thus we can always prevent any overflow by accepting two less 255 steps. This patch also reduces the CPU overhead and actually increases performance by 1.1% compared to the initial code, while the previous fix costs 3.1% (measured on x86_64). The fix needs to be backported to all currently supported stable kernels. Reported-by: Willem Pinckaers <willem@lekkertech.net> Cc: "Don A. Bailey" <donb@securitymouse.com> Cc: stable <stable@vger.kernel.org> Signed-off-by: Willy Tarreau <w@1wt.eu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--lib/lzo/lzo1x_decompress_safe.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/lib/lzo/lzo1x_decompress_safe.c b/lib/lzo/lzo1x_decompress_safe.c
index 569985d522d5..a1c387f6afba 100644
--- a/lib/lzo/lzo1x_decompress_safe.c
+++ b/lib/lzo/lzo1x_decompress_safe.c
@@ -25,6 +25,16 @@
25#define NEED_OP(x) if (!HAVE_OP(x)) goto output_overrun 25#define NEED_OP(x) if (!HAVE_OP(x)) goto output_overrun
26#define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun 26#define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun
27 27
28/* This MAX_255_COUNT is the maximum number of times we can add 255 to a base
29 * count without overflowing an integer. The multiply will overflow when
30 * multiplying 255 by more than MAXINT/255. The sum will overflow earlier
31 * depending on the base count. Since the base count is taken from a u8
32 * and a few bits, it is safe to assume that it will always be lower than
33 * or equal to 2*255, thus we can always prevent any overflow by accepting
34 * two less 255 steps. See Documentation/lzo.txt for more information.
35 */
36#define MAX_255_COUNT ((((size_t)~0) / 255) - 2)
37
28int lzo1x_decompress_safe(const unsigned char *in, size_t in_len, 38int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
29 unsigned char *out, size_t *out_len) 39 unsigned char *out, size_t *out_len)
30{ 40{
@@ -55,12 +65,19 @@ int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
55 if (t < 16) { 65 if (t < 16) {
56 if (likely(state == 0)) { 66 if (likely(state == 0)) {
57 if (unlikely(t == 0)) { 67 if (unlikely(t == 0)) {
68 size_t offset;
69 const unsigned char *ip_last = ip;
70
58 while (unlikely(*ip == 0)) { 71 while (unlikely(*ip == 0)) {
59 t += 255;
60 ip++; 72 ip++;
61 NEED_IP(1); 73 NEED_IP(1);
62 } 74 }
63 t += 15 + *ip++; 75 offset = ip - ip_last;
76 if (unlikely(offset > MAX_255_COUNT))
77 return LZO_E_ERROR;
78
79 offset = (offset << 8) - offset;
80 t += offset + 15 + *ip++;
64 } 81 }
65 t += 3; 82 t += 3;
66copy_literal_run: 83copy_literal_run:
@@ -116,12 +133,19 @@ copy_literal_run:
116 } else if (t >= 32) { 133 } else if (t >= 32) {
117 t = (t & 31) + (3 - 1); 134 t = (t & 31) + (3 - 1);
118 if (unlikely(t == 2)) { 135 if (unlikely(t == 2)) {
136 size_t offset;
137 const unsigned char *ip_last = ip;
138
119 while (unlikely(*ip == 0)) { 139 while (unlikely(*ip == 0)) {
120 t += 255;
121 ip++; 140 ip++;
122 NEED_IP(1); 141 NEED_IP(1);
123 } 142 }
124 t += 31 + *ip++; 143 offset = ip - ip_last;
144 if (unlikely(offset > MAX_255_COUNT))
145 return LZO_E_ERROR;
146
147 offset = (offset << 8) - offset;
148 t += offset + 31 + *ip++;
125 NEED_IP(2); 149 NEED_IP(2);
126 } 150 }
127 m_pos = op - 1; 151 m_pos = op - 1;
@@ -134,12 +158,19 @@ copy_literal_run:
134 m_pos -= (t & 8) << 11; 158 m_pos -= (t & 8) << 11;
135 t = (t & 7) + (3 - 1); 159 t = (t & 7) + (3 - 1);
136 if (unlikely(t == 2)) { 160 if (unlikely(t == 2)) {
161 size_t offset;
162 const unsigned char *ip_last = ip;
163
137 while (unlikely(*ip == 0)) { 164 while (unlikely(*ip == 0)) {
138 t += 255;
139 ip++; 165 ip++;
140 NEED_IP(1); 166 NEED_IP(1);
141 } 167 }
142 t += 7 + *ip++; 168 offset = ip - ip_last;
169 if (unlikely(offset > MAX_255_COUNT))
170 return LZO_E_ERROR;
171
172 offset = (offset << 8) - offset;
173 t += offset + 7 + *ip++;
143 NEED_IP(2); 174 NEED_IP(2);
144 } 175 }
145 next = get_unaligned_le16(ip); 176 next = get_unaligned_le16(ip);