From patchwork Thu Oct 29 13:16:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 11866463 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=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 8E38BC2D0A3 for ; Thu, 29 Oct 2020 13:45:37 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 00415207DE for ; Thu, 29 Oct 2020 13:45:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="KwyhRKnF"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Ahi/J3DS"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="kQJZtCyA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 00415207DE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=/dm6VJoBLKYZUI6Tn1mbcv8/ugCxImzuYBTu1ZA8aqc=; b=KwyhRKnFRx/GjLlFuPNFtVlxj d36hEmeouNbDViQp5SWb4sWTJiFRAGkZiK2e7H6WzZo1LSYqbJYG3a5KRq+fyg26Kv5Q9R91cEMg8 NRTgf2NIB6bblvFeq0OsEp9M+km1Zk4RN5vDlj8VGpswSd+GZ05ZXzEfTcdE99v0ucnWcxMCYmnFu CK0YEZPkxYc52ItNfFwSeLIV+EJUKtnZtzahLLp/RYpIZgz+sDKgr/OkzuayV3g0kaMQGhgRWnUvq BUNLvDlAp6y0biIxYv5BM30kZCxdriNuJV8H9NWjewFnujJ0S6E0SDKfaUAm8Z+321jfHPJ7xxcWl /K6dxXF3A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kY8Cm-00088n-3Q; Thu, 29 Oct 2020 13:43:05 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kY8Ap-0007Dr-EY for linux-arm-kernel@merlin.infradead.org; Thu, 29 Oct 2020 13:41:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=MDxWQURSl79g8vKPdG5uHAVWcBR/CQ6D0GDay6Dns1U=; b=Ahi/J3DS2qo/dyU8bTVXEnZL1+ DFbUYdsL5Mp/tS3MCMR8s6Ntx9ViisFzvuexeocbKPFboqDdE+R5HHgZPofLmw3K2lnkgQhrUD1Iq 0KFrd/U4FhrIOjQ3OQ6EpDvmKSQm8okX8ECOks4Jjpl5s8Do4FIAezr+ucUhj7Z4Bjg7niauANPor PuQ5BhiyK+MctqE4l5ySjgbLNx41mbMHxcztG/Odndb5L1QA9Jb6clCdmz13USXXwo00OkGmRE7EO cA05mTMXi3j3Kq7ZbXNvtPp30Wsj3870fUM6b3oH64QrLnvYoNtdhH8Ovem35VqeOk3tXnTH1gxdn XYxHadhA==; Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kY7no-00074r-4f for linux-arm-kernel@lists.infradead.org; Thu, 29 Oct 2020 13:17:19 +0000 Received: by mail-wm1-x34a.google.com with SMTP id t21so463001wmt.8 for ; Thu, 29 Oct 2020 06:17:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=MDxWQURSl79g8vKPdG5uHAVWcBR/CQ6D0GDay6Dns1U=; b=kQJZtCyAjs6lBIZl6M96SWe1LnR8kpabQD8Fy+8pAgMgKbEuOFIyCrpy6wfOXCw2/p 9ZagryngHTQS2HvLFfpcG9gPJ+G1tXFcG2pbAW5wd2iBRpwHNRPdvcatZG3UhMoqpAF2 ZZiC7KFjIpRZOvWEtiYrwNGwupA73SczxDjpZwhnuaWaN0Ke5NtQM3//jXL9XWjOHmXZ MkGZ73JdSkLq+TxhjDetAIxGstREfAbKu7WPvQ231Lv+4LGRoRm3tLfpwZwDIbAnoFkx E5kILUZJ60tJnonGxepa3ElL6illiCAPgpl6vE1zbwnzRtEcmGmWRQ+IKAcFRDnvdGuY Nj9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=MDxWQURSl79g8vKPdG5uHAVWcBR/CQ6D0GDay6Dns1U=; b=kTaFctSYQssy3SbQ0cLiM56u9hqzZq4B8pg8wNXjEIPMqMtfW+F+VK4EXrfJ3D7Ccs ueam2m8RPeSNz1OCXpLGwQ6kC84m0mE0drXo/McHNR0krOTgoAXjfR+GYJKQEl1QAzw2 1dV34xmNIXL7/8KKv7ahsoeHGrX3hI9d8JD4PSGHElQJ/9oIWhanqz/QPrQTK0JN2Dxb pOJNX8ztoSzDjGL64rF4z9f83zmmKOxounh2/1oB1SDYW3PrNp2QuyO07LgRa9hhpcMV mp9kbDH/APdmjRikrJ12FjpVv8S6n3ExmOWlMjGw5+yg7eApDu3QUfY57094KiXO8tm7 CWTQ== X-Gm-Message-State: AOAM533EyDK/07II8Y0e4BDOwbW+aGWehMV3X+J6glWubMSc2zArQaDj ywmEfih26uM1Wa2YO4y35ptTPAyBfw== X-Google-Smtp-Source: ABdhPJwtl8iFb0b8559uBiSux7XVBqCBPqzo+++eeYJgPPUGzTPe4wnLZlOGRZttf8xQ8fTS3kLmR6uqjQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:a1c:2901:: with SMTP id p1mr4533662wmp.170.1603977433686; Thu, 29 Oct 2020 06:17:13 -0700 (PDT) Date: Thu, 29 Oct 2020 14:16:46 +0100 In-Reply-To: <20201029131649.182037-1-elver@google.com> Message-Id: <20201029131649.182037-7-elver@google.com> Mime-Version: 1.0 References: <20201029131649.182037-1-elver@google.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v6 6/9] kfence, kasan: make KFENCE compatible with KASAN From: Marco Elver To: elver@google.com, akpm@linux-foundation.org, glider@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201029_131716_575773_E5B9B04B X-CRM114-Status: GOOD ( 17.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, hdanton@sina.com, linux-doc@vger.kernel.org, peterz@infradead.org, catalin.marinas@arm.com, dave.hansen@linux.intel.com, linux-mm@kvack.org, edumazet@google.com, hpa@zytor.com, cl@linux.com, will@kernel.org, sjpark@amazon.com, corbet@lwn.net, x86@kernel.org, kasan-dev@googlegroups.com, mingo@redhat.com, vbabka@suse.cz, rientjes@google.com, aryabinin@virtuozzo.com, joern@purestorage.com, keescook@chromium.org, paulmck@kernel.org, jannh@google.com, andreyknvl@google.com, bp@alien8.de, luto@kernel.org, Jonathan.Cameron@huawei.com, tglx@linutronix.de, dvyukov@google.com, linux-arm-kernel@lists.infradead.org, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, penberg@kernel.org, iamjoonsoo.kim@lge.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Alexander Potapenko We make KFENCE compatible with KASAN for testing KFENCE itself. In particular, KASAN helps to catch any potential corruptions to KFENCE state, or other corruptions that may be a result of freepointer corruptions in the main allocators. To indicate that the combination of the two is generally discouraged, CONFIG_EXPERT=y should be set. It also gives us the nice property that KFENCE will be build-tested by allyesconfig builds. Reviewed-by: Dmitry Vyukov Co-developed-by: Marco Elver Signed-off-by: Marco Elver Signed-off-by: Alexander Potapenko Reviewed-by: Jann Horn --- v5: * Also guard kasan_unpoison_shadow with is_kfence_address(), as it may be called from SL*B internals, currently ksize(). * Make kasan_record_aux_stack() compatible with KFENCE, which may be called from outside KASAN runtime. --- lib/Kconfig.kfence | 2 +- mm/kasan/common.c | 15 +++++++++++++++ mm/kasan/generic.c | 3 ++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/Kconfig.kfence b/lib/Kconfig.kfence index d24baa3bce4a..639b48cc75d4 100644 --- a/lib/Kconfig.kfence +++ b/lib/Kconfig.kfence @@ -5,7 +5,7 @@ config HAVE_ARCH_KFENCE menuconfig KFENCE bool "KFENCE: low-overhead sampling-based memory safety error detector" - depends on HAVE_ARCH_KFENCE && !KASAN && (SLAB || SLUB) + depends on HAVE_ARCH_KFENCE && (!KASAN || EXPERT) && (SLAB || SLUB) depends on JUMP_LABEL # To ensure performance, require jump labels select STACKTRACE help diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 950fd372a07e..ac1d404fb41e 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,14 @@ void kasan_unpoison_shadow(const void *address, size_t size) */ address = reset_tag(address); + /* + * We may be called from SL*B internals, such as ksize(): with a size + * not a multiple of machine-word size, avoid poisoning the invalid + * portion of the word for KFENCE memory. + */ + if (is_kfence_address(address)) + return; + kasan_poison_shadow(address, size, tag); if (size & KASAN_SHADOW_MASK) { @@ -396,6 +405,9 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, tagged_object = object; object = reset_tag(object); + if (is_kfence_address(object)) + return false; + if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) != object)) { kasan_report_invalid_free(tagged_object, ip); @@ -444,6 +456,9 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object, if (unlikely(object == NULL)) return NULL; + if (is_kfence_address(object)) + return (void *)object; + redzone_start = round_up((unsigned long)(object + size), KASAN_SHADOW_SCALE_SIZE); redzone_end = round_up((unsigned long)object + cache->object_size, diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 248264b9cb76..1069ecd1cd55 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -332,7 +333,7 @@ void kasan_record_aux_stack(void *addr) struct kasan_alloc_meta *alloc_info; void *object; - if (!(page && PageSlab(page))) + if (is_kfence_address(addr) || !(page && PageSlab(page))) return; cache = page->slab_cache;