From patchwork Tue Dec 1 16:16:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 11943225 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.5 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,URIBL_BLOCKED,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 EC946C64E7A for ; Tue, 1 Dec 2020 16:16:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 53A4322240 for ; Tue, 1 Dec 2020 16:16:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=axtens.net header.i=@axtens.net header.b="nMVol2Mb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 53A4322240 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 DA3348D0006; Tue, 1 Dec 2020 11:16:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D53508D0001; Tue, 1 Dec 2020 11:16:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF3EB8D0006; Tue, 1 Dec 2020 11:16:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id A828A8D0001 for ; Tue, 1 Dec 2020 11:16:48 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 624EC82499A8 for ; Tue, 1 Dec 2020 16:16:48 +0000 (UTC) X-FDA: 77545216896.06.toy21_03090b7273ab Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 26276100529C6 for ; Tue, 1 Dec 2020 16:16:48 +0000 (UTC) X-HE-Tag: toy21_03090b7273ab X-Filterd-Recvd-Size: 6191 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Dec 2020 16:16:47 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id f17so1456515pge.6 for ; Tue, 01 Dec 2020 08:16:46 -0800 (PST) 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=ktiBTqbjFAD6ffRKByOoWmP2o55vmJOJ49Xz+HTLOBA=; b=nMVol2MbqeRm1ME8ZCx4q5FQy/6FnTLRoo0uT3QwUXVdnICzf95JYxDl0Ku/Qfm8KB m7ud9zijGlPMWe4ffBlX6sLOdDZv6+jzTqcJlKX7lDk2SfpA8M+f8bXeZn6PAWFk3oe4 f5g1svqNrW2w5c/+vabfoLINQtbdLDQ0Z4msI= 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=ktiBTqbjFAD6ffRKByOoWmP2o55vmJOJ49Xz+HTLOBA=; b=B2H87sY+HcdW54JJ4/bOwlpJajgqst0YbX1YVdNCWmx235x3yjJw9hYQW7T6hpT3T2 W0jIXfnvpAKgMRF5GApHWUqMO0t4oUb/7lxXEnXyCDXSxOA02Nj4Pl8Q1YQgPszsaDwU ONh7Hp9CNomfxebbqJ+0bx5sHWeiflCSuHj1jabdj9YnHX1tALqvgMnpHOiOtkByN0Xo aalc2Ulhn1QdpiD+cttDoXG32+pEKl2gFABsWzUyC9rIRAj/yRZLMZTUq9i8RiYVo7bk 6SNuWBuODAYzWujoRalKBQbpfnFnm/M6TwZV17eK7zbQFJOavALrdPpWxxqpxevF4Z+Z hWew== X-Gm-Message-State: AOAM531y5FRZwP9YX53u+oZX9CBDXpLBXKk1miBsp6z/Y/YiMz/QJTm5 Fu9eKnaGHyMdspzft3srSQ/1KA== X-Google-Smtp-Source: ABdhPJxe/iHg9AKjvKSBXr2X/NJHI4J9BcoDNkvJXtHFX2PKH0KNKCgpa3c2HYXTBvQEAO5qretNPg== X-Received: by 2002:a62:7f56:0:b029:18b:a70:4f76 with SMTP id a83-20020a627f560000b029018b0a704f76mr3167081pfd.8.1606839406112; Tue, 01 Dec 2020 08:16:46 -0800 (PST) Received: from localhost (2001-44b8-111e-5c00-f932-2db6-916f-25e2.static.ipv6.internode.on.net. [2001:44b8:111e:5c00:f932:2db6:916f:25e2]) by smtp.gmail.com with ESMTPSA id jz7sm160129pjb.14.2020.12.01.08.16.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 08:16:45 -0800 (PST) From: Daniel Axtens To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, kasan-dev@googlegroups.com, christophe.leroy@c-s.fr, aneesh.kumar@linux.ibm.com, bsingharora@gmail.com Cc: Daniel Axtens , "Aneesh Kumar K . V" Subject: [PATCH v9 2/6] kasan: allow architectures to provide an outline readiness check Date: Wed, 2 Dec 2020 03:16:28 +1100 Message-Id: <20201201161632.1234753-3-dja@axtens.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201201161632.1234753-1-dja@axtens.net> References: <20201201161632.1234753-1-dja@axtens.net> MIME-Version: 1.0 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 HAVE_ARCH_NO_KASAN_INLINE if it requires this. Cc: Balbir Singh Cc: Aneesh Kumar K.V Signed-off-by: Christophe Leroy Signed-off-by: Daniel Axtens Signed-off-by: has to be the author of the patch. --- 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 | 10 ++++++++++ mm/kasan/generic.c | 3 +++ 3 files changed, 17 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 30d343b4a40a..3df66fdf6662 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -20,6 +20,10 @@ struct kunit_kasan_expectation { bool report_found; }; +#ifndef kasan_arch_is_ready +static inline bool kasan_arch_is_ready(void) { return true; } +#endif + extern unsigned char kasan_early_shadow_page[PAGE_SIZE]; extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE]; extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD]; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 950fd372a07e..ba7744d3e319 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -117,6 +117,9 @@ void kasan_poison_shadow(const void *address, size_t size, u8 value) { void *shadow_start, *shadow_end; + 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 @@ -134,6 +137,9 @@ void kasan_unpoison_shadow(const void *address, size_t size) { u8 tag = get_tag(address); + if (!kasan_arch_is_ready()) + return; + /* * Perform shadow offset calculation based on untagged address, as * some of the callers (e.g. kasan_unpoison_object_data) pass tagged @@ -406,6 +412,10 @@ static 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; + shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object)); if (shadow_invalid(tag, shadow_byte)) { kasan_report_invalid_free(tagged_object, ip); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 248264b9cb76..e87404026b2b 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -169,6 +169,9 @@ static __always_inline bool check_memory_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;