From: "Jan Beulich" Subject: [PATCH 3/3] lzo: properly check for overruns Date: Wed, 02 Jul 2014 15:27:31 +0100 Message-ID: <53B432F3020000780001F9E2@mail.emea.novell.com> References: <53B42ADE020000780001F94F@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part5A68B0C3.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1X2LVX-0007PI-DX for xen-devel@lists.xenproject.org; Wed, 02 Jul 2014 14:27:35 +0000 In-Reply-To: <53B42ADE020000780001F94F@mail.emea.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Ian Campbell , Keir Fraser , Ian Jackson , Tim Deegan List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part5A68B0C3.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline From: Greg Kroah-Hartman The lzo decompressor can, if given some really crazy data, possibly overrun some variable types. Modify the checking logic to properly detect overruns before they happen. Reported-by: "Don A. Bailey" Signed-off-by: Greg Kroah-Hartman Original Linux commit: 206a81c18401c0cde6e579164f752c4b147324ce. This is CVE-2014-4607 (but not a security issue in Xen, since the code is only used for loading the Dom0 kernel and _inside_ an eventual DomU for loading its kernel). Signed-off-by: Jan Beulich --- a/xen/common/lzo.c +++ b/xen/common/lzo.c @@ -375,11 +375,31 @@ int lzo1x_1_compress(const unsigned char * Richard Purdie */ =20 -#define HAVE_IP(x) ((size_t)(ip_end - ip) >=3D (size_t)(x)) -#define HAVE_OP(x) ((size_t)(op_end - op) >=3D (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 +#define HAVE_IP(t, x) \ + (((size_t)(ip_end - ip) >=3D (size_t)(t + x)) && \ + (((t + x) >=3D t) && ((t + x) >=3D x))) + +#define HAVE_OP(t, x) \ + (((size_t)(op_end - op) >=3D (size_t)(t + x)) && \ + (((t + x) >=3D t) && ((t + x) >=3D 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) =20 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len, unsigned char *out, size_t *out_len) @@ -414,14 +434,14 @@ int lzo1x_decompress_safe(const unsigned while (unlikely(*ip =3D=3D 0)) { t +=3D 255; ip++; - NEED_IP(1); + NEED_IP(1, 0); } t +=3D 15 + *ip++; } t +=3D 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 =3D ip + t; unsigned char *oe =3D op + t; do { @@ -437,8 +457,8 @@ int lzo1x_decompress_safe(const unsigned } else #endif { - NEED_OP(t); - NEED_IP(t + 3); + NEED_OP(t, 0); + NEED_IP(t, 3); do { *op++ =3D *ip++; } while (--t > 0); @@ -451,7 +471,7 @@ int lzo1x_decompress_safe(const unsigned m_pos -=3D t >> 2; m_pos -=3D *ip++ << 2; TEST_LB(m_pos); - NEED_OP(2); + NEED_OP(2, 0); op[0] =3D m_pos[0]; op[1] =3D m_pos[1]; op +=3D 2; @@ -475,10 +495,10 @@ int lzo1x_decompress_safe(const unsigned while (unlikely(*ip =3D=3D 0)) { t +=3D 255; ip++; - NEED_IP(1); + NEED_IP(1, 0); } t +=3D 31 + *ip++; - NEED_IP(2); + NEED_IP(2, 0); } m_pos =3D op - 1; next =3D get_unaligned_le16(ip); @@ -493,10 +513,10 @@ int lzo1x_decompress_safe(const unsigned while (unlikely(*ip =3D=3D 0)) { t +=3D 255; ip++; - NEED_IP(1); + NEED_IP(1, 0); } t +=3D 7 + *ip++; - NEED_IP(2); + NEED_IP(2, 0); } next =3D get_unaligned_le16(ip); ip +=3D 2; @@ -510,7 +530,7 @@ int lzo1x_decompress_safe(const unsigned #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) if (op - m_pos >=3D 8) { unsigned char *oe =3D op + t; - if (likely(HAVE_OP(t + 15))) { + if (likely(HAVE_OP(t, 15))) { do { COPY8(op, m_pos); op +=3D 8; @@ -520,7 +540,7 @@ int lzo1x_decompress_safe(const unsigned m_pos +=3D 8; } while (op < oe); op =3D oe; - if (HAVE_IP(6)) { + if (HAVE_IP(6, 0)) { state =3D next; COPY4(op, ip); op +=3D next; @@ -528,7 +548,7 @@ int lzo1x_decompress_safe(const unsigned continue; } } else { - NEED_OP(t); + NEED_OP(t, 0); do { *op++ =3D *m_pos++; } while (op < oe); @@ -537,7 +557,7 @@ int lzo1x_decompress_safe(const unsigned #endif { unsigned char *oe =3D op + t; - NEED_OP(t); + NEED_OP(t, 0); op[0] =3D m_pos[0]; op[1] =3D m_pos[1]; op +=3D 2; @@ -550,15 +570,15 @@ int lzo1x_decompress_safe(const unsigned state =3D next; t =3D next; #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) - if (likely(HAVE_IP(6) && HAVE_OP(4))) { + if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) { COPY4(op, ip); op +=3D t; ip +=3D t; } else #endif { - NEED_IP(t + 3); - NEED_OP(t); + NEED_IP(t, 3); + NEED_OP(t, 0); while (t > 0) { *op++ =3D *ip++; t--; --=__Part5A68B0C3.1__= Content-Type: text/plain; name="lzo-check-overruns.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="lzo-check-overruns.patch" lzo: properly check for overruns=0A=0AThe lzo decompressor can, if given = some really crazy data, possibly=0Aoverrun some variable types. Modify = the checking logic to properly=0Adetect overruns before they happen.=0A=0AR= eported-by: "Don A. Bailey" =0ASigned-off-by: Greg = Kroah-Hartman =0A=0AOriginal Linux commit: = 206a81c18401c0cde6e579164f752c4b147324ce.=0A=0AThis is CVE-2014-4607 (but = not a security issue in Xen, since the code=0Ais only used for loading the = Dom0 kernel and _inside_ an eventual DomU=0Afor loading its kernel).=0A=0AS= igned-off-by: Jan Beulich =0A=0A--- a/xen/common/lzo.c= =0A+++ b/xen/common/lzo.c=0A@@ -375,11 +375,31 @@ int lzo1x_1_compress(cons= t unsigned char=0A * Richard Purdie =0A */=0A = =0A-#define HAVE_IP(x) ((size_t)(ip_end - ip) >=3D (size_t)(x))=0A-#def= ine HAVE_OP(x) ((size_t)(op_end - op) >=3D (size_t)(x))=0A-#define = NEED_IP(x) if (!HAVE_IP(x)) goto input_overrun=0A-#define NEED_OP(x) = if (!HAVE_OP(x)) goto output_overrun=0A-#define TEST_LB(m_pos) if = ((m_pos) < out) goto lookbehind_overrun=0A+#define HAVE_IP(t, x) = \=0A+ (((size_t)(ip_end - ip) >=3D (size_t)(t + x)) = && \=0A+ (((t + x) >=3D t) && ((t + x) >=3D x)))=0A+=0A+#define = HAVE_OP(t, x) \=0A+ (((size_t)(op_end - = op) >=3D (size_t)(t + x)) && \=0A+ (((t + x) >=3D t) && ((t + x) >=3D = x)))=0A+=0A+#define NEED_IP(t, x) \=0A+ do { = \=0A+ if (!HAVE_IP(t, x)) \=0A+ = goto input_overrun; \=0A+ } while (0)=0A+=0A+#define NEED_OP(t, x) = \=0A+ do { \=0A+ if = (!HAVE_OP(t, x)) \=0A+ goto output_overrun; \=0A+ = } while (0)=0A+=0A+#define TEST_LB(m_pos) \=0A+ do { = \=0A+ if ((m_pos) < out) \=0A+ = goto lookbehind_overrun; \=0A+ } while (0)=0A =0A int = lzo1x_decompress_safe(const unsigned char *in, size_t in_len,=0A = unsigned char *out, size_t *out_len)=0A@@ -414,14 +434,14 = @@ int lzo1x_decompress_safe(const unsigned=0A while = (unlikely(*ip =3D=3D 0)) {=0A t +=3D 255;=0A = ip++;=0A- NEED_IP(1);=0A+ = NEED_IP(1, 0);=0A }=0A = t +=3D 15 + *ip++;=0A }=0A t +=3D 3;=0A = copy_literal_run:=0A #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)= =0A- if (likely(HAVE_IP(t + 15) && HAVE_OP(t + 15))) {=0A+ = if (likely(HAVE_IP(t, 15) && HAVE_OP(t, 15))) {=0A = const unsigned char *ie =3D ip + t;=0A = unsigned char *oe =3D op + t;=0A do {=0A@@ -437,8 = +457,8 @@ int lzo1x_decompress_safe(const unsigned=0A } = else=0A #endif=0A {=0A- NEED_OP(t);=0A- = NEED_IP(t + 3);=0A+ NEED_OP(t, = 0);=0A+ NEED_IP(t, 3);=0A do {=0A = *op++ =3D *ip++;=0A } while (--t = > 0);=0A@@ -451,7 +471,7 @@ int lzo1x_decompress_safe(const unsigned=0A = m_pos -=3D t >> 2;=0A m_pos -=3D *ip++ << = 2;=0A TEST_LB(m_pos);=0A- NEED_OP(2);=0A+ = NEED_OP(2, 0);=0A op[0] =3D m_pos[0];=0A = op[1] =3D m_pos[1];=0A op +=3D 2;=0A@@ -475,10 = +495,10 @@ int lzo1x_decompress_safe(const unsigned=0A = while (unlikely(*ip =3D=3D 0)) {=0A t +=3D 255;=0A = ip++;=0A- NEED_IP(1);=0A+ = NEED_IP(1, 0);=0A }=0A t +=3D 31 + = *ip++;=0A- NEED_IP(2);=0A+ NEED_IP(2, 0);=0A = }=0A m_pos =3D op - 1;=0A next =3D = get_unaligned_le16(ip);=0A@@ -493,10 +513,10 @@ int lzo1x_decompress_safe(c= onst unsigned=0A while (unlikely(*ip =3D=3D 0)) {=0A = t +=3D 255;=0A ip++;=0A- = NEED_IP(1);=0A+ NEED_IP(1, 0);=0A = }=0A t +=3D 7 + *ip++;=0A- NEED_IP(2);=0A+ = NEED_IP(2, 0);=0A }=0A next =3D = get_unaligned_le16(ip);=0A ip +=3D 2;=0A@@ -510,7 +530,7 @@ = int lzo1x_decompress_safe(const unsigned=0A #if defined(CONFIG_HAVE_EFFICIE= NT_UNALIGNED_ACCESS)=0A if (op - m_pos >=3D 8) {=0A = unsigned char *oe =3D op + t;=0A- if (likely(HAVE_OP(t + 15))) = {=0A+ if (likely(HAVE_OP(t, 15))) {=0A do {=0A = COPY8(op, m_pos);=0A op +=3D = 8;=0A@@ -520,7 +540,7 @@ int lzo1x_decompress_safe(const unsigned=0A = m_pos +=3D 8;=0A } while (op < oe);=0A = op =3D oe;=0A- if (HAVE_IP(6)) {=0A+ = if (HAVE_IP(6, 0)) {=0A state =3D next;=0A = COPY4(op, ip);=0A op +=3D next;=0A@@ -528,7 = +548,7 @@ int lzo1x_decompress_safe(const unsigned=0A = continue;=0A }=0A } else {=0A- = NEED_OP(t);=0A+ NEED_OP(t, 0);=0A do {=0A = *op++ =3D *m_pos++;=0A } while (op < = oe);=0A@@ -537,7 +557,7 @@ int lzo1x_decompress_safe(const unsigned=0A = #endif=0A {=0A unsigned char *oe =3D op + t;=0A- = NEED_OP(t);=0A+ NEED_OP(t, 0);=0A op[0] =3D = m_pos[0];=0A op[1] =3D m_pos[1];=0A op +=3D = 2;=0A@@ -550,15 +570,15 @@ int lzo1x_decompress_safe(const unsigned=0A = state =3D next;=0A t =3D next;=0A #if defined(CONFIG_HAVE_EFFIC= IENT_UNALIGNED_ACCESS)=0A- if (likely(HAVE_IP(6) && HAVE_OP(4))) = {=0A+ if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) {=0A = COPY4(op, ip);=0A op +=3D t;=0A ip +=3D t;=0A = } else=0A #endif=0A {=0A- NEED_IP(t + 3);=0A- = NEED_OP(t);=0A+ NEED_IP(t, 3);=0A+ NEED_OP(t, = 0);=0A while (t > 0) {=0A *op++ =3D *ip++;=0A = t--;=0A --=__Part5A68B0C3.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part5A68B0C3.1__=--