diff mbox series

[v2,1/2] lib: decompress_unzstd: Limit output size

Message ID 20200825210127.17850-1-paul@crapouillou.net (mailing list archive)
State Superseded
Headers show
Series [v2,1/2] lib: decompress_unzstd: Limit output size | expand

Commit Message

Paul Cercueil Aug. 25, 2020, 9:01 p.m. UTC
The zstd decompression code, as it is right now, will have internal
values overflow on 32-bit systems when the output size is bigger than
1 GiB.

Until someone smarter than me can figure out how to fix the zstd code
properly, limit the destination buffer size to 1 GiB, which should be
enough for everybody, in order to make it usable on 32-bit systems.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Reviewed-by: Nick Terrell <terrelln@fb.com>
---

Notes:
    v2: Change limit to 1 GiB

 lib/decompress_unzstd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Nick Terrell Aug. 25, 2020, 10:51 p.m. UTC | #1
> On Aug 25, 2020, at 2:01 PM, Paul Cercueil <paul@crapouillou.net> wrote:
> 
> The zstd decompression code, as it is right now, will have internal
> values overflow on 32-bit systems when the output size is bigger than
> 1 GiB.
> 
> Until someone smarter than me can figure out how to fix the zstd code
> properly, limit the destination buffer size to 1 GiB, which should be
> enough for everybody, in order to make it usable on 32-bit systems.

I was talking with Yann Collet, and we believe that it isn’t the long that
is overflowing, but the pointers. Zstd expects to be given a valid output
size. It generally uses a begin/end pointer with its output buffer. So when
it is given a very large output size in 32-bit mode the end pointer will
overflow the pointer either causing UB, or end pointer < begin pointer,
which breaks zstd.

Zstd will probably never be able to work properly in this way. A better
solution might be to pass MAX_ADDRESS_PTR - OUTPUT_PTR as
the size to the __decompress() call. Or some other size that won’t
overflow the pointer.

Best,
Nick

> Signed-off-by: Paul Cercueil <paul@crapouillou.net>
> Reviewed-by: Nick Terrell <terrelln@fb.com>
> ---
> 
> Notes:
>    v2: Change limit to 1 GiB
> 
> lib/decompress_unzstd.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
> index 0ad2c15479ed..414517baedb0 100644
> --- a/lib/decompress_unzstd.c
> +++ b/lib/decompress_unzstd.c
> @@ -77,6 +77,7 @@
> 
> #include <linux/decompress/mm.h>
> #include <linux/kernel.h>
> +#include <linux/sizes.h>
> #include <linux/zstd.h>
> 
> /* 128MB is the maximum window size supported by zstd. */
> @@ -179,7 +180,7 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
> 	size_t ret;
> 
> 	if (out_len == 0)
> -		out_len = LONG_MAX; /* no limit */
> +		out_len = SZ_1G; /* should be big enough, right? */
> 
> 	if (fill == NULL && flush == NULL)
> 		/*
> -- 
> 2.28.0
>
diff mbox series

Patch

diff --git a/lib/decompress_unzstd.c b/lib/decompress_unzstd.c
index 0ad2c15479ed..414517baedb0 100644
--- a/lib/decompress_unzstd.c
+++ b/lib/decompress_unzstd.c
@@ -77,6 +77,7 @@ 
 
 #include <linux/decompress/mm.h>
 #include <linux/kernel.h>
+#include <linux/sizes.h>
 #include <linux/zstd.h>
 
 /* 128MB is the maximum window size supported by zstd. */
@@ -179,7 +180,7 @@  static int INIT __unzstd(unsigned char *in_buf, long in_len,
 	size_t ret;
 
 	if (out_len == 0)
-		out_len = LONG_MAX; /* no limit */
+		out_len = SZ_1G; /* should be big enough, right? */
 
 	if (fill == NULL && flush == NULL)
 		/*