From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42DC9C4332F for ; Tue, 1 Nov 2022 22:35:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231146AbiKAWfE (ORCPT ); Tue, 1 Nov 2022 18:35:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231282AbiKAWdZ (ORCPT ); Tue, 1 Nov 2022 18:33:25 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 708C7E14 for ; Tue, 1 Nov 2022 15:33:23 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id m6so14804427pfb.0 for ; Tue, 01 Nov 2022 15:33:23 -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 :message-id:reply-to; bh=TR4DpXpArHwTB3Q/Fu/HeOb4zcIBa/dUESXAom+m+r0=; b=XR/R95kGIa5eEkN76ZwYFuGmF701nv7uoFFFHP2TDaZ1ZmYYGsHVPdtxhWMxyL/WIi nCK6B5wZeapBAY98ZRSshrqH6a9cTH0OVkAfTMWjC1cvjhFcFP4YA0gCgsYYWTHaaWCN e1LQ8yAUTs+wD/EXihacl/voB7iSFt12/H44M= 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:message-id:reply-to; bh=TR4DpXpArHwTB3Q/Fu/HeOb4zcIBa/dUESXAom+m+r0=; b=vEAanmQD0J9o5eeUYFX0nKu+QHS85wdUx7WJ1HOHFrZ8vM5Zn9p+17N2usOmpvxnZK ioygxbvSrKW6U1PNFsrjTkhF3/SAuwqrkrNaNSANBucVYOnUqBh74A/4IzNXX8hZLB9U YQgh13bNXnI/uAlC5AU3rvlRSG6yYPVDxpxfs0/L6Pm4abiIi7jn11zHB5NEWRX9SkEe 9w6FjSKQpJJADrShvwZta/fxgYHIOp15fJ/XXlRmjTyArevHif5E8lQrIXCLO2voA1yJ XT23sQoaKeilaGQEKJ+q48VtunvPpJkA5s8mqbIXp0mOGXuS8P7nIREJP55SVrTf3pjd ELzw== X-Gm-Message-State: ACrzQf1Vy9V1xtRJcoElfTsx/pHX3dS1FxfYWKHJo5pxzoh6TN2cJkZe Zg83P9ZB7pKWgWSZB2u9mjf0dQ== X-Google-Smtp-Source: AMsMyM4g9RheqgMMXT+kdLVomwlfmHmSq5aYyTAxpX3GmEp3SjBAvxDOTWRc3EMBzPy3MDlV+aTaZw== X-Received: by 2002:a63:ed58:0:b0:439:b3a:4f01 with SMTP id m24-20020a63ed58000000b004390b3a4f01mr18942728pgk.327.1667342002870; Tue, 01 Nov 2022 15:33:22 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x6-20020a626306000000b00565c8634e55sm7000554pfb.135.2022.11.01.15.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:22 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, David Gow , Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 1/6] slab: Clean up SLOB vs kmalloc() definition Date: Tue, 1 Nov 2022 15:33:09 -0700 Message-Id: <20221101223321.1326815-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2026; h=from:subject; bh=qLerDmEzne6lpaq4KVx5bE//rWAPpMJFdcLBFfnifO8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6pLIVN1gPOfxNyEH/PkysO24BYL1rHsOiqB1i7 kWFfzeSJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqQAKCRCJcvTf3G3AJrdeD/ 4sAhVz4+NctD5DHBg37o6IgG9K0ySMSyZK6zpStQkAyDq2iJBxfDehPUeOvWXCzi0t1pj4FcJKPKEm rmF3jK6fNN9l/YgIcfA2vsfbpcNlIPSz76g4AsJbeDB2h2+i26p17KgcqvewxqWVF4Q02iW0A0De+I 4oYzPaQ0m9NF1qhJPzxZB9e1GE6j8cUvXDSA9yLbZj0PnLyrlvlul4Tpx/08wQPiAy2Q86mQId55ff /tq9+XQQ5mebvYuakkhQ8FRzgDZNrrHrCvKLbkH5zqkJ3MoF80v8rTPNBDSZW3gaE/GUQPW6TfUgaC mfU+pPrGsMSsz0a89B6UBppmirX9+bt1FanpHkvJtGGLFO2CtoLnQQJKHQXiejPbzpvRidg5bO6kZg HH3p7tMaxy7agCjUaLN9ztuvyRTKe2+kZKOKUteAq4qnEeDqxU3aRvK1APufm4pmniPE0Qn4aJOFxT 76rbAfdpsYv6dmYqpt4eBnuZExBOP6tUTbZIiONbnqnfttx/IAtoqJ4N9LP509HXUoW963HfvoAle+ +DdkpOwzNyjyT+eMv3R7i1ltBDcWj+sUV13ARcRXfpT3ib9UpkLn69A88U3E1PXmlAx6rsGsfIv/Gd OI5HWzey0ZmhYUdvHowXft3iJ8J2zccRoZT2HYEYNpBvBi4hP8I/X6mDxy4Q== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" As already done for kmalloc_node(), clean up the #ifdef usage in the definition of kmalloc() so that the SLOB-only version is an entirely separate and much more readable function. Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slab.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 90877fcde70b..e08fe7978b5c 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -559,15 +559,15 @@ void *kmalloc_large_node(size_t size, gfp_t flags, in= t node) __assume_page_align * Try really hard to succeed the allocation but fail * eventually. */ +#ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t fl= ags) { if (__builtin_constant_p(size)) { -#ifndef CONFIG_SLOB unsigned int index; -#endif + if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); -#ifndef CONFIG_SLOB + index =3D kmalloc_index(size); =20 if (!index) @@ -576,10 +576,18 @@ static __always_inline __alloc_size(1) void *kmalloc(= size_t size, gfp_t flags) return kmalloc_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, size); -#endif } return __kmalloc(size, flags); } +#else +static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t fl= ags) +{ + if (__builtin_constant_p(size) && size > KMALLOC_MAX_CACHE_SIZE) + return kmalloc_large(size, flags); + + return __kmalloc(size, flags); +} +#endif =20 #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp= _t flags, int node) --=20 2.34.1 From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2387C4332F for ; Tue, 1 Nov 2022 22:34:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230437AbiKAWd4 (ORCPT ); Tue, 1 Nov 2022 18:33:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231292AbiKAWd0 (ORCPT ); Tue, 1 Nov 2022 18:33:26 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4CDCEBC for ; Tue, 1 Nov 2022 15:33:23 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id k7so5168811pll.6 for ; Tue, 01 Nov 2022 15:33:23 -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 :message-id:reply-to; bh=nltp5RJKJpJiy+lN1fjyj0tni6FBvjdhxkkPgl9tDbA=; b=gm5F1w4iALvkaMLVUfNT4doyFvvcUHI/RRJNOKifZ5C24lG0VjpTlUZ7Fq6t8XkpKj gqS+JLFVnIHH0zNQaQwjuoWbwEGtfO/BQaKn66WNxlVOE0VfuMNfQg95e/c9emk+sjhk T1jjHDpxnS5pxHyY/PoOzerTiEwjNzlYj77FY= 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:message-id:reply-to; bh=nltp5RJKJpJiy+lN1fjyj0tni6FBvjdhxkkPgl9tDbA=; b=6NK1ZLzEGwVz5BXkmzRbic0JqGqSsrt4rryq8DmWMulCBMq8XI7gho29ZOyuMmM+0f rCr1hNtXBIwcbIoc8N9xZvRPGqVbQARb+/GOLUrR8+FD8Nb+so6KslUYD4+Ygs0ixSxy nHNFZAJydrEMxeuIbbHpMfATj8eonUoN7/pQterohi1k2sK/L7towdPCqv9PjwgLveq3 c2epWqoXLdt5uR0j7DYm8uTNUSPFaFTOSEPq8adEnC3DXJlzl9iZUBmfYQUWFPVUPl4Z Acv/1cGbU0da1usINF5bm+R1zWSmOfMaPnmT4hTQ70DgvHNafn2tbag9Pya+aJ35n6Pc Zk3w== X-Gm-Message-State: ACrzQf3D6ZSyGe3rNtKwtEynhDcKLuUecTFA0c3CTyupehZovgF+xHKA 4z2U14ED13IfJfhjCM5MciwdNw== X-Google-Smtp-Source: AMsMyM7QCr77GngdG52tbkpTWR+ry5SrJoyzn5pEPMgQfu1HqHVFuUzm4y7P9gFfLfMKQ/X0910S2g== X-Received: by 2002:a17:902:ef51:b0:180:7922:ce40 with SMTP id e17-20020a170902ef5100b001807922ce40mr21648024plx.8.1667342003248; Tue, 01 Nov 2022 15:33:23 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p14-20020a170902780e00b00186b280a441sm6818215pll.239.2022.11.01.15.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:22 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, David Gow , Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 2/6] slab: Remove special-casing of const 0 size allocations Date: Tue, 1 Nov 2022 15:33:10 -0700 Message-Id: <20221101223321.1326815-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2371; h=from:subject; bh=Orq7dTL01awncjfej5/kmy0rhx2Dkjzjknkna6lKTxc=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6pSkAAJZnxJQ9xGB8j+DBQxCAiERH1Mj1nwMYm gw5gZNWJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqQAKCRCJcvTf3G3AJsm8D/ 0eLG4a2rjG7cnYY/B7BTI50H9t73pU5m3LGv6pIt6e1lw/3d+bA90FWRTEqFL6JQAqkpvbYMuLXnR5 P/4+X17Z9FLSyFqaVh5xQZw5HU2YoCy3Y2BYqBdXjPeUZDvoCyEhSj5A4Szpd5DhuU9/zxCpXpl21t G4M1fsuAQyC2PbBc0Gumphp8cMjF1+ZcskuaCZqEVBmgJYOSJTYql7Fg1GMnzYCvJ2woEGQ1IsIv8Y 2+NhAWv1nGvEOSzs3ry2w7jDcZvh4HFJMcjbg1FyXsvOBYfhkjpotYs6fB4J3yhksLZH8n15AeMiEO idAblEEIBtUfvgvz9o/5jxVbr/sR+eoz+L65byXBdzj8kGsOwlfKVCbT683coTpAMK+qms3pMrZ6cl WTyFGJwXytQH27WOq3c6NVsi3XCgtykkzfzW0jiaue05uaty3VzGTYe1VA+NpcqhWvvzBT2DyVBXT1 XuX+XpkfACWcL9gn4VXOjWKb+tfbxT+nle17naSGPO5cqt9JGNaOh5O0QVGt1rGVVguRP7lmHtMs/m VqSCQ5b6/Bfs5+3A8q1bqWGuFUVBpS7q5DtFAkpB5G8Wke3+wv/l4GTzoGgqIlD4r8FkL9H0fsgjp6 jahyLYHwgVUoupO2TQwC4V4TUtmKK/sQ1F1ChE/7Kn7fKxG5pgQeCOaBPMFQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Passing a constant-0 size allocation into kmalloc() or kmalloc_node() does not need to be a fast-path operation, so the static return value can be removed entirely. This is in preparation for making sure that all paths through the inlines result in a full extern function call, where __alloc_size() hints will actually be seen[1] by GCC. (A constant return value of 0 means the "0" allocation size won't be propagated by the inline.) [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96503 Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- include/linux/slab.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index e08fe7978b5c..970e9504949e 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -562,17 +562,13 @@ void *kmalloc_large_node(size_t size, gfp_t flags, in= t node) __assume_page_align #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t fl= ags) { - if (__builtin_constant_p(size)) { + if (__builtin_constant_p(size) && size) { unsigned int index; =20 if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); =20 index =3D kmalloc_index(size); - - if (!index) - return ZERO_SIZE_PTR; - return kmalloc_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, size); @@ -592,17 +588,13 @@ static __always_inline __alloc_size(1) void *kmalloc(= size_t size, gfp_t flags) #ifndef CONFIG_SLOB static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp= _t flags, int node) { - if (__builtin_constant_p(size)) { + if (__builtin_constant_p(size) && size) { unsigned int index; =20 if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large_node(size, flags, node); =20 index =3D kmalloc_index(size); - - if (!index) - return ZERO_SIZE_PTR; - return kmalloc_node_trace( kmalloc_caches[kmalloc_type(flags)][index], flags, node, size); --=20 2.34.1 From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD3B6C4332F for ; Tue, 1 Nov 2022 22:34:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231383AbiKAWeE (ORCPT ); Tue, 1 Nov 2022 18:34:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231294AbiKAWd2 (ORCPT ); Tue, 1 Nov 2022 18:33:28 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DED2AFD7 for ; Tue, 1 Nov 2022 15:33:24 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id d13-20020a17090a3b0d00b00213519dfe4aso283570pjc.2 for ; Tue, 01 Nov 2022 15:33:24 -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 :message-id:reply-to; bh=87RNyMS5uX8ym9d4i08lSZonwWmXQDb7t+1Mtox/7yY=; b=KPuYVSv0nSM3xzmtsA2iZPi4FbNUt2u+J40XlA1k1oQQpVJTS9b6QN9F44Up65E+Dy ZEZDhFfCcAcXwoR6dC2i7yVaDmDWk5ahJKFXf7F78lPa3a9PuAhXHM1Bu7aJ4wvSpABe tdFQi2tLp11aSMFI3sSj0s/g/VSKZK5PRes3I= 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:message-id:reply-to; bh=87RNyMS5uX8ym9d4i08lSZonwWmXQDb7t+1Mtox/7yY=; b=l4xhxTc3fUVn7r4f6b+ht4ZgqAh9V8lnpQrQkxyLS8GMs6+Y643QgexcXeGVf7cyyf W1GtnYAs2RvVmQRSxG7jL7JMTj54h0tSyEHCWGEP2mwEA+SLycz5FX0NVSusc4Lya2B7 L7hSWH2iyEXXyKbzzUFhRz/0u/fiWR5Vfnckdf7Ec+tm4crStfYsT/52MMv4CPTaus5n wnA7BTnZNv3cOy9qzOE+1TV7ouNckZ3PNopOxdAsWpoqAbDLqOP3y0LR1Gp8rWkDvdi4 Z1Bd920HBUdq2U4SFT1RvIJU1A06CfQfR/jJ0uUWqm3l3jmTXtcfC6w/kFC3vy6MAXdx D0hQ== X-Gm-Message-State: ACrzQf2kQ5+4mTBFdSHAhmfUl8AWOI7SgmWQSKFiudgk2Dg+n5QNDk1+ Az5jTNPFzGt/QR0B6JTWj1908g== X-Google-Smtp-Source: AMsMyM5nrJHbUGPB6olYPQsGSIKe4o16+BOFvZoWcvoBMUjk7mAVQmawGuJrbP4nRhkPmubegbnSrQ== X-Received: by 2002:a17:903:1205:b0:178:ac4e:70b8 with SMTP id l5-20020a170903120500b00178ac4e70b8mr21525995plh.52.1667342003962; Tue, 01 Nov 2022 15:33:23 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 11-20020a63134b000000b0043ae1797e2bsm6253514pgt.63.2022.11.01.15.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:22 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, David Gow , Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 3/6] slab: Provide functional __alloc_size() hints to kmalloc_trace*() Date: Tue, 1 Nov 2022 15:33:11 -0700 Message-Id: <20221101223321.1326815-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3777; h=from:subject; bh=7/m3dEaCfyTXz7DjZgXoExKzu8Xq2Kk2B9qgNF6RcGY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qEP2c2SjseVkvdeN6bkMUPKJVGJtbp1au+Zyw YWPCnLGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJrNyD/ 9AyySkd0dVZRcUAxeWk0ceyiENXBqnciPCjbF1LZnBp23ob+33YYuxYUgP9m4No9Ikisn0wDrMq0rJ c3TyrMW705BCcJ+9lbBCofVn9TIGFD7JhKd++KHwtef8oUTAG58EXkQbcVgVGvq1Jv4jNH+bpQlXOl uhQqH4GyghPppdIGRLTh1XjesN55zxz/jurH9h15oDDr+zxCyO6mVUIBQSriDgMRoDv1kCZL3G+URj gG83q7qxiVIu+QrRxph18SMAuz602+/uLDZZr8TnMuK89nUA4/MFj4l+ZbgRbK7ga+R9WVW8sVB3Wb JVOqkhmQtmMSEJLIoQGtkbobICmQcGtzzqUBlQ9VZuRc69Kw2bHAXxHsveb2DqRFUKWNYmfX13BVGn sJNfVCUb1x+Rv0TBIhjBTPgjNaQ7giL7KrbW5RgEfRIKNhi355QFPy6JI+EKjjucq/lT2r3cBQJZXE +7xvDIp6xphk+A+FKr9nyUA2O7eS5Ub0fd1g52rt2noSmGeymkatG8uhCVgENOxps0Gv42yGAByoKX 1WQu19FknjYAvQTr7LZdPpNkfdUfDNRGZNlMEjknJITtCJ4ztqyuKCnD6Lkm6RvZRLxVyacyW5cfRY F4Ls0x39ONT774Syx5nmWvPH8iYaMN8Ri5QDo8evxuWgJv8khtAVCf5MbUqA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since GCC cannot apply the __alloc_size attributes to inlines[1], all allocator inlines need to explicitly call into extern functions that contain a size argument. Provide these wrappers that end up just ignoring the size argument for the actual allocation. This allows CONFIG_FORTIFY_SOURCE=3Dy to see all various dynamic allocation sizes under GCC 12+ and all supported Clang versions. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96503 Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook --- include/linux/slab.h | 8 ++++++-- mm/slab_common.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 970e9504949e..051d86ca31a8 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -442,6 +442,8 @@ static_assert(PAGE_SHIFT <=3D 20); =20 void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __all= oc_size(1); void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_slab_al= ignment __malloc; +void *kmem_cache_alloc_sized(struct kmem_cache *s, gfp_t flags, size_t siz= e) + __assume_slab_alignment __alloc_size(3); void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc; void kmem_cache_free(struct kmem_cache *s, void *objp); @@ -469,6 +471,8 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node= ) __assume_kmalloc_alignm __alloc_size(1); void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) _= _assume_slab_alignment __malloc; +void *kmem_cache_alloc_node_sized(struct kmem_cache *s, gfp_t flags, int n= ode, size_t size) + __assume_slab_alignment __alloc_size(4); =20 #ifdef CONFIG_TRACING void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size) @@ -482,7 +486,7 @@ void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gf= pflags, static __always_inline __alloc_size(3) void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size) { - void *ret =3D kmem_cache_alloc(s, flags); + void *ret =3D kmem_cache_alloc_sized(s, flags, size); =20 ret =3D kasan_kmalloc(s, ret, size, flags); return ret; @@ -492,7 +496,7 @@ static __always_inline __alloc_size(4) void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, int node, size_t size) { - void *ret =3D kmem_cache_alloc_node(s, gfpflags, node); + void *ret =3D kmem_cache_alloc_node_sized(s, gfpflags, node, size); =20 ret =3D kasan_kmalloc(s, ret, size, gfpflags); return ret; diff --git a/mm/slab_common.c b/mm/slab_common.c index 33b1886b06eb..5fa547539a6a 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1457,6 +1457,20 @@ size_t ksize(const void *objp) } EXPORT_SYMBOL(ksize); =20 +/* Wrapper so __alloc_size() can see the actual allocation size. */ +void *kmem_cache_alloc_sized(struct kmem_cache *s, gfp_t flags, size_t siz= e) +{ + return kmem_cache_alloc(s, flags); +} +EXPORT_SYMBOL(kmem_cache_alloc_sized); + +/* Wrapper so __alloc_size() can see the actual allocation size. */ +void *kmem_cache_alloc_node_sized(struct kmem_cache *s, gfp_t flags, int n= ode, size_t size) +{ + return kmem_cache_alloc_node(s, flags, node); +} +EXPORT_SYMBOL(kmem_cache_alloc_node_sized); + /* Tracepoints definitions. */ EXPORT_TRACEPOINT_SYMBOL(kmalloc); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); --=20 2.34.1 From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2ACAC433FE for ; Tue, 1 Nov 2022 22:34:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231259AbiKAWee (ORCPT ); Tue, 1 Nov 2022 18:34:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231191AbiKAWd1 (ORCPT ); Tue, 1 Nov 2022 18:33:27 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEECCFE9 for ; Tue, 1 Nov 2022 15:33:24 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id v4-20020a17090a088400b00212cb0ed97eso274308pjc.5 for ; Tue, 01 Nov 2022 15:33:24 -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 :message-id:reply-to; bh=eQzGoj4PWhL+YVsgQJ5z9+/Kih3t/rfTwXU0TvQKFM8=; b=Kzs1G6hQ0kuz+t6Hyj2luEYUctJSzYNjr/RiQSddlkXMvlC8WZSqxbKBR2GxuqenLf OQUaP6LafEUW9QQSk2N1CHCFrlEna5vrjPlqMt/MY6K5NPsabM+UDg7xhMpyYJBvp/TB Qhr391tCSEJAxfKc6RXWCg9m4yKOpz8jet1NI= 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:message-id:reply-to; bh=eQzGoj4PWhL+YVsgQJ5z9+/Kih3t/rfTwXU0TvQKFM8=; b=vfgK1wstPB3zdNZNC1PLsaOudxBIV9Hf+6I1s36/5tiG5jqliin2ekTUT/sEqBlL3Y QpvjFgOeFJZZmiy8I78pAXZVy7I5A3XOwwMl7oYbvl1CaG/tIurBQsYiPo1STTRmYIkm 9qvA6xrxo/OkjssYcQ6KZRSK4DBlm0SoI2uUgUit8F0kBQUakvEvR51N1LJPHDkv9FRj KIZ/ON/nkrEP72jm1tdkGdXaJuJ2ElOiFy1RGI/oBee1HaImhbuipifyAV6dA1Uy5DrC QvTHf7j9NTpgGYIqAjad8XLBG/TP/6j/yv5GhyPLyWmODg88s7h1neW8TM8M87rL1IPg 0aow== X-Gm-Message-State: ACrzQf0FRLwKfTDG06hKm5vtwSlqLQ++tTHG4yQmA1XMavlnPAbNWOSP yet/oSTfp8clGAAXRy/6Fgr/Lf+8F/lBQA== X-Google-Smtp-Source: AMsMyM7TjTXBopAgHEeLlCi6VSVxIf33KmWZT5LrXPEUzyU+oej4mwrStX0PEDyrKovQWQA34pT/Bg== X-Received: by 2002:a17:90a:c38a:b0:213:119e:ebce with SMTP id h10-20020a17090ac38a00b00213119eebcemr22669388pjt.159.1667342004156; Tue, 01 Nov 2022 15:33:24 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id g16-20020aa79dd0000000b0056cd54ac8a0sm6979590pfq.197.2022.11.01.15.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:22 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , David Gow , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 4/6] string: Add __realloc_size hint to kmemdup() Date: Tue, 1 Nov 2022 15:33:12 -0700 Message-Id: <20221101223321.1326815-4-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2029; h=from:subject; bh=66bqcDCKUcRos1t3IlFIPskpfbQxNO3VtEaxc728EbA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qbleitpvvOHVivxE2GM3C7WRtYicICVcezr+K bBy/ZYGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJrxyD/ 0Sz5EBlX00TyOBKDue5S/TEPKu9FlK6tMshCI96Akt4QtwgM/FBaeIhUn5+WVlyWsSNnOkEiXOWNxf ymZEc2e4ip37GLLxvPOMgosfpIYt0RcwkaR7QQ9q+KHX2NzK3Nxk1DhkQAVNG7BC5wlkEBtUdWPK/b xdg34XDeb0ZVx1f1iL99s36MEkhWeztSoXoCLOVVT2v2r5dzGBa6j04E8oFt/2KV8VPZzsgvk59Wjo dZqvH0TOW5wKWLeN3AbHhRmhSArnFXiCvW7Gh7gwknSwVDGO6SQYWqUIvG8P26lPY504wXm6j7t08z xHujiAKshTjNBk4czRB5OEWIIZOquiBaCfcaWCKD3PPRU6YeAOv4RZ21DwgdjoVb9Pq9Dstq4lgy6W 9ue2fGR7chwod+xcu1qT4CUORf4DemZ2DfBaiUyrJZ2jq4pFoCJfLcfaRQO0iUCAuXWEc3ZC13q+oA kh0aDQbQMbsdRW2wcnl5gT8As3lVlFb7Sg7v/vbWHxtz5z9l3T035nWeFK0nf/PE1WY+gAfvSNGEX+ 5hcYyHvMmBj0lzQb2Ld+lBE9utZib3KNIy8hr+LNYpmVxxRpNJbcCr+SKpIbf2wSGg0ZZd0aGysEX0 L62xCGtjZMX4rSYWvhmXvBRca9taL7b603KSa7zEAwMFItsv7hzzN+y5tcyw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add __realloc_size() hint to kmemdup() so the compiler can reason about the length of the returned buffer. (These must not use __alloc_size, since those include __malloc which says the contents aren't defined[1]). [1] https://lore.kernel.org/linux-hardening/d199c2af-06af-8a50-a6a1-00eefa0= b67b4@prevas.dk/ Cc: Rasmus Villemoes Cc: Guenter Roeck Cc: Andy Shevchenko Cc: Paolo Abeni Cc: Geert Uytterhoeven Signed-off-by: Kees Cook --- include/linux/fortify-string.h | 2 +- include/linux/string.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 9e2d96993c30..aa31f54f8b57 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -670,7 +670,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const PO= S0 p, int c, size_t size) } =20 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENA= ME(kmemdup) - __alloc_size(2); + __realloc_size(2); __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp= _t gfp) { size_t p_size =3D __struct_size(p); diff --git a/include/linux/string.h b/include/linux/string.h index af1d69e5610e..db28802ab0a6 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -176,7 +176,7 @@ extern void kfree_const(const void *x); extern char *kstrdup(const char *s, gfp_t gfp) __malloc; extern const char *kstrdup_const(const char *s, gfp_t gfp); extern char *kstrndup(const char *s, size_t len, gfp_t gfp); -extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __alloc_size(= 2); +extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_siz= e(2); extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); =20 extern char **argv_split(gfp_t gfp, const char *str, int *argcp); --=20 2.34.1 From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CD8DC433FE for ; Tue, 1 Nov 2022 22:34:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231321AbiKAWeU (ORCPT ); Tue, 1 Nov 2022 18:34:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230388AbiKAWd2 (ORCPT ); Tue, 1 Nov 2022 18:33:28 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D2AC2192 for ; Tue, 1 Nov 2022 15:33:25 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id 130so14775170pfu.8 for ; Tue, 01 Nov 2022 15:33:25 -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 :message-id:reply-to; bh=gzM6J7UGnorFDlDysY0MD6P7BjaP+6EbrbAqldiRrKE=; b=OKuDa6YSbANP/NsNkIWjApa9dbOPZfAN/KtOhEl7y//1+lLuJ6c0t6cE9j/ifht6Vy 10XhU7TJYo8HMJE/VscMRVZgUI2flZNTZY+0daayeJydtHb94FxLwQJGgZ0qCpAcBHPl 7eIkUxKRPJyW+uxeOKrY9C5oPis/Bie6gRUZA= 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:message-id:reply-to; bh=gzM6J7UGnorFDlDysY0MD6P7BjaP+6EbrbAqldiRrKE=; b=aDfK9/yZTMH2AB8kyDYCYVkBXxC7ji49JXL8MK5npJqmZMEWw2w4MjMpUJg7q+c/ZG ER8Cs+esxnwckAgeTvTdUGioAbhH7p3bArimJ3Vj2DLGpUcQJMN+zdGKuas67lV8jgF+ 5+jzEMc/GlfsXW+UfGwX0Z98B2TcUioZrX2+5k2n/XagJa61TTL2QL2m01pj/twsdt7V p54UdV6zcxUoTSAj9ZJM6dfnueNbgYKzwa4FHOi2JbY8MFWul5yy8stxe65Pwse9HWC0 18JWa2cki9atAGihpTBDtt0lWjXBUZ5Gaep+89+BoMqWYI7INS/0xssNcxmPWWTUjvo3 85KA== X-Gm-Message-State: ACrzQf2u1/zncORSGazEAmVJALxIlkDLok77VS6o9NX5KpWWaSTAeEIX TkcB9+ycSNnVJZIjAnGcALMgSA== X-Google-Smtp-Source: AMsMyM5/UJCRTY42PEpY1Y0wufzzSsIn6sCjMrikkeh+nFyBBYr8szUuO8piqBRMJ8AUIXH3cwv2yA== X-Received: by 2002:a63:950d:0:b0:46e:d655:b377 with SMTP id p13-20020a63950d000000b0046ed655b377mr19259111pgd.191.1667342004776; Tue, 01 Nov 2022 15:33:24 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s7-20020a170902988700b001811a197797sm6778934plp.194.2022.11.01.15.33.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:24 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Greg Kroah-Hartman , Rasmus Villemoes , Thomas Gleixner , Jason Gunthorpe , Nishanth Menon , Michael Kelley , Dan Williams , Won Chung , David Gow , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 5/6] driver core: Add __alloc_size hint to devm allocators Date: Tue, 1 Nov 2022 15:33:13 -0700 Message-Id: <20221101223321.1326815-5-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2391; h=from:subject; bh=aUB9QFgSGPW6FN3jHA0HE05KQ7SqHyl2yIuilOndYc8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qr8iT9TeVIzVavaub9kYVPpos80os4w71z521 I5fSEqiJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJsI+EA CCoCqTQVECoE72alYcrcqBPY9Z20kQHjcfMq2clHkeoyFdKB7/Ud8kY1NoGi9q0m7Xda+lbswp0Jlt WBofqN/HFcFfrXp3FK8cKt+E6z83q0MQ0tPoIJ8VrbX4/wOJvdwTMD01BGiRQ0CbVNKJ7r2fSpRHS8 6wR/YVw5UWOYYryexrUjU3p7shnbVQ78CHVuSyCWWBAbvDwjqN1kQqmGB/xhqF30AtrLCJZeaXlMHI 6OlrqKUn+Izi77UJ7rrqoS6x931kwNHbO1+ZBj/lU9O5yZ7ff8PbhX3q5SJhLRKIVdaADnYGIqt296 LRbxLOfFTiDPd8DdumdGCzmXLZaUhS5Ef1GlGQJ9Ofz+SqZK6WjgYaw/RpelAbMhqf3Pa7NAUSRQ/T 0UZSTeVpU8AwIRrAhLh5s2zUneuHm2F3vZUdRjgBPdXjFW3Q8FsuKiDApeD5UShVJ5L64NL7K2I83F thVgiY7h7bGlpRRORua5rQEQLSEg4vsYiKvvUMbRLIuiacpWqT9b0zk2fLyBRESTF5/gChxZkF3d2S w2P/b+Zmj4VAqX324oLiSUkdASYfidRxN3jN1y+7w1rLl9h/VDvCFRZbbLicPvvMiNK9V6KClVIOZs yZWijWPTj3w0TMU+Cpgdq8ksSrwHICryAxiyTeUEHR3ni/aE+UAIMm7je+Vw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Mark the devm_*alloc()-family of allocations with appropriate __alloc_size()/__realloc_size() hints so the compiler can attempt to reason about buffer lengths from allocations. Cc: Greg Kroah-Hartman Cc: Rasmus Villemoes Cc: Thomas Gleixner Cc: Jason Gunthorpe Cc: Nishanth Menon Cc: Michael Kelley Cc: Dan Williams Cc: Won Chung Signed-off-by: Kees Cook Reviewed-by: Greg Kroah-Hartman Link: https://lore.kernel.org/r/20221029074734.gonna.276-kees@kernel.org --- This is already in -next, but I'm including it here again to avoid any confusion about this series landing (or being tested) via another tree. --- include/linux/device.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/linux/device.h b/include/linux/device.h index 424b55df0272..5e4cd857e74f 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -197,9 +197,9 @@ void devres_remove_group(struct device *dev, void *id); int devres_release_group(struct device *dev, void *id); =20 /* managed devm_k.alloc/kfree for device drivers */ -void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc; +void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __alloc_siz= e(2); void *devm_krealloc(struct device *dev, void *ptr, size_t size, - gfp_t gfp) __must_check; + gfp_t gfp) __must_check __realloc_size(3); __printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt, va_list ap) __malloc; __printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp, @@ -226,7 +226,8 @@ static inline void *devm_kcalloc(struct device *dev, void devm_kfree(struct device *dev, const void *p); char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gf= p); -void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t = gfp); +void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t = gfp) + __realloc_size(3); =20 unsigned long devm_get_free_pages(struct device *dev, gfp_t gfp_mask, unsigned int order); --=20 2.34.1 From nobody Thu Apr 9 01:47:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDCD7C433FE for ; Tue, 1 Nov 2022 22:34:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231168AbiKAWeZ (ORCPT ); Tue, 1 Nov 2022 18:34:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230410AbiKAWd2 (ORCPT ); Tue, 1 Nov 2022 18:33:28 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8B1B2DDF for ; Tue, 1 Nov 2022 15:33:25 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 78so14646108pgb.13 for ; Tue, 01 Nov 2022 15:33:25 -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 :message-id:reply-to; bh=ZgD6VkoidY1K6fgTV3eaPZ8+VeXqzeB8ij/KEzvrtPE=; b=Zw+jdonF3srm2oOPHvE/wciZkC1yJTpG3TlH5S3YyHIINs0uHGKyoAYf5vGGcyPEjk Yhw+0mU8iighIYZhu/NhquSMkNwGmNcRshoLuOHe0mxCTR+CYWlIgtW9rrBMmmmXlpk+ C8NLcj1NWzw+ZnaBV55BWXOspaUdrT2J2AQG8= 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:message-id:reply-to; bh=ZgD6VkoidY1K6fgTV3eaPZ8+VeXqzeB8ij/KEzvrtPE=; b=i/4Lxwj4LxLeP3WUK0bpcz+WWVNtecIpTFUAkmJuKtUVAbcFaNTIiPkPqgUqNoTXo7 hputs6jeTwcYuh3V2MjvqaqPkJ1MSX5irltFeSJWRbOQB3Quen1cVqBaOvXq0GmU42xp UfkGJBy4V4vqzC8zuMxZM8I+grrafjyUbHD23IWBTM8h3AbEzhJiUSRvgAWwky2sGmZM lDyWUZSsYxQCQqI4LHbuo/N24/R2gUZZ/PrqdSsCZQD7gVJktaBNY90JK2cNU+3uf8tB Dg5mamth2x3/XF1zi9BD5EthNuAlb9txiMLzel6jxFDmdqZyJ5oyL5fqB1ZtLNdOqv+d 0DGw== X-Gm-Message-State: ACrzQf2ZGl9wrzr6wbZ2Iw35+d8pmJHi1mqAa6fM88pqscp8IrW8NTcZ a/fkiTQBApn5DvXV+LhHW4jmIw== X-Google-Smtp-Source: AMsMyM7pxv2Lvo5vKR9F8rfeCTRMS0FxQ0HTHdi2PJZNgWTfKuMPs74vyBlEnyTTOhIdTXzsE6uwaA== X-Received: by 2002:a63:dc4a:0:b0:470:5e7:6bba with SMTP id f10-20020a63dc4a000000b0047005e76bbamr1285044pgj.143.1667342004961; Tue, 01 Nov 2022 15:33:24 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id w22-20020a1709026f1600b001708c4ebbaesm6735258plk.309.2022.11.01.15.33.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:24 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , David Gow , linux-hardening@vger.kernel.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-mm@kvack.org, llvm@lists.linux.dev Subject: [PATCH 6/6] kunit/fortify: Validate __alloc_size attribute results Date: Tue, 1 Nov 2022 15:33:14 -0700 Message-Id: <20221101223321.1326815-6-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=13607; h=from:subject; bh=wSAVvLhC49WrEsey+rzI3Mf2kVppLeoS2Xvz7/CIGX8=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qOpdEK8ymLzEQRofKPXlon+ENY5S/tTJEZj6i spotAHaJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJmAFD/ 90tYxqc7iT6OunNqTOrQkbn9mc7bapmfR+YkkyhSnAZKeDuHwRfN4p2kwOVaCGcHxgJwS9QQc9wsyJ MyEFyMTdlVu81h3ztDGPI/9oI7qLp0lVbh6hg573DsWPmYrVu0HvzMQA0mp3xtUtrWR5BdcCWkAgsr 8HhVuNTwFSTjLM8DGRnQtIq0FZ6EZ7/6gmcrfVAQ5HsSsYHxVZMp5t+6A5Op4Vf4EqOqSe0TGIAgSw s50gIlJL4hzs0PgV++sohnEJZuskR9o+Y7Rs7qjBsdY4l8BG2N4WB8CSwEtLSWSouiyXbt6aXW/49r 1gAVCV0/uf/LsQ3sMtXkYVEQb+zhut27bgLVDyppx/NJBPUBZLUMbi5vQFTedUG6cVAu/n5RIurbCz 9tVvHM1Q2SEqILXvxOTHjR4bjOIihBxNIV/hfitfkOn5+zMLAid9WcWqkZve2lxVeN+7zL5Wa5HICx J5U9cOj/rwWX1SabXm4vjCRGNjwL+qmd4TIDEDFunxhJU9lmqyA6eDMLFs7u+zz2Pw6T60dy9a3S90 7xUr0yQeXkNdEu2OKNWWbCK74B+jGqtFR9HIRbIFxPI4j8mFBiPUtM6hxahuJNWL/m4XOno+Y4/KKf +N4+iSiPB57uHYauGeWNsoToUSZlEtHpOVent5FYkbsQMY166Avk8ph3mWAA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Validate the effect of the __alloc_size attribute on allocators. If the compiler doesn't support __builtin_dynamic_object_size(), skip the associated tests. (For GCC, just remove the "--make_options" line below...) $ ./tools/testing/kunit/kunit.py run --arch x86_64 \ --kconfig_add CONFIG_FORTIFY_SOURCE=3Dy \ --make_options LLVM=3D1 fortify ... [15:16:30] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D fortify (= 10 subtests) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [15:16:30] [PASSED] known_sizes_test [15:16:30] [PASSED] control_flow_split_test [15:16:30] [PASSED] alloc_size_kmalloc_const_test [15:16:30] [PASSED] alloc_size_kmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_vmalloc_const_test [15:16:30] [PASSED] alloc_size_vmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_kvmalloc_const_test [15:16:30] [PASSED] alloc_size_kvmalloc_dynamic_test [15:16:30] [PASSED] alloc_size_devm_kmalloc_const_test [15:16:30] [PASSED] alloc_size_devm_kmalloc_dynamic_test [15:16:30] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = [PASSED] fortify =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D [15:16:30] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [15:16:30] Testing complete. Ran 10 tests: passed: 10 [15:16:31] Elapsed time: 8.348s total, 0.002s configuring, 6.923s building,= 1.075s running For earlier GCC prior to version 12, the dynamic tests will be skipped: [15:18:59] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D fortify (= 10 subtests) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [15:18:59] [PASSED] known_sizes_test [15:18:59] [PASSED] control_flow_split_test [15:18:59] [PASSED] alloc_size_kmalloc_const_test [15:18:59] [SKIPPED] alloc_size_kmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_vmalloc_const_test [15:18:59] [SKIPPED] alloc_size_vmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_kvmalloc_const_test [15:18:59] [SKIPPED] alloc_size_kvmalloc_dynamic_test [15:18:59] [PASSED] alloc_size_devm_kmalloc_const_test [15:18:59] [SKIPPED] alloc_size_devm_kmalloc_dynamic_test [15:18:59] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = [PASSED] fortify =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D [15:18:59] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [15:18:59] Testing complete. Ran 10 tests: passed: 6, skipped: 4 [15:18:59] Elapsed time: 11.965s total, 0.002s configuring, 10.540s buildin= g, 1.068s running Cc: David Gow Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- lib/Makefile | 1 + lib/fortify_kunit.c | 255 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+) diff --git a/lib/Makefile b/lib/Makefile index 77c7951c8cf0..d197079ef22a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -377,6 +377,7 @@ obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) +=3D is_signed_= type_kunit.o obj-$(CONFIG_OVERFLOW_KUNIT_TEST) +=3D overflow_kunit.o CFLAGS_stackinit_kunit.o +=3D $(call cc-disable-warning, switch-unreachabl= e) obj-$(CONFIG_STACKINIT_KUNIT_TEST) +=3D stackinit_kunit.o +CFLAGS_fortify_kunit.o +=3D $(call cc-disable-warning, unsequenced) obj-$(CONFIG_FORTIFY_KUNIT_TEST) +=3D fortify_kunit.o obj-$(CONFIG_STRSCPY_KUNIT_TEST) +=3D strscpy_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) +=3D siphash_kunit.o diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c index 409af07f340a..78acfdbda835 100644 --- a/lib/fortify_kunit.c +++ b/lib/fortify_kunit.c @@ -16,7 +16,10 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include +#include #include +#include =20 static const char array_of_10[] =3D "this is 10"; static const char *ptr_of_11 =3D "this is 11!"; @@ -60,9 +63,261 @@ static void control_flow_split_test(struct kunit *test) KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); } =20 +#define KUNIT_EXPECT_BOS(test, p, expected, name) \ + KUNIT_EXPECT_EQ_MSG(test, __builtin_object_size(p, 1), \ + expected, \ + "__alloc_size() not working with __bos on " name "\n") + +#if !__has_builtin(__builtin_dynamic_object_size) +#define KUNIT_EXPECT_BDOS(test, p, expected, name) \ + /* Silence "unused variable 'expected'" warning. */ \ + KUNIT_EXPECT_EQ(test, expected, expected) +#else +#define KUNIT_EXPECT_BDOS(test, p, expected, name) \ + KUNIT_EXPECT_EQ_MSG(test, __builtin_dynamic_object_size(p, 1), \ + expected, \ + "__alloc_size() not working with __bdos on " name "\n") +#endif + +/* If the execpted size is a constant value, __bos can see it. */ +#define check_const(_expected, alloc, free) do { \ + size_t expected =3D (_expected); \ + void *p =3D alloc; \ + KUNIT_EXPECT_TRUE_MSG(test, p !=3D NULL, #alloc " failed?!\n"); \ + KUNIT_EXPECT_BOS(test, p, expected, #alloc); \ + KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \ + free; \ +} while (0) + +/* If the execpted size is NOT a constant value, __bos CANNOT see it. */ +#define check_dynamic(_expected, alloc, free) do { \ + size_t expected =3D (_expected); \ + void *p =3D alloc; \ + KUNIT_EXPECT_TRUE_MSG(test, p !=3D NULL, #alloc " failed?!\n"); \ + KUNIT_EXPECT_BOS(test, p, SIZE_MAX, #alloc); \ + KUNIT_EXPECT_BDOS(test, p, expected, #alloc); \ + free; \ +} while (0) + +/* Assortment of constant-value kinda-edge cases. */ +#define CONST_TEST_BODY(TEST_alloc) do { \ + /* Special-case vmalloc()-family to skip 0-sized allocs. */ \ + if (strcmp(#TEST_alloc, "TEST_vmalloc") !=3D 0) \ + TEST_alloc(check_const, 0, 0); \ + TEST_alloc(check_const, 1, 1); \ + TEST_alloc(check_const, 128, 128); \ + TEST_alloc(check_const, 1023, 1023); \ + TEST_alloc(check_const, 1025, 1025); \ + TEST_alloc(check_const, 4096, 4096); \ + TEST_alloc(check_const, 4097, 4097); \ +} while (0) + +static volatile size_t zero_size; +static volatile size_t unknown_size =3D 50; + +#if !__has_builtin(__builtin_dynamic_object_size) +#define DYNAMIC_TEST_BODY(TEST_alloc) \ + kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() sup= port\n") +#else +#define DYNAMIC_TEST_BODY(TEST_alloc) do { \ + size_t size =3D unknown_size; \ + \ + /* \ + * Expected size is "size" in each test, before it is then \ + * internally incremented in each test. Requires we disable \ + * -Wunsequenced. \ + */ \ + TEST_alloc(check_dynamic, size, size++); \ + /* Make sure incrementing actually happened. */ \ + KUNIT_EXPECT_NE(test, size, unknown_size); \ +} while (0) +#endif + +#define DEFINE_ALLOC_SIZE_TEST_PAIR(allocator) \ +static void alloc_size_##allocator##_const_test(struct kunit *test) \ +{ \ + CONST_TEST_BODY(TEST_##allocator); \ +} \ +static void alloc_size_##allocator##_dynamic_test(struct kunit *test) \ +{ \ + DYNAMIC_TEST_BODY(TEST_##allocator); \ +} + +#define TEST_kmalloc(checker, expected_size, alloc_size) do { \ + gfp_t gfp =3D GFP_KERNEL | __GFP_NOWARN; \ + void *orig; \ + size_t len; \ + \ + checker(expected_size, kmalloc(alloc_size, gfp), \ + kfree(p)); \ + checker(expected_size, \ + kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, kzalloc(alloc_size, gfp), \ + kfree(p)); \ + checker(expected_size, \ + kzalloc_node(alloc_size, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, kcalloc(1, alloc_size, gfp), \ + kfree(p)); \ + checker(expected_size, kcalloc(alloc_size, 1, gfp), \ + kfree(p)); \ + checker(expected_size, \ + kcalloc_node(1, alloc_size, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, \ + kcalloc_node(alloc_size, 1, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, kmalloc_array(1, alloc_size, gfp), \ + kfree(p)); \ + checker(expected_size, kmalloc_array(alloc_size, 1, gfp), \ + kfree(p)); \ + checker(expected_size, \ + kmalloc_array_node(1, alloc_size, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, \ + kmalloc_array_node(alloc_size, 1, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + checker(expected_size, __kmalloc(alloc_size, gfp), \ + kfree(p)); \ + checker(expected_size, \ + __kmalloc_node(alloc_size, gfp, NUMA_NO_NODE), \ + kfree(p)); \ + \ + orig =3D kmalloc(alloc_size, gfp); \ + KUNIT_EXPECT_TRUE(test, orig !=3D NULL); \ + checker((expected_size) * 2, \ + krealloc(orig, (alloc_size) * 2, gfp), \ + kfree(p)); \ + orig =3D kmalloc(alloc_size, gfp); \ + KUNIT_EXPECT_TRUE(test, orig !=3D NULL); \ + checker((expected_size) * 2, \ + krealloc_array(orig, 1, (alloc_size) * 2, gfp), \ + kfree(p)); \ + orig =3D kmalloc(alloc_size, gfp); \ + KUNIT_EXPECT_TRUE(test, orig !=3D NULL); \ + checker((expected_size) * 2, \ + krealloc_array(orig, (alloc_size) * 2, 1, gfp), \ + kfree(p)); \ + \ + len =3D 11; \ + /* Using memdup() with fixed size, so force unknown length. */ \ + if (!__builtin_constant_p(expected_size)) \ + len +=3D zero_size; \ + checker(len, kmemdup("hello there", len, gfp), kfree(p)); \ +} while (0) +DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc) + +/* Sizes are in pages, not bytes. */ +#define TEST_vmalloc(checker, expected_pages, alloc_pages) do { \ + gfp_t gfp =3D GFP_KERNEL | __GFP_NOWARN; \ + checker((expected_pages) * PAGE_SIZE, \ + vmalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + vzalloc((alloc_pages) * PAGE_SIZE), vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + __vmalloc((alloc_pages) * PAGE_SIZE, gfp), vfree(p)); \ +} while (0) +DEFINE_ALLOC_SIZE_TEST_PAIR(vmalloc) + +/* Sizes are in pages (and open-coded for side-effects), not bytes. */ +#define TEST_kvmalloc(checker, expected_pages, alloc_pages) do { \ + gfp_t gfp =3D GFP_KERNEL | __GFP_NOWARN; \ + size_t prev_size; \ + void *orig; \ + \ + checker((expected_pages) * PAGE_SIZE, \ + kvmalloc((alloc_pages) * PAGE_SIZE, gfp), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvmalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvzalloc((alloc_pages) * PAGE_SIZE, gfp), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvzalloc_node((alloc_pages) * PAGE_SIZE, gfp, NUMA_NO_NODE), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvcalloc(1, (alloc_pages) * PAGE_SIZE, gfp), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvcalloc((alloc_pages) * PAGE_SIZE, 1, gfp), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvmalloc_array(1, (alloc_pages) * PAGE_SIZE, gfp), \ + vfree(p)); \ + checker((expected_pages) * PAGE_SIZE, \ + kvmalloc_array((alloc_pages) * PAGE_SIZE, 1, gfp), \ + vfree(p)); \ + \ + prev_size =3D (expected_pages) * PAGE_SIZE; \ + orig =3D kvmalloc(prev_size, gfp); \ + KUNIT_EXPECT_TRUE(test, orig !=3D NULL); \ + checker(((expected_pages) * PAGE_SIZE) * 2, \ + kvrealloc(orig, prev_size, \ + ((alloc_pages) * PAGE_SIZE) * 2, gfp), \ + kvfree(p)); \ +} while (0) +DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc) + +#define TEST_devm_kmalloc(checker, expected_size, alloc_size) do { \ + gfp_t gfp =3D GFP_KERNEL | __GFP_NOWARN; \ + const char const dev_name[] =3D "fortify-test"; \ + struct device *dev; \ + void *orig; \ + size_t len; \ + \ + /* Create dummy device for devm_kmalloc()-family tests. */ \ + dev =3D root_device_register(dev_name); \ + KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \ + "Cannot register test device\n"); \ + \ + checker(expected_size, devm_kmalloc(dev, alloc_size, gfp), \ + devm_kfree(dev, p)); \ + checker(expected_size, devm_kzalloc(dev, alloc_size, gfp), \ + devm_kfree(dev, p)); \ + checker(expected_size, \ + devm_kmalloc_array(dev, 1, alloc_size, gfp), \ + devm_kfree(dev, p)); \ + checker(expected_size, \ + devm_kmalloc_array(dev, alloc_size, 1, gfp), \ + devm_kfree(dev, p)); \ + checker(expected_size, \ + devm_kcalloc(dev, 1, alloc_size, gfp), \ + devm_kfree(dev, p)); \ + checker(expected_size, \ + devm_kcalloc(dev, alloc_size, 1, gfp), \ + devm_kfree(dev, p)); \ + \ + orig =3D devm_kmalloc(dev, alloc_size, gfp); \ + KUNIT_EXPECT_TRUE(test, orig !=3D NULL); \ + checker((expected_size) * 2, \ + devm_krealloc(dev, orig, (alloc_size) * 2, gfp), \ + devm_kfree(dev, p)); \ + \ + len =3D 4; \ + /* Using memdup() with fixed size, so force unknown length. */ \ + if (!__builtin_constant_p(expected_size)) \ + len +=3D zero_size; \ + checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \ + devm_kfree(dev, p)); \ + \ + device_unregister(dev); \ +} while (0) +DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc) + static struct kunit_case fortify_test_cases[] =3D { KUNIT_CASE(known_sizes_test), KUNIT_CASE(control_flow_split_test), + KUNIT_CASE(alloc_size_kmalloc_const_test), + KUNIT_CASE(alloc_size_kmalloc_dynamic_test), + KUNIT_CASE(alloc_size_vmalloc_const_test), + KUNIT_CASE(alloc_size_vmalloc_dynamic_test), + KUNIT_CASE(alloc_size_kvmalloc_const_test), + KUNIT_CASE(alloc_size_kvmalloc_dynamic_test), + KUNIT_CASE(alloc_size_devm_kmalloc_const_test), + KUNIT_CASE(alloc_size_devm_kmalloc_dynamic_test), {} }; =20 --=20 2.34.1