From patchwork Mon Nov 20 17:47:19 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: 13461835 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 64C76C197A0 for ; Mon, 20 Nov 2023 17:50:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 823AA6B0388; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 760396B038C; Mon, 20 Nov 2023 12:50:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 588EE6B038D; Mon, 20 Nov 2023 12:50:48 -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 3E53D6B0388 for ; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 166B0C0977 for ; Mon, 20 Nov 2023 17:50:48 +0000 (UTC) X-FDA: 81479072976.25.5B999B1 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by imf14.hostedemail.com (Postfix) with ESMTP id 2213F10001E for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DNgy9uYy; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 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=1700502646; 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=RvoTVlNH/9QJKGjG7ts3ES/7xJIaYyT0JIUdEPtk5OM=; b=lfrcZs/Y4I7oZVgDBUPd/dGAuSSqv3H/24ziVQShfY1sc+WCQFFkMjMXzKPoS1ttsonoxS NX5fv7II8xwiwfIfg/GnNEHF6TFdJ8Zs9fNG5UXlhxOUdxAu5IhlAEoa5ALiinq7pqIXlX NP49Fhawow8ad7dhGskBBrzbMqgWcas= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DNgy9uYy; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502646; a=rsa-sha256; cv=none; b=PHHbPjx54HNVEpgGRzSKKkiR5tRa2u2Tu6XtLIe8MDO/0lK+IWOiu35RDdEJAVOQS+4mX8 mzk2YwQ9txBtMnBodYITsM2b23Q8BI7PlcDoblDZ0unu4NPVlwYvBvIiu7nLS3hJ2XKdUp 4xfSSRs0iy6bucGWbrfrBEZjlxTcxEw= 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=1700502644; 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=RvoTVlNH/9QJKGjG7ts3ES/7xJIaYyT0JIUdEPtk5OM=; b=DNgy9uYyYrfck71UIDDMmeBlP2Bik/eXdRS59wwnt2UBuC9bsjPHLcrxPmB7E98KHzUlfS OnFiyTSIdvZJFlR6bvb819vsoHa3zDRv09EFK0vHzurXdv2QeiuJeuOu21i9cfXyy/okM9 r0Wi2UF4LVMIbr0H8VW9kEyPJyGdVBE= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 21/22] kasan: use stack_depot_put for Generic mode Date: Mon, 20 Nov 2023 18:47:19 +0100 Message-Id: <5cef104d9b842899489b4054fe8d1339a71acee0.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2213F10001E X-Stat-Signature: 3kp3mypr13cb5oqrc1ndy7c5jwbgegp7 X-Rspam-User: X-HE-Tag: 1700502645-759361 X-HE-Meta: U2FsdGVkX19swcpYf0H7BfUvAZF0tEN/dfKGjMukAbIEKCDvZa5xm10HkTzjVU+QlvUkdmCDY2WudoB2WwtLVcByaQR7F/TT/PlXw9aJpz9O+cbg/88I8yFGRoQD5VTI1iQBDPhAcmPBVPuY+0qYKepBbTF2MkRyn6KMLC+dalW5gni2eXXhTVcXKLv2yKy8MvPHwZHfyJCv/YABnFlfVbxfmfy7sX4RiNhPsME7jXScCwZ/ZML1DC3JsFLZnd5QTnk27RoOqeNRgyVWzTwd5qi528xPVqwQuLTAKW3iDzoWYk5qli7Y9e4wm5iMI26t+cXV9OJY4j77DhJzYwT5UF03RPU3AUZgUVQEmjNwrDvSRSDF1FZLPlBRdu9/bQtv/pV73tlHFAzibBHRpRcfU1NsZ6vFhb0U5RT5gyZeB+cJvoorSAJCY3XX12oW+OSVffQF39ac1VPE0pnIU7RJrbdRL3n+kLJJyXfvcuSvuov4It0Y5p7Ts2zBN5Xldvqo4ttO6dTsZbyrTJV0bAfta2fYMvrHKPJTpzfa8UsPbUhPvslMOdBci0TmQBXMKgdTNbmsStSzQRsNYhinqNvjua8D6lSKoR6E5n44Ak/LHP1jVhkwBnVkqltBMMFB5A5GXPxszfputltBubSxOGNiU6E+kNDSiOO4p0WoAR2xZkqGMncudvgj9Vg4kvr/IZx+Cfg2ht7xouSAyde7k7zv6oNDAzchRD46Mo3HS8n/rujPQZOo0pEHb027WTsxOf/3HDvTTSpDEkI/19eP23ExtmpB3uNjVllRxkuZS9jtI4RGBb3QcuoSpvSv3umWM4KWsvC830fmTOg1pdXzb9oiZbuNVMHBshCoM+PJ/iBQYogcBb/Emutk8KeNDcubLGISQjTlEwyHSlYh2MBgTMsp0juiYY5n0JoEYx2NKjaTKArXfLLASLxQwzMOI3txZwOayuwnw9PDs1AiiUQBJgs Bu+aRsEJ 5TsCnR69WuaPdDoVkwrkLKb8JJwIxugf9hOOqGWJRFw7Im+3XD39tuyHUcRrRql2A5BdEt7XJzLIfgIQdmW4eeDCITMXR4lX/08MC8mjSGEZEPfE= 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 Evict alloc/free stack traces from the stack depot for Generic KASAN once they are evicted from the quaratine. For auxiliary stack traces, evict the oldest stack trace once a new one is saved (KASAN only keeps references to the last two). Also evict all saved stack traces on krealloc. To avoid double-evicting and mis-evicting stack traces (in case KASAN's metadata was corrupted), reset KASAN's per-object metadata that stores stack depot handles when the object is initialized and when it's evicted from the quarantine. Note that stack_depot_put is no-op if the handle is 0. Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 3 ++- mm/kasan/generic.c | 22 ++++++++++++++++++---- mm/kasan/quarantine.c | 26 ++++++++++++++++++++------ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 825a0240ec02..b5d8bd26fced 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -50,7 +50,8 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags) void kasan_set_track(struct kasan_track *track, gfp_t flags) { track->pid = current->pid; - track->stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); + track->stack = kasan_save_stack(flags, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); } #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 5d168c9afb32..50cc519e23f4 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -449,10 +449,14 @@ struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { struct kasan_alloc_meta *alloc_meta; + struct kasan_free_meta *free_meta; alloc_meta = kasan_get_alloc_meta(cache, object); if (alloc_meta) __memset(alloc_meta, 0, sizeof(*alloc_meta)); + free_meta = kasan_get_free_meta(cache, object); + if (free_meta) + __memset(free_meta, 0, sizeof(*free_meta)); } size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object) @@ -489,18 +493,20 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) if (!alloc_meta) return; + stack_depot_put(alloc_meta->aux_stack[1]); alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); } void kasan_record_aux_stack(void *addr) { - return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); + return __kasan_record_aux_stack(addr, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); } void kasan_record_aux_stack_noalloc(void *addr) { - return __kasan_record_aux_stack(addr, 0); + return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_GET); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) @@ -508,8 +514,16 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) struct kasan_alloc_meta *alloc_meta; alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - kasan_set_track(&alloc_meta->alloc_track, flags); + if (!alloc_meta) + return; + + /* Evict previous stack traces (might exist for krealloc). */ + stack_depot_put(alloc_meta->alloc_track.stack); + stack_depot_put(alloc_meta->aux_stack[0]); + stack_depot_put(alloc_meta->aux_stack[1]); + __memset(alloc_meta, 0, sizeof(*alloc_meta)); + + kasan_set_track(&alloc_meta->alloc_track, flags); } void kasan_save_free_info(struct kmem_cache *cache, void *object) diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c index ca4529156735..265ca2bbe2dd 100644 --- a/mm/kasan/quarantine.c +++ b/mm/kasan/quarantine.c @@ -143,11 +143,22 @@ static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache) static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) { void *object = qlink_to_object(qlink, cache); - struct kasan_free_meta *meta = kasan_get_free_meta(cache, object); + struct kasan_alloc_meta *alloc_meta = kasan_get_alloc_meta(cache, object); + struct kasan_free_meta *free_meta = kasan_get_free_meta(cache, object); unsigned long flags; - if (IS_ENABLED(CONFIG_SLAB)) - local_irq_save(flags); + if (alloc_meta) { + stack_depot_put(alloc_meta->alloc_track.stack); + stack_depot_put(alloc_meta->aux_stack[0]); + stack_depot_put(alloc_meta->aux_stack[1]); + __memset(alloc_meta, 0, sizeof(*alloc_meta)); + } + + if (free_meta && + *(u8 *)kasan_mem_to_shadow(object) == KASAN_SLAB_FREETRACK) { + stack_depot_put(free_meta->free_track.stack); + free_meta->free_track.stack = 0; + } /* * If init_on_free is enabled and KASAN's free metadata is stored in @@ -157,14 +168,17 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) */ if (slab_want_init_on_free(cache) && cache->kasan_info.free_meta_offset == 0) - memzero_explicit(meta, sizeof(*meta)); + memzero_explicit(free_meta, sizeof(*free_meta)); /* - * As the object now gets freed from the quarantine, assume that its - * free track is no longer valid. + * As the object now gets freed from the quarantine, + * take note that its free track is no longer exists. */ *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREE; + if (IS_ENABLED(CONFIG_SLAB)) + local_irq_save(flags); + ___cache_free(cache, object, _THIS_IP_); if (IS_ENABLED(CONFIG_SLAB))