From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56CDFC433F5 for ; Fri, 13 May 2022 21:15:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A48398D0009; Fri, 13 May 2022 17:15:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F3928D0008; Fri, 13 May 2022 17:15:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8BC998D0009; Fri, 13 May 2022 17:15:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7BE688D0008 for ; Fri, 13 May 2022 17:15:52 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 496F260774 for ; Fri, 13 May 2022 21:15:52 +0000 (UTC) X-FDA: 79461976944.12.26ACAB9 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf19.hostedemail.com (Postfix) with ESMTP id A4C041A00BB for ; Fri, 13 May 2022 21:15:42 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A34F2B831D6; Fri, 13 May 2022 21:15:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DD3CC34100; Fri, 13 May 2022 21:15:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1652476548; bh=hKhlGQE6yUX/6q7co+A4+VhVblQH+h2cw2WCUG1hQtY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=RLyu/cOTMIEMDYCFfUTUkNz2nNo/5G+JgVV0bSjMB8yobftcq7oRSZ6tLH1Qa2nXh uiGSebp9Hre/8oZ7ysI3fAkueaQoV0pnMKMR8HbS77wPttknQbxJafdiMJPcB/0fio vPZZAkKGXLehDUeKmsYKJ918IaXV2jiKPmgpdW30= Date: Fri, 13 May 2022 14:15:47 -0700 From: Andrew Morton To: Luo Meng Cc: , , Subject: Re: [PTACH] tmpfs: Fix undefined-behaviour in shmem_reconfigure() Message-Id: <20220513141547.efed1057ef60c6d4eec7be2b@linux-foundation.org> In-Reply-To: <20220513025225.2678727-1-luomeng12@huawei.com> References: <20220513025225.2678727-1-luomeng12@huawei.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A4C041A00BB Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="RLyu/cOT"; spf=pass (imf19.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Rspam-User: X-Stat-Signature: 91wk6y3xqqxoio4ai5nf341ocpbuaj31 X-HE-Tag: 1652476542-507603 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, 13 May 2022 10:52:25 +0800 Luo Meng wrote: > When shmem_reconfigure() calls __percpu_counter_compare(), the second > parameter is unsigned int. Actually unsigned long long. > But when define __percpu_counter_compare(), > the second parameter is signed int. Actually s64. > So the abs of count - rhs, UBSAN shows the following warning: > Here's my amended paragraph: : When shmem_reconfigure() calls __percpu_counter_compare(), the second : parameter is unsigned long long. But in the definition of : __percpu_counter_compare(), the second parameter is s64. So when : __percpu_counter_compare() executes abs(count - rhs), UBSAN shows the : following warning: > ... > > Signed-off-by: Luo Meng > --- > mm/shmem.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/mm/shmem.c b/mm/shmem.c > index 4b2fea33158e..15733c480318 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -3487,6 +3487,10 @@ static int shmem_reconfigure(struct fs_context *fc) > > raw_spin_lock(&sbinfo->stat_lock); > inodes = sbinfo->max_inodes - sbinfo->free_inodes; > + if (ctx->blocks > S64_MAX) { > + err = "The number of blocks too large"; Should be "is too large". But to match the other error messages' style, we can use simply "Number of blocks too large". > + goto out; > + } > if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { > if (!sbinfo->max_blocks) { > err = "Cannot retroactively limit size"; I suppose that's a reasonable fix. I prefer "get the darned types right" but shmem_options.blocks should be unsigned (can't have a negative block count) and changing percpu_counter() would be tough. --- a/mm/shmem.c~tmpfs-fix-undefined-behaviour-in-shmem_reconfigure-fix +++ a/mm/shmem.c @@ -3477,7 +3477,7 @@ static int shmem_reconfigure(struct fs_c raw_spin_lock(&sbinfo->stat_lock); inodes = sbinfo->max_inodes - sbinfo->free_inodes; if (ctx->blocks > S64_MAX) { - err = "The number of blocks too large"; + err = "Number of blocks too large"; goto out; } if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { _