From patchwork Mon Apr 5 22:06:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12183655 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 19274C433B4 for ; Mon, 5 Apr 2021 22:07:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 94BED613C3 for ; Mon, 5 Apr 2021 22:07:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 94BED613C3 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 227806B006E; Mon, 5 Apr 2021 18:07:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FDF36B0073; Mon, 5 Apr 2021 18:07:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 078806B0075; Mon, 5 Apr 2021 18:07:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0176.hostedemail.com [216.40.44.176]) by kanga.kvack.org (Postfix) with ESMTP id E19DC6B006E for ; Mon, 5 Apr 2021 18:07:04 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 9A98F3653 for ; Mon, 5 Apr 2021 22:07:04 +0000 (UTC) X-FDA: 77999699568.37.DB8912E Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf05.hostedemail.com (Postfix) with ESMTP id A945BE000118 for ; Mon, 5 Apr 2021 22:07:03 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id c1so11398243qke.8 for ; Mon, 05 Apr 2021 15:07:03 -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=oq7Q6pke93drxQnu1FB4iss8DOMmcIhW1Wkw424yBvw=; b=NRFQ6rj28/xEiBVJhnJ5Nnf+cyw92Ho+867IcELNhmGQ4+uXfTLnWmKGyWuMpvx8d2 hVW/m44biQO5BanHKtxpm20AilrGgaDrd7c+dsDMETy682svLhebVHc3ytpPjo1ZLpJS iUX0LzE7lkkTCZhsIsKMNPg3EkrfL/DLGCnuYgghRYSMvjLXYQcIaaBxuAj4xTxJV2/N hirYZNpY3eDkimpFFDagiuGtIinpX9Hl2z/b1d4c0DyKVOhH/ralGT3OtpBh9c5GmfaO GzeUWnN12GRR3bSxenr81uObWpiNoQvLfl4FgwMnWMTkGxHrLTdcO270a4xg9u46Er9F A1+Q== 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=oq7Q6pke93drxQnu1FB4iss8DOMmcIhW1Wkw424yBvw=; b=j4ur2832n4MHoydz/9Em0GxYm0cbMZS5zj80489OWgeGLPdrDVkz5tIn9y0HfF/s3T Oi9PZAUYscuAa0XxNLB+Rvmu7dKAkWZZuVV+IAPpALUkjeNUgSHFcqoj50MlKjeHwdVW RMFFb7yNHiJTeeqMDceDoz+EezGkcasEcSI1YpQuhdQKfWE8z7OPYWHWote1MRK/fk10 0pKAT89CRof/BFhKevSflqmsCAxAeAtcZpGiO8SNK3YILg3K80Z8+y/z2VLCKGrqcVOG b5Uph/e00Jj9acdE9zgL6jesvuSCp/fskLPXXXoONYIwW2IrVMM234yxB1OnPQpB7Dvs Q0hg== X-Gm-Message-State: AOAM532WTkdsJe+4DRKA0C4dFrZ6L6j/QFKdOt0AHb6xFwq5XHTccYi4 WTlZ0mOADueipVwhQq8DycL4/L4= X-Google-Smtp-Source: ABdhPJw/FvdKrezus0FvdqjuqO/coYz385HNHw4xQdJZ1V8k6AvPYHweCxZ2pWvNtYectV06sdsvtdo= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:0:8495:4fdf:186d:cc80]) (user=pcc job=sendgmr) by 2002:a0c:d7ca:: with SMTP id g10mr25290130qvj.16.1617660423429; Mon, 05 Apr 2021 15:07:03 -0700 (PDT) Date: Mon, 5 Apr 2021 15:06:47 -0700 Message-Id: <20210405220647.1965262-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH v2] 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-Rspamd-Queue-Id: A945BE000118 X-Stat-Signature: xzn5e4figp4cpxk3r8sybg38ogmz3gon X-Rspamd-Server: rspam02 Received-SPF: none (flex--pcc.bounces.google.com>: No applicable sender policy available) receiver=imf05; identity=mailfrom; envelope-from="<3B4prYAMKCNID004CC492.0CA96BIL-AA8Jy08.CF4@flex--pcc.bounces.google.com>"; helo=mail-qk1-f201.google.com; client-ip=209.85.222.201 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617660423-52207 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 Reviewed-by: Andrey Konovalov Reviewed-by: Marco Elver --- mm/kasan/kasan.h | 3 +-- mm/kasan/sw_tags.c | 10 +++++++--- 2 files changed, 8 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..00ae8913fc74 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -121,10 +121,14 @@ 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; - return (shadow_byte != KASAN_TAG_INVALID) && - (tag == KASAN_TAG_KERNEL || tag == shadow_byte); + if (untagged_addr < kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) + return false; + + shadow_byte = READ_ONCE(*(u8 *)kasan_mem_to_shadow(untagged_addr)); + return tag == KASAN_TAG_KERNEL || tag == shadow_byte; } #define DEFINE_HWASAN_LOAD_STORE(size) \