Skip to contentSkip to conversation

lzo: refine overrun checking

PATCH In review Jan Beulich 3 participants 6 messages Nov 3, 2014 – Nov 4, 2014

lore
Share & tools
lore
#0 lore raw

cover lzo: refine overrun checking

The original fix we inherited from Linux go re-done. Let's do so too.

1: Revert "lzo: properly check for overruns"
2: lzo: check for length overrun in variable length encoding

#1 lore raw

PATCH 1/2 Revert "lzo: properly check for overruns"

This reverts commit 504f70b6 ("lzo: properly check for overruns").

As analysed by Willem Pinckaers, this fix is still incomplete on certain rare corner cases, and it is easier to restart from the original code.

[original Linux commit: af958a38]
xen/common/lzo.c +21 -41
@@ -375,31 +375,11 @@ int lzo1x_1_compress(const unsigned char
  *  Richard Purdie <[email protected]>
  */
 
-#define HAVE_IP(t, x)                              \
-    (((size_t)(ip_end - ip) >= (size_t)(t + x)) && \
-     (((t + x) >= t) && ((t + x) >= x)))
-
-#define HAVE_OP(t, x)                              \
-    (((size_t)(op_end - op) >= (size_t)(t + x)) && \
-     (((t + x) >= t) && ((t + x) >= x)))
-
-#define NEED_IP(t, x)                \
-    do {                             \
-        if (!HAVE_IP(t, x))          \
-            goto input_overrun;      \
-    } while (0)
-
-#define NEED_OP(t, x)                \
-    do {                             \
-        if (!HAVE_OP(t, x))          \
-            goto output_overrun;     \
-    } while (0)
-
-#define TEST_LB(m_pos)               \
-    do {                             \
-        if ((m_pos) < out)           \
-            goto lookbehind_overrun; \
-    } while (0)
+#define HAVE_IP(x)     ((size_t)(ip_end - ip) >= (size_t)(x))
+#define HAVE_OP(x)     ((size_t)(op_end - op) >= (size_t)(x))
+#define NEED_IP(x)     if (!HAVE_IP(x)) goto input_overrun
+#define NEED_OP(x)     if (!HAVE_OP(x)) goto output_overrun
+#define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun
 
 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
                           unsigned char *out, size_t *out_len)
@@ -434,14 +414,14 @@ int lzo1x_decompress_safe(const unsigned
                     while (unlikely(*ip == 0)) {
                         t += 255;
                         ip++;
-                        NEED_IP(1, 0);
+                        NEED_IP(1);
                     }
                     t += 15 + *ip++;
                 }
                 t += 3;
  copy_literal_run:
 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
-                if (likely(HAVE_IP(t, 15) && HAVE_OP(t, 15))) {
+                if (likely(HAVE_IP(t + 15) && HAVE_OP(t + 15))) {
                     const unsigned char *ie = ip + t;
                     unsigned char *oe = op + t;
                     do {
@@ -457,8 +437,8 @@ int lzo1x_decompress_safe(const unsigned
                 } else
 #endif
                 {
-                    NEED_OP(t, 0);
-                    NEED_IP(t, 3);
+                    NEED_OP(t);
+                    NEED_IP(t + 3);
                     do {
                         *op++ = *ip++;
                     } while (--t > 0);
@@ -471,7 +451,7 @@ int lzo1x_decompress_safe(const unsigned
                 m_pos -= t >> 2;
                 m_pos -= *ip++ << 2;
                 TEST_LB(m_pos);
-                NEED_OP(2, 0);
+                NEED_OP(2);
                 op[0] = m_pos[0];
                 op[1] = m_pos[1];
                 op += 2;
@@ -495,10 +475,10 @@ int lzo1x_decompress_safe(const unsigned
                 while (unlikely(*ip == 0)) {
                     t += 255;
                     ip++;
-                    NEED_IP(1, 0);
+                    NEED_IP(1);
                 }
                 t += 31 + *ip++;
-                NEED_IP(2, 0);
+                NEED_IP(2);
             }
             m_pos = op - 1;
             next = get_unaligned_le16(ip);
@@ -513,10 +493,10 @@ int lzo1x_decompress_safe(const unsigned
                 while (unlikely(*ip == 0)) {
                     t += 255;
                     ip++;
-                    NEED_IP(1, 0);
+                    NEED_IP(1);
                 }
                 t += 7 + *ip++;
-                NEED_IP(2, 0);
+                NEED_IP(2);
             }
             next = get_unaligned_le16(ip);
             ip += 2;
@@ -530,7 +510,7 @@ int lzo1x_decompress_safe(const unsigned
 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
         if (op - m_pos >= 8) {
             unsigned char *oe = op + t;
-            if (likely(HAVE_OP(t, 15))) {
+            if (likely(HAVE_OP(t + 15))) {
                 do {
                     COPY8(op, m_pos);
                     op += 8;
@@ -540,7 +520,7 @@ int lzo1x_decompress_safe(const unsigned
                     m_pos += 8;
                 } while (op < oe);
                 op = oe;
-                if (HAVE_IP(6, 0)) {
+                if (HAVE_IP(6)) {
                     state = next;
                     COPY4(op, ip);
                     op += next;
@@ -548,7 +528,7 @@ int lzo1x_decompress_safe(const unsigned
                     continue;
                 }
             } else {
-                NEED_OP(t, 0);
+                NEED_OP(t);
                 do {
                     *op++ = *m_pos++;
                 } while (op < oe);
@@ -557,7 +537,7 @@ int lzo1x_decompress_safe(const unsigned
 #endif
         {
             unsigned char *oe = op + t;
-            NEED_OP(t, 0);
+            NEED_OP(t);
             op[0] = m_pos[0];
             op[1] = m_pos[1];
             op += 2;
@@ -570,15 +550,15 @@ int lzo1x_decompress_safe(const unsigned
         state = next;
         t = next;
 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
-        if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) {
+        if (likely(HAVE_IP(6) && HAVE_OP(4))) {
             COPY4(op, ip);
             op += t;
             ip += t;
         } else
 #endif
         {
-            NEED_IP(t, 3);
-            NEED_OP(t, 0);
+            NEED_IP(t + 3);
+            NEED_OP(t);
             while (t > 0) {
                 *op++ = *ip++;
                 t--;

Revert "lzo: properly check for overruns"

This reverts commit 504f70b6 ("lzo: properly check for overruns").

As analysed by Willem Pinckaers, this fix is still incomplete on certain rare corner cases, and it is easier to restart from the original code.

[original Linux commit: af958a38]
xen/common/lzo.c +21 -41

144 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw

PATCH 2/2 lzo: check for length overrun in variable length encoding

In reply to Jan Beulich

This fix ensures that we never meet an integer overflow while adding 255 while parsing a variable length encoding. It works differently from commit 504f70b6 ("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.

[original Linux commit: 72cf9012]
xen/common/lzo.c +37 -6
@@ -381,6 +381,16 @@ int lzo1x_1_compress(const unsigned char
 #define NEED_OP(x)     if (!HAVE_OP(x)) goto output_overrun
 #define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun
 
+/* This 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. See Documentation/lzo.txt for more information.
+ */
+#define MAX_255_COUNT      ((((size_t)~0) / 255) - 2)
+
 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
                           unsigned char *out, size_t *out_len)
 {
@@ -411,12 +421,19 @@ int lzo1x_decompress_safe(const unsigned
         if (t < 16) {
             if (likely(state == 0)) {
                 if (unlikely(t == 0)) {
+                    size_t offset;
+                    const unsigned char *ip_last = ip;
+
                     while (unlikely(*ip == 0)) {
-                        t += 255;
                         ip++;
                         NEED_IP(1);
                     }
-                    t += 15 + *ip++;
+                    offset = ip - ip_last;
+                    if (unlikely(offset > MAX_255_COUNT))
+                        return LZO_E_ERROR;
+
+                    offset = (offset << 8) - offset;
+                    t += offset + 15 + *ip++;
                 }
                 t += 3;
  copy_literal_run:
@@ -472,12 +489,19 @@ int lzo1x_decompress_safe(const unsigned
         } else if (t >= 32) {
             t = (t & 31) + (3 - 1);
             if (unlikely(t == 2)) {
+                size_t offset;
+                const unsigned char *ip_last = ip;
+
                 while (unlikely(*ip == 0)) {
-                    t += 255;
                     ip++;
                     NEED_IP(1);
                 }
-                t += 31 + *ip++;
+                offset = ip - ip_last;
+                if (unlikely(offset > MAX_255_COUNT))
+                    return LZO_E_ERROR;
+
+                offset = (offset << 8) - offset;
+                t += offset + 31 + *ip++;
                 NEED_IP(2);
             }
             m_pos = op - 1;
@@ -490,12 +514,19 @@ int lzo1x_decompress_safe(const unsigned
             m_pos -= (t & 8) << 11;
             t = (t & 7) + (3 - 1);
             if (unlikely(t == 2)) {
+                size_t offset;
+                const unsigned char *ip_last = ip;
+
                 while (unlikely(*ip == 0)) {
-                    t += 255;
                     ip++;
                     NEED_IP(1);
                 }
-                t += 7 + *ip++;
+                offset = ip - ip_last;
+                if (unlikely(offset > MAX_255_COUNT))
+                    return LZO_E_ERROR;
+
+                offset = (offset << 8) - offset;
+                t += offset + 7 + *ip++;
                 NEED_IP(2);
             }
             next = get_unaligned_le16(ip);

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 504f70b6 ("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.

[original Linux commit: 72cf9012]
xen/common/lzo.c +37 -6

79 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by on the v1 series
#5 lore raw
In reply to Jan Beulich
Quoted 1 line · Jan Beulich

The original fix we inherited from Linux go re-done. Let's do so too.

Mainly on the basis that it was reviewed by Linux maintainers and then by you rather than because I've looked at it closely myself.

Ian.

Quoted 6 lines

1: Revert "lzo: properly check for overruns"
2: lzo: check for length overrun in variable length encoding

#3 lore raw

Re: [PATCH 2/2] lzo: check for length overrun in variable length encoding

In reply to Jan Beulich

Jan Beulich writes ("[PATCH 2/2] lzo: check for length overrun in variable length encoding"):

Quoted 6 lines

This fix ensures that we never meet an integer overflow while adding 255 while parsing a variable length encoding. It works differently from commit 504f70b6 ("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.

AFAICT this decompressor is exposed to untrusted guest kernel images.

Ian.

#4 lore raw

Re: [PATCH 2/2] lzo: check for length overrun in variable length encoding

Quoted 9 lines · Ian Jackson

Jan Beulich writes ("[PATCH 2/2] lzo: check for length overrun in variable length encoding"):

Quoted 6 lines

This fix ensures that we never meet an integer overflow while adding 255 while parsing a variable length encoding. It works differently from commit 504f70b6 ("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.

AFAICT this decompressor is exposed to untrusted guest kernel images.

Only in mini-os context, I think. AIUI dom0 hosted libxc uses liblzo2.

Not 100% sure of that, but tools/libxc/Makefile's handling of xc_dom_decompress_unsafe_lzo1x seems to confirm what I thought.

Aside from that, I presume you are trying to say that the description of the fix suggests the code would be vulnerable to untrusted input? It looks to me as if it is infact OK for untrusted input, but perhaps I've misunderstood what it is doing.

Ian.