Message ID | 20240228140152.1824901-1-arnd@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3364223dyb; Wed, 28 Feb 2024 06:03:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUk/DGRoU6OD7kVYppCUWmROxi0yQqd11tdoiXvOz+jgipF/EQtDMyabY0JrgfUfIx8jTb9ULa/GoP/R8nvK3gPm8HbdQ== X-Google-Smtp-Source: AGHT+IHuYL+SUzPrwaSgPSipt6/mrnPMzZPLr6Ar4LthOVjRKv9MENw44Y5VBeBO5yjv5G7rg0Q9 X-Received: by 2002:a05:6e02:1c46:b0:365:88b:3fc2 with SMTP id d6-20020a056e021c4600b00365088b3fc2mr2192261ilg.4.1709129013320; Wed, 28 Feb 2024 06:03:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709129013; cv=pass; d=google.com; s=arc-20160816; b=sisdGerY5dUCeS8ezxK74PNr29tCcJr3vmnhGdn6Mn3O+kanvzjvYIFZ35Ca2NTB5j nuV2LdXtP4GaXTI8pubN8jYTGcEKexWUsXbMuKp7PWl64VaHv3E/42fv64J4Rcl8opIR FtWOfi/nhiFHEE487e3Zm9jQ2DZQ/1uLEeLfEIi03flMKpPoNMI5WLGd4po7CVnd3cRX GKaMG4+jZoL0iakRppO6f7tsCNDum6tqCNk0jEGxZy65BT0YwYtERjEzAWAgefpg5Gwf Fq73X/qIeyTnUbzsCx4WiXWChGHrAsSMG00tYVVbKYYiY47R/nsclvz+P6ZU1uc1ljdf MHNQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=xH9acuERQIKw/8heDBUpo4U/2frlzxk4m6jGxz4xtvk=; fh=sxnzREYXJhlsZGY86QZhPxY7ThZOXEaQPxfaonHtC+M=; b=RPIhrXh5gmJIp6Nn7vMUwAwNrpAAyVcW4+JgWqk/oIlyezFvkZ1sKYm57bfFjFWwhW fTbRU6BtBpEiSdlm7YYRphvbKbjyNHwk+DpzkLd5HJ8pKvZZ3a+VAULT0idqwuRK4FjE Gf9sfca4R2RJ8eOYooW4dxj6A6DCyuq0cBAj5Ra8bpQ3Rl6C6axHPTYuZ5FKAVJu869+ muQPJ1WIZBC52tKeTVrYSN3ABNsOKW/NNeOSV+mf4eU5LyhFJgQCEENCkNj1Ue9AOHNX gSqyy4vFQwoi7UnTbgp9wmEW/pJJKhp3OHw3S4hPuIusrIGwG9/CAv0sj1XOnQTNfp0f 4tkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JZBoBpj1; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l13-20020a056e021c0d00b0036421ce7752si3078189ilh.201.2024.02.28.06.03.33 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 06:03:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JZBoBpj1; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85150-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AFA7F289075 for <ouuuleilei@gmail.com>; Wed, 28 Feb 2024 14:02:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A6881552FD; Wed, 28 Feb 2024 14:02:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JZBoBpj1" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A8F0153509; Wed, 28 Feb 2024 14:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709128927; cv=none; b=Lc0uNjoOyvQhj3iiL0KfSIupofmtfestWrdbubBQHA+Y/nH2SkpOFXAwTb7fyErX8V4T6XwrANfM/GfKLhEHUs23vTEdSxV4JAUUPct1fH2Z4RrbyVhTUWdsIrczp6Z+vBX82KaKZf4HC5CcNHMRpJTIgPCfXtO9qEkhDQCC0ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709128927; c=relaxed/simple; bh=v8piWjx7eAY0lLoFwqlEm66435Li26xyEz8YoqwyhTQ=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=iq9v7toUprYqnLGkT58ueoMcGEi3VQMTEulX5KfW/e4hLEvCKZjXA0VjBOygl0Yotp8QvTrwqDKev5y0WJzL66A6uUaE1jgO+EmFMWTf+scxDfsg35sBqdLJFNEImUfqh3wx1904ZrUbBBl0ctiMA1KE2z96D9cEAYgf2MWspVs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JZBoBpj1; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64A7EC433C7; Wed, 28 Feb 2024 14:02:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709128926; bh=v8piWjx7eAY0lLoFwqlEm66435Li26xyEz8YoqwyhTQ=; h=From:To:Cc:Subject:Date:From; b=JZBoBpj1/3w8VbLj0n2spVtSh9Dn90lTiUjQBWPhL/llNwsZWE4FEIKZ1O+aK9EmG QUioOYYCKmXIVuqLd+ypwjYC0LVp6d/ERGta+GreOP2rrS8vHWLhWyxPFX5DAbWwbB UPx+2kzf9c3nvuCjJ6ldRRPAtAU8cL0l2mlKAqJnM8eyJAeuZMuD5w/GxDY4JcnmC2 0ELr6yF4y7gnIT4Crly95B3nVW+ABTy42C7VsM098zO682kt34TyQ23PWoVnOINgmI +oLRC5b+LE+IGNPHo8/r/aVWzg/+BhY+jKYt/5jmW/9kBOk/Z8kA+q+DhtiV8Ms4G1 NVlMMJx5m9ikQ== From: Arnd Bergmann <arnd@kernel.org> To: Kees Cook <keescook@chromium.org>, Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>, Nathan Chancellor <nathan@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de>, Nick Desaulniers <ndesaulniers@google.com>, Bill Wendling <morbo@google.com>, Justin Stitt <justinstitt@google.com>, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] ALSA: asihpi: work around clang-17+ false positive fortify-string warning Date: Wed, 28 Feb 2024 15:01:45 +0100 Message-Id: <20240228140152.1824901-1-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792151664212211777 X-GMAIL-MSGID: 1792151664212211777 |
Series |
ALSA: asihpi: work around clang-17+ false positive fortify-string warning
|
|
Commit Message
Arnd Bergmann
Feb. 28, 2024, 2:01 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de> One of the memory copies in this driver triggers a warning about a possible out of range access: In file included from /home/arnd/arm-soc/sound/pci/asihpi/hpimsgx.c:13: /home/arnd/arm-soc/include/linux/fortify-string.h:553:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] 553 | __write_overflow_field(p_size_field, size); | ^ Adding a range check avoids the problem, though I don't quite see why it warns in the first place if clang has no knowledge of the actual range of the type, or why I never saw the warning in previous randconfig tests. Signed-off-by: Arnd Bergmann <arnd@arndb.de> --- sound/pci/asihpi/hpimsgx.c | 3 +++ 1 file changed, 3 insertions(+)
Comments
On Wed, 28 Feb 2024 15:01:45 +0100, Arnd Bergmann wrote: > > From: Arnd Bergmann <arnd@arndb.de> > > One of the memory copies in this driver triggers a warning about a > possible out of range access: > > In file included from /home/arnd/arm-soc/sound/pci/asihpi/hpimsgx.c:13: > /home/arnd/arm-soc/include/linux/fortify-string.h:553:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] > 553 | __write_overflow_field(p_size_field, size); > | ^ Hmm, I can't see the relevance of those messages with the code you touched. Do you have more info? > Adding a range check avoids the problem, though I don't quite see > why it warns in the first place if clang has no knowledge of the > actual range of the type, or why I never saw the warning in previous > randconfig tests. It's indeed puzzling. If it's really about adapter_prepare() call, the caller is only HPIMSGX__init(), and there is already an assignment with that index value beforehand: hpi_entry_points[hr.u.s.adapter_index] = entry_point_func; and this array is also the size of HPI_MAX_ADAPTERS. That is, the same check should have caught here... thanks, Takashi > > Signed-off-by: Arnd Bergmann <arnd@arndb.de> > --- > sound/pci/asihpi/hpimsgx.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/sound/pci/asihpi/hpimsgx.c b/sound/pci/asihpi/hpimsgx.c > index d0caef299481..4f245c3956b1 100644 > --- a/sound/pci/asihpi/hpimsgx.c > +++ b/sound/pci/asihpi/hpimsgx.c > @@ -576,6 +576,9 @@ static u16 adapter_prepare(u16 adapter) > /* Open the adapter and streams */ > u16 i; > > + if (adapter >= ARRAY_SIZE(rESP_HPI_ADAPTER_OPEN)) > + return 0; > + > /* call to HPI_ADAPTER_OPEN */ > hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER, > HPI_ADAPTER_OPEN); > -- > 2.39.2 >
On Wed, Feb 28, 2024, at 15:37, Takashi Iwai wrote: > On Wed, 28 Feb 2024 15:01:45 +0100, > Arnd Bergmann wrote: >> >> From: Arnd Bergmann <arnd@arndb.de> >> >> One of the memory copies in this driver triggers a warning about a >> possible out of range access: >> >> In file included from /home/arnd/arm-soc/sound/pci/asihpi/hpimsgx.c:13: >> /home/arnd/arm-soc/include/linux/fortify-string.h:553:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] >> 553 | __write_overflow_field(p_size_field, size); >> | ^ > > Hmm, I can't see the relevance of those messages with the code you > touched. Do you have more info? Not much. The warning is caused by this line: memcpy(&rESP_HPI_ADAPTER_OPEN[adapter], &hr, sizeof(rESP_HPI_ADAPTER_OPEN[0])); rESP_HPI_ADAPTER_OPEN[] is a global array with a fixed length of 20 elements, and 'adapter' is a 16-bit index into that array. The warning is intended to trigger when there is a code path that will overflow, but it normally warns only if there is a known value range that the variable can take, not for an unrestricted index. My first thought was that clang warns about it here because the 'u16 adapter' declaration limits the index to something smaller than an 'int' or 'long', but changing the type did not get rid of the warning. >> Adding a range check avoids the problem, though I don't quite see >> why it warns in the first place if clang has no knowledge of the >> actual range of the type, or why I never saw the warning in previous >> randconfig tests. > > It's indeed puzzling. If it's really about adapter_prepare() call, > the caller is only HPIMSGX__init(), and there is already an assignment > with that index value beforehand: > hpi_entry_points[hr.u.s.adapter_index] = entry_point_func; > > and this array is also the size of HPI_MAX_ADAPTERS. That is, the > same check should have caught here... The fortified-string warning only triggers for string.h operations (memset, memcpy, memcmp, strn*...), not for a direct assignment. Arnd
On Wed, 28 Feb 2024 16:03:56 +0100, Arnd Bergmann wrote: > > On Wed, Feb 28, 2024, at 15:37, Takashi Iwai wrote: > > On Wed, 28 Feb 2024 15:01:45 +0100, > > Arnd Bergmann wrote: > >> > >> From: Arnd Bergmann <arnd@arndb.de> > >> > >> One of the memory copies in this driver triggers a warning about a > >> possible out of range access: > >> > >> In file included from /home/arnd/arm-soc/sound/pci/asihpi/hpimsgx.c:13: > >> /home/arnd/arm-soc/include/linux/fortify-string.h:553:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning] > >> 553 | __write_overflow_field(p_size_field, size); > >> | ^ > > > > Hmm, I can't see the relevance of those messages with the code you > > touched. Do you have more info? > > Not much. The warning is caused by this line: > > memcpy(&rESP_HPI_ADAPTER_OPEN[adapter], &hr, > sizeof(rESP_HPI_ADAPTER_OPEN[0])); > > rESP_HPI_ADAPTER_OPEN[] is a global array with a fixed > length of 20 elements, and 'adapter' is a 16-bit index > into that array. The warning is intended to trigger when > there is a code path that will overflow, but it normally > warns only if there is a known value range that the > variable can take, not for an unrestricted index. > > My first thought was that clang warns about it here because > the 'u16 adapter' declaration limits the index to something > smaller than an 'int' or 'long', but changing the type > did not get rid of the warning. > > >> Adding a range check avoids the problem, though I don't quite see > >> why it warns in the first place if clang has no knowledge of the > >> actual range of the type, or why I never saw the warning in previous > >> randconfig tests. > > > > It's indeed puzzling. If it's really about adapter_prepare() call, > > the caller is only HPIMSGX__init(), and there is already an assignment > > with that index value beforehand: > > hpi_entry_points[hr.u.s.adapter_index] = entry_point_func; > > > > and this array is also the size of HPI_MAX_ADAPTERS. That is, the > > same check should have caught here... > > The fortified-string warning only triggers for string.h operations > (memset, memcpy, memcmp, strn*...), not for a direct assignment. Ah, I see. Then from the logical POV, it's better to have a check before that assignment; otherwise it'd overflow silently there. Does putting the check beforehand (like the one below) fix similarly? thanks, Takashi --- a/sound/pci/asihpi/hpimsgx.c +++ b/sound/pci/asihpi/hpimsgx.c @@ -708,7 +708,8 @@ static u16 HPIMSGX__init(struct hpi_message *phm, phr->error = HPI_ERROR_PROCESSING_MESSAGE; return phr->error; } - if (hr.error == 0) { + if (hr.error == 0 && + hr.u.s.adapter_index < ARRAY_SIZE(rESP_HPI_ADAPTER_OPEN)) { /* the adapter was created successfully save the mapping for future use */ hpi_entry_points[hr.u.s.adapter_index] = entry_point_func;
On Wed, Feb 28, 2024, at 17:24, Takashi Iwai wrote: > On Wed, 28 Feb 2024 16:03:56 +0100, >> On Wed, Feb 28, 2024, at 15:37, Takashi Iwai wrote: >> > On Wed, 28 Feb 2024 15:01:45 +0100, >> >> The fortified-string warning only triggers for string.h operations >> (memset, memcpy, memcmp, strn*...), not for a direct assignment. > > Ah, I see. Then from the logical POV, it's better to have a check > before that assignment; otherwise it'd overflow silently there. > > Does putting the check beforehand (like the one below) fix similarly? Indeed, it does address the issue. I'll send a v2 with that version, since it clearly makes more sense. Arnd
On Wed, Feb 28, 2024 at 04:03:56PM +0100, Arnd Bergmann wrote: > My first thought was that clang warns about it here because > the 'u16 adapter' declaration limits the index to something > smaller than an 'int' or 'long', but changing the type > did not get rid of the warning. Our current theory is that Clang has a bug with __builtin_object_size/__builtin_dynamic_object_size when doing loop unrolling (or other kinds of execution flow splitting). Some examples: https://github.com/ClangBuiltLinux/linux/issues?q=label%3A%22loop+unroller%22+ Which is perhaps related to __bos miscalculations: https://github.com/ClangBuiltLinux/linux/issues?q=label%3A%22%5B__bos%5D+miscalculation%22+ -Kees
On Wed, Feb 28, 2024 at 9:39 AM Kees Cook <keescook@chromium.org> wrote: > > On Wed, Feb 28, 2024 at 04:03:56PM +0100, Arnd Bergmann wrote: > > My first thought was that clang warns about it here because > > the 'u16 adapter' declaration limits the index to something > > smaller than an 'int' or 'long', but changing the type > > did not get rid of the warning. > > Our current theory is that Clang has a bug with > __builtin_object_size/__builtin_dynamic_object_size when doing loop > unrolling (or other kinds of execution flow splitting). Some examples: > https://github.com/ClangBuiltLinux/linux/issues?q=label%3A%22loop+unroller%22+ > > Which is perhaps related to __bos miscalculations: > https://github.com/ClangBuiltLinux/linux/issues?q=label%3A%22%5B__bos%5D+miscalculation%22+ > The idea that there's a bug with the __b{d}os builtins is controversial. The consensus among GCC and Clang compiler developers is that returning *a* valid size, rather than the one asked for, is okay as long as it doesn't go past the current object's max size. (I couldn't disagree more.) There are a lot of situations where Clang reverts to that behavior. I'm working to change that. -bw
diff --git a/sound/pci/asihpi/hpimsgx.c b/sound/pci/asihpi/hpimsgx.c index d0caef299481..4f245c3956b1 100644 --- a/sound/pci/asihpi/hpimsgx.c +++ b/sound/pci/asihpi/hpimsgx.c @@ -576,6 +576,9 @@ static u16 adapter_prepare(u16 adapter) /* Open the adapter and streams */ u16 i; + if (adapter >= ARRAY_SIZE(rESP_HPI_ADAPTER_OPEN)) + return 0; + /* call to HPI_ADAPTER_OPEN */ hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER, HPI_ADAPTER_OPEN);