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=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 42505C432BE for ; Thu, 19 Aug 2021 08:27:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 508DB61042 for ; Thu, 19 Aug 2021 08:27:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 508DB61042 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=rasmusvillemoes.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 9DA966B006C; Thu, 19 Aug 2021 04:27:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 98AF96B0071; Thu, 19 Aug 2021 04:27:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8531B6B0072; Thu, 19 Aug 2021 04:27:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0249.hostedemail.com [216.40.44.249]) by kanga.kvack.org (Postfix) with ESMTP id 6BDEF6B006C for ; Thu, 19 Aug 2021 04:27:38 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1F3B9267E3 for ; Thu, 19 Aug 2021 08:27:38 +0000 (UTC) X-FDA: 78491151396.04.19B6862 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by imf10.hostedemail.com (Postfix) with ESMTP id 997FA600F2DB for ; Thu, 19 Aug 2021 08:27:37 +0000 (UTC) Received: by mail-lf1-f48.google.com with SMTP id g13so11035886lfj.12 for ; Thu, 19 Aug 2021 01:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=fnux9c4rs4QAwYzHEIhmWyQkib9slllVEKuUIkxZE4U=; b=hOYdmFoKoEgNHqgucA6LMg9vfQKBXNg9AF8pFlq42o1xRgIW3yE+LurXE3ntggZBTH b7UmQz756HBNv7YCtADHrbPG1UFWZ4SkfysIRSgPbVM2DhcjX0peKKJ7ojvvZjcWFNYA dZg/1y9o+kgZmwmu7JL8BuLC1vLmhEZXdESTI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=fnux9c4rs4QAwYzHEIhmWyQkib9slllVEKuUIkxZE4U=; b=lzDSEB4jSbjpyCTqvvKHCOO16Mxmkd+G7GYpRZlqbiSuD9ptJN/CH7FaMawK2HpdyJ UmnjnWZ3U2rS6NHe8rC8DtdNv4GPQf8lAkdXOoHIIY/ijYCU5KEIreEcMSp+Zq+7kDK2 Xo7WAjVt7JFOmkvI7dq54sjiBhwvwgQX0kUxrxf7u+x0ynx0z1O5q83PBYP1hewpTFos fB/5NnCODxb+Btdr/D9W7Q9braIrUm1ckeNpaElk99VmiY2vs9ACPdfyPwUKQz3FHPKK Ykrj0XEAJh1x4B9XWNoJsNYB6r4heEYeIFBW/7VzBrfz1/60zCjpVW41iv8QzvPIzgWA GKzQ== X-Gm-Message-State: AOAM532KuZflwqdd7MgNWH95G6+yOfePJnMAZsa6RPcgqE9+53scXXEJ jRaXReOTPVMwRi01zRDMSSrJTQ== X-Google-Smtp-Source: ABdhPJyouvHEQUt0g1C4b6sjpes/07r3gE6nWd5YO8P/tYJueCo9kkvIivms2bRFv5I/d17VlOiGTQ== X-Received: by 2002:ac2:5f0f:: with SMTP id 15mr9840865lfq.25.1629361655585; Thu, 19 Aug 2021 01:27:35 -0700 (PDT) Received: from [172.16.11.1] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id r7sm256655lfr.60.2021.08.19.01.27.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Aug 2021 01:27:35 -0700 (PDT) Subject: Re: [PATCH v2 4/7] slab: Add __alloc_size attributes for better bounds checking 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, Joe Perches , Miguel Ojeda , Nathan Chancellor , Nick Desaulniers , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn , Dennis Zhou , Tejun Heo , Masahiro Yamada , Michal Marek , clang-built-linux@googlegroups.com, linux-kbuild@vger.kernel.org, linux-hardening@vger.kernel.org References: <20210818214021.2476230-1-keescook@chromium.org> <20210818214021.2476230-5-keescook@chromium.org> From: Rasmus Villemoes Message-ID: Date: Thu, 19 Aug 2021 10:27:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210818214021.2476230-5-keescook@chromium.org> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rasmusvillemoes.dk header.s=google header.b=hOYdmFoK; dmarc=none; spf=pass (imf10.hostedemail.com: domain of linux@rasmusvillemoes.dk designates 209.85.167.48 as permitted sender) smtp.mailfrom=linux@rasmusvillemoes.dk X-Stat-Signature: db7e6xd9kmdyg9khshefxpmm6d43r7tw X-Rspamd-Queue-Id: 997FA600F2DB X-Rspamd-Server: rspam05 X-HE-Tag: 1629361657-745765 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 18/08/2021 23.40, 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. > > #ifdef CONFIG_NUMA > +__alloc_size(1) > void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_slab_alignment __malloc; Eh, can we keep all the attributes together instead of having some before, some after? I don't necessarily think this is a good idea, but just throwing it out there: __alloc_size almost always goes along with __malloc, so one could define __alloc_size in such a way that it implies __malloc, then just have a "raw" ____alloc_size version to use for krealloc() and similar. But I guess it's cleaner to keep it this way. While declared in string.h, kmemdup() is also eligible for alloc_size(2). Which brings me to an old wishlist item of mine [it's almost christmas]: that alloc_size could understand more general expressions for the size of the returned memory, not just the primitive one based on malloc()/calloc() prototypes. So e.g. kmemdup_nul() returns something of size $2+1, while it is also very common to have a alloc_foo(void) helper which returns something of size sizeof(struct foo). Unfortunately I don't think gcc's attribute parsing machinery can easily be tweaked into accepting struct bar *alloc_bars(unsigned count) __new_a_s(count * sizeof(struct bar)) but maybe clang could. If a compiler could understand that kind of attribute, it would also pave the way for implementing __attribute__((__buffer_size__(param, size, access))) e.g. memchr(src, c, size) __buffer_size(src, size, "r") clk_bulk_get(struct device *dev, int num_clks, struct clk_bulk_data *clks) __buffer_size(clks, num_clks * sizeof(*clks), "rw") which could be used for both static analysis and optional run-time instrumentation. Rasmus