crypto: lzo - Fix compression buffer overrun
PATCH 1/1 crypto: lzo - Fix compression buffer overrun
From: Herbert Xu <[email protected]>
[ Upstream commit cc47f07234f72cbd8e2c973cdbf2a6730660a463 ]
Unlike the decompression code, the compression code in LZO never checked for output overruns. It instead assumes that the caller always provides enough buffer space, disregarding the buffer length provided by the caller.
Add a safe compression interface that checks for the end of buffer before each write. Use the safe interface in crypto/lzo.
Signed-off-by Herbert Xu <[email protected]>
Reviewed-by David Sterba <[email protected]>
Signed-off-by Herbert Xu <[email protected]>
Signed-off-by Roman Smirnov <[email protected]>
Fixes 64c70b1cf43d ("Add LZO1X algorithm to the kernel")
Signed-off-by Viktoriya Danchenko <[email protected]>
---
Backport fix for CVE-2025-38068
--- crypto/lzo-rle.c | 2 +- crypto/lzo.c | 2 +- include/linux/lzo.h | 8 +++ lib/lzo/Makefile | 2 +- lib/lzo/lzo1x_compress.c | 101 +++++++++++++++++++++++++--------- lib/lzo/lzo1x_compress_safe.c | 18 ++++++ 6 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 lib/lzo/lzo1x_compress_safe.c
crypto/lzo-rle.c +1 -1
@@ -55,7 +55,7 @@ static int __lzorle_compress(const u8 *src, unsigned int slen,size_t tmp_len = *dlen;/* size_t(ulong) <-> uint on 64 bit */ int err; - err =lzorle1x_1_compress (src, slen, dst, &tmp_len, ctx); + err =lzorle1x_1_compress_safe (src, slen, dst, &tmp_len, ctx);if (err !=LZO_E_OK )return -EINVAL ;
crypto/lzo.c +1 -1
@@ -55,7 +55,7 @@ static int __lzo_compress(const u8 *src, unsigned int slen,size_t tmp_len = *dlen;/* size_t(ulong) <-> uint on 64 bit */ int err; - err =lzo1x_1_compress (src, slen, dst, &tmp_len, ctx); + err =lzo1x_1_compress_safe (src, slen, dst, &tmp_len, ctx);if (err !=LZO_E_OK )return -EINVAL ;
include/linux/lzo.h +8 -0
@@ -24,10 +24,18 @@int lzo1x_1_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); +/* Same as above but does not write more than dst_len to dst. */ +int lzo1x_1_compress_safe (const unsigned char *src,size_t src_len, +unsigned char *dst,size_t *dst_len,void *wrkmem); +/* This requires 'wrkmem' of size LZO1X_1_MEM_COMPRESS */ int lzorle1x_1_compress (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len,void *wrkmem); +/* Same as above but does not write more than dst_len to dst. */ +int lzorle1x_1_compress_safe (const unsigned char *src,size_t src_len, +unsigned char *dst,size_t *dst_len,void *wrkmem); +/* safe decompression with overrun testing */ int lzo1x_decompress_safe (const unsigned char *src,size_t src_len,unsigned char *dst,size_t *dst_len);
lib/lzo/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -lzo_compress-objs := lzo1x_compress.o +lzo_compress-objs := lzo1x_compress.o lzo1x_compress_safe.o lzo_decompress-objs := lzo1x_decompress_safe.o obj-$(CONFIG_LZO_COMPRESS) += lzo_compress.o
lib/lzo/lzo1x_compress.c +76 -25
242 lines not on this page · raw message
lib/lzo/lzo1x_compress_safe.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* LZO1X Compressor from LZO +* +* Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <[email protected]> +* +* The full LZO package can be found at: +* http://www.oberhumer.com/opensource/lzo/ +* +* Changed for Linux kernel use by: +* Nitin Gupta <[email protected]> +* Richard Purdie <[email protected]> +*/ + +#define LZO_SAFE (name) name##_safe +#define HAVE_OP (x) ((size_t)(op_end - op) >= (size_t)(x)) + +#include "lzo1x_compress.c"
Signature · 1 line
2.43.0