Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp279740pxj; Thu, 17 Jun 2021 02:32:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7tSYnwaVCN6R1M2V8yoLdc3aDHrI0jzsKaN4XXv7PmxZH54VDaWdNegJIsaJQn+g5Ge30 X-Received: by 2002:a02:8521:: with SMTP id g30mr3682683jai.113.1623922323824; Thu, 17 Jun 2021 02:32:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623922323; cv=none; d=google.com; s=arc-20160816; b=PcxURXwAxHIrHiACqJ5K/cjDIUdcVbuRCGrWIi1qt//f5aE0H0FfoI7T7V7V7uIjKb x96drlWt4TMU8VJbso5vEZiyOmzFYSD4sl3zOTnsZ1D3MhII+6DBYP1znEN+SFx2f/aC emu7fBXtZvan/C+fLX7dNdvxHwqxfK0net6aajPq2ivN58uRHuAL/ADbz4e6ntXgOhA1 WDBuMb0ZcZ9f0iaSmdKo1dBHE+2rIQ4HrKxoM6m1AmME9phmBFPxkcY/3o7wzulLShPm 6M++tHztCdQyAFktxIDYj5EAbr5OvldF7VIkGZ6X8GlXIbqCFMKb5C4heiMu0tNCZnFL QLug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jAXfPAXKGHI5d5AfauBRh2rw8y0WXn7UCziFxxBa2G8=; b=oVT/TxJAtv+XBjLgxQFXn+mbhWPbRvxvUEyne9f4V/ovaoQ13SMubdBdNnCZRdDvXt 1/+S/6VeuSWri+V3ZUqqpxzDKOuTPD9UErJR04SdZpwlQeiN/VDcO36lUHleLtf+uCdG oyfi2JI6bytT4byU4HoE4vqrTpDZTxqvGHYz42Y9ZzTf94dGRIeEacfKHkQl17i1oeLY QgIp8/C2t/pOGzfOWOQcaLzFO/7mu/mOkswD1l7s5wf+c9G4x6FeUlkTo0NgRUIEowLW gE3ZXRaQcxNRDSccbBryhPrZmvvitVNdF0INvkoBob7UB/6z0cRWnG1PIF/eQi0Sh3Qk oJjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=J9G3pN4F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a2si5560594jaq.37.2021.06.17.02.31.51; Thu, 17 Jun 2021 02:32:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@axtens.net header.s=google header.b=J9G3pN4F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231651AbhFQJcy (ORCPT + 99 others); Thu, 17 Jun 2021 05:32:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231648AbhFQJcy (ORCPT ); Thu, 17 Jun 2021 05:32:54 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3596FC061574 for ; Thu, 17 Jun 2021 02:30:47 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id ei4so3435560pjb.3 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=UWmsS9jw7bQ7TaGNNyF5KzlTxAn1uKmQlm3DiJlgEOpxZDLklAnEssiYRRsYyEVsjC s3VKRp8/e+w8Rc3YwbvQ8u3h+6Y+1LVaky1GXhIB9GkNBjzF2X17v8MW+nXsGW0nOXsi +y6CjYLDl8aB9hx6saA/NyDNYkOZ/ZG2v00IPxnWLYEj1da0ifikOVYipUVYO8fDrIUv 9pwFe7MnQPZn9A0ZKRtytKzmva0EY2ENpLDjG8Jg9dsXBjg16FX5FUEzRKHkqlG9xIq7 v8ixW/ged1RToN1wabfZkEiebdBDNmTwIDUSOgOCyNT8M0fnT4fBD1kBOnE1oxyFNYuX j9fQ== X-Gm-Message-State: AOAM533VXI1tmpY62D8Z0n9bh1Xf4EWwsPa0aYEbrbMBZI25yjUwFylm ijgOhMqy53tdt2HwqF+dTfzZiXaYT5Lbqw== 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.30.2