From patchwork Tue Oct 25 23:36:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13019983 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0C00C38A2D for ; Tue, 25 Oct 2022 23:36:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 275BC8E0002; Tue, 25 Oct 2022 19:36:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 225BA8E0001; Tue, 25 Oct 2022 19:36:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0EF118E0002; Tue, 25 Oct 2022 19:36:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F009E8E0001 for ; Tue, 25 Oct 2022 19:36:29 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 719A340E7F for ; Tue, 25 Oct 2022 23:36:27 +0000 (UTC) X-FDA: 80061083214.01.26C86AF Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf15.hostedemail.com (Postfix) with ESMTP id 127EFA0037 for ; Tue, 25 Oct 2022 23:36:26 +0000 (UTC) Received: by mail-pj1-f52.google.com with SMTP id d59-20020a17090a6f4100b00213202d77e1so658651pjk.2 for ; Tue, 25 Oct 2022 16:36:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=AGtKXigXG9GEUy3d0vDj3qy0ftcRaftXZM1jOrI1Icc=; b=dNQjW5t3D8tfppJYPI4FhUKWrCRWqRl5vraJjM+XSqyyGfsQCqbtXk/IzGLlout0kL 9rC8n0q9VaV2x87PLzOOrnQFXpT1q5NfhXz+WExks9ljvBvt4Y1Cu90Uf2nkHbjZsdwb AFh/N2qcgtcQTg3qkkF3HuHldDD8YYah0PSSA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AGtKXigXG9GEUy3d0vDj3qy0ftcRaftXZM1jOrI1Icc=; b=oNQC49aW7x3U2i1P2bI9GDV2m9Ru1tzDYkHGjnHSK/SxVKpcQoHCMbH/ZRmwkFJsms BpB9wfSHDnKa+gLEK0uumoY+4Bc/RSkrP0vViKweHy5fOdxSWDulOHHdFUVU/7UifuB1 cREc1vTiAwVgh0/X0hbsPLbTHkRtUsgMrlEmgkt7pVTp1HwgKKhR8f0YQSwJSsQhPQc8 xQq9EoAMWuYaHwW5SEBbWseMfg1MLE1zREGs47qvo0S/Ug2XPCxXELz0o4wViAV2X33+ kVT18zcD7FiCa7wJt1Wczy6iUjqUSui1BodZfxwcy2MqoKWHHQkEn3UmaHDrlweJhuST +pzw== X-Gm-Message-State: ACrzQf2705Es0ojRnMD6/Ciz1QgliRfNTsjMJwBG136LIlm8LdY4Rmph qfJm3kaTOFXwtXkw0rOl+ylG3Q== X-Google-Smtp-Source: AMsMyM67e5J8KuTy6uusYenbNSudfaUf9vI6Vjeqp4+aQ9El3zzLt6CHVs8XnJ4OeyIXbHXZ2kZsMg== X-Received: by 2002:a17:90b:4c52:b0:20d:489b:5607 with SMTP id np18-20020a17090b4c5200b0020d489b5607mr883820pjb.40.1666740986031; Tue, 25 Oct 2022 16:36:26 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id j10-20020a170902da8a00b00186b5c1a715sm1697455plx.182.2022.10.25.16.36.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 16:36:25 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Andrey Konovalov , David Rientjes , Marco Elver , Vincenzo Frascino , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v3] mempool: Do not use ksize() for poisoning Date: Tue, 25 Oct 2022 16:36:22 -0700 Message-Id: <20221025233421.you.825-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2548; h=from:subject:message-id; bh=DzRLzcBQvhCrbv8dqqugqEjPL6g0B/ORdhfzQm5GdoI=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjWHL23XANz0fyOFVUEswQiVca2LWQjEpYhi04g7Qf yhp2li+JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY1hy9gAKCRCJcvTf3G3AJgJmD/ 0YsgEIllXxuFczQPOKT5OyXY2E91me/tQO8Z6Gu/9W2iMlsKclcMR9Rv4FAOtfHdgvDip4FLJOLJLc /CKVJKqpYcqOOgMNYRay9vB3WFaRROjtfbxaSRGuqYJaaKlAuMMN6SzsvGcYElMES2eZ5ehr1Z7JSv 0qXKGztFjZqHHEdCxKO4no9wkVm7H7jqcAFN8EnmfqFdTCmXdelGOjUP4QQaOG8HQzfIpTooL6O4lO GT5A3Z6HOoLpxQqP59yMR3MVnHEAMhrTnm2DtZjivUlnCGoTESBO+j6itl18bIgJyfTUIpGUbCAVT2 FNugjNDHkfvzwsObrx7H2wCEJl6IlvQhJ+3zUQD2RKOfhEhs59gKDmwWY2KvgPi7iIZLdkYG4iqYuC HdvUImlPGL34J+keBIuU5AqFua5IhXdCcbDkUuu21mtbAAjApTF5aEdGoxL7TMd1f/c/W+NHv1tIQI ZlMFK7WnxeUdJkvU2MdvWfmQQOQ8Bq4LOkNy5gVle6JSJiT8+rntb6gYRZPQ2F16KPELAwO41z8yRR pRowiBKTghX6N7f5WT7DJistKtwKeWYTPTU6rAQ39FNBx0X5jjLho1958siE1P41CmtPkIHp22AOlL sNopLfijMyInV7h6SkihdcGY+JLhZuH1blJP2c44Qzvf7Pb5St67DbTPx3mw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666740987; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=AGtKXigXG9GEUy3d0vDj3qy0ftcRaftXZM1jOrI1Icc=; b=rLUuFPLWhW2XSqZJEVut3tjcTNnMtTxkbt0OFIKjRe8Z6jpNDdOWmCc/37gnkF2JIsyYu9 rHvVc88XZm5QRQwD3QW9H9p+NZbBRjDcOd0NAGFH9/wicsMgzxAHvy1ukDmqGMVmjUwzLy E72KdJkmble1pOwiH4ZbejOD/hDABXE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=dNQjW5t3; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.52 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666740987; a=rsa-sha256; cv=none; b=aq9clhnULyi2ZVd8S5oFLeMiymg0k8rHlMofsJXXK+DgzBnzeg1OR+XZbP6OtmCf/KuOq0 EOySFd+ADyoFVVPEep07GO1Kbs0R+u7LSaD1nYzbenTNCO/vjh8+k4kteko81CYMMDyMte m88PgHrZC4LHSCN65YNb3oJ5mvGZzIM= X-Rspamd-Queue-Id: 127EFA0037 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=dNQjW5t3; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.52 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Rspam-User: X-Rspamd-Server: rspam10 X-Stat-Signature: c8m1tzoct1nsqqpn8xoeco9w9ppq96fy X-HE-Tag: 1666740986-445095 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: Nothing appears to be using ksize() within the kmalloc-backed mempools except the mempool poisoning logic. Use the actual pool size instead of the ksize() to avoid needing any special handling of the memory as needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE. Suggested-by: Vlastimil Babka Link: https://lore.kernel.org/lkml/f4fc52c4-7c18-1d76-0c7a-4058ea2486b9@suse.cz/ Cc: Andrey Konovalov Cc: David Rientjes Cc: Marco Elver Cc: Vincenzo Frascino Cc: Andrew Morton Cc: linux-mm@kvack.org Signed-off-by: Kees Cook Acked-by: Vlastimil Babka Reviewed-by: Andrey Konovalov --- v3: remove ksize() calls instead of adding kmalloc_roundup_size() calls (vbabka) v2: https://lore.kernel.org/lkml/20221018090323.never.897-kees@kernel.org/ v1: https://lore.kernel.org/lkml/20220923202822.2667581-14-keescook@chromium.org/ --- mm/mempool.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/mempool.c b/mm/mempool.c index 96488b13a1ef..54204065037d 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -58,7 +58,7 @@ static void check_element(mempool_t *pool, void *element) { /* Mempools backed by slab allocator */ if (pool->free == mempool_free_slab || pool->free == mempool_kfree) { - __check_element(pool, element, ksize(element)); + __check_element(pool, element, (size_t)pool->pool_data); } else if (pool->free == mempool_free_pages) { /* Mempools backed by page allocator */ int order = (int)(long)pool->pool_data; @@ -81,7 +81,7 @@ static void poison_element(mempool_t *pool, void *element) { /* Mempools backed by slab allocator */ if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) { - __poison_element(element, ksize(element)); + __poison_element(element, (size_t)pool->pool_data); } else if (pool->alloc == mempool_alloc_pages) { /* Mempools backed by page allocator */ int order = (int)(long)pool->pool_data; @@ -112,7 +112,7 @@ static __always_inline void kasan_poison_element(mempool_t *pool, void *element) static void kasan_unpoison_element(mempool_t *pool, void *element) { if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) - kasan_unpoison_range(element, __ksize(element)); + kasan_unpoison_range(element, (size_t)pool->pool_data); else if (pool->alloc == mempool_alloc_pages) kasan_unpoison_pages(element, (unsigned long)pool->pool_data, false);