From patchwork Mon Nov 6 20:10:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13447461 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 D719EC4167B for ; Mon, 6 Nov 2023 20:12:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C780E6B02A1; Mon, 6 Nov 2023 15:12:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C27FF6B02A2; Mon, 6 Nov 2023 15:12:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A82AD6B02A4; Mon, 6 Nov 2023 15:12:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8E7796B02A2 for ; Mon, 6 Nov 2023 15:12:48 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 899881208CF for ; Mon, 6 Nov 2023 20:12:47 +0000 (UTC) X-FDA: 81428627574.27.26F9CF4 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) by imf16.hostedemail.com (Postfix) with ESMTP id C4248180012 for ; Mon, 6 Nov 2023 20:12:45 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Se8wvGtj; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.177 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301566; a=rsa-sha256; cv=none; b=MpGaGa9/kPpGA0GRYkHnoOwHpq3RsDxsyiOzx4dcFSYVdEpKYOSkJZMQ7t9gFe2oHP+Q/U pxmJuTdYDFunG9Q0XELA7sBZSHm8IIa1hnNbdMKPbd0A1orPJsPqq7YvF++sLJcPbrqCPs 3S2d2dZ3LLYmRK4G6vT0sOkOZi5g8yw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Se8wvGtj; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.177 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699301566; 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:in-reply-to:references:references:dkim-signature; bh=Y7WYl7BvMbHXvuo0GG2Dw3ttgTpXyQGpBAxBpCO6Yug=; b=YwMbL5AQJsE1Fj10XxzCu13H1SGeN6qhKAtXCo3wRwMchIwr5+BisCcOfiwvlDcdzETTNW di3CyGK1gnoeqdFpv0joLux4YXOgDSEzbt4NOXBRv2Wo3sDZQI1JTH89FWP1AUVj39Jnzv NfpqpJZuEqd6kD74vXEid8G2wW2Q26U= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1699301564; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y7WYl7BvMbHXvuo0GG2Dw3ttgTpXyQGpBAxBpCO6Yug=; b=Se8wvGtj/WVgiMvB7sZyg/heScqMLOAwYAwTo7zIDTlOs7/E4MpQ3WkYM5OZWwaEklWfOa 6LljcHgyM247RHb2O+0uPsJYfuEgHjfHFdFXD7jDjGFaAlpmIDR4Qn3spNLTA9EIg7y9u6 nCPJ9UBSsabPf9Y6+LauRsZG7NRpK20= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH RFC 13/20] mempool: use new mempool KASAN hooks Date: Mon, 6 Nov 2023 21:10:22 +0100 Message-Id: <35771e9e5fc0fe2169c59f190fbd6bfc901b7c09.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C4248180012 X-Stat-Signature: 767ubw1ym3ue48ymb9jp33mzqg6eut7n X-Rspam-User: X-HE-Tag: 1699301565-95316 X-HE-Meta: U2FsdGVkX1+b2I+CSYZj6CZzi/kZyl+EPTVApWXIy74hbMqjqRHTKme0agAFZDlCGmgxOe01LV4p3d4oCjYEifEppVP2mAQ+jaC3jF/mgIHqizOZ+zMSzGucwzY36EDFRf4pmierLX1zd5KJ8SF8Q9uIKOnAY2LwXrld4AgCUXYb/0FuWNRYo3bhcecHCbxW62Do4BDw4oLPAkO5shKGwNjkbhtatBSIyGWA9CUTzPJcvmnHD6BMUjyvGftLYR/pq1EKHgk2tDiTKZRGF3+QqRmBcX7MaWBxrFWF6MnIHIAbg2oIl8a/VTgGgHCLbLJZOcL6KRV5WbEkmT/9dTdVbyrGYUYOx72T1OflL+lUTkyDmwbvxs7z66PekY8afUx3ETGwCttDjsycfyFdy/fTomowgdMJYvh1OOi6xeveei8UBOn9dc9o6WErDZz6RTTmN3K2lXnN/noCsckjoU8BbD1CbOzlFxmMnjhQmnpZXwpIk2doM9A4Y4VYHVvZLaScrBudbjr/10AbiBovvjNafxtYCjoIoqE1487vxpt9kwK5QelfpcKOoIU4w926ifGCT+EvPBFQnfFzZeksh+TgvbXMnLwIEfzYNRFCO+EHUiykC5wHRB+/JgYeTOBjfV5MCVDci5fyL3HFR/SOR7Isf43QPsqPCXvWKTvamnG728zmct06Vtevz9ouwmQ2MS1ssHS/0BCz0iXLHardiNUz/MJ3o9/xFBGpo7JLHm9flXDXYYrOKvvZZhpiGS6+47PjTRnGXqZG5dEcIjvt0ehlOI8NgBOpVnSCRtckbJFPkVcgkPDkTADAFFucV+/wTDokKQDrpceB2ovMsDD0RcQ9/31Wm0tVDUbV03ff2Erhmc6dAhYDpjojrqlZoa0Rsa0ePpMAx9FAattQZLPU5AzvBC7pPPBVRX7QPD46p8mNSxGm3ZBoMGFPHOCjFYhHTFoDnuVfgsX2tSrEQajEg6C v1Pt+1Yv gRbqI9TzGYYGSlvYAKwsn0i0CYmiQf1UT6ePPd12CsH/oToTd2QDIjl/tW+R5vLoTBQs8x/b5drTmWsp31VCDB2fRvSowIEkgqHUtaog3Q79eL9MEtk6bV5HebonTw4ilwHB5WYFcZTg5Qx6nDpvf67JwrA== 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: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Update the mempool code to use the new mempool KASAN hooks. Rely on the return value of kasan_mempool_poison_object and kasan_mempool_poison_pages to prevent double-free and invalid-free bugs. Signed-off-by: Andrey Konovalov --- mm/mempool.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/mempool.c b/mm/mempool.c index 768cb39dc5e2..f67ca6753332 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -104,32 +104,34 @@ static inline void poison_element(mempool_t *pool, void *element) } #endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */ -static __always_inline void kasan_poison_element(mempool_t *pool, void *element) +static __always_inline bool kasan_poison_element(mempool_t *pool, void *element) { if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc) - kasan_mempool_poison_object(element); + return kasan_mempool_poison_object(element); else if (pool->alloc == mempool_alloc_pages) - kasan_poison_pages(element, (unsigned long)pool->pool_data, - false); + return kasan_mempool_poison_pages(element, + (unsigned long)pool->pool_data); + return true; } static void kasan_unpoison_element(mempool_t *pool, void *element) { if (pool->alloc == mempool_kmalloc) - kasan_unpoison_range(element, (size_t)pool->pool_data); + kasan_mempool_unpoison_object(element, (size_t)pool->pool_data); else if (pool->alloc == mempool_alloc_slab) - kasan_unpoison_range(element, kmem_cache_size(pool->pool_data)); + kasan_mempool_unpoison_object(element, + kmem_cache_size(pool->pool_data)); else if (pool->alloc == mempool_alloc_pages) - kasan_unpoison_pages(element, (unsigned long)pool->pool_data, - false); + kasan_mempool_unpoison_pages(element, + (unsigned long)pool->pool_data); } static __always_inline void add_element(mempool_t *pool, void *element) { BUG_ON(pool->curr_nr >= pool->min_nr); poison_element(pool, element); - kasan_poison_element(pool, element); - pool->elements[pool->curr_nr++] = element; + if (kasan_poison_element(pool, element)) + pool->elements[pool->curr_nr++] = element; } static void *remove_element(mempool_t *pool)