Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3758829pxv; Mon, 19 Jul 2021 08:06:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpgwW875zhG93euQaAwNGtw3oFJjKKZU54dbBDRvR4cmYoRTb9HFlVjv1iCpKf6eFzeN8g X-Received: by 2002:a05:6e02:1d0f:: with SMTP id i15mr15888474ila.199.1626707200381; Mon, 19 Jul 2021 08:06:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626707200; cv=none; d=google.com; s=arc-20160816; b=thfN8wSzP+KfdkVms0T6L/ITKV4Q1lWamInEC8beHDHoQvT0YintQ2srFZtBu6EIHD SkVza39PBElREG0RNMnVK9896XJ0NRPrKkjqXb5nORFA79TrIuCzYRsRw7SK4aveiwo9 gV2YC1NzPIKREdguo+cWwvXoyKobUS3MPRn/pSojeiBaNyuHl9iUCxJxpRGFw+Rj1zvV AFSBoftgrtEHYJWdnnp1HJxkXwcxZ8D3GXzl5/SifstGvOPMG4DMwLI5nZsaKuiDChHC wxPwgiatvtytsT/XiUpgMBKEoMMaH0GH3GI7V08q71Sx1xIERaiVkwJf2HVVmmQbjJv3 Bqlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PZ7wHsmWolu5FaZyrnyDX220i6EsesWNuHmlsaQOER0=; b=hjbZ1e6S7fhE95og1kUbq8GHeVyHmedO/q1RS07EcJTqyM+K68SxxabzK5E2Pd+YbH u7L05nV7Fv3dSBflhJg4DSu9v37egWMOyIhWDv8OXGqRq5JRCO5OY/RZ1xDnXItQGWq7 n9gdDUwCvurMCepoV9TFxr8EAF9idt1FZqdHX0GLKQxqP2Hj9m6qjftYCZo1YiKuVHsF KE20Fxqj9Oi/tL0Ks+V7gsefGu4pLkc64Hq+hOx/Zbzga43axQ0/V93lsV/Z5MiCGdVh HCywVSx8u1LM6x2z4/pqlb7BtacQl3f/c8fy1rGwHDBu1QhIysAANW4CCNiqaOrVXs4f S11w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Gg7B/Wwt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si20458368jat.114.2021.07.19.08.06.27; Mon, 19 Jul 2021 08:06:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Gg7B/Wwt"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243691AbhGSOYV (ORCPT + 99 others); Mon, 19 Jul 2021 10:24:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:55876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242522AbhGSOWZ (ORCPT ); Mon, 19 Jul 2021 10:22:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 66126611F2; Mon, 19 Jul 2021 15:02:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626706950; bh=YXEHPD2Vi2xEUHW1TMUFHUcpQOipBhA/X0A+7MOjTEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gg7B/WwtkXbFjqcljeN2fQiZ1/rFCBV8c1kJAggDX68hhdQh9o7w9Ge9QI8QX28zC VMCPZhursUFGIvrpe2MBEvdwhNxicb4aCsAWRBNi5Zm7fhci9TGXZyIdiUTACRGxBe DBD29nnITjK4Hrr7fnrkXCFzKbGpLq82p2ZF+Ai0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dimitri John Ledkov , Kyungsik Lee , Yinghai Lu , Bongkyu Kim , Kees Cook , Sven Schmidt <4sschmid@informatik.uni-hamburg.de>, Rajat Asthana , Nick Terrell , Gao Xiang , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.4 163/188] lib/decompress_unlz4.c: correctly handle zero-padding around initrds. Date: Mon, 19 Jul 2021 16:52:27 +0200 Message-Id: <20210719144941.828329620@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144913.076563739@linuxfoundation.org> References: <20210719144913.076563739@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dimitri John Ledkov [ Upstream commit 2c484419efc09e7234c667aa72698cb79ba8d8ed ] 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 Cc: Kyungsik Lee Cc: Yinghai Lu Cc: Bongkyu Kim Cc: Kees Cook Cc: Sven Schmidt <4sschmid@informatik.uni-hamburg.de> Cc: Rajat Asthana Cc: Nick Terrell Cc: Gao Xiang Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- lib/decompress_unlz4.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/decompress_unlz4.c b/lib/decompress_unlz4.c index 036fc882cd72..f1449244fdd4 100644 --- a/lib/decompress_unlz4.c +++ b/lib/decompress_unlz4.c @@ -115,6 +115,9 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, error("data corrupted"); goto exit_2; } + } else if (size < 4) { + /* empty or end-of-file */ + goto exit_3; } chunksize = get_unaligned_le32(inp); @@ -128,6 +131,10 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, continue; } + if (!fill && chunksize == 0) { + /* empty or end-of-file */ + goto exit_3; + } if (posp) *posp += 4; @@ -184,6 +191,7 @@ STATIC inline int INIT unlz4(u8 *input, long in_len, } } +exit_3: ret = 0; exit_2: if (!input) -- 2.30.2