From: "Jan Beulich" Subject: [PATCH 2/2] lzo: check for length overrun in variable length encoding Date: Mon, 03 Nov 2014 15:02:32 +0000 Message-ID: <5457A71802000078000447AA@mail.emea.novell.com> References: <5457A3AD020000780004478B@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartEADFE418.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XlJ9Q-0003sR-Ou for xen-devel@lists.xenproject.org; Mon, 03 Nov 2014 15:02:36 +0000 In-Reply-To: <5457A3AD020000780004478B@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. --=__PartEADFE418.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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. Reported-by: Willem Pinckaers Signed-off-by: Willy Tarreau [original Linux commit: 72cf9012] Signed-off-by: Jan Beulich --- a/xen/common/lzo.c +++ b/xen/common/lzo.c @@ -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 =20 +/* 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 =3D=3D 0)) { if (unlikely(t =3D=3D 0)) { + size_t offset; + const unsigned char *ip_last =3D ip; + while (unlikely(*ip =3D=3D 0)) { - t +=3D 255; ip++; NEED_IP(1); } - t +=3D 15 + *ip++; + offset =3D ip - ip_last; + if (unlikely(offset > MAX_255_COUNT)) + return LZO_E_ERROR; + + offset =3D (offset << 8) - offset; + t +=3D offset + 15 + *ip++; } t +=3D 3; copy_literal_run: @@ -472,12 +489,19 @@ int lzo1x_decompress_safe(const unsigned } else if (t >=3D 32) { t =3D (t & 31) + (3 - 1); if (unlikely(t =3D=3D 2)) { + size_t offset; + const unsigned char *ip_last =3D ip; + while (unlikely(*ip =3D=3D 0)) { - t +=3D 255; ip++; NEED_IP(1); } - t +=3D 31 + *ip++; + offset =3D ip - ip_last; + if (unlikely(offset > MAX_255_COUNT)) + return LZO_E_ERROR; + + offset =3D (offset << 8) - offset; + t +=3D offset + 31 + *ip++; NEED_IP(2); } m_pos =3D op - 1; @@ -490,12 +514,19 @@ int lzo1x_decompress_safe(const unsigned m_pos -=3D (t & 8) << 11; t =3D (t & 7) + (3 - 1); if (unlikely(t =3D=3D 2)) { + size_t offset; + const unsigned char *ip_last =3D ip; + while (unlikely(*ip =3D=3D 0)) { - t +=3D 255; ip++; NEED_IP(1); } - t +=3D 7 + *ip++; + offset =3D ip - ip_last; + if (unlikely(offset > MAX_255_COUNT)) + return LZO_E_ERROR; + + offset =3D (offset << 8) - offset; + t +=3D offset + 7 + *ip++; NEED_IP(2); } next =3D get_unaligned_le16(ip); --=__PartEADFE418.1__= Content-Type: text/plain; name="lzo-check-length-overrun.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="lzo-check-length-overrun.patch" lzo: check for length overrun in variable length encoding=0A=0AThis fix = ensures that we never meet an integer overflow while adding=0A255 while = parsing a variable length encoding. It works differently from=0Acommit = 504f70b6 ("lzo: properly check for overruns") because instead of=0Aensuring= that we don't overrun the input, which is tricky to guarantee=0Adue to = many assumptions in the code, it simply checks that the cumulated=0Anumber = of 255 read cannot overflow by bounding this number.=0A=0AThe MAX_255_COUNT= is the maximum number of times we can add 255 to a base=0Acount without = overflowing an integer. The multiply will overflow when=0Amultiplying 255 = by more than MAXINT/255. The sum will overflow earlier=0Adepending on the = base count. Since the base count is taken from a u8=0Aand a few bits, it = is safe to assume that it will always be lower than=0Aor equal to 2*255, = thus we can always prevent any overflow by accepting=0Atwo less 255 = steps.=0A=0AThis patch also reduces the CPU overhead and actually = increases performance=0Aby 1.1% compared to the initial code, while the = previous fix costs 3.1%=0A(measured on x86_64).=0A=0AThe fix needs to be = backported to all currently supported stable kernels.=0A=0AReported-by: = Willem Pinckaers =0ASigned-off-by: Willy Tarreau = =0A[original Linux commit: 72cf9012]=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/common/lzo.c=0A+++ b/xen/common/= lzo.c=0A@@ -381,6 +381,16 @@ int lzo1x_1_compress(const unsigned char=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 =0A+/* This = MAX_255_COUNT is the maximum number of times we can add 255 to a base=0A+ = * count without overflowing an integer. The multiply will overflow = when=0A+ * multiplying 255 by more than MAXINT/255. The sum will overflow = earlier=0A+ * depending on the base count. Since the base count is taken = from a u8=0A+ * and a few bits, it is safe to assume that it will always = be lower than=0A+ * or equal to 2*255, thus we can always prevent any = overflow by accepting=0A+ * two less 255 steps. See Documentation/lzo.txt = for more information.=0A+ */=0A+#define MAX_255_COUNT ((((size_t)~0) = / 255) - 2)=0A+=0A int lzo1x_decompress_safe(const unsigned char *in, = size_t in_len,=0A unsigned char *out, size_t = *out_len)=0A {=0A@@ -411,12 +421,19 @@ int lzo1x_decompress_safe(const = unsigned=0A if (t < 16) {=0A if (likely(state =3D=3D = 0)) {=0A if (unlikely(t =3D=3D 0)) {=0A+ = size_t offset;=0A+ const unsigned char *ip_last =3D = ip;=0A+=0A while (unlikely(*ip =3D=3D 0)) {=0A- = t +=3D 255;=0A ip++;=0A = NEED_IP(1);=0A }=0A- t = +=3D 15 + *ip++;=0A+ offset =3D ip - ip_last;=0A+ = if (unlikely(offset > MAX_255_COUNT))=0A+ = return LZO_E_ERROR;=0A+=0A+ offset =3D (offset << 8) = - offset;=0A+ t +=3D offset + 15 + *ip++;=0A = }=0A t +=3D 3;=0A copy_literal_run:=0A@@ -472,12 = +489,19 @@ int lzo1x_decompress_safe(const unsigned=0A } else if = (t >=3D 32) {=0A t =3D (t & 31) + (3 - 1);=0A if = (unlikely(t =3D=3D 2)) {=0A+ size_t offset;=0A+ = const unsigned char *ip_last =3D ip;=0A+=0A while = (unlikely(*ip =3D=3D 0)) {=0A- t +=3D 255;=0A = ip++;=0A NEED_IP(1);=0A = }=0A- t +=3D 31 + *ip++;=0A+ offset =3D ip - = ip_last;=0A+ if (unlikely(offset > MAX_255_COUNT))=0A+ = return LZO_E_ERROR;=0A+=0A+ offset =3D = (offset << 8) - offset;=0A+ t +=3D offset + 31 + *ip++;=0A = NEED_IP(2);=0A }=0A m_pos =3D op - = 1;=0A@@ -490,12 +514,19 @@ int lzo1x_decompress_safe(const unsigned=0A = m_pos -=3D (t & 8) << 11;=0A t =3D (t & 7) + (3 - = 1);=0A if (unlikely(t =3D=3D 2)) {=0A+ size_t = offset;=0A+ const unsigned char *ip_last =3D ip;=0A+=0A = while (unlikely(*ip =3D=3D 0)) {=0A- t +=3D = 255;=0A ip++;=0A NEED_IP(1);=0A = }=0A- t +=3D 7 + *ip++;=0A+ = offset =3D ip - ip_last;=0A+ if (unlikely(offset > = MAX_255_COUNT))=0A+ return LZO_E_ERROR;=0A+=0A+ = offset =3D (offset << 8) - offset;=0A+ t +=3D offset = + 7 + *ip++;=0A NEED_IP(2);=0A }=0A = next =3D get_unaligned_le16(ip);=0A --=__PartEADFE418.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 --=__PartEADFE418.1__=--