From patchwork Mon Nov 2 16:04:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11874389 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B3C5C00A89 for ; Mon, 2 Nov 2020 16:11:37 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4CC2022275 for ; Mon, 2 Nov 2020 16:11:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="r+nklASI"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="BSLt9OKa" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4CC2022275 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=SY4VZ93LEeXmP2LCJpYoPu4XqxZkJn6r3gTXpUV0+qw=; b=r+nklASIX6rmkh/OnncTW8tmh MtiL/jBdr0WA+FkmS4R0TwfY4SvS8ogZEFMDuQ2B5E9n27sfTTe5VLupD/JcLz1xo5TT/GzHjNJ+M FhyAit7gfM0Xv0GNPIrPx4ptKRq5uadzOwkSVoA37y1bnITw7hraNzb+B4ITQq0RJNHkMcQU277fL THAn0xwAwvBvwbL82U4oy3ovzF8Bcf0Eme+uV8iB/aUU9GS1oycXVw5YpIo5/wrPUZ9nfAaDKOgqj GR14+2EgmAzTcAQDHAT9Zpn/23csd618LUFMLtutSRF5d9xUIIInkEsKKVBNYp/i/b0Oi+dNoW0f8 NnJDo2MWw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kZcOi-0000ry-4l; Mon, 02 Nov 2020 16:09:32 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kZcKg-0006kC-8j for linux-arm-kernel@lists.infradead.org; Mon, 02 Nov 2020 16:05:31 +0000 Received: by mail-wm1-x34a.google.com with SMTP id o81so952688wma.0 for ; Mon, 02 Nov 2020 08:05:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=JrbTjwkrdKqcTf3OBQQXqXKZQLwLc5XAsTu4uoIDNkU=; b=BSLt9OKaearay+FGWxrm4o7h2bkHk/gdUbGPcfYRGHQdZUj8sw/1erDb+9+jujTLw0 Ug225r6/pntadIDfVbDTace6LXDbzEzYtWFc9wcVlA+lolJ0moGKw3h0zScXyFynl9CB BQp4ViTMUAR7sTv9TbxNtHPnaqAT9H3yUKkYK08j0+ZXg5Ar/OwpIWVEdsJE5FxizL4S WS2h9maD5lysR8dijjWXKPQLDA59x7DKG1WXtqkgetf/O6iCGhGmLmer1316cHGTushF CNq7JLufAyz95XwjMEHNNtSYSt2k+XIy1UfP8Key0nYicM3iGFpuQnzO/Rtd0FJq+vYk nlnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=JrbTjwkrdKqcTf3OBQQXqXKZQLwLc5XAsTu4uoIDNkU=; b=ZfPqKwgufBBcDT+IthIk60XTLA+olqwHFNi1CceztVA6pjU3w+xL23PvBdBwhHqEhJ pKJ90vMOitTSfNPgkYUwPTwguEKjrWnHn1+KvG73fVrcY6W/b34E4Z8wpyag7dRvVwRg DQ3n6KujHW8Xf2qykQqcefRmphEbGH15LuFbpAhpeK1jDQSnExz71etOaYkDOKpmxOjD edp9WGpVif80UxR3IzhSYhZU5wCBLd7UlI6/MWehOgL1yf+7fSJWFpCE7d+geQNcawtQ F95exl9SmrKcnj3kFNSguOhcsKHO7gSEs6zmlenvLOZ3v0ukgRO1t42bstSg6SJPIAWD LRdA== X-Gm-Message-State: AOAM530y3C2XBkE55g9MrPWxA1C91hEHiEblUtLv3F32cANF76lBuXRM IZGW4P+DFsrKrBHKzKbkXfx1zrzqI3WrVO4U X-Google-Smtp-Source: ABdhPJyOvs6s9ctpkM6Ce8d6mYTqLAZR/nAOMWlFghuqE5Zo1zLXZqWjNGBUbKu6n2PsowbXwY5h/PzVQRvYgQue X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a5d:6a85:: with SMTP id s5mr21657731wru.90.1604333119324; Mon, 02 Nov 2020 08:05:19 -0800 (PST) Date: Mon, 2 Nov 2020 17:04:02 +0100 In-Reply-To: Message-Id: <499c0824a10e32c7dbb29c2f28e9a76c771c0da0.1604333009.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v7 22/41] kasan: hide invalid free check implementation From: Andrey Konovalov To: Catalin Marinas , Will Deacon X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201102_110522_477720_5E41D7C4 X-CRM114-Status: GOOD ( 17.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, Marco Elver , Elena Petrova , Andrey Konovalov , Kevin Brodsky , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Ryabinin , Andrew Morton , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This is a preparatory commit for the upcoming addition of a new hardware tag-based (MTE-based) KASAN mode. For software KASAN modes the check is based on the value in the shadow memory. Hardware tag-based KASAN won't be using shadow, so hide the implementation of the check in check_invalid_free(). Also simplify the code for software tag-based mode. No functional changes for software modes. Signed-off-by: Andrey Konovalov Signed-off-by: Vincenzo Frascino Reviewed-by: Marco Elver --- Change-Id: I5fae9531c9fc948eb4d4e0c589744032fc5a0789 --- mm/kasan/common.c | 19 +------------------ mm/kasan/generic.c | 7 +++++++ mm/kasan/kasan.h | 2 ++ mm/kasan/sw_tags.c | 9 +++++++++ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 123abfb760d4..543e6bf2168f 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -272,25 +272,9 @@ void * __must_check kasan_init_slab_obj(struct kmem_cache *cache, return (void *)object; } -static inline bool shadow_invalid(u8 tag, s8 shadow_byte) -{ - if (IS_ENABLED(CONFIG_KASAN_GENERIC)) - return shadow_byte < 0 || - shadow_byte >= KASAN_GRANULE_SIZE; - - /* else CONFIG_KASAN_SW_TAGS: */ - if ((u8)shadow_byte == KASAN_TAG_INVALID) - return true; - if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte)) - return true; - - return false; -} - static bool __kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip, bool quarantine) { - s8 shadow_byte; u8 tag; void *tagged_object; unsigned long rounded_up_size; @@ -309,8 +293,7 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) return false; - shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object)); - if (shadow_invalid(tag, shadow_byte)) { + if (check_invalid_free(tagged_object)) { kasan_report_invalid_free(tagged_object, ip); return true; } diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index ec4417156943..e1af3b6c53b8 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -187,6 +187,13 @@ bool check_memory_region(unsigned long addr, size_t size, bool write, return check_memory_region_inline(addr, size, write, ret_ip); } +bool check_invalid_free(void *addr) +{ + s8 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(addr)); + + return shadow_byte < 0 || shadow_byte >= KASAN_GRANULE_SIZE; +} + void kasan_cache_shrink(struct kmem_cache *cache) { quarantine_remove_cache(cache); diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index d8f54efb2899..04df1481a033 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -164,6 +164,8 @@ void kasan_poison_memory(const void *address, size_t size, u8 value); bool check_memory_region(unsigned long addr, size_t size, bool write, unsigned long ret_ip); +bool check_invalid_free(void *addr); + void *find_first_bad_addr(void *addr, size_t size); const char *get_bug_type(struct kasan_access_info *info); diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 4bdd7dbd6647..b2638c2cd58a 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -121,6 +121,15 @@ bool check_memory_region(unsigned long addr, size_t size, bool write, return true; } +bool check_invalid_free(void *addr) +{ + u8 tag = get_tag(addr); + u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(reset_tag(addr))); + + return (shadow_byte == KASAN_TAG_INVALID) || + (tag != KASAN_TAG_KERNEL && tag != shadow_byte); +} + #define DEFINE_HWASAN_LOAD_STORE(size) \ void __hwasan_load##size##_noabort(unsigned long addr) \ { \