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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B329BCD98C0 for ; Tue, 10 Oct 2023 23:10:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Uli1Rm63cpSDhoWPDmAXZbgLGTd5LaeGOVMoJfji/2o=; b=smYMCneLqqNSXE UymhU1Ce0405u8xNVViF9pbo2++rJ3/xW1mgxgianQ8z84QnfWRc3k/ueMOo/+AHLE/L5yn6kNvHc GL4Rj5gYAjeCwKoDdFnNZL5LHU9vMe+4IawRB5usA0wnvONfgNoILHQjixns2b9Tf+7hblgKVCbJr lsW490bRiVGyC18iNlupWeZQqy66Z3SqgL1+xcR4r8+5l419KzBj7bJzZQnDQXBk82EomakDGzudd 0VovTvAqFQHTJijNu1ymb2bTuUghOTa8ta0mDnrgsVVibpp/kBMIXcVgIf1nGks5Ngg1jbzNgO2FK YPan8DbE5PlWxX8D6AXQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qqLrB-00EL4L-1c; Tue, 10 Oct 2023 23:09:41 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qqLr8-00EL3k-1I for linux-arm-kernel@lists.infradead.org; Tue, 10 Oct 2023 23:09:39 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1c735473d1aso43004795ad.1 for ; Tue, 10 Oct 2023 16:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696979377; x=1697584177; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=T9NRsBIuS7/t1xSzM4HVRP0dsTmAqJy+of1PCaXbcgM=; b=m/BZYYVcOn9a6DhVTTwUAYtsvE5EvRJYvVz/8gm/NSXg1/YNe3EESojHsPrswu0jvY ssgAlMIXGufrbXxqbX8GpH7r6fmNU6K+SxKq6YjizLJobILMHkMRT7Pe78+uJMYYdjOl eeDiIsEmCvVhfmiBGnfcathqMwGGXriYnswAk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696979377; x=1697584177; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=T9NRsBIuS7/t1xSzM4HVRP0dsTmAqJy+of1PCaXbcgM=; b=b1OYXSR015v1DwIo9cPtg3rwsQ3lV99MYjpD8dbCp6oRdU5kcGVr+JRs8kLuzDnVk3 amruwfk1KiPFL4qdKz25GFzdjlIKUOHLofvaiyzwLUqla820WMVxEY/ICKiA6dsT9kNz XUgGbc6YgBqwU++sMSGHSnXstBqHASeszfYcZX2CF9pbL5y6jaF1Ygt9AozaMokK/inz Ry3TfG0DRMNmH3veqPZodXSVyTdwOvRtS0YAXRZksynZFJS0CfyMg7EwrSCxB3siRW0B 4YFAlM+S53mZmq6A0fqB8OauNWyyTiudN+rmcS2HxG0Gh/DsitLNErOwRwuzN86LpS4r 9T4Q== X-Gm-Message-State: AOJu0Yy1r8vR/O7oGjo9KkrlS2U7bCo+r2ZFpuAaxQlDO4CpDm7ErmUk M9CjflCEXnytxLccKi3Bjts/3g== X-Google-Smtp-Source: AGHT+IGgmzpC22sgiK9oMsgsREK3LFJzysUpnKl171CEVnXwEJGOya787Jhv0d5rIBgLPK6gEOlMUA== X-Received: by 2002:a17:902:ab13:b0:1c3:eb95:2d27 with SMTP id ik19-20020a170902ab1300b001c3eb952d27mr17223508plb.48.1696979377307; Tue, 10 Oct 2023 16:09:37 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id x9-20020a170902a38900b001c9c5a1b477sm728016pla.169.2023.10.10.16.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 16:09:36 -0700 (PDT) Date: Tue, 10 Oct 2023 16:09:35 -0700 From: Kees Cook To: "Gustavo A. R. Silva" Cc: Russell King , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH][next] atags_proc: Add __counted_by for struct buffer and use struct_size() Message-ID: <202310101607.3D7ED0A@keescook> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231010_160938_477018_7A97C370 X-CRM114-Status: GOOD ( 21.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Oct 10, 2023 at 06:46:50AM -0600, Gustavo A. R. Silva wrote: > Prepare for the coming implementation by GCC and Clang of the __counted_by > attribute. Flexible array members annotated with __counted_by can have > their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for > array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family > functions). > > While there, use struct_size() helper, instead of the open-coded > version, to calculate the size for the allocation of the whole > flexible structure, including of course, the flexible-array member. > > This code was found with the help of Coccinelle, and audited and > fixed manually. > > Signed-off-by: Gustavo A. R. Silva > --- > arch/arm/kernel/atags_proc.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/arch/arm/kernel/atags_proc.c b/arch/arm/kernel/atags_proc.c > index 3ec2afe78423..cd09f8ab93e3 100644 > --- a/arch/arm/kernel/atags_proc.c > +++ b/arch/arm/kernel/atags_proc.c > @@ -7,7 +7,7 @@ > > struct buffer { > size_t size; > - char data[]; > + char data[] __counted_by(size); > }; > > static ssize_t atags_read(struct file *file, char __user *buf, > @@ -54,7 +54,7 @@ static int __init init_atags_procfs(void) > > WARN_ON(tag->hdr.tag != ATAG_NONE); > > - b = kmalloc(sizeof(*b) + size, GFP_KERNEL); > + b = kmalloc(struct_size(b, data, size), GFP_KERNEL); > if (!b) > goto nomem; > Yup, "size" is assigned before accessing "data". Reviewed-by: Kees Cook -- Kees Cook _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel