From patchwork Mon Apr 5 21:43:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12183605 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=-26.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,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 E1AF0C433B4 for ; Mon, 5 Apr 2021 21:43:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6822060FE6 for ; Mon, 5 Apr 2021 21:43:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6822060FE6 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E753D6B006E; Mon, 5 Apr 2021 17:43:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E256E6B0073; Mon, 5 Apr 2021 17:43:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC5F86B0075; Mon, 5 Apr 2021 17:43:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0242.hostedemail.com [216.40.44.242]) by kanga.kvack.org (Postfix) with ESMTP id AFF716B006E for ; Mon, 5 Apr 2021 17:43:31 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 666A58249980 for ; Mon, 5 Apr 2021 21:43:31 +0000 (UTC) X-FDA: 77999640222.17.85D83AA Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 7F46C40002C6 for ; Mon, 5 Apr 2021 21:43:28 +0000 (UTC) Received: by mail-qk1-f202.google.com with SMTP id g62so11355238qkf.18 for ; Mon, 05 Apr 2021 14:43:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=gfJiVLWnR3u0HT0QPuAVfbid+Hjz+9NTHVVf/avPiLs=; b=M3yvuA1uL2HlxtaCa3D1y4Poke4m37DPILkYUmES0Opj+tc41x9ZEqA/ZwrM4Xnj+a Uf71VEuiHHo4pF9BJy1ete9zY0Pc7+dKlrsUZZUyZNgv9dQ/WPFgZ6QH1ijw+HUkvVWA YRzWwslXWsa8XFuZ10GyZKAWqsLKS+yUcRiYIMJqQiiw/65GhFIRCow3tkbZw61Sgva3 VBwyscj0bhQTvoqa0tWxx6oveok66n6ajFTlsSOvwKUUSsbuLQXfBnCfRmcYTdR5tAdt OHHWCk5Tr/hAtIxjEEbyAjeuEJsRYQ8FWLOYlPNvzPTw6GsoEvVx2jtChEDBaOgw2aKd S0OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=gfJiVLWnR3u0HT0QPuAVfbid+Hjz+9NTHVVf/avPiLs=; b=sip/kuehr8Q8f3dkbmJONNAsoz2MRauJM0qI8PzPMqRmLfD5+T+86fOWluGoXpjy49 CBgkV4vr6pMD5gmPtGG56l1JFOE5/7sKRUEYQEm4dPMIhQZwwhT3CaaesoMMmEBNNopf MIVCjSHw8D9EbNqOvrPd/BfMItlmnTOf+qL2DGmATGpgwnR7WzbAMMWfVLLEloeVrtaY nymGL4DpLJ+lArGe0k/h43+z9KmaHOGgPP1jQLFFa91RSUeI57GNQkO9oDEtsC9wEhxa ThL96F0jW42mMzeESjX6y1w/qPAvK5VBjbXTdIJuLV1vFClRwGE07vmpFZ9LM9QvJF03 Yvgg== X-Gm-Message-State: AOAM530Xgs4SJh5TQMUa/9Sx8Cw7lMMRc/eYge8t4FSfVES/wImc/Vyl FWJDdEYY5QuIeqoqRyZz7VbkC0Y= X-Google-Smtp-Source: ABdhPJxV6U+sF4CX99Re8zx+JaWecKrw4a4IkQ+sv2HqAwztwfRVsj6d3/HXUwzvOOBrR05cED/suBo= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:0:8495:4fdf:186d:cc80]) (user=pcc job=sendgmr) by 2002:a0c:e9c7:: with SMTP id q7mr25192903qvo.7.1617659010098; Mon, 05 Apr 2021 14:43:30 -0700 (PDT) Date: Mon, 5 Apr 2021 14:43:21 -0700 Message-Id: <20210405214321.1953019-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH] kasan: fix kasan_byte_accessible() to be consistent with actual checks From: Peter Collingbourne To: Marco Elver , Dmitry Vyukov , Alexander Potapenko Cc: Peter Collingbourne , Evgenii Stepanov , Andrey Konovalov , linux-mm@kvack.org, linux-kernel@vger.kernel.org X-Stat-Signature: t5m6qe7zfcpp1c186tf93ncypcypidpm X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7F46C40002C6 Received-SPF: none (flex--pcc.bounces.google.com>: No applicable sender policy available) receiver=imf10; identity=mailfrom; envelope-from="<3goRrYAMKCEMuhhlttlqj.htrqnsz2-rrp0fhp.twl@flex--pcc.bounces.google.com>"; helo=mail-qk1-f202.google.com; client-ip=209.85.222.202 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617659008-238930 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: We can sometimes end up with kasan_byte_accessible() being called on non-slab memory. For example ksize() and krealloc() may end up calling it on KFENCE allocated memory. In this case the memory will be tagged with KASAN_SHADOW_INIT, which a subsequent patch ("kasan: initialize shadow to TAG_INVALID for SW_TAGS") will set to the same value as KASAN_TAG_INVALID, causing kasan_byte_accessible() to fail when called on non-slab memory. This highlighted the fact that the check in kasan_byte_accessible() was inconsistent with checks as implemented for loads and stores (kasan_check_range() in SW tags mode and hardware-implemented checks in HW tags mode). kasan_check_range() does not have a check for KASAN_TAG_INVALID, and instead has a comparison against KASAN_SHADOW_START. In HW tags mode, we do not have either, but we do set TCR_EL1.TCMA which corresponds with the comparison against KASAN_TAG_KERNEL. Therefore, update kasan_byte_accessible() for both SW and HW tags modes to correspond with the respective checks on loads and stores. Link: https://linux-review.googlesource.com/id/Ic6d40803c57dcc6331bd97fbb9a60b0d38a65a36 Signed-off-by: Peter Collingbourne --- mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 8c55634d6edd..e18e8da35255 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -368,8 +368,7 @@ static inline bool kasan_byte_accessible(const void *addr) u8 ptr_tag = get_tag(addr); u8 mem_tag = hw_get_mem_tag((void *)addr); - return (mem_tag != KASAN_TAG_INVALID) && - (ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag); + return ptr_tag == KASAN_TAG_KERNEL || ptr_tag == mem_tag; } #else /* CONFIG_KASAN_HW_TAGS */ diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 94c2d33be333..914225eeda99 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -121,10 +121,12 @@ bool kasan_check_range(unsigned long addr, size_t size, bool write, bool kasan_byte_accessible(const void *addr) { u8 tag = get_tag(addr); - u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(kasan_reset_tag(addr))); + void *untagged_addr = kasan_reset_tag(addr); + u8 shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr)); - return (shadow_byte != KASAN_TAG_INVALID) && - (tag == KASAN_TAG_KERNEL || tag == shadow_byte); + return untagged_addr >= + kasan_shadow_to_mem((void *)KASAN_SHADOW_START) && + (tag == KASAN_TAG_KERNEL || tag == shadow_byte); } #define DEFINE_HWASAN_LOAD_STORE(size) \