Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp378228pxb; Thu, 19 Aug 2021 01:29:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQqa/AFBYxaOMpvekOgFPlHzaR4Y3Twy+A/MNk2V1aexaI9L0ap8BNEnbTWTYuMxHN1iX3 X-Received: by 2002:a17:906:e0d2:: with SMTP id gl18mr14449542ejb.35.1629361774254; Thu, 19 Aug 2021 01:29:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629361774; cv=none; d=google.com; s=arc-20160816; b=i2drob0QocI9/vCfWQSboQ+NrkC6GXicqZdVPmE75BvgYMCDn6J840oGaI04uz8W6A wQt1cdSZWdrYFkdDMTbnG38tykqJOGc5LFqWH2roBUt82Ck7LdwK9/jnlHLSWyXObN+/ 694gDEHMBpsq9gdT13QXrLk7FBe60c07DHk4eLIW8rd94i/MGE+cvau5CjVOmIFTlIa8 fwOXMPnrTkjV9u2DB7FiPE61NmJcHDwEwCwiEJEwYdkDmpDdiU7CbvmypvtFTd5yJkbM bNBi/jMU2OmOkm9oRhJC6P16w97qki+6wdtfafGMg4X222Ga5gWxA5i/omAM+UQ+JPZu C+aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=fnux9c4rs4QAwYzHEIhmWyQkib9slllVEKuUIkxZE4U=; b=PnWSJEKGBc7NHAo3X3XP6GqedfI2C741agIEZNJqC53wLnjiXVyCKL4D1INNeGWs7K 7TtpjVBxPLT3SpI0HXi1MJU0V5N7fegcdFtWgskpoIlyAQv91FAtVpWpptdSOB5lpQEM gxmu8dKT2NnzxpvvkK9GprXOdz10/YU9w80ufrQIsMsC4tDnIXm+GGQuEh2ZGkxt3kO3 +6YCHWYKAx9YrQqKCgUHbxujAIv9RBFYEQbKkDR8lZBCFhysJxkhytBjrO9u59Cs0yEQ LCDh8e/mw2lbcSrHD9BZetDJqAxyTSDP4bPbZtqU0Cx8hns3YWTxsjzeqz62e8rHTDlb q+aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=hOYdmFoK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ee28si2512197edb.265.2021.08.19.01.29.11; Thu, 19 Aug 2021 01:29:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=hOYdmFoK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232653AbhHSI2O (ORCPT + 99 others); Thu, 19 Aug 2021 04:28:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237176AbhHSI2N (ORCPT ); Thu, 19 Aug 2021 04:28:13 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BD28C061756 for ; Thu, 19 Aug 2021 01:27:37 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id d4so11044308lfk.9 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=dZEscI111/LBGDlFP/AeMeBtUz/8/GtoNriMj6bnYtmdrVnaT+IYh7JcOBJGshlzu8 Bm8HTvQgAHt9J4O3wgbfzZY8VvS43nfOKoj1HcVJZNH8y4q+njewyLlwUbpGvBl6OFrE wlxSdkDgGqqPFd45A33HnqZlRhWMmqFnhdrumYJYsFk9tCTDJP0H2o2KtNi7cdOEaQBI Ul/wtVATo1PW+NMtcFIpbDWJFZ8lZUgOOVDINnBwTsv6j83BwxHmhkUz6xk3CMI9MuBz /duFBxWSTF9Nqn/rA6xb9gU10zHUF8TIhQpnRYGYx+AWHC1KDUe/qMG1ooeFhnoF5b3T li0w== X-Gm-Message-State: AOAM5316UGkZqAiQctBcMoKBJdcX1Xw2i6wlpxZ7UFbzaVbxPbSq5VLz jDHuY1OwSpePQTHiT4gIG+3K3g== 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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