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 X-Spam-Level: X-Spam-Status: No, score=-8.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59A87C4338F for ; Wed, 18 Aug 2021 05:31:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DEF2A61076 for ; Wed, 18 Aug 2021 05:31:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DEF2A61076 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5811A8D0001; Wed, 18 Aug 2021 01:31:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5312E6B0072; Wed, 18 Aug 2021 01:31:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 447DD8D0001; Wed, 18 Aug 2021 01:31:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0149.hostedemail.com [216.40.44.149]) by kanga.kvack.org (Postfix) with ESMTP id 28F5F6B006C for ; Wed, 18 Aug 2021 01:31:39 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B52CF181CB14F for ; Wed, 18 Aug 2021 05:31:38 +0000 (UTC) X-FDA: 78487079076.14.FF6FFAC Received: from smtprelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by imf25.hostedemail.com (Postfix) with ESMTP id 66FB5B0025B9 for ; Wed, 18 Aug 2021 05:31:38 +0000 (UTC) Received: from omf17.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id 0B102183F79A5; Wed, 18 Aug 2021 05:31:38 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf17.hostedemail.com (Postfix) with ESMTPA id 9403D27DD38; Wed, 18 Aug 2021 05:31:33 +0000 (UTC) Message-ID: Subject: Re: [PATCH 2/5] slab: Add __alloc_size attributes for better bounds checking From: Joe Perches To: Kees Cook , linux-kernel@vger.kernel.org Cc: Daniel Micay , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , linux-mm@kvack.org, Miguel Ojeda , Nathan Chancellor , Nick Desaulniers , Dennis Zhou , Tejun Heo , Masahiro Yamada , Michal Marek , clang-built-linux@googlegroups.com, linux-kbuild@vger.kernel.org, linux-hardening@vger.kernel.org Date: Tue, 17 Aug 2021 22:31:32 -0700 In-Reply-To: <20210818050841.2226600-3-keescook@chromium.org> References: <20210818050841.2226600-1-keescook@chromium.org> <20210818050841.2226600-3-keescook@chromium.org> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.40.0-1 MIME-Version: 1.0 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1/uEF5/fBpMfVSWUWGASCCc76xkvowtGVk= X-HE-Tag: 1629264693-636294 X-Rspamd-Queue-Id: 66FB5B0025B9 Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=none; spf=none (imf25.hostedemail.com: domain of joe@perches.com has no SPF policy when checking 216.40.44.235) smtp.mailfrom=joe@perches.com X-Rspamd-Server: rspam04 X-Stat-Signature: 95jng4gnwjrpu8b3rfordds9wzhj6d6r X-HE-Tag: 1629264698-591571 Content-Transfer-Encoding: quoted-printable 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 Tue, 2021-08-17 at 22:08 -0700, Kees Cook wrote: > As already done in GrapheneOS, add the __alloc_size attribute for > regular kmalloc interfaces, to provide additional hinting for better > bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler > optimizations. [] > diff --git a/include/linux/slab.h b/include/linux/slab.h [] > @@ -181,7 +181,7 @@ int kmem_cache_shrink(struct kmem_cache *); > =A0/* > =A0=A0* Common kmalloc functions provided by all allocators > =A0=A0*/ > -void * __must_check krealloc(const void *, size_t, gfp_t); > +void * __must_check krealloc(const void *, size_t, gfp_t) __alloc_size= (2); I suggest the __alloc_size attribute be placed at the beginning of the function declaration to be more similar to the common __printf attribute location uses. __alloc_size(2) void * __must_check krealloc(const void *, size_t, gfp_t); I really prefer the __must_check to be with the other attribute and that function declarations have argument names too like: __alloc_size(2) __must_check void *krealloc(const void *ptr, size_t size, gfp_t gfp); but there are a _lot_ of placement of __must_check after the return type Lastly __alloc_size should probably be added to checkpatch Maybe: --- scripts/checkpatch.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 161ce7fe5d1e5..1a166b5cf3447 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -489,7 +489,8 @@ our $Attribute =3D qr{ ____cacheline_aligned| ____cacheline_aligned_in_smp| ____cacheline_internodealigned_in_smp| - __weak + __weak| + __alloc_size\s*\(\s*\d+\s*(?:,\s*d+\s*){0,5}\) }x; our $Modifier; our $Inline =3D qr{inline|__always_inline|noinline|__inline|__inline__};