From patchwork Tue Dec 19 22:28:48 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: 13499299 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 2F9DFC41535 for ; Tue, 19 Dec 2023 22:29:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4F456B009A; Tue, 19 Dec 2023 17:29:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D00128D000A; Tue, 19 Dec 2023 17:29:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B77B38D0009; Tue, 19 Dec 2023 17:29:19 -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 A0CEF6B009A for ; Tue, 19 Dec 2023 17:29:19 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6A9951A0711 for ; Tue, 19 Dec 2023 22:29:19 +0000 (UTC) X-FDA: 81585010038.08.1FFDFBE Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf29.hostedemail.com (Postfix) with ESMTP id D61A0120019 for ; Tue, 19 Dec 2023 22:29:15 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dzo5xM0U; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703024956; a=rsa-sha256; cv=none; b=n8AcK602tGdtzG2mz/2lZ1Xm56TOef2Tmj9wxA1xGttqnTaS2QAWv6Tdlek2IRYrZo2jG5 6dSN9moUiQEkApejYe8XtzuVa+qD875LZ6V8E/WLvE18PI+uM4763DX3X9AhGTZpt0Gi51 Ve1ZVqvpUAqZ6zd28S1wl6DI6C+r+2k= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dzo5xM0U; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1703024956; 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=W+GO4UdeS5AdNTnDLBObG20S1KoxsxGlKzUPAgkpjho=; b=iLHk6tK9VNihrZObXftGcPvPWoVki8XhZHEhRz/lw6lpVA8UmqJjfX9IAVUCA2tEFuSSZz SiTvGbdgmJ0R1qUCWxHBH4v22xKFBWFEzjhsmdVeE6J0a7Ey/fncMWR8Nd0+Hf2j7+IDfQ UIkz3j7SSgCT6TPbUZ37dvSdc3BnsLg= 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=1703024954; 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=W+GO4UdeS5AdNTnDLBObG20S1KoxsxGlKzUPAgkpjho=; b=dzo5xM0U129ISs8wP5QYvHL5occ5Ub/oL2lz1qj/FqHB9brhX1Gfjk4Uw9COo67ZEnW8ww KhbMK460okwgrIt1sc0CA8v8HSc2psAxv4a5/jCjrofVYNGNlcaua/Ot8Y8/UKGJ7gqOyM 9nt9J9X1zm2DTKxesXWV16ZmBqZDjO0= 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 04/21] kasan: add return value for kasan_mempool_poison_object Date: Tue, 19 Dec 2023 23:28:48 +0100 Message-Id: <618af65273875fb9f56954285443279b15f1fcd9.1703024586.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D61A0120019 X-Stat-Signature: nq8ds8caox4i6zc9umksozbjcsjkajpj X-HE-Tag: 1703024955-59970 X-HE-Meta: U2FsdGVkX1+KcRxei8/H4g4LFr0OEJDtWqIUEECm/kXTIcFDnrlx6Uy6o3NNMDEsMR7JySFgO9yGlxKayBq+eRv4bKbGADKmHa4fM4k/slmOWpf/J1epM89RaCtUEjaQ5k4KrauG1p0/vtnAz0IzRjJNYhAWst7sUZTFfrvbDxtPJ788OVVVPQ8lspoQyuH/00IEXSoXPRTbnM/YVepcFreJwU6BSF46gm1J/b8zTF9o+zV+3MS2zXu5T4bj/Y8i5PNfsF3SXzcrF72vC68ZMugsWDdZVGqC9Lw5UYgj6/SytrO69KyOVXyWdphbAwPps9nmdoOoKxpHd4TOK7dYzhEG5H9CPO3D/KWAIxjqNO2gZGbPB3MWeOaaTRRsPlZ/W2P0auLYM98/KnFMAhuPZOj3vbLYF9X3S5Mir/50uIihgoxzjJYDUafJwxFKWJf6ry4Y+8LVrLKwA6PFNBJw2LecmwNw/r9uncIUiUCBQR23zrgA1LsioFG4JrARkvU37EJrjIypfD8Un63QFn3akuPOJy2rl3+sfauNMU+MTOJKUOH26cH+JuOTTthclqHMDp6rZqbPrivBU/HokB3+VspzWGtt33Rdf33xrQv7Xt8TIlXQLJmRxQaUvMCQZ86Aa20M8Xn+pUCl2QoMZvVheMiydwCjqODcwATpPY6yNvpItahLtYUbfoSaqT2Re2sPgfnE1AmrWbakZT4jl3sUeWI9asEIRlfkyu1GGptxaxJg2ne4/6xoSTLJrweY/182GCfqy+zcBEIv2NrBwPqLhUL87ep3c0T2u1x6xFm3cvpu5Eyo1qefTOvg3/0Q/s4IxAq/1HYmOFMTW755KYOh9hE/68FEUdwc7uG2E0/FuGiK8Igzc3b8e+N1E9aUISalW0cBbXAwJJ4pSy0ycBzvv+S2wdIs7Z0bOujVSivmafaSz9VlLQKrMJ3ylgZrO16T8ldz0cFYaCEB4gfxD9V ZrpmhOYz HXPsyiMq0flr41hcjkOpa5yWYKjD/MzMabAXknSrMuKi/pIMSu7rxFStRz8wrtYHx2Q7JTu5ASCjf1cuQydIZY0Ee9kpsGEqkhOPCa5Bt/xS9Yu+Fb4AnP3gtKmUjZE77ocRJtEEASd40JJ/4VpJ6KGSGEmvVh3b4G9Baz1ciM4bfylBoHlZXi8/dDtCcErUN50uaA1GLrfXCj8mhyDCrhzKZZUQxwQwTqm1b 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 Add a return value for kasan_mempool_poison_object that lets the caller know whether the allocation is affected by a double-free or an invalid-free bug. The caller can use this return value to stop operating on the object. Also introduce a check_page_allocation helper function to improve the code readability. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 17 ++++++++++++----- mm/kasan/common.c | 21 ++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index bbf6e2fa4ffd..33387e254caa 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -212,7 +212,7 @@ static __always_inline void * __must_check kasan_krealloc(const void *object, return (void *)object; } -void __kasan_mempool_poison_object(void *ptr, unsigned long ip); +bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); /** * kasan_mempool_poison_object - Check and poison a mempool slab allocation. * @ptr: Pointer to the slab allocation. @@ -225,16 +225,20 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip); * without putting it into the quarantine (for the Generic mode). * * This function also performs checks to detect double-free and invalid-free - * bugs and reports them. + * bugs and reports them. The caller can use the return value of this function + * to find out if the allocation is buggy. * * This function operates on all slab allocations including large kmalloc * allocations (the ones returned by kmalloc_large() or by kmalloc() with the * size > KMALLOC_MAX_SIZE). + * + * Return: true if the allocation can be safely reused; false otherwise. */ -static __always_inline void kasan_mempool_poison_object(void *ptr) +static __always_inline bool kasan_mempool_poison_object(void *ptr) { if (kasan_enabled()) - __kasan_mempool_poison_object(ptr, _RET_IP_); + return __kasan_mempool_poison_object(ptr, _RET_IP_); + return true; } /* @@ -293,7 +297,10 @@ static inline void *kasan_krealloc(const void *object, size_t new_size, { return (void *)object; } -static inline void kasan_mempool_poison_object(void *ptr) {} +static inline bool kasan_mempool_poison_object(void *ptr) +{ + return true; +} static inline bool kasan_check_byte(const void *address) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index fc7f711607e1..2b4869de4985 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -254,7 +254,7 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object, return ____kasan_slab_free(cache, object, ip, true, init); } -static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip) +static inline bool check_page_allocation(void *ptr, unsigned long ip) { if (!kasan_arch_is_ready()) return false; @@ -269,17 +269,14 @@ static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip) return true; } - /* - * The object will be poisoned by kasan_poison_pages() or - * kasan_mempool_poison_object(). - */ - return false; } void __kasan_kfree_large(void *ptr, unsigned long ip) { - ____kasan_kfree_large(ptr, ip); + check_page_allocation(ptr, ip); + + /* The object will be poisoned by kasan_poison_pages(). */ } void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, @@ -429,7 +426,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag return ____kasan_kmalloc(slab->slab_cache, object, size, flags); } -void __kasan_mempool_poison_object(void *ptr, unsigned long ip) +bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) { struct folio *folio; @@ -442,13 +439,15 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip) * KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc. */ if (unlikely(!folio_test_slab(folio))) { - if (____kasan_kfree_large(ptr, ip)) - return; + if (check_page_allocation(ptr, ip)) + return false; kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); + return true; } else { struct slab *slab = folio_slab(folio); - ____kasan_slab_free(slab->slab_cache, ptr, ip, false, false); + return !____kasan_slab_free(slab->slab_cache, ptr, ip, + false, false); } }