Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3234703pxv; Sun, 27 Jun 2021 23:21:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPne8PMUQmRBBeEV+BhqBNQUG8N0D6Db0OKiqjTt3Bekblppvn23oPbk6eUmZj/emafdpe X-Received: by 2002:a5e:a612:: with SMTP id q18mr6491449ioi.76.1624861294498; Sun, 27 Jun 2021 23:21:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624861294; cv=none; d=google.com; s=arc-20160816; b=XxyOYLSMvw92GwuuayMNnS6saiLFxHwpjs9PFIOo+BHt3B2joXpgufsSl0sJUr8VKK H5LrSBnuaBtZ87ixTZY/B8Ot/2pBH4SgoZhchfFS1nOWwjJ5+qhQfaYWsetkmN0ZeNuk 8qvSBsYk8vOvwPaTpdk/M5QjYwzdBzlzCLWtKRhwTJMZ+0Ayd232WgIztZPLqv+/89q2 ePft2ilNVh8KcsjQrGmB2AOh5xY4qCCoXA5t1P+xR0cMezhwz7sK3nbYYVI7xC+xnwEA pIbgmv9wpt89eD7q2qc6EKWueqtbWu/mTqKC83A1f4leBWkbsrx0nxHaJ/LrsZ6j/3Nu 7FDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=pKd6dJBV4N8YLl9Ne/rLrhUWGWga9X9929E81Ir9VwE=; b=mUkwLYRpHij5AOAl3BeVyhCxDUBiyagfegwTAPyydUkmyXoymWQ3sgkS361JNi4O8Y 1kkBYHyzoHq1155846uL/RumEk3i5snzMTxyWEXbYNUHvlmirI0oyAQJ35ynP8GHC0dv N50mmY5EB3l5SY3MHyKyzjYLEkxubhhlda4AYv+uJ1AkW/Kfe2OCdajqRzCywqFmX5V9 WDPAhfxlCn29m0ePqDaxEe2U4LADwLR9fK4yYjn5sBxiTc1YzKwEUPQ3GfsMG8Xl259i 6xl2Wg53kLxX+lFGPyo8m5tHn2KMi0+IW3Xxsd5C4TgimKUub7KxobN9k8vqjoLcwbN/ p+lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wOYvQpib; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g25si8185042iox.51.2021.06.27.23.21.22; Sun, 27 Jun 2021 23:21:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wOYvQpib; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232246AbhF1GCu (ORCPT + 99 others); Mon, 28 Jun 2021 02:02:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232215AbhF1GCs (ORCPT ); Mon, 28 Jun 2021 02:02:48 -0400 Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7497C061766 for ; Sun, 27 Jun 2021 23:00:21 -0700 (PDT) Received: by mail-ot1-x333.google.com with SMTP id v5-20020a0568301bc5b029045c06b14f83so17569285ota.13 for ; Sun, 27 Jun 2021 23:00:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pKd6dJBV4N8YLl9Ne/rLrhUWGWga9X9929E81Ir9VwE=; b=wOYvQpibWIPMRLcfT6idVBBV8wATamkZFDKpHusk7Fo3lgzVLAWMN9D+M9vwwsiW6C DDeMAp21F4ZS7WK3YPM0vfSAAbmY5/qtn/8ZrABf8D8qxyyEtpWaqdgULKhkvsoRGFCw HKvQI1zNqgG0tDUPSVFzKcon/gWQDfLANAQeEf6NtHyUEL8YXOcuVRMtqAjgmWryNRAs sPVlwKrrzOCMd1ylsWPQPKZsMXUj1o5kDzo5/ieVx/I/iH0uP2/3x3tVtCdHZEm2GvMU Vsc2U1OTqOz0MzDTmfi1ctc82bH7XmFqxMCfq9bzBQs3YcxmevH3xo0Vwyag5KJMNjJk ly5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pKd6dJBV4N8YLl9Ne/rLrhUWGWga9X9929E81Ir9VwE=; b=K99vqVuED4tyYJunYzPNXJvBSXOY3Vkr/GSJ1DKpmYZctgajK5Bbz+cwZQBE3wefJw RVBRkJkZPk2fyY2G/LQVdHkDAzHyKEHc5vKxPobZ4SoRSEWGsvurlxrs/afFq9gg+l5U iHb8gwiyMfi/bcY0AjSUXdYRdhyIQadv07dnXeFXQaVA5Ve3JDDmcEc1Yd3yGpDbOHB6 6fl7jYmGPSs7aOZ68izcHVT0WvUmlA1bmgkSWR5HNyPqc6wUrfGT4Q2ZhBVZqaAzQ/No SCLk0Br6SlgE90dY2DNm7hXD+C+mmFDpnhVI3hIB0oR1+X0r5LUaJE93YH5UgDuP7ssW s0kw== X-Gm-Message-State: AOAM531408GklN864Za/EclAXq5QVELn4MDBtJebbp85KOgGP9RLR+7Q /934ILWZ9uWr+nETxjs+FmOx4W5xb2T48AK+JHJAtQ== X-Received: by 2002:a05:6830:93:: with SMTP id a19mr19965428oto.17.1624860021048; Sun, 27 Jun 2021 23:00:21 -0700 (PDT) MIME-Version: 1.0 References: <20210626100931.22794-1-Kuan-Ying.Lee@mediatek.com> <20210626100931.22794-3-Kuan-Ying.Lee@mediatek.com> In-Reply-To: <20210626100931.22794-3-Kuan-Ying.Lee@mediatek.com> From: Marco Elver Date: Mon, 28 Jun 2021 08:00:00 +0200 Message-ID: Subject: Re: [PATCH v4 2/3] kasan: integrate the common part of two KASAN tag-based modes To: Kuan-Ying Lee Cc: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Matthias Brugger , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com, chinwen.chang@mediatek.com, nicholas.tang@mediatek.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 26 Jun 2021 at 12:09, Kuan-Ying Lee wrote: > > 1. Move kasan_get_free_track() and kasan_set_free_info() > into tags.c and combine these two functions for > SW_TAGS and HW_TAGS kasan mode. > 2. Move kasan_get_bug_type() to report_tags.c and > make this function compatible for SW_TAGS and > HW_TAGS kasan mode. > > Signed-off-by: Kuan-Ying Lee > Suggested-by: Marco Elver > Suggested-by: Greg Kroah-Hartman > Cc: Andrey Ryabinin > Cc: Alexander Potapenko > Cc: Andrey Konovalov > Cc: Dmitry Vyukov > Cc: Andrew Morton Reviewed-by: Marco Elver > --- > mm/kasan/Makefile | 4 +-- > mm/kasan/hw_tags.c | 22 --------------- > mm/kasan/report_hw_tags.c | 5 ---- > mm/kasan/report_sw_tags.c | 43 ---------------------------- > mm/kasan/report_tags.c | 51 +++++++++++++++++++++++++++++++++ > mm/kasan/sw_tags.c | 41 --------------------------- > mm/kasan/tags.c | 59 +++++++++++++++++++++++++++++++++++++++ > 7 files changed, 112 insertions(+), 113 deletions(-) > create mode 100644 mm/kasan/report_tags.c > create mode 100644 mm/kasan/tags.c > > diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile > index 9fe39a66388a..adcd9acaef61 100644 > --- a/mm/kasan/Makefile > +++ b/mm/kasan/Makefile > @@ -37,5 +37,5 @@ CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) > > obj-$(CONFIG_KASAN) := common.o report.o > obj-$(CONFIG_KASAN_GENERIC) += init.o generic.o report_generic.o shadow.o quarantine.o > -obj-$(CONFIG_KASAN_HW_TAGS) += hw_tags.o report_hw_tags.o > -obj-$(CONFIG_KASAN_SW_TAGS) += init.o report_sw_tags.o shadow.o sw_tags.o > +obj-$(CONFIG_KASAN_HW_TAGS) += hw_tags.o report_hw_tags.o tags.o report_tags.o > +obj-$(CONFIG_KASAN_SW_TAGS) += init.o report_sw_tags.o shadow.o sw_tags.o tags.o report_tags.o > diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c > index ed5e5b833d61..4ea8c368b5b8 100644 > --- a/mm/kasan/hw_tags.c > +++ b/mm/kasan/hw_tags.c > @@ -216,28 +216,6 @@ void __init kasan_init_hw_tags(void) > pr_info("KernelAddressSanitizer initialized\n"); > } > > -void kasan_set_free_info(struct kmem_cache *cache, > - void *object, u8 tag) > -{ > - struct kasan_alloc_meta *alloc_meta; > - > - alloc_meta = kasan_get_alloc_meta(cache, object); > - if (alloc_meta) > - kasan_set_track(&alloc_meta->free_track[0], GFP_NOWAIT); > -} > - > -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, > - void *object, u8 tag) > -{ > - struct kasan_alloc_meta *alloc_meta; > - > - alloc_meta = kasan_get_alloc_meta(cache, object); > - if (!alloc_meta) > - return NULL; > - > - return &alloc_meta->free_track[0]; > -} > - > void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags) > { > /* > diff --git a/mm/kasan/report_hw_tags.c b/mm/kasan/report_hw_tags.c > index 42b2168755d6..5dbbbb930e7a 100644 > --- a/mm/kasan/report_hw_tags.c > +++ b/mm/kasan/report_hw_tags.c > @@ -15,11 +15,6 @@ > > #include "kasan.h" > > -const char *kasan_get_bug_type(struct kasan_access_info *info) > -{ > - return "invalid-access"; > -} > - > void *kasan_find_first_bad_addr(void *addr, size_t size) > { > return kasan_reset_tag(addr); > diff --git a/mm/kasan/report_sw_tags.c b/mm/kasan/report_sw_tags.c > index 821a14a19a92..d2298c357834 100644 > --- a/mm/kasan/report_sw_tags.c > +++ b/mm/kasan/report_sw_tags.c > @@ -29,49 +29,6 @@ > #include "kasan.h" > #include "../slab.h" > > -const char *kasan_get_bug_type(struct kasan_access_info *info) > -{ > -#ifdef CONFIG_KASAN_TAGS_IDENTIFY > - struct kasan_alloc_meta *alloc_meta; > - struct kmem_cache *cache; > - struct page *page; > - const void *addr; > - void *object; > - u8 tag; > - int i; > - > - tag = get_tag(info->access_addr); > - addr = kasan_reset_tag(info->access_addr); > - page = kasan_addr_to_page(addr); > - if (page && PageSlab(page)) { > - cache = page->slab_cache; > - object = nearest_obj(cache, page, (void *)addr); > - alloc_meta = kasan_get_alloc_meta(cache, object); > - > - if (alloc_meta) { > - for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { > - if (alloc_meta->free_pointer_tag[i] == tag) > - return "use-after-free"; > - } > - } > - return "out-of-bounds"; > - } > - > -#endif > - /* > - * If access_size is a negative number, then it has reason to be > - * defined as out-of-bounds bug type. > - * > - * Casting negative numbers to size_t would indeed turn up as > - * a large size_t and its value will be larger than ULONG_MAX/2, > - * so that this can qualify as out-of-bounds. > - */ > - if (info->access_addr + info->access_size < info->access_addr) > - return "out-of-bounds"; > - > - return "invalid-access"; > -} > - > void *kasan_find_first_bad_addr(void *addr, size_t size) > { > u8 tag = get_tag(addr); > diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c > new file mode 100644 > index 000000000000..8a319fc16dab > --- /dev/null > +++ b/mm/kasan/report_tags.c > @@ -0,0 +1,51 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (c) 2014 Samsung Electronics Co., Ltd. > + * Copyright (c) 2020 Google, Inc. > + */ > + > +#include "kasan.h" > +#include "../slab.h" > + > +const char *kasan_get_bug_type(struct kasan_access_info *info) > +{ > +#ifdef CONFIG_KASAN_TAGS_IDENTIFY > + struct kasan_alloc_meta *alloc_meta; > + struct kmem_cache *cache; > + struct page *page; > + const void *addr; > + void *object; > + u8 tag; > + int i; > + > + tag = get_tag(info->access_addr); > + addr = kasan_reset_tag(info->access_addr); > + page = kasan_addr_to_page(addr); > + if (page && PageSlab(page)) { > + cache = page->slab_cache; > + object = nearest_obj(cache, page, (void *)addr); > + alloc_meta = kasan_get_alloc_meta(cache, object); > + > + if (alloc_meta) { > + for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { > + if (alloc_meta->free_pointer_tag[i] == tag) > + return "use-after-free"; > + } > + } > + return "out-of-bounds"; > + } > +#endif > + > + /* > + * If access_size is a negative number, then it has reason to be > + * defined as out-of-bounds bug type. > + * > + * Casting negative numbers to size_t would indeed turn up as > + * a large size_t and its value will be larger than ULONG_MAX/2, > + * so that this can qualify as out-of-bounds. > + */ > + if (info->access_addr + info->access_size < info->access_addr) > + return "out-of-bounds"; > + > + return "invalid-access"; > +} > diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c > index dd05e6c801fa..bd3f540feb47 100644 > --- a/mm/kasan/sw_tags.c > +++ b/mm/kasan/sw_tags.c > @@ -167,47 +167,6 @@ void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size) > } > EXPORT_SYMBOL(__hwasan_tag_memory); > > -void kasan_set_free_info(struct kmem_cache *cache, > - void *object, u8 tag) > -{ > - struct kasan_alloc_meta *alloc_meta; > - u8 idx = 0; > - > - alloc_meta = kasan_get_alloc_meta(cache, object); > - if (!alloc_meta) > - return; > - > -#ifdef CONFIG_KASAN_TAGS_IDENTIFY > - idx = alloc_meta->free_track_idx; > - alloc_meta->free_pointer_tag[idx] = tag; > - alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS; > -#endif > - > - kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT); > -} > - > -struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, > - void *object, u8 tag) > -{ > - struct kasan_alloc_meta *alloc_meta; > - int i = 0; > - > - alloc_meta = kasan_get_alloc_meta(cache, object); > - if (!alloc_meta) > - return NULL; > - > -#ifdef CONFIG_KASAN_TAGS_IDENTIFY > - for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { > - if (alloc_meta->free_pointer_tag[i] == tag) > - break; > - } > - if (i == KASAN_NR_FREE_STACKS) > - i = alloc_meta->free_track_idx; > -#endif > - > - return &alloc_meta->free_track[i]; > -} > - > void kasan_tag_mismatch(unsigned long addr, unsigned long access_info, > unsigned long ret_ip) > { > diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c > new file mode 100644 > index 000000000000..8f48b9502a17 > --- /dev/null > +++ b/mm/kasan/tags.c > @@ -0,0 +1,59 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * This file contains common tag-based KASAN code. > + * > + * Copyright (c) 2018 Google, Inc. > + * Copyright (c) 2020 Google, Inc. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "kasan.h" > + > +void kasan_set_free_info(struct kmem_cache *cache, > + void *object, u8 tag) > +{ > + struct kasan_alloc_meta *alloc_meta; > + u8 idx = 0; > + > + alloc_meta = kasan_get_alloc_meta(cache, object); > + if (!alloc_meta) > + return; > + > +#ifdef CONFIG_KASAN_TAGS_IDENTIFY > + idx = alloc_meta->free_track_idx; > + alloc_meta->free_pointer_tag[idx] = tag; > + alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS; > +#endif > + > + kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT); > +} > + > +struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, > + void *object, u8 tag) > +{ > + struct kasan_alloc_meta *alloc_meta; > + int i = 0; > + > + alloc_meta = kasan_get_alloc_meta(cache, object); > + if (!alloc_meta) > + return NULL; > + > +#ifdef CONFIG_KASAN_TAGS_IDENTIFY > + for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { > + if (alloc_meta->free_pointer_tag[i] == tag) > + break; > + } > + if (i == KASAN_NR_FREE_STACKS) > + i = alloc_meta->free_track_idx; > +#endif > + > + return &alloc_meta->free_track[i]; > +} > -- > 2.18.0 > > -- > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210626100931.22794-3-Kuan-Ying.Lee%40mediatek.com.