Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp189957rwb; Wed, 21 Sep 2022 20:15:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5ius4B/1+XSjkvm/XCjRZLEUdJpviBT6g/Btu/ohMXsqHQfNfflkyPlTwVTBPPwmkaXWsZ X-Received: by 2002:a05:6402:c8a:b0:44e:81b3:4b7e with SMTP id cm10-20020a0564020c8a00b0044e81b34b7emr1120387edb.181.1663816500429; Wed, 21 Sep 2022 20:15:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663816500; cv=none; d=google.com; s=arc-20160816; b=WAnOxGO8P9mE6l9elE8Mt6+yvI8b3A5NmYpUi98VxUIJfVr0bYg/7S/oFO/jL+zRpn CAWrAh5GaOub1i5vdg7FjiO6CwhVfa9/wATdGuzim+VLvurCQd4Ki21FNLI7Pe2hUvPM 5U4torT+GuO2ZDJnArI4rGxwoR1c8NaokUZ4Chfh7w5piGeBVr6iwlUDWEKKjlclSK0v wonO/d2iz4GaKlSwhV/+i5v7ZHo2D2SudN9Z2kHxuTLrvhl+pAseJOHmQUduoKsq8PG3 4b1hS69CqW5vgMfV/VTGVEAp4Mcnjiap2XStqH2FqMWkwREv/TSZo8/IZOQx35vNxwjC smHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bpEbyFBm8ujPK6iqrGWXCPwqEismRLAX26z3I+Rspz8=; b=YjeeldLXNA1vBKNDeUVcYeRiIWeDTjS/LeHpFwePwEaCisW0Ym+kZJ2tw8KnH33rDn hqHbtyZ3urt1bAz2HsA2xQddVrIymW9mv4WyzjUgMMTH6A4CKgYsGNK2AGGE8VB+3i73 LyvWUxPp3KQY/UyDtdhr5/fkQo+ehGphS0wDG5CIJzgEKXYYXmjOH7y2U2bGgZt4XjP6 C1rVruaA3NubBkcyHv535vk9Gck9zCCINxmOYwlNC89S2XkWVDWv2lI7EYgCZP87qsa/ cocw2odYNVmJ0iV/O8jJCbBb5JBTkHk355j38bAUhoGQELrtSq5Um2AuESNTqO73xZ5y OsLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GwEKRlSG; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j25-20020a170906411900b00718c7b4cbeasi3548168ejk.991.2022.09.21.20.14.37; Wed, 21 Sep 2022 20:15:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GwEKRlSG; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229913AbiIVDNn (ORCPT + 63 others); Wed, 21 Sep 2022 23:13:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231278AbiIVDLx (ORCPT ); Wed, 21 Sep 2022 23:11:53 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE84DA2207 for ; Wed, 21 Sep 2022 20:10:33 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id d11so127759pll.8 for ; Wed, 21 Sep 2022 20:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=bpEbyFBm8ujPK6iqrGWXCPwqEismRLAX26z3I+Rspz8=; b=GwEKRlSGWNZZz0bSOhH09wT/dIlAzUgEDzlKCcSGfqUiFWltnmUuICqFqj3LlzCRek BVgZYAczODxRtxBqW9kNHbHyqaba8VuQcAtk82kUBEiaAlU+daUZZIQGOA7CX1koggEg yep8B8ilhj99/u+e0tErmzGRiobAHfyNKzCog= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=bpEbyFBm8ujPK6iqrGWXCPwqEismRLAX26z3I+Rspz8=; b=SDwxUOCqKZIYH2fq+3PDvxptTBmwRX5xI6A8npeXheA/vBHgtCR7QIB0tPohpINAuC WNMqqYQ7PnDBeblirYLO12gXXQhQec4lwVN80b+UQnzF08f/OP/JPbT/E+qVa4UU6K7w GiiGOFKM1K2t/gAHqFKj/yD8vbuBXl+DL8DMy4oh5vb8KbzV/xIUN+wvi1afD/jjKqlA lKV6xA3qhe3CNZ4s4ZPHDb/VqeNJy99fq73kpcCaSD3J17zeMBmize3E/l9sAvYJR/ww zk0uPXcLMtafUX+AvY19huqsjnF+OlNaXlPq/8k303OIHsOdfbXJN3dbUrEjS/SneQSF vK0w== X-Gm-Message-State: ACrzQf0nXr7CuSInK/gMZU/Y6CZXMCd6etOseyPD/s7fYuQyTKCxeNLi KAKTpRDZZ+XHhj8t7vzWXU4JDA== X-Received: by 2002:a17:902:e54b:b0:177:e29e:a0c0 with SMTP id n11-20020a170902e54b00b00177e29ea0c0mr1359878plf.66.1663816231700; Wed, 21 Sep 2022 20:10:31 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u3-20020a17090341c300b0016d6963cb12sm2781495ple.304.2022.09.21.20.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 20:10:26 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Nick Desaulniers , Hao Luo , Marco Elver , linux-mm@kvack.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Greg Kroah-Hartman , Alex Elder , Josef Bacik , David Sterba , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jesse Brandeburg , Daniel Micay , Yonghong Song , Miguel Ojeda , Jacob Shin , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-fsdevel@vger.kernel.org, intel-wired-lan@lists.osuosl.org, dev@openvswitch.org, x86@kernel.org, linux-wireless@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH 11/12] slab: Remove __malloc attribute from realloc functions Date: Wed, 21 Sep 2022 20:10:12 -0700 Message-Id: <20220922031013.2150682-12-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220922031013.2150682-1-keescook@chromium.org> References: <20220922031013.2150682-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3544; h=from:subject; bh=oS2Iv+uVGC72Kt/ZsRdKSQv1v3zTVhJ3IWvWWRabp+g=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjK9IUEKv90igkAnqqd37b6kR1bxxEz6G+6mdwmL8d hU8qttyJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYyvSFAAKCRCJcvTf3G3AJqdIEA CK12iuKhH2Gb+F4wapUYpaSc373MS6rjJoFpsNm84+/ObvNHGEz2kq5kYHkOs7G3e8jhHlr4YXru45 YnbkR3SRCmUyJ7loADAzHlj0yCxkYniAAXvuZzfvc9luBT3TIsBLzOyoo8E8ylHkAK0aVycGXvAy+W PGkSD/jL+EQrHdUK+hra5t32/YXsbXr4NStVZ9IzHolQ9EI054AF8/LV77MBWpPIX5Ho1mGWzl1ijJ E9wFPHhl2hfKzU9eXD+N4EzOrlLceF+ztZPDuq2MdUfMxOdv5GAJKiW5FRq+IFpNkwIM9nhSPM5iQE YBr/ivEWQqdNFsPnRtgWssO370vtzYb+x61dFRrUn7/FKR7Om27TEYwAd6+P+FnDwRoMTV/BPuFe6C 7iiHIRbFsfrIUn/r4jYTZt4u6NtpaDA1FVV/Em1JDkKtSh3fWi4Ku18TGZQm81iMdScRZHubomFfI+ vxHjePLahrOHkSfaNZvuJLrLPDU7WsDADYD6e/53VTiL3yyHRtO0XH1DyMkIDUA2WPNZy6ISq70TQ5 eN54uMg3p8o5Bz9r112LSXwEkIA+2aRlSgWZDi0KwDLacLHhcuECUcvzHrANynElUzrtH5uoqtYyZh Y8ppyJSFYyPmpNrkQ/M6ZKVaeCwZCHu2ZcgtL5o8/NTiuMaB6SQl9nFcWJRg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org The __malloc attribute should not be applied to "realloc" functions, as the returned pointer may alias the storage of the prior pointer. Instead of splitting __malloc from __alloc_size, which would be a huge amount of churn, just create __realloc_size for the few cases where it is needed. Additionally removes the conditional test for __alloc_size__, which is always defined now. Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Nick Desaulniers Cc: Hao Luo Cc: Marco Elver Cc: linux-mm@kvack.org Signed-off-by: Kees Cook --- include/linux/compiler_types.h | 13 +++++-------- include/linux/slab.h | 12 ++++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 4f2a819fd60a..f141a6f6b9f6 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -271,15 +271,12 @@ struct ftrace_likely_data { /* * Any place that could be marked with the "alloc_size" attribute is also - * a place to be marked with the "malloc" attribute. Do this as part of the - * __alloc_size macro to avoid redundant attributes and to avoid missing a - * __malloc marking. + * a place to be marked with the "malloc" attribute, except those that may + * be performing a _reallocation_, as that may alias the existing pointer. + * For these, use __realloc_size(). */ -#ifdef __alloc_size__ -# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc -#else -# define __alloc_size(x, ...) __malloc -#endif +#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc +#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) #ifndef asm_volatile_goto #define asm_volatile_goto(x...) asm goto(x) diff --git a/include/linux/slab.h b/include/linux/slab.h index 4fc41e4ed4a2..ac3832b50dbb 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -184,7 +184,7 @@ int kmem_cache_shrink(struct kmem_cache *s); /* * Common kmalloc functions provided by all allocators */ -void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __alloc_size(2); +void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2); void kfree(const void *objp); void kfree_sensitive(const void *objp); size_t __ksize(const void *objp); @@ -661,10 +661,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_ * @new_size: new size of a single member of the array * @flags: the type of memory to allocate (see kmalloc) */ -static inline __alloc_size(2, 3) void * __must_check krealloc_array(void *p, - size_t new_n, - size_t new_size, - gfp_t flags) +static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p, + size_t new_n, + size_t new_size, + gfp_t flags) { size_t bytes; @@ -788,7 +788,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla } extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) - __alloc_size(3); + __realloc_size(3); extern void kvfree(const void *addr); extern void kvfree_sensitive(const void *addr, size_t len); -- 2.34.1