From patchwork Tue Dec 19 22:28:56 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: 13499306 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 9B5F8C41535 for ; Tue, 19 Dec 2023 22:31:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B22B6B0092; Tue, 19 Dec 2023 17:31:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 362298D0010; Tue, 19 Dec 2023 17:31:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22A148D000D; Tue, 19 Dec 2023 17:31:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0D0C76B0092 for ; Tue, 19 Dec 2023 17:31:27 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CF87FA021D for ; Tue, 19 Dec 2023 22:31:26 +0000 (UTC) X-FDA: 81585015372.19.780C1C8 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) by imf12.hostedemail.com (Postfix) with ESMTP id D11BF40023 for ; Tue, 19 Dec 2023 22:31:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=r2ZaOIS6; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.178 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=1703025085; 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=g+O4MJvijsH/fxNfhEtO0ZT1bhv1NQvjMHnoD8zQplM=; b=u2cOhh8NqwgRBZXlhEt2aI/Svnz/LIhf/uZUvYtr6H6QbAqqXu3IBxwS8VBJNDt92vQ8Vk nib1BCBaP+m+LMCbP8q762BkHiIBLKEGBaDj2UKCQRsermsRpbF/0wnue5evR0e/ymsvjJ ISsIeb6l0bZ8o5DgbxvRdS0nV8BNi1k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703025085; a=rsa-sha256; cv=none; b=ksogYZRmQEAbLK42g35eIzpvzgNI6r1Sy/jYNEJ59rNVfupiTmD0YLQIj9l5qw4gddOmhS 3f0etQkWXyiAAhTtfyI6Em68ufxNk751J77kDx69qUdQHo/Li2dd3guh+Imhuwt1VGRWyh OqeCIJVj+x6EXdv39pdifAcybbNK3h8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=r2ZaOIS6; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.178 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=1703025083; 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=g+O4MJvijsH/fxNfhEtO0ZT1bhv1NQvjMHnoD8zQplM=; b=r2ZaOIS6ZMwbcRwAxJW59qk9nmH2ETUbfqjNUSa+35AQ7mxBRhFueCynEMDfgvcNnxrrtH sElLzMZVBT/HNZgRFD9eQQjvBsU+nFyNWnkw46nJ6bBbaBE5Zd1NMueq/it/uzQfhtXh75 B0otrG1zpXfsZNehE7M3FBuN9mYYkzY= 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 12/21] kasan: save alloc stack traces for mempool Date: Tue, 19 Dec 2023 23:28:56 +0100 Message-Id: <05ad235da8347cfe14d496d01b2aaf074b4f607c.1703024586.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: D11BF40023 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ac3y9bhj83yyumettk5rxxqeub4d4zof X-HE-Tag: 1703025084-164221 X-HE-Meta: U2FsdGVkX1+TScNZoW6x+PXOq2TUQQZppUc7qBARq2Gh6/6si5XFSaVE0kLHI3YR0AwYmDKVEyheo2zVyQV/aWuqL+GJnSTyhXIAURbzb++YD7xkO/nXKiY48KDt2gUbIV7UQEbGmdAfImPqakPgB51iGYHbIguq8ZG8K+veMTiyQXCxlXnNqYtJvGa2AQZzOkRUr4ChM0sXmawSf2thlIqzyOV2+0rP92pY1FmeH3uzDngO4hyRh5k3xvQTFoBrk/rJlnq8EtIisqA2XVs37SWWojK5crLJmsNgnDApe2wtmTcuQCGp8kjg1X508tTJD5Tdd4x+NM5yemOVkM2H+JNJMeTT2tPo9jGO2xK6Bp456WCmKT7WGBCN4zyNMELEt7mVmnJMGeUN8Lv06gdaqEuavaUXsDdLGNPaSwbKq7OSYd834/K0fovJKdUi5sxhm/aRHD/7WYei94N+s9dLoOl59ae024l8tng7lKB/tHbOFwB7nNJgCqHhOciwyg+1xYM8C35GHV06YsUrUYCwwCrkroEcDWxlzk0ur1z0FRxd3Hu9HPk1e6gTlc9pmKRUgUk7LVIrIih7EvHkT6tWqK4R/nHoAQ2MPOrrOvKUg0N3nGXEQp70DjjuxgHJeI67uiytAIHsv2M3Nlb0rJVgN1GJtsYAB4VBcWSwzzDDsUdAPFALq+wMKinQIYJH6b7CanfbwBYMG+UJsD4Xzr+V+ukATvVkNzv7NbzZtaP001wZqa1t1eopXC6DZLehM6WDgjhpdiRGmyRQ0J706SepvKnhLgNTohuo0yMPgz3d3oln2iHOHVn5e3l7owTeNP8soFP0M/48pcdKdMwpGzblH0fuqpWcbNJt/amN/ngeLiuG3yErZOIJjqEx+bx7oIPeGvuzrOUHJKdnvTB1lE8+r/qzxd/VQViX4201hp8F3KwQd+5eZwLMrPTpIQ63zx1oqDNrogX33JwPtzWfrw2 U6MYmh+m CXt2PvaYGkaSi9EBFOHyFIoZB45B4BqSriMq+ZqamnNizj58CSYosqyl3fHygxESm+r3XL2jd+Ort3W+947FQYaHHV/spgcelzgXCFuwaOAwqNiWi5OK49K0Zd7DP1cNafS2LU56i+XFgQjECox7BaJbwktfDUjm1JH3WfduukK077D8= 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 kasan_mempool_unpoison_object to properly poison the redzone and save alloc strack traces for kmalloc and slab pools. As a part of this change, split out and use a unpoison_slab_object helper function from __kasan_slab_alloc. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 7 +++--- mm/kasan/common.c | 50 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index e636a00e26ba..7392c5d89b92 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -303,9 +303,10 @@ void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip); * mempool). * * This function unpoisons a slab allocation that was previously poisoned via - * kasan_mempool_poison_object() without initializing its memory. For the - * tag-based modes, this function does not assign a new tag to the allocation - * and instead restores the original tags based on the pointer value. + * kasan_mempool_poison_object() and saves an alloc stack trace for it without + * initializing the allocation's memory. For the tag-based modes, this function + * does not assign a new tag to the allocation and instead restores the + * original tags based on the pointer value. * * This function operates on all slab allocations including large kmalloc * allocations (the ones returned by kmalloc_large() or by kmalloc() with the diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 962805bf5f62..b8e7416f83af 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -277,6 +277,20 @@ void __kasan_kfree_large(void *ptr, unsigned long ip) /* The object will be poisoned by kasan_poison_pages(). */ } +void unpoison_slab_object(struct kmem_cache *cache, void *object, gfp_t flags, + bool init) +{ + /* + * Unpoison the whole object. For kmalloc() allocations, + * poison_kmalloc_redzone() will do precise poisoning. + */ + kasan_unpoison(object, cache->object_size, init); + + /* Save alloc info (if possible) for non-kmalloc() allocations. */ + if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache)) + kasan_save_alloc_info(cache, object, flags); +} + void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags, bool init) { @@ -299,15 +313,8 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, tag = assign_tag(cache, object, false); tagged_object = set_tag(object, tag); - /* - * Unpoison the whole object. - * For kmalloc() allocations, kasan_kmalloc() will do precise poisoning. - */ - kasan_unpoison(tagged_object, cache->object_size, init); - - /* Save alloc info (if possible) for non-kmalloc() allocations. */ - if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache)) - kasan_save_alloc_info(cache, tagged_object, flags); + /* Unpoison the object and save alloc info for non-kmalloc() allocations. */ + unpoison_slab_object(cache, tagged_object, flags, init); return tagged_object; } @@ -482,7 +489,30 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) { - kasan_unpoison(ptr, size, false); + struct slab *slab; + gfp_t flags = 0; /* Might be executing under a lock. */ + + if (is_kfence_address(kasan_reset_tag(ptr))) + return; + + slab = virt_to_slab(ptr); + + /* + * This function can be called for large kmalloc allocation that get + * their memory from page_alloc. + */ + if (unlikely(!slab)) { + kasan_unpoison(ptr, size, false); + poison_kmalloc_large_redzone(ptr, size, flags); + return; + } + + /* Unpoison the object and save alloc info for non-kmalloc() allocations. */ + unpoison_slab_object(slab->slab_cache, ptr, size, flags); + + /* Poison the redzone and save alloc info for kmalloc() allocations. */ + if (is_kmalloc_cache(slab->slab_cache)) + poison_kmalloc_redzone(slab->slab_cache, ptr, size, flags); } bool __kasan_check_byte(const void *address, unsigned long ip)