From patchwork Fri Nov 13 22:15:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11904995 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 7F7EAC55ABD for ; Fri, 13 Nov 2020 22:21:02 +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 8041E22258 for ; Fri, 13 Nov 2020 22:21:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="S1xXjBgf"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="kEaoU6im" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8041E22258 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=3nkFpi2nC6VORpmW8vSA13Jh12+zyquGbXNPejUYgHA=; b=S1xXjBgfqONVMLHQKhM0zxuYf FCIH7fP21oShPuUIEZyROrnTn2F6ZLaXhqGj09D/U/1/4Gu1fNjk/8PDhrw7gNFzP2J3AMyjpbzxJ lsdlhgsDOAxLjvHwMh8RLkl1K0o2IMZrpLzdEsmeVmeQgsCCH16bRpau9iylBTVdVk9lg5nGxZFZu zyWavtjKEwZfnRxnHO39XrmoLQRWuUyTRAZKeq2vQ7rxSbI9BTyi2EHhyEzlUNmTzkdswWo639j+M 7cRXQMfeOs82P2fAyGyLKF/UDaE0L5iDdqzwQ2Zg9MSYSWM7zXVQKtdwsOExZfxSxjFHnR2pcR83N oNE9DpaJw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhQJ-0001H9-8z; Fri, 13 Nov 2020 22:20:04 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kdhN8-0008Rv-Ut for linux-arm-kernel@lists.infradead.org; Fri, 13 Nov 2020 22:16:58 +0000 Received: by mail-qk1-x74a.google.com with SMTP id 143so3743669qkg.20 for ; Fri, 13 Nov 2020 14:16:44 -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=KBw2xb89iGf5nkQBhVPMBrv+WXUlpFCEGVTeqj9IKE8=; b=kEaoU6imARJ9PUMGylo3WxrvNJwhz4ehol5ihwX1PAJZetS+MYeRsmLL73YYgZJPSj Y1to6lX3L3cZywbseOl5yJksMji3kaOqt5ZEvniI49snje8Yn7/BqWcjwSRkOyDMStR9 u0UH19eQdTaD7lyammRYs91tUiy1mx+SlweE8yjpHaXB+D4rIyALl/bOswEGdcXyo4ir /uotDXbwDUcN93YlnCwaj+NWLIPhIcE20bCRcldSJoQRvhxBZXVdNMjev6tvLfvsE1c/ OWDZy5n0UnAhwdiLLnq7YQCs5rB7gMRE0EToE+ZffqJCxIS7vKMt/zoshLvu8CU2Kpy0 fnVQ== 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=KBw2xb89iGf5nkQBhVPMBrv+WXUlpFCEGVTeqj9IKE8=; b=TEn9M21bvTqLEGNjLOuFES0yNJyXLyuIa4l8Lf1M+Zw0AKxjgRLDZGEF9zVMVik3hS v2hgMPKjYvgyD9cPmVwNkJIlH2UT7AmAJPDJYej00V35Te348hTzR+Q650NFhzdVJUXs H+jQhE9Z0czT+N8tMYlqhLjeKgeIaxgnOzyYCzalFrwGhkyYbf857Hk51lPvirII5k0v cECN9TsYID/uIRohSQ+hvaEeEEiOBTp87g57V1VaAfoLEwyxYmZiK6AZ24ypMVK6roWA h9ZWXQjxdumtx5TIx1Ggqo4WnlXTzVyUbIwFw3BzgjTG4agzPySSXUMN+zTGl8w7cCOC BzZg== X-Gm-Message-State: AOAM532i2gLSp0F8L+tLlBaPOx8I0uVxULwIcHY+dB+XzqyJeJx8oeVt NfnvolSGWGPsMf00Yq5k2+EYlDFF5f4B32UN X-Google-Smtp-Source: ABdhPJz28oDTu0JrMVUyvh42LUnx0OaovzkH2m5z3QnEQ90fl2RtrA3qpCClpjEC/kSPUPZ0vfdSYddbzI61o9DW X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:ad4:54cd:: with SMTP id j13mr4460435qvx.8.1605305803272; Fri, 13 Nov 2020 14:16:43 -0800 (PST) Date: Fri, 13 Nov 2020 23:15:40 +0100 In-Reply-To: Message-Id: <6b1a801b2132bf11e19c4421b2b079d242b152f3.1605305705.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH mm v10 12/42] kasan: hide invalid free check implementation From: Andrey Konovalov To: Andrew Morton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201113_171647_047388_332D6293 X-CRM114-Status: GOOD ( 17.25 ) 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 , Catalin Marinas , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Konovalov , Andrey Ryabinin , 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 Reviewed-by: Alexander Potapenko --- 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 b3ebee6fcfca..ae55570b4d32 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -278,25 +278,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; @@ -318,8 +302,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 754217c258a8..67642acafe92 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -188,6 +188,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 eec88bf28c64..e5b5f60bc963 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -166,6 +166,8 @@ void unpoison_range(const void *address, size_t size); 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 c0b3f327812b..64540109c461 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) \ { \