Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1146095pxv; Fri, 9 Jul 2021 19:28:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySp5xfFiOAt6X7EbyEP9cQgjD5gOdm4U9YhlscmW4Zcb3OJO5xez8tGY2+2r5w6Q1B75t7 X-Received: by 2002:a17:906:4e12:: with SMTP id z18mr18144836eju.543.1625884133876; Fri, 09 Jul 2021 19:28:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625884133; cv=none; d=google.com; s=arc-20160816; b=Nuh9ayzsvzhsQwocsUMyS9Q38AU/XCmv9w0zJy0aovgQCIIpgSA3gycPGVssiYWy+3 Pz+IcJRjcK8wLkxy39LGbyejeyKhw4vR5Mgw2XQimgWXaHY0vgPN6cesLNBrpizIVFfy hNxFnjGsE49Z1UXz44fImsD+vKJKaOOAOHisRY+x/bMxj5ZTYYRuINvsWBI8QcomdU55 +lHjUuPEso2VBFofTMNC6ZjKoQQX6K+Tm2bjApwVZNAEbkocDiukC8pXEA0nHJUxRH4R rDrVILhzIJCq3E6IA8W8iThW7Fh4Mk1vnMoSnhJOOU/V47s37nheF2mMb+hJYf75znEy z2Ww== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/DIp/8hMMiOCBsqS/i67RPmBTBX12HeutLFUJKHpvBo=; b=ExDu8xT+ahYwG2Q8YHP9+S/2IEMD1PYQSQrWkUHSJNgK5iFtH7SPk1ezDJ3nQFPTIM ENQfsD/RVyTZ2fg8MlBU2y+skRko1630a3kNZiiIgLy/1iYraCLYlTSmutSvU/+qt0TM 3POFuNZ05ewwCySSDc6ZWrX14EzEu5azgMbLs6zYu6160XTPHKo9/7uosqBEheWMeNnW fuEKnXhC1YOikuchTm8MrSE2lgqx4cLnVZxu+Zsn8ccfF6adWTXI1G9avN3LUI54GY10 2f+o2bpm+8s8GjM2Jn0WH5tF+daS6ioVdzCYWRPjYnhZ+PF8dVWBKbIMlbPpCTWpM+k5 DZIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ES3gJtmG; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gt34si9126280ejc.285.2021.07.09.19.28.31; Fri, 09 Jul 2021 19:28:53 -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=@kernel.org header.s=k20201202 header.b=ES3gJtmG; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233399AbhGJCaZ (ORCPT + 99 others); Fri, 9 Jul 2021 22:30:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:42146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233805AbhGJC1W (ORCPT ); Fri, 9 Jul 2021 22:27:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8E646613FE; Sat, 10 Jul 2021 02:24:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625883862; bh=+uFTDpvKhWHlQ5H7ysMc7Ohh59fU2zT9cKN7k1vT62k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ES3gJtmGFftTU6cXUagkFdkQvB9cTW8SC2kJNC1uCSUrsZMf36vJhGw+7/IF/0o9k swRf91QaKZJdfRaeq2eVln0MC2y9xhKzgaUqV818beV1cgkY9L8LK2K5+xCatpDqn+ ll3YKmgyqOzzGnUs14/SEO6QtmWCCJNmNFojtZy5xrto3PMrhYRSyBy1dhOkiv9Qv3 yUqsLnoyt8Uv4F7RnMSOPSTbNw/YoupNcP+lCJr+j9Tgqs6ipnnCK4DWwbmC6vklTO +DD1Vc9QF9VLEcjXA3sH2gGjZ/24HoIMM593FHeoOSb1Kn7CZUTNjWD/m5lbcONqXj ge6REHFa9dz2g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: 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 AUTOSEL 5.12 103/104] lib/decompress_unlz4.c: correctly handle zero-padding around initrds. Date: Fri, 9 Jul 2021 22:21:55 -0400 Message-Id: <20210710022156.3168825-103-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210710022156.3168825-1-sashal@kernel.org> References: <20210710022156.3168825-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 c0cfcfd486be..e6327391b6b6 100644 --- a/lib/decompress_unlz4.c +++ b/lib/decompress_unlz4.c @@ -112,6 +112,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); @@ -125,6 +128,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