[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 4/7] xz: avoid overlapping memcpy() with invalid input with in-place decompression


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 6 Dec 2021 14:31:58 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dWQiEscHMitckPtyowVTpPJHsuAivRldTvGBRFOefis=; b=C4lYfd5u8+rfYb4FiJJApHqJFt84lnEdLzquapTlmZmG5u+anwYt8vpLogNOGwp0LHxuHC1coFytqjZ1Sd2/9Qi1s5b8S58F1sVR9NAf/cX9BA/H48lPlU/bbNtpXO3QF34dfhSPQxnsWU6lujkICWMrMjiCCwqHEFHOOOcz8F+3Pe/abeFQMTjriXkDeTUn3OAN2K9ExlSJpgcwV5rmMrAyTWArluGZjI1rgpv22TydE6OprDSqhmlk3HFJr9R0Mmo7Xcr+XDqsxqy9Cn49tyQCeg/9OPYq/EBfODmWC1NtqsJ/jWm4xc9ThDI4nyDLCGDypXQT0U7tfk/6jXuLdw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H+zX6MH6j8KzjVL8+V2jneBYcmEqAH55NfIQ1Z0VUdO8VWSOAwdaWXa47EtMCwRb294F8V1hjIJvGCvMvCRI5ZBCq4CEaqTRVwJFYl4canveTG3ByhkYnZB0zhXlL5ao/gGtiaxAq3NLRDHRKD50qQBXkD7Pq9FjTISYK0fP3gChrKr45kLHnF5B98n1hUMKZ6S1esmCZn+WcCwhWXnzoWJqLxuT86O1axsjz6etWIUh3AaGQrce02v4GzGfRATkHm9WLlhmAFWNIIZBG8jUBXmyfp35CjXoISZ7tiWVLalX85lLwAy4XvNmyvlEkZ/T5vDVp8Zv6EGOJJxDicQqsQ==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Mon, 06 Dec 2021 13:37:07 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

From: Lasse Collin <lasse.collin@xxxxxxxxxxx>

With valid files, the safety margin described in lib/decompress_unxz.c
ensures that these buffers cannot overlap. But if the uncompressed size
of the input is larger than the caller thought, which is possible when
the input file is invalid/corrupt, the buffers can overlap. Obviously
the result will then be garbage (and usually the decoder will return
an error too) but no other harm will happen when such an over-run occurs.

This change only affects uncompressed LZMA2 chunks and so this
should have no effect on performance.

Link: https://lore.kernel.org/r/20211010213145.17462-2-xiang@xxxxxxxxxx
Signed-off-by: Lasse Collin <lasse.collin@xxxxxxxxxxx>
Signed-off-by: Gao Xiang <hsiangkao@xxxxxxxxxxxxxxxxx>
[Linux commit: 83d3c4f22a36d005b55f44628f46cc0d319a75e8]
Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
Reviewed-by: Luca Fancellu <luca.fancellu@xxxxxxx>
---
v2: Retain one more S-o-b.

--- a/xen/common/unxz.c
+++ b/xen/common/unxz.c
@@ -127,7 +127,7 @@
  * memeq and memzero are not used much and any remotely sane implementation
  * is fast enough. memcpy/memmove speed matters in multi-call mode, but
  * the kernel image is decompressed in single-call mode, in which only
- * memcpy speed can matter and only if there is a lot of uncompressible data
+ * memmove speed can matter and only if there is a lot of uncompressible data
  * (LZMA2 stores uncompressible chunks in uncompressed form). Thus, the
  * functions below should just be kept small; it's probably not worth
  * optimizing for speed.
--- a/xen/common/xz/dec_lzma2.c
+++ b/xen/common/xz/dec_lzma2.c
@@ -387,7 +387,14 @@ static void __init dict_uncompressed(str
 
                *left -= copy_size;
 
-               memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
+               /*
+                * If doing in-place decompression in single-call mode and the
+                * uncompressed size of the file is larger than the caller
+                * thought (i.e. it is invalid input!), the buffers below may
+                * overlap and cause undefined behavior with memcpy().
+                * With valid inputs memcpy() would be fine here.
+                */
+               memmove(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
                dict->pos += copy_size;
 
                if (dict->full < dict->pos)
@@ -397,7 +404,11 @@ static void __init dict_uncompressed(str
                        if (dict->pos == dict->end)
                                dict->pos = 0;
 
-                       memcpy(b->out + b->out_pos, b->in + b->in_pos,
+                       /*
+                        * Like above but for multi-call mode: use memmove()
+                        * to avoid undefined behavior with invalid input.
+                        */
+                       memmove(b->out + b->out_pos, b->in + b->in_pos,
                                        copy_size);
                }
 
@@ -421,6 +432,12 @@ static uint32_t __init dict_flush(struct
                if (dict->pos == dict->end)
                        dict->pos = 0;
 
+               /*
+                * These buffers cannot overlap even if doing in-place
+                * decompression because in multi-call mode dict->buf
+                * has been allocated by us in this file; it's not
+                * provided by the caller like in single-call mode.
+                */
                memcpy(b->out + b->out_pos, dict->buf + dict->start,
                                copy_size);
        }




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.