Received: by 2002:a05:7412:d008:b0:f9:6acb:47ec with SMTP id bd8csp217290rdb; Tue, 19 Dec 2023 14:33:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHm8AQI6pvQvOymuNHtV0T9KAUkm7wdBMGpHkPtaw3qnfD0rFcVKuKHpJSYgqRG5HQEqhP2 X-Received: by 2002:a05:600c:5246:b0:40d:2d22:5a9f with SMTP id fc6-20020a05600c524600b0040d2d225a9fmr612999wmb.27.1703025184562; Tue, 19 Dec 2023 14:33:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703025184; cv=none; d=google.com; s=arc-20160816; b=APpSz7OKRFy6iIL29Hvu67CVci/gL7n45s2Fcz5GO3L2oKZMAWBun6ORf6k0VLLSVq UXa5c35EG+0fUkSkxPxjo2BhcjchLCETVohrTtFTW0PlsyAgffFQgCDUz/92qrd2XdHE m6AGvJJi5lrOWrmzID38DxLivvOpo1iy0s+EsFhX+U2QZW+JavWT8J2uMC/f1yE328FX qLHC7AbqFnQ7uTui7UqrIWfo5LS+JnL3HTk3nyxQIstV1ZucdkB6qawwk0OQdFLp1uQN 95gYmbmqLLMX1tqO8qj61pK+Tj1mdwPidtKHnu+dNluL0jwd/5/s/Idg5F270edFaLtE G79Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ZKMuCi1Fs+yi8i1iMj8jEYKxpwLo+oL8GkzWTmOTGSI=; fh=AIzsiqkKUtrB7QnM+XRgAblgvDINIC5pDvD0mg8EzrU=; b=c9CfyRFKe9dsSe0EQbMF9ecwi2n9KBuPMrnu/FCr4UV5iejd5wvG2vk4AaMu6WLULa 1eyLvOm+EvO7w71swihjLZZ6B6umO/91bbfeuX1bao8mz9hjg3wL0K8ljVa3W91rigc/ IKB4Rl6IAs1TGZcKzF9ptgUv+kpQZ4X74gw+ooBl1qarr+L/p5H6PSxvv+YjqRu4GBpB xg7gxXg1P36v6twBVvVGy7dyGAuUpdfGRmynCCungoFklzE0bHl1+5q/G5RsalyectFp Xwdasiz1Ex6T9aG2fldtIZ+xI0CYsDXW3MldHGfmYpVR/HgMFyD2kZzVUmMFy8qr3GQY IIuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=fbafFq1j; spf=pass (google.com: domain of linux-kernel+bounces-6054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6054-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q16-20020a170906541000b00a232eeeca11si3158025ejo.160.2023.12.19.14.33.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 14:33:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-6054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=fbafFq1j; spf=pass (google.com: domain of linux-kernel+bounces-6054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-6054-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 50CA11F25A1C for ; Tue, 19 Dec 2023 22:33:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 180A53FE4E; Tue, 19 Dec 2023 22:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fbafFq1j" X-Original-To: linux-kernel@vger.kernel.org Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BE023D0BC for ; Tue, 19 Dec 2023 22:31:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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) -- 2.25.1