From mboxrd@z Thu Jan 1 00:00:00 1970 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 B21D6C2D0A3 for ; Wed, 4 Nov 2020 23:19:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6555720786 for ; Wed, 4 Nov 2020 23:19:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kkZxgHLi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732699AbgKDXTp (ORCPT ); Wed, 4 Nov 2020 18:19:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732677AbgKDXTl (ORCPT ); Wed, 4 Nov 2020 18:19:41 -0500 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74AABC0613CF for ; Wed, 4 Nov 2020 15:19:39 -0800 (PST) Received: by mail-ej1-x649.google.com with SMTP id c10so57649ejm.15 for ; Wed, 04 Nov 2020 15:19:39 -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=KbFeAZ0fCuPi+p3+zRj17HQjRaGn7T9HM7M+MvodvIE=; b=kkZxgHLi8E6+8WINef706wMtuOi52H0wp4uRiV0SrMVYLwWxjhBDSmwWATsCm8SBV4 ihJeRsvpAzMgJ+rVrlev+QbTaEtnVtJH1tJ0UzEQ7LWzDfnQCYTgshJNrYK8TMe8EXxv f91IhDc4KbTIhhkPjqYS9WMjDPEYmcGylg5VBa2NJ21Wqip+nyR8fLCm8+AqWvAC7q1S HBXwOF1yiXvyDOfs1GWuZhhLHwGnTn+M5yOoeDPlq12TFZc8n+k6IoMKoOVKI8vLDEXT udZXVN90mVjv0OD08oKlWjyVxslINVc3wRjaRt8//bRXufK3xVKm1ukaZ5bi2a1uR17F exVA== 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=KbFeAZ0fCuPi+p3+zRj17HQjRaGn7T9HM7M+MvodvIE=; b=ZROTImA7/5dZLMPHTUbCqeMw3u5v6GI9KrMngwfhVC6rHenDoUIJgZfmKRKTcCo68M RXWhJspbw7iezih48tQJawDFPKNwQHJACOPXAciK+qHv6w3MOjV/FTEDKUctDNbAzfQM ad3fOQNpCQ+KWJ08azEe3tXABZ5SHKJhfe7fYhsB8GvLW4cWfXDwRl76DroSEH9qg199 pxsNdTOXD2EiF3486ob070cNYnMCvSqvZipVkXAdTU7jySfB8IVxZIMXQim45azd4mmO YwqCLci6UjMNICbW4CPbPInePdpJlyf3JZ1u3eCHTZGAy0L5qaDZyC56KaxbAno62cVj sOJg== X-Gm-Message-State: AOAM532adsISvSWdgASXd/SfIPP0Lm9A4j5nq3UAzCF23i0bhMgYznwG nHtzFk/HcrM2exT9SziVGN5WWd5KhiJ8BYOU X-Google-Smtp-Source: ABdhPJyeynchU+B2StC/j5HyMg/W0mhgdCL2wRr0XlfTUsWP11EHOKULobOTxCDxoCElKltT9ByCyurYRGiQny+b Sender: "andreyknvl via sendgmr" X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6402:759:: with SMTP id p25mr215552edy.22.1604531977800; Wed, 04 Nov 2020 15:19:37 -0800 (PST) Date: Thu, 5 Nov 2020 00:18:28 +0100 In-Reply-To: Message-Id: <7366c8102820448b082445924a7a014976e1f252.1604531793.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v8 13/43] kasan: hide invalid free check implementation From: Andrey Konovalov To: Catalin Marinas Cc: Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , Andrew Morton , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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 1865bb92d47a..3eff57e71ff5 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) \ { \ -- 2.29.1.341.ge80a0c044ae-goog