2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2013-07-09 03:01:46 +04:00
/*
* Wrapper for decompressing LZ4 - compressed kernel , initramfs , and initrd
*
* Copyright ( C ) 2013 , LG Electronics , Kyungsik Lee < kyungsik . lee @ lge . com >
*/
# ifdef STATIC
# define PREBOOT
# include "lz4/lz4_decompress.c"
# else
# include <linux/decompress/unlz4.h>
# endif
# include <linux/types.h>
# include <linux/lz4.h>
# include <linux/decompress/mm.h>
# include <linux/compiler.h>
# include <asm/unaligned.h>
/*
* Note : Uncompressed chunk size is used in the compressor side
* ( userspace side for compression ) .
* It is hardcoded because there is not proper way to extract it
* from the binary stream which is generated by the preliminary
* version of LZ4 tool so far .
*/
# define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
# define ARCHIVE_MAGICNUMBER 0x184C2102
initramfs: support initramfs that is bigger than 2GiB
Now with 64bit bzImage and kexec tools, we support ramdisk that size is
bigger than 2g, as we could put it above 4G.
Found compressed initramfs image could not be decompressed properly. It
turns out that image length is int during decompress detection, and it
will become < 0 when length is more than 2G. Furthermore, during
decompressing len as int is used for inbuf count, that has problem too.
Change len to long, that should be ok as on 32 bit platform long is
32bits.
Tested with following compressed initramfs image as root with kexec.
gzip, bzip2, xz, lzma, lzop, lz4.
run time for populate_rootfs():
size name Nehalem-EX Westmere-EX Ivybridge-EX
9034400256 root_img : 26s 24s 30s
3561095057 root_img.lz4 : 28s 27s 27s
3459554629 root_img.lzo : 29s 29s 28s
3219399480 root_img.gz : 64s 62s 49s
2251594592 root_img.xz : 262s 260s 183s
2226366598 root_img.lzma: 386s 376s 277s
2901482513 root_img.bz2 : 635s 599s
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Rashika Kheria <rashika.kheria@gmail.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: P J P <ppandit@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: "Daniel M. Weeks" <dan@danweeks.net>
Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: Jan Beulich <JBeulich@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-09 01:23:14 +04:00
STATIC inline int INIT unlz4 ( u8 * input , long in_len ,
long ( * fill ) ( void * , unsigned long ) ,
long ( * flush ) ( void * , unsigned long ) ,
u8 * output , long * posp ,
2013-07-09 03:01:46 +04:00
void ( * error ) ( char * x ) )
{
int ret = - 1 ;
size_t chunksize = 0 ;
size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE ;
u8 * inp ;
u8 * inp_start ;
u8 * outp ;
initramfs: support initramfs that is bigger than 2GiB
Now with 64bit bzImage and kexec tools, we support ramdisk that size is
bigger than 2g, as we could put it above 4G.
Found compressed initramfs image could not be decompressed properly. It
turns out that image length is int during decompress detection, and it
will become < 0 when length is more than 2G. Furthermore, during
decompressing len as int is used for inbuf count, that has problem too.
Change len to long, that should be ok as on 32 bit platform long is
32bits.
Tested with following compressed initramfs image as root with kexec.
gzip, bzip2, xz, lzma, lzop, lz4.
run time for populate_rootfs():
size name Nehalem-EX Westmere-EX Ivybridge-EX
9034400256 root_img : 26s 24s 30s
3561095057 root_img.lz4 : 28s 27s 27s
3459554629 root_img.lzo : 29s 29s 28s
3219399480 root_img.gz : 64s 62s 49s
2251594592 root_img.xz : 262s 260s 183s
2226366598 root_img.lzma: 386s 376s 277s
2901482513 root_img.bz2 : 635s 599s
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Rashika Kheria <rashika.kheria@gmail.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: P J P <ppandit@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: "Daniel M. Weeks" <dan@danweeks.net>
Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: Jan Beulich <JBeulich@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-09 01:23:14 +04:00
long size = in_len ;
2013-07-09 03:01:46 +04:00
# ifdef PREBOOT
size_t out_len = get_unaligned_le32 ( input + in_len ) ;
# endif
size_t dest_len ;
if ( output ) {
outp = output ;
} else if ( ! flush ) {
error ( " NULL output pointer and no flush function provided " ) ;
goto exit_0 ;
} else {
outp = large_malloc ( uncomp_chunksize ) ;
if ( ! outp ) {
error ( " Could not allocate output buffer " ) ;
goto exit_0 ;
}
}
if ( input & & fill ) {
error ( " Both input pointer and fill function provided, " ) ;
goto exit_1 ;
} else if ( input ) {
inp = input ;
} else if ( ! fill ) {
error ( " NULL input pointer and missing fill function " ) ;
goto exit_1 ;
} else {
2017-02-25 02:01:16 +03:00
inp = large_malloc ( LZ4_compressBound ( uncomp_chunksize ) ) ;
2013-07-09 03:01:46 +04:00
if ( ! inp ) {
error ( " Could not allocate input buffer " ) ;
goto exit_1 ;
}
}
inp_start = inp ;
if ( posp )
* posp = 0 ;
2014-08-09 01:23:10 +04:00
if ( fill ) {
size = fill ( inp , 4 ) ;
if ( size < 4 ) {
error ( " data corrupted " ) ;
goto exit_2 ;
}
}
2013-07-09 03:01:46 +04:00
chunksize = get_unaligned_le32 ( inp ) ;
if ( chunksize = = ARCHIVE_MAGICNUMBER ) {
2014-08-09 01:23:10 +04:00
if ( ! fill ) {
inp + = 4 ;
size - = 4 ;
}
2013-07-09 03:01:46 +04:00
} else {
error ( " invalid header " ) ;
goto exit_2 ;
}
if ( posp )
* posp + = 4 ;
for ( ; ; ) {
2014-08-09 01:23:10 +04:00
if ( fill ) {
size = fill ( inp , 4 ) ;
if ( size = = 0 )
break ;
if ( size < 4 ) {
error ( " data corrupted " ) ;
goto exit_2 ;
}
lib/decompress_unlz4.c: correctly handle zero-padding around initrds.
lz4 compatible decompressor is simple. The format is underspecified and
relies on EOF notification to determine when to stop. Initramfs buffer
format[1] explicitly states that it can have arbitrary number of zero
padding. Thus when operating without a fill function, be extra careful to
ensure that sizes less than 4, or apperantly empty chunksizes are treated
as EOF.
To test this I have created two cpio initrds, first a normal one,
main.cpio. And second one with just a single /test-file with content
"second" second.cpio. Then i compressed both of them with gzip, and with
lz4 -l. Then I created a padding of 4 bytes (dd if=/dev/zero of=pad4 bs=1
count=4). To create four testcase initrds:
1) main.cpio.gzip + extra.cpio.gzip = pad0.gzip
2) main.cpio.lz4 + extra.cpio.lz4 = pad0.lz4
3) main.cpio.gzip + pad4 + extra.cpio.gzip = pad4.gzip
4) main.cpio.lz4 + pad4 + extra.cpio.lz4 = pad4.lz4
The pad4 test-cases replicate the initrd load by grub, as it pads and
aligns every initrd it loads.
All of the above boot, however /test-file was not accessible in the initrd
for the testcase #4, as decoding in lz4 decompressor failed. Also an
error message printed which usually is harmless.
Whith a patched kernel, all of the above testcases now pass, and
/test-file is accessible.
This fixes lz4 initrd decompress warning on every boot with grub. And
more importantly this fixes inability to load multiple lz4 compressed
initrds with grub. This patch has been shipping in Ubuntu kernels since
January 2021.
[1] ./Documentation/driver-api/early-userspace/buffer-format.rst
BugLink: https://bugs.launchpad.net/bugs/1835660
Link: https://lore.kernel.org/lkml/20210114200256.196589-1-xnox@ubuntu.com/ # v0
Link: https://lkml.kernel.org/r/20210513104831.432975-1-dimitri.ledkov@canonical.com
Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: Bongkyu Kim <bongkyu.kim@lge.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
Cc: Rajat Asthana <thisisrast7@gmail.com>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Gao Xiang <hsiangkao@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-07-01 04:56:16 +03:00
} else if ( size < 4 ) {
/* empty or end-of-file */
goto exit_3 ;
2014-08-09 01:23:10 +04:00
}
2013-07-09 03:01:46 +04:00
chunksize = get_unaligned_le32 ( inp ) ;
if ( chunksize = = ARCHIVE_MAGICNUMBER ) {
2014-08-09 01:23:10 +04:00
if ( ! fill ) {
inp + = 4 ;
size - = 4 ;
}
2013-07-09 03:01:46 +04:00
if ( posp )
* posp + = 4 ;
continue ;
}
2014-08-09 01:23:10 +04:00
lib/decompress_unlz4.c: correctly handle zero-padding around initrds.
lz4 compatible decompressor is simple. The format is underspecified and
relies on EOF notification to determine when to stop. Initramfs buffer
format[1] explicitly states that it can have arbitrary number of zero
padding. Thus when operating without a fill function, be extra careful to
ensure that sizes less than 4, or apperantly empty chunksizes are treated
as EOF.
To test this I have created two cpio initrds, first a normal one,
main.cpio. And second one with just a single /test-file with content
"second" second.cpio. Then i compressed both of them with gzip, and with
lz4 -l. Then I created a padding of 4 bytes (dd if=/dev/zero of=pad4 bs=1
count=4). To create four testcase initrds:
1) main.cpio.gzip + extra.cpio.gzip = pad0.gzip
2) main.cpio.lz4 + extra.cpio.lz4 = pad0.lz4
3) main.cpio.gzip + pad4 + extra.cpio.gzip = pad4.gzip
4) main.cpio.lz4 + pad4 + extra.cpio.lz4 = pad4.lz4
The pad4 test-cases replicate the initrd load by grub, as it pads and
aligns every initrd it loads.
All of the above boot, however /test-file was not accessible in the initrd
for the testcase #4, as decoding in lz4 decompressor failed. Also an
error message printed which usually is harmless.
Whith a patched kernel, all of the above testcases now pass, and
/test-file is accessible.
This fixes lz4 initrd decompress warning on every boot with grub. And
more importantly this fixes inability to load multiple lz4 compressed
initrds with grub. This patch has been shipping in Ubuntu kernels since
January 2021.
[1] ./Documentation/driver-api/early-userspace/buffer-format.rst
BugLink: https://bugs.launchpad.net/bugs/1835660
Link: https://lore.kernel.org/lkml/20210114200256.196589-1-xnox@ubuntu.com/ # v0
Link: https://lkml.kernel.org/r/20210513104831.432975-1-dimitri.ledkov@canonical.com
Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: Bongkyu Kim <bongkyu.kim@lge.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
Cc: Rajat Asthana <thisisrast7@gmail.com>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Gao Xiang <hsiangkao@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-07-01 04:56:16 +03:00
if ( ! fill & & chunksize = = 0 ) {
/* empty or end-of-file */
goto exit_3 ;
}
2013-07-09 03:01:46 +04:00
if ( posp )
* posp + = 4 ;
2014-08-09 01:23:10 +04:00
if ( ! fill ) {
inp + = 4 ;
size - = 4 ;
} else {
2017-02-25 02:01:16 +03:00
if ( chunksize > LZ4_compressBound ( uncomp_chunksize ) ) {
2013-07-09 03:01:46 +04:00
error ( " chunk length is longer than allocated " ) ;
goto exit_2 ;
}
2014-08-09 01:23:10 +04:00
size = fill ( inp , chunksize ) ;
if ( size < chunksize ) {
error ( " data corrupted " ) ;
goto exit_2 ;
}
2013-07-09 03:01:46 +04:00
}
# ifdef PREBOOT
if ( out_len > = uncomp_chunksize ) {
dest_len = uncomp_chunksize ;
out_len - = dest_len ;
} else
dest_len = out_len ;
2017-02-25 02:01:16 +03:00
ret = LZ4_decompress_fast ( inp , outp , dest_len ) ;
chunksize = ret ;
2013-07-09 03:01:46 +04:00
# else
dest_len = uncomp_chunksize ;
2017-02-25 02:01:16 +03:00
ret = LZ4_decompress_safe ( inp , outp , chunksize , dest_len ) ;
dest_len = ret ;
2013-07-09 03:01:46 +04:00
# endif
if ( ret < 0 ) {
error ( " Decoding failed " ) ;
goto exit_2 ;
}
2014-01-24 03:56:20 +04:00
ret = - 1 ;
2013-07-09 03:01:46 +04:00
if ( flush & & flush ( outp , dest_len ) ! = dest_len )
goto exit_2 ;
if ( output )
outp + = dest_len ;
if ( posp )
* posp + = chunksize ;
2014-08-09 01:23:10 +04:00
if ( ! fill ) {
size - = chunksize ;
2013-07-09 03:01:46 +04:00
2014-08-09 01:23:10 +04:00
if ( size = = 0 )
break ;
else if ( size < 0 ) {
error ( " data corrupted " ) ;
goto exit_2 ;
}
inp + = chunksize ;
2013-07-09 03:01:46 +04:00
}
}
lib/decompress_unlz4.c: correctly handle zero-padding around initrds.
lz4 compatible decompressor is simple. The format is underspecified and
relies on EOF notification to determine when to stop. Initramfs buffer
format[1] explicitly states that it can have arbitrary number of zero
padding. Thus when operating without a fill function, be extra careful to
ensure that sizes less than 4, or apperantly empty chunksizes are treated
as EOF.
To test this I have created two cpio initrds, first a normal one,
main.cpio. And second one with just a single /test-file with content
"second" second.cpio. Then i compressed both of them with gzip, and with
lz4 -l. Then I created a padding of 4 bytes (dd if=/dev/zero of=pad4 bs=1
count=4). To create four testcase initrds:
1) main.cpio.gzip + extra.cpio.gzip = pad0.gzip
2) main.cpio.lz4 + extra.cpio.lz4 = pad0.lz4
3) main.cpio.gzip + pad4 + extra.cpio.gzip = pad4.gzip
4) main.cpio.lz4 + pad4 + extra.cpio.lz4 = pad4.lz4
The pad4 test-cases replicate the initrd load by grub, as it pads and
aligns every initrd it loads.
All of the above boot, however /test-file was not accessible in the initrd
for the testcase #4, as decoding in lz4 decompressor failed. Also an
error message printed which usually is harmless.
Whith a patched kernel, all of the above testcases now pass, and
/test-file is accessible.
This fixes lz4 initrd decompress warning on every boot with grub. And
more importantly this fixes inability to load multiple lz4 compressed
initrds with grub. This patch has been shipping in Ubuntu kernels since
January 2021.
[1] ./Documentation/driver-api/early-userspace/buffer-format.rst
BugLink: https://bugs.launchpad.net/bugs/1835660
Link: https://lore.kernel.org/lkml/20210114200256.196589-1-xnox@ubuntu.com/ # v0
Link: https://lkml.kernel.org/r/20210513104831.432975-1-dimitri.ledkov@canonical.com
Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: Bongkyu Kim <bongkyu.kim@lge.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
Cc: Rajat Asthana <thisisrast7@gmail.com>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Gao Xiang <hsiangkao@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-07-01 04:56:16 +03:00
exit_3 :
2013-07-09 03:01:46 +04:00
ret = 0 ;
exit_2 :
if ( ! input )
large_free ( inp_start ) ;
exit_1 :
if ( ! output )
large_free ( outp ) ;
exit_0 :
return ret ;
}
# ifdef PREBOOT
2015-09-10 01:39:12 +03:00
STATIC int INIT __decompress ( unsigned char * buf , long in_len ,
initramfs: support initramfs that is bigger than 2GiB
Now with 64bit bzImage and kexec tools, we support ramdisk that size is
bigger than 2g, as we could put it above 4G.
Found compressed initramfs image could not be decompressed properly. It
turns out that image length is int during decompress detection, and it
will become < 0 when length is more than 2G. Furthermore, during
decompressing len as int is used for inbuf count, that has problem too.
Change len to long, that should be ok as on 32 bit platform long is
32bits.
Tested with following compressed initramfs image as root with kexec.
gzip, bzip2, xz, lzma, lzop, lz4.
run time for populate_rootfs():
size name Nehalem-EX Westmere-EX Ivybridge-EX
9034400256 root_img : 26s 24s 30s
3561095057 root_img.lz4 : 28s 27s 27s
3459554629 root_img.lzo : 29s 29s 28s
3219399480 root_img.gz : 64s 62s 49s
2251594592 root_img.xz : 262s 260s 183s
2226366598 root_img.lzma: 386s 376s 277s
2901482513 root_img.bz2 : 635s 599s
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Rashika Kheria <rashika.kheria@gmail.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: P J P <ppandit@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: "Daniel M. Weeks" <dan@danweeks.net>
Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: Jan Beulich <JBeulich@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-09 01:23:14 +04:00
long ( * fill ) ( void * , unsigned long ) ,
long ( * flush ) ( void * , unsigned long ) ,
2015-09-10 01:39:12 +03:00
unsigned char * output , long out_len ,
initramfs: support initramfs that is bigger than 2GiB
Now with 64bit bzImage and kexec tools, we support ramdisk that size is
bigger than 2g, as we could put it above 4G.
Found compressed initramfs image could not be decompressed properly. It
turns out that image length is int during decompress detection, and it
will become < 0 when length is more than 2G. Furthermore, during
decompressing len as int is used for inbuf count, that has problem too.
Change len to long, that should be ok as on 32 bit platform long is
32bits.
Tested with following compressed initramfs image as root with kexec.
gzip, bzip2, xz, lzma, lzop, lz4.
run time for populate_rootfs():
size name Nehalem-EX Westmere-EX Ivybridge-EX
9034400256 root_img : 26s 24s 30s
3561095057 root_img.lz4 : 28s 27s 27s
3459554629 root_img.lzo : 29s 29s 28s
3219399480 root_img.gz : 64s 62s 49s
2251594592 root_img.xz : 262s 260s 183s
2226366598 root_img.lzma: 386s 376s 277s
2901482513 root_img.bz2 : 635s 599s
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Rashika Kheria <rashika.kheria@gmail.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Kyungsik Lee <kyungsik.lee@lge.com>
Cc: P J P <ppandit@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: "Daniel M. Weeks" <dan@danweeks.net>
Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: Jan Beulich <JBeulich@suse.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-09 01:23:14 +04:00
long * posp ,
2015-09-10 01:39:12 +03:00
void ( * error ) ( char * x )
2013-07-09 03:01:46 +04:00
)
{
return unlz4 ( buf , in_len - 4 , fill , flush , output , posp , error ) ;
}
# endif