From patchwork Fri Mar 19 14:40:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12151201 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 4490FC433E0 for ; Fri, 19 Mar 2021 14:41:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DD0D264E41 for ; Fri, 19 Mar 2021 14:41:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD0D264E41 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 887B06B0078; Fri, 19 Mar 2021 10:41:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 820046B007D; Fri, 19 Mar 2021 10:41:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C5376B0080; Fri, 19 Mar 2021 10:41:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0112.hostedemail.com [216.40.44.112]) by kanga.kvack.org (Postfix) with ESMTP id 2DE016B007D for ; Fri, 19 Mar 2021 10:41:27 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DA3221808E594 for ; Fri, 19 Mar 2021 14:41:26 +0000 (UTC) X-FDA: 77936886972.38.F5F024B Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf07.hostedemail.com (Postfix) with ESMTP id 0A271A0009FF for ; Fri, 19 Mar 2021 14:41:14 +0000 (UTC) Received: by mail-pg1-f177.google.com with SMTP id m7so3814678pgj.8 for ; Fri, 19 Mar 2021 07:41:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NqhHo+YeTtQNKJqI1Nmg9Lhc7h/uxmrUcSCxPqX7mCM=; b=FyZSmlP4DGRtdJxzH1AItezVYjTeONejnnz+aTt5sRjMbNhtjZ6PX7iClDrbPlg8Tg e4YYc/4dlnkyvEKJTIM6zBaidNWPYG3WuWDJXK3dNsh6oY/pKl7IXzTCoPeoW3YDVW/Q h02HpsHMGPKu0lDL87/RE+idZVOHCCR0Z5i+I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NqhHo+YeTtQNKJqI1Nmg9Lhc7h/uxmrUcSCxPqX7mCM=; b=IUTYHtN08H/d/Pdoj4vhqZ9f+iulbXzfXiOzP5LNbyRr1wDhlshvdvaKGR1cJZ9IMl +N5T2Jtw5bjPAOCBzuqGKrtMjiBUeYC2XKst7iDkSriz0x3en/dU14wNbKuqsckj8Gf1 vZK/XYDi21O0/1+yuYQaObZ8EfcQV4e2tSrOCAp9qFBkNzQx+dQWNiTaMhPvo4g26NRG MvqCVJjvY05t0dCmdoUKCjGxeQn0eyROro4FD9wHAh9sS5eFU/nMcaaZSeCbS1ymOnji hUm95Qlc05hAiV5VY+H4GBLfG09I/i76aYA2d1tFm9FIwRK6+/qJbjVLXCkyzZZV+ab1 fX9Q== X-Gm-Message-State: AOAM533wlKa3KlFtMA4CHzUJ+Y8ZjJzdqq8mAyUKgSRE7ukco2HN2Nxl KO/zrsOg9En3YkQe6WMeto63Yw== X-Google-Smtp-Source: ABdhPJwhZsubqhuhBqxD8pjftO2o+fW3bkpwXwbu7NU660do1sNc0/EcHx+g+rkua6W/EyKwG07E8g== X-Received: by 2002:a65:4901:: with SMTP id p1mr780094pgs.310.1616164872494; Fri, 19 Mar 2021 07:41:12 -0700 (PDT) Received: from localhost (2001-44b8-111e-5c00-674e-5c6f-efc9-136d.static.ipv6.internode.on.net. [2001:44b8:111e:5c00:674e:5c6f:efc9:136d]) by smtp.gmail.com with ESMTPSA id s28sm5943535pfd.155.2021.03.19.07.41.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Mar 2021 07:41:12 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, kasan-dev@googlegroups.com, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com Cc: Daniel Axtens , "Aneesh Kumar K . V" Subject: [PATCH v11 2/6] kasan: allow architectures to provide an outline readiness check Date: Sat, 20 Mar 2021 01:40:54 +1100 Message-Id: <20210319144058.772525-3-dja@axtens.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210319144058.772525-1-dja@axtens.net> References: <20210319144058.772525-1-dja@axtens.net> MIME-Version: 1.0 X-Stat-Signature: qcqx3rdeiuzpazbj4n3my74fb7aeejaa X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0A271A0009FF Received-SPF: none (axtens.net>: No applicable sender policy available) receiver=imf07; identity=mailfrom; envelope-from=""; helo=mail-pg1-f177.google.com; client-ip=209.85.215.177 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616164874-480408 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: Allow architectures to define a kasan_arch_is_ready() hook that bails out of any function that's about to touch the shadow unless the arch says that it is ready for the memory to be accessed. This is fairly uninvasive and should have a negligible performance penalty. This will only work in outline mode, so an arch must specify ARCH_DISABLE_KASAN_INLINE if it requires this. Cc: Balbir Singh Cc: Aneesh Kumar K.V Suggested-by: Christophe Leroy Signed-off-by: Daniel Axtens --- I discuss the justfication for this later in the series. Also, both previous RFCs for ppc64 - by 2 different people - have needed this trick! See: - https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series - https://patchwork.ozlabs.org/patch/795211/ # ppc radix series --- include/linux/kasan.h | 4 ++++ mm/kasan/common.c | 4 ++++ mm/kasan/generic.c | 3 +++ mm/kasan/shadow.c | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 8b3b99d659b7..6bd8343f0033 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -23,6 +23,10 @@ struct kunit_kasan_expectation { #endif +#ifndef kasan_arch_is_ready +static inline bool kasan_arch_is_ready(void) { return true; } +#endif + #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) #include diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 6bb87f2acd4e..f23a9e2dce9f 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -345,6 +345,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) return false; + /* We can't read the shadow byte if the arch isn't ready */ + if (!kasan_arch_is_ready()) + return false; + if (!kasan_byte_accessible(tagged_object)) { kasan_report_invalid_free(tagged_object, ip); return true; diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 53cbf28859b5..c3f5ba7a294a 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -163,6 +163,9 @@ static __always_inline bool check_region_inline(unsigned long addr, size_t size, bool write, unsigned long ret_ip) { + if (!kasan_arch_is_ready()) + return true; + if (unlikely(size == 0)) return true; diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index 727ad4629173..1f650c521037 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -80,6 +80,10 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init) */ addr = kasan_reset_tag(addr); + /* Don't touch the shadow memory if arch isn't ready */ + if (!kasan_arch_is_ready()) + return; + /* Skip KFENCE memory if called explicitly outside of sl*b. */ if (is_kfence_address(addr)) return;