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

[PATCH 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: Fri, 19 Nov 2021 11:22:17 +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=Poc0wRx/mUi3cWDnszzqwQVxElnm2r9mLP8h58FAsVw=; b=AbhQdo828vE7c2lcOyuV5VoCvRqcc6BxTVpLOLKCRV/B9kYqi9Lzzvo2GDnFNPXxRQuILBubZrUVrStRGow/A2xTN/VhdqAdX58HPwps++UxIq9yWPWjHDNpEVx1sozXUpjxJ05TlCIvaNEWbWiAlEAd182wSsaLR4llSR4802P1yyXukA5+QMkvTmp2CJQc/gMHX0AfbOxnEGpabvkS10R3GBofmHb+4fYdgEJ/x8cugukPdvZGK6uqroWg4Q554gvAEZoUuc5OlJ2I3EyMXopq3r0TWzGsLKw+haqF8m6BLIqDw4XxnSf5lsKny7xYQ9xRk/eZfOagONYoB8cqpQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EUpd8WeQi5qeK1VJ/CD9bcv14ONyU7aU0qCno6b3zKzpYnsS7VerEDG47GbEBjFiKcqv9qjaqqNBxabZPiAlgDmvFzOmMXcsWRyKgSlhV0PtDoYWC/R5h6Pdgij2O5AyzMLX3k8xz41LX2+aaAwtw2Du5RTS0q5ehSmN7mxMFN/4vCliERfAeyy/lnfsGdVZPti5e+uox8cOBLaNR3cGVb+qKn/ya17HtZVzAT0SVHU971Ss1m6k01qUsRyDTgHlxlrY9XOQp8gIdVv3fT0gYs5bsJLjQUeFc1ynOl60vVHVYimOUmEASl9KOTrLq611H35AZroYLMQUvUUnReMA6A==
  • 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: Fri, 19 Nov 2021 10:22:24 +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>
[Linux commit: 83d3c4f22a36d005b55f44628f46cc0d319a75e8]
Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>

--- 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®.