Skip to content

Commit b1af431

Browse files
Phillip Loughertorvalds
Phillip Lougher
authored andcommitted
bzip2/lzma: remove nasty uncompressed size hack in pre-boot environment
decompress_bunzip2 and decompress_unlzma have a nasty hack that subtracts 4 from the input length if being called in the pre-boot environment. This is a nasty hack because it relies on the fact that flush = NULL only when called from the pre-boot environment (i.e. arch/x86/boot/compressed/misc.c). initramfs.c/do_mounts_rd.c pass in a flush buffer (flush != NULL). This hack prevents the decompressors from being used with flush = NULL by other callers unless knowledge of the hack is propagated to them. This patch removes the hack by making decompress (called only from the pre-boot environment) a wrapper function that subtracts 4 from the input length before calling the decompressor. Signed-off-by: Phillip Lougher <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent daeb6b6 commit b1af431

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/decompress_bunzip2.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
*/
4646

4747

48-
#ifndef STATIC
48+
#ifdef STATIC
49+
#define PREBOOT
50+
#else
4951
#include <linux/decompress/bunzip2.h>
50-
#endif /* !STATIC */
52+
#endif /* STATIC */
5153

5254
#include <linux/decompress/mm.h>
5355
#include <linux/slab.h>
@@ -681,9 +683,7 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
681683
set_error_fn(error_fn);
682684
if (flush)
683685
outbuf = malloc(BZIP2_IOBUF_SIZE);
684-
else
685-
len -= 4; /* Uncompressed size hack active in pre-boot
686-
environment */
686+
687687
if (!outbuf) {
688688
error("Could not allocate output bufer");
689689
return -1;
@@ -733,4 +733,14 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
733733
return i;
734734
}
735735

736-
#define decompress bunzip2
736+
#ifdef PREBOOT
737+
STATIC int INIT decompress(unsigned char *buf, int len,
738+
int(*fill)(void*, unsigned int),
739+
int(*flush)(void*, unsigned int),
740+
unsigned char *outbuf,
741+
int *pos,
742+
void(*error_fn)(char *x))
743+
{
744+
return bunzip2(buf, len - 4, fill, flush, outbuf, pos, error_fn);
745+
}
746+
#endif

lib/decompress_unlzma.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
*Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3030
*/
3131

32-
#ifndef STATIC
32+
#ifdef STATIC
33+
#define PREBOOT
34+
#else
3335
#include <linux/decompress/unlzma.h>
3436
#endif /* STATIC */
3537

@@ -543,9 +545,7 @@ STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
543545
int ret = -1;
544546

545547
set_error_fn(error_fn);
546-
if (!flush)
547-
in_len -= 4; /* Uncompressed size hack active in pre-boot
548-
environment */
548+
549549
if (buf)
550550
inbuf = buf;
551551
else
@@ -645,4 +645,15 @@ STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
645645
return ret;
646646
}
647647

648-
#define decompress unlzma
648+
#ifdef PREBOOT
649+
STATIC int INIT decompress(unsigned char *buf, int in_len,
650+
int(*fill)(void*, unsigned int),
651+
int(*flush)(void*, unsigned int),
652+
unsigned char *output,
653+
int *posp,
654+
void(*error_fn)(char *x)
655+
)
656+
{
657+
return unlzma(buf, in_len - 4, fill, flush, output, posp, error_fn);
658+
}
659+
#endif

0 commit comments

Comments
 (0)