• Dimitri John Ledkov's avatar
    lib/decompress_unlz4.c: correctly handle zero-padding around initrds. · 2c484419
    Dimitri John Ledkov authored
    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.comSigned-off-by: default avatarDimitri 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: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    2c484419
decompress_unlz4.c 4.18 KB