Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 272AFC433F5 for ; Thu, 19 May 2022 05:01:29 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1073A83ABF; Thu, 19 May 2022 07:01:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1652936487; bh=jp43FIsTqCh/6dL4DzTWpap9vsJGJJZ5aKBI7t2YSCk=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Wlg2zy3HFoduAX2VybCEQM9NfnYz1vdoQzrQFu1SMLn0KCJ764fVNJaJ65AwXEmCD kFZ+6wPMcF2axoMG7O2NZixpj7w4oP7G54aXGxpJPrORhmH/oloJLmenZtGtvq0Uqb Cw/gGLoyIroY8b/vCNigeAyesJd7LeVorBqwuKvy0bpo0yf1nd81Q2sQbPjHz6M3FO EPM48jwGAyH5gmBvBlY/l9ABvzz34psFiNuwnV3ra65sKZXAxutvX16QHPYgHobgyx TGUkHoqxRcVX/YfkfJ8ExIp7y7ia6A27ipvVrKoZd25ZQNnoHHAcpSFVV3FLJ3YejG JIoi9fqyIppwA== Received: by phobos.denx.de (Postfix, from userid 109) id DA28383BDB; Thu, 19 May 2022 07:01:24 +0200 (CEST) Received: from mout-u-107.mailbox.org (mout-u-107.mailbox.org [IPv6:2001:67c:2050:101:465::107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 288E68210C for ; Thu, 19 May 2022 07:01:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4L3d401XLTz9sTZ; Thu, 19 May 2022 07:01:20 +0200 (CEST) Message-ID: <1289c1f2-e316-11a8-a0fc-ffd4557e4d66@denx.de> Date: Thu, 19 May 2022 07:01:19 +0200 MIME-Version: 1.0 Subject: Re: [PATCH] ubifs: Fix lockup/crash when reading files Content-Language: en-US To: =?UTF-8?Q?Pali_Roh=c3=a1r?= Cc: u-boot@lists.denx.de References: <20220517204528.7277-1-pali@kernel.org> From: Stefan Roese In-Reply-To: <20220517204528.7277-1-pali@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4L3d401XLTz9sTZ X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean On 17.05.22 22:45, Pali Rohár wrote: > Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the > requested size") added optimization to do not read more bytes than it is > really needed. But this commit introduced incorrect handling of the hole at > the end of file. This logic cause U-Boot to crash or lockup when trying to > read from the ubifs filesystem. > > When read_block() call returns -ENOENT error (not an error, but the hole) > then dn-> structure is not filled and contain garbage. So using of dn->size > for memcpy() argument cause that U-Boot tries to copy unspecified amount of > bytes from possible unmapped memory. Which randomly cause lockup of P2020 > CPU. > > Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when > dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer > itself and read_block() fills buffer by zeros when it returns -ENOENT. > > This patch fixes ubifsload on P2020. > > Fixes: b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") > Signed-off-by: Pali Rohár Reviewed-by: Stefan Roese > --- > > Stefan, could you please look at this patch? Mentioned commit was > introduced by you more than 11 years ago. I'm surprised that nobody hit > this issue yet, but it looks like older U-Boot versions somehow filled > small garbage number into dn->size and did not cause U-Boot > lockup/crash. So long ago. I don't really remember the details. IIRC, I introduced the UBIFS support for some MIPS based board at that time. My best assumption is, that UBIFS is rarely used in U-Boot in general. Thanks, Stefan > --- > fs/ubifs/ubifs.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/fs/ubifs/ubifs.c b/fs/ubifs/ubifs.c > index d6be5c947d7e..d3026e310168 100644 > --- a/fs/ubifs/ubifs.c > +++ b/fs/ubifs/ubifs.c > @@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode, > buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE); > if (!buff) { > printf("%s: Error, malloc fails!\n", > __func__); > err = -ENOMEM; > break; > } > > /* Read block-size into temp buffer */ > ret = read_block(inode, buff, block, dn); > if (ret) { > err = ret; > if (err != -ENOENT) { > free(buff); > break; > } > } > > if (last_block_size) > dlen = last_block_size; > + else if (ret) > + dlen = UBIFS_BLOCK_SIZE; > else > dlen = le32_to_cpu(dn->size); > > /* Now copy required size back to dest */ > memcpy(addr, buff, dlen); > > free(buff); > } else { > ret = read_block(inode, addr, block, dn); > if (ret) { > err = ret; > if (err != -ENOENT) > break; > } > } > } > if (++i >= UBIFS_BLOCKS_PER_PAGE) > break; > block += 1; > addr += UBIFS_BLOCK_SIZE; Viele Grüße, Stefan Roese -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de