From patchwork Thu Jun 17 09:30:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 12327107 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 33AA5C49361 for ; Thu, 17 Jun 2021 09:30:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D3972610CA for ; Thu, 17 Jun 2021 09:30:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D3972610CA 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 74AD86B0072; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7215F6B0073; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C41B6B0074; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id 25BB16B0072 for ; Thu, 17 Jun 2021 05:30:48 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id C71A81A4BE for ; Thu, 17 Jun 2021 09:30:47 +0000 (UTC) X-FDA: 78262696134.38.EB5F839 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf08.hostedemail.com (Postfix) with ESMTP id B50C5801934C for ; Thu, 17 Jun 2021 09:30:36 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id v12so2608986plo.10 for ; Thu, 17 Jun 2021 02:30:47 -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=jAXfPAXKGHI5d5AfauBRh2rw8y0WXn7UCziFxxBa2G8=; b=J9G3pN4FhbqRJPqASQ8qt+CEBkWuvvRU/rw38HT/3meNPQk+HthB+65dgOlLVPsG/+ OgtxlTlJ26xmGHZ34aiyGW1dqvAoOBnFAsJcoqPGmRFN49OrSQjpIAEfsPIblsbQsb8R sfesjLQh+52EJAox9Ost1FtWIufCsCNqgf0yI= 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=jAXfPAXKGHI5d5AfauBRh2rw8y0WXn7UCziFxxBa2G8=; b=g772Lgi2Nn0XVE9t7yzJDNndNw7QfqG3BzAo/eq6BjW1MEUR9uLZnMWHh9nAIlbVdv SKSW3OuyWaCdLuZbCH1wolOuSnth+A7DKl/D0ePTi48hAivInNTQSVb4s2AFYJQbMD8D jAs35Pcl5fmqtdKSeNImdj1zCCgtTVfwy30gfHVkfm6k64XGsCD3aFjLu3LrLjfL+Usu spqyQztHsy2TlygZAQucpELVFLVK1Wf3VSyydBkSSWg1M2/2uFjuee2VfTu3Gsvc7/tF fOy9CKwc9KxBhu4EWoQPyJzwCRhSduOJGXwSTFVDUJ67GMlbVqzPae7C9EkpXRkBAk7i r23w== X-Gm-Message-State: AOAM533Nyr8mDzJ2w+iGWPG8KaTSHymX30VzrRkqjY6NPpIqvBhPxmGw 8FILunbnWunYg8Eohj7vG6iSDA== X-Google-Smtp-Source: ABdhPJziDsm92Q8ZeF77qbXKOrLgOBSfL1/bSWAlt+9ZfcRv3k6RL55+fUErfACWDJuxtEoYMzljXQ== X-Received: by 2002:a17:90a:f094:: with SMTP id cn20mr4668681pjb.157.1623922246569; Thu, 17 Jun 2021 02:30:46 -0700 (PDT) Received: from localhost ([203.206.29.204]) by smtp.gmail.com with ESMTPSA id t14sm5692272pgm.9.2021.06.17.02.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:30:46 -0700 (PDT) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, elver@google.com, akpm@linux-foundation.org, andreyknvl@gmail.com Cc: linuxppc-dev@lists.ozlabs.org, christophe.leroy@csgroup.eu, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com, Daniel Axtens Subject: [PATCH v15 2/4] kasan: allow architectures to provide an outline readiness check Date: Thu, 17 Jun 2021 19:30:30 +1000 Message-Id: <20210617093032.103097-3-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210617093032.103097-1-dja@axtens.net> References: <20210617093032.103097-1-dja@axtens.net> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: B50C5801934C Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=axtens.net header.s=google header.b=J9G3pN4F; spf=pass (imf08.hostedemail.com: domain of dja@axtens.net designates 209.85.214.179 as permitted sender) smtp.mailfrom=dja@axtens.net; dmarc=none X-Stat-Signature: yrdzmwd31iadtzcafc1w9sxgg8h5rfxr X-HE-Tag: 1623922236-545240 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 Reviewed-by: Marco Elver Signed-off-by: Daniel Axtens --- 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 Build tested on arm64 with SW_TAGS and x86 with INLINE: the error fires if I add a kasan_arch_is_ready define. --- mm/kasan/common.c | 4 ++++ mm/kasan/generic.c | 3 +++ mm/kasan/kasan.h | 6 ++++++ mm/kasan/shadow.c | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 10177cc26d06..0ad615f3801d 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -331,6 +331,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, u8 tag; void *tagged_object; + /* Bail if the arch isn't ready */ + if (!kasan_arch_is_ready()) + return false; + tag = get_tag(object); tagged_object = object; object = kasan_reset_tag(object); 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/kasan.h b/mm/kasan/kasan.h index 8f450bc28045..4dbc8def64f4 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -449,6 +449,12 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) { #endif /* CONFIG_KASAN_GENERIC */ +#ifndef kasan_arch_is_ready +static inline bool kasan_arch_is_ready(void) { return true; } +#elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE) +#error kasan_arch_is_ready only works in KASAN generic outline mode! +#endif + /* * Exported functions for interfaces called from assembly or from generated * code. Declarations here to avoid warning about missing declarations. diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index 082ee5b6d9a1..3c7f7efe6f68 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -73,6 +73,10 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init) { void *shadow_start, *shadow_end; + /* Don't touch the shadow memory if arch isn't ready */ + if (!kasan_arch_is_ready()) + return; + /* * Perform shadow offset calculation based on untagged address, as * some of the callers (e.g. kasan_poison_object_data) pass tagged @@ -99,6 +103,10 @@ EXPORT_SYMBOL(kasan_poison); #ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule(const void *addr, size_t size) { + /* Don't touch the shadow memory if arch isn't ready */ + if (!kasan_arch_is_ready()) + return; + if (size & KASAN_GRANULE_MASK) { u8 *shadow = (u8 *)kasan_mem_to_shadow(addr + size); *shadow = size & KASAN_GRANULE_MASK;