From patchwork Tue Dec 19 22:28:58 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: 13499308 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 CD39AC46CCD for ; Tue, 19 Dec 2023 22:31:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2069F8D0011; Tue, 19 Dec 2023 17:31:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B63A8D000D; Tue, 19 Dec 2023 17:31:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F24048D0011; Tue, 19 Dec 2023 17:31:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D368A8D000D for ; Tue, 19 Dec 2023 17:31:27 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B52A51C0C7D for ; Tue, 19 Dec 2023 22:31:27 +0000 (UTC) X-FDA: 81585015414.11.F65A4AA Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) by imf02.hostedemail.com (Postfix) with ESMTP id 28BD480014 for ; Tue, 19 Dec 2023 22:31:25 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=fbafFq1j; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.171 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=1703025086; 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=ZKMuCi1Fs+yi8i1iMj8jEYKxpwLo+oL8GkzWTmOTGSI=; b=5vR0NgrVcg/TJMGd7APM9gVoaIuBOHDGg8qXz5RMZhx6hBMYzot2qpSOZbVtxWu2gK1rWK Evf0D8puQ822gMphbW7u7LRAjDFD0U/eemwjl9BdivJgaf1dp4yerZwXV7nLQ06+7hQbOO FrNGMlmMoLm2EjNoDVAwO7utT515hGg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703025086; a=rsa-sha256; cv=none; b=uYzfyVtSSzPC68yfh3FQlqR/JJQjbpbFuvYDtYHc7W+6vD2M/3AGxHBjuitzW79KEiA/sp yng7IOnWXHY2eq1BSRps9uSK7wLw7XI6ct32FDh8PDrHrRcYKEHTW9fDSOamjF4b2wq+sG ApTeRy+yMs818YUGATQgz5Yy8Sey8ME= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=fbafFq1j; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.171 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1703025084; 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=ZKMuCi1Fs+yi8i1iMj8jEYKxpwLo+oL8GkzWTmOTGSI=; b=fbafFq1jlKC1cw8l0es+kRgSLJMag+jZz1zaVwws6qJ/XwEE/QAU3b2QRQ4d4gY3ik5ERb tj3mXVFI3saO1VRCl/G6F+6cQtebPvlWXWmXRbKaD42JplnE469NR+x6bSXKkfuSwR+j0Y wmoNA/R34bjTYOY+bjg9F2pqKV9n4xU= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Breno Leitao , Alexander Lobakin , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm 14/21] mempool: use new mempool KASAN hooks Date: Tue, 19 Dec 2023 23:28:58 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 28BD480014 X-Rspam-User: X-Stat-Signature: oej8uk34hdwe16wkfee15uxpcokffpph X-Rspamd-Server: rspam03 X-HE-Tag: 1703025085-398751 X-HE-Meta: U2FsdGVkX19WRcSGCLOsKz/6tSGCUOoB7p7t0FkERnxy0PtIQvhE8Iu0k9cOAJqEN0tQ40He49HyzBnmt9/pOCC+i4bmWy3lmuDZT8QO3eyTDM+VfAVPq/tD87bI3xWSgFBegDkA0aFyhti8Xhd7VExtMtDbC8oAc0RA7BB0KjJZwXN11dQtAPlKclu11lMKqIzwaFGCEdU3AjELsCgXLC4U8YE80TJSl9xERfnpKbRDIuJtJLet+FDQCCL62GG68EnO3lkPCqWQwZvMiOt+uVif2WcQTdikTbhSTBob4obGDn61lXEejp+JU5CvxQNmk+AEIrSSelT+1es1jSe9rBX7L9qF9nxeoluo59bks1jj6vmfK2VKN8ug/eAu563i5IMgyrBAfDmrV3AZZtgN/eVKWldZJuhZdoZ5eDYr65WgbKp9flT3m6gUMNYL7ehH4g9LSgAUjgFHpDUraR81zJCRmFzOG2O/EFl93kkz1CAPBIULaAA7+eJZxTm8bgyeoDMPIoc+J2S5wWq2g1u8gR4wMF/k4QuLnz0mzp8BpOSGXUtodiLZgB3qKMmnCaSAkHmI2Zy1RMKp67Qkczi5pLQ7kELHKPAdUich/oQkjGqQAUtZVdXu7jdcayg5tSRNKbz0NM0ahk9/9zpWML99NwikNxzCmEjxtuOxLzNv/PR47GGabI3q/f7N2L1a8jSQ75a7XaW/gYgUmK+A4E7jmVl+oI/MeYxHRgzGR/dbIR4Hp3Kve9MNDk71UOwC7eDpK8oMS7BRhEeKEH3GJfr8y0XbuS5NGcxWJ4N0wHdSOX8lOE+bugg9iV5sGBWsyV1Ufy1r92E9VCrPkliVRloESgQKHUnM9GanDRd81FXWksM9A/liHaXfd6QLemAe1rz0yq0wV3bkVzfOJU7DD0Odg+CdqX0CaKM4 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 1fd39478c85e..103dc4770cfb 100644 --- a/mm/mempool.c +++ b/mm/mempool.c @@ -112,32 +112,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)