From patchwork Wed Aug 14 08:55:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13763125 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DD561C531DC for ; Wed, 14 Aug 2024 09:02:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9hz49lwxt/ore9sEZ/uYZptf0bZHlG9fVMF8g6BZbdk=; b=ZPM2Rz9GtocX3yyTX2MeAj8BHy NXIdG8S/DSUgUj6JzzsiU0SYufG0yMavtfGwFDfgPUbZV9bX9K0L2Bo6/HlNMlPWCz+jt9ebAnWhl sqoIA2ASxIutMevtZOB1v9DRFhmZ5ob1D9z/fH8G8QuGtMsYxVQNGuP4o/7g+KJfU5Tv5XAzgB+GR sRjYfmFi0L0R6GDA6XnOKLMslbZTAL1fP9+l9YkxKw6lReV8DzOsgMXQIazKRcL3Ha4kV/Smx0C8B sk5hMT8BNBmIqKiMnO3qYen/avwHgflfE4RzxEFnSR5p/wPLECq1ZoMEc17Q5oXzMPl9KKA+U4K9T i/4lyDfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1se9tJ-00000006Jno-2Lzn; Wed, 14 Aug 2024 09:02:01 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1se9o5-00000006IEV-4BZB for linux-arm-kernel@bombadil.infradead.org; Wed, 14 Aug 2024 08:56:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=9hz49lwxt/ore9sEZ/uYZptf0bZHlG9fVMF8g6BZbdk=; b=IKneuVzCPsHhPtUAbd36G4dy9d d1bQTsxx5gWoIzJAbS4WiLVxwq1JTfw+6NNaCzbwfgs6qT/8QCtVmaG3lZ59HATPsNEK7GMMo/bCb ONTFE8ZDi1Iv9cUGkFe9aXQGTbrmea/bQBMjyleWH6PAbvRwS1WNXovtNcUkhmIIz3sKCbScrni+D hrLkhj/F0JXxBqnzSamdE3t3ZF2MngNmlwg1TDtfFKqvO+lCGDl2F//qTIQElTZRxWASguaoB8NF4 s09mtzaP+B5r8PSUwQpmTLSzYh/u/L7sJdEdj9QHljXmJghcQPzr0wItybG+kke0FwzAMrdLl4iJ/ CqlneHLg==; Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1se9o2-00000007zGG-1URy for linux-arm-kernel@lists.infradead.org; Wed, 14 Aug 2024 08:56:36 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1fd78c165eeso51138275ad.2 for ; Wed, 14 Aug 2024 01:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723625793; x=1724230593; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9hz49lwxt/ore9sEZ/uYZptf0bZHlG9fVMF8g6BZbdk=; b=BmfK4k+OUsqPfzJxo03M8csv/ozcTptoALRIoq5jJFvG+ksMuL/7aD6KPhIb2CCCOU s8il86M7+jM79fZem5FQoz0oDbGuUIhgUuB5Ew5/gPF6opBDakFneB+Ptrdbnc3PWtXY fiXsf7TDv1Sdmb+7R4IBDKkom2gziRYiPHTwUF11O5gOEzDmN7NFYuKfK/otvXKQkIdr aOgKa6PS1a3/HTYAjIQtKy8jweCwTFmCXTzaKmAvW1gVw+QN3t2SBodCuQ569NN7uwRq Tfz06weKdshja9rdF4Qrgx0UsHvyh1zQNfT5I1VJwRxxyDRo/OoljnaEEgVpUM9AdXZR Cn8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723625793; x=1724230593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9hz49lwxt/ore9sEZ/uYZptf0bZHlG9fVMF8g6BZbdk=; b=id21WgQA/oMqzjMZsf2zWkhc96egqQBP/qC+Wgfbrb/VGbn3FxVV+GS1PbrJNhfGdm /l1VgHAWtWSA+fXzVCn4uVWD/ZG3YuEbTqzy9bIW1KiIb6ZHFU7wt1MWbn2Sd00z6AcJ VWRzgA8o7mbVz8bQHW8MB41m+WoDx+xUHQSmwShX4Q/TQSmQlsbQKZ2nILc/KPKC8rus psTpnsZo2V2lLi8rEY43Vte8zsmBAqX3GZt/XFQhtmXd8FMmt5TDnumgEGxki9t3bu7J wsN7SKdYkFQJVn9UqKAOrPR6m0bmnaka+p588VTd94ZnJlO+FxkupaXSmQEqGXstFs81 WD0A== X-Forwarded-Encrypted: i=1; AJvYcCWT0HR9007ebMRqOf/PoE8knecEjFtMZz9l7ymuLlWqSmgW/QBbUiZn3je7+fu5iy5PAfwkxlw2K+VVb7hWmWqZukUzWAwrEIXfPlVZwoXhtz5BBRY= X-Gm-Message-State: AOJu0Yyf3ccjcvoo8+lNXs/h/wiOToAXrYmUHI6cmihqNeuHnfR9La1d fSCh0vrvpePRFNur/qXdVWrmVq8veDhDCBqsx+LJtb1b7EFC2zVNPsxbbw1ZcPg= X-Google-Smtp-Source: AGHT+IEjBmXCU7mNTlJDHLEXVaA9qjlxh2UbxM2PLdod3xVt/jyJ0BnNxQ5PpAdVoFfN1q2eB+NLCQ== X-Received: by 2002:a17:903:1c9:b0:1fc:2ee3:d46f with SMTP id d9443c01a7336-201d638d797mr29420295ad.11.1723625792838; Wed, 14 Aug 2024 01:56:32 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201cd14a7b8sm25439615ad.100.2024.08.14.01.56.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 01:56:32 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, linux-kernel@vger.kernel.org, Alexandre Ghiti , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [RFC PATCH 7/7] kasan: sw_tags: Support runtime stack tagging control for RISC-V Date: Wed, 14 Aug 2024 01:55:35 -0700 Message-ID: <20240814085618.968833-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814085618.968833-1-samuel.holland@sifive.com> References: <20240814085618.968833-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240814_095634_550589_5E09D4B2 X-CRM114-Status: GOOD ( 11.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This allows the kernel to boot on systems without pointer masking support when stack tagging is enabled. Signed-off-by: Samuel Holland Reviewed-by: Andrey Konovalov --- mm/kasan/kasan.h | 2 ++ mm/kasan/sw_tags.c | 9 +++++++++ scripts/Makefile.kasan | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index fb2b9ac0659a..01e945cb111d 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -630,6 +630,8 @@ void *__asan_memset(void *addr, int c, ssize_t len); void *__asan_memmove(void *dest, const void *src, ssize_t len); void *__asan_memcpy(void *dest, const void *src, ssize_t len); +u8 __hwasan_generate_tag(void); + void __hwasan_load1_noabort(void *); void __hwasan_store1_noabort(void *); void __hwasan_load2_noabort(void *); diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c index 220b5d4c6876..32435d33583a 100644 --- a/mm/kasan/sw_tags.c +++ b/mm/kasan/sw_tags.c @@ -70,6 +70,15 @@ u8 kasan_random_tag(void) return (u8)(state % (KASAN_TAG_MAX + 1)); } +u8 __hwasan_generate_tag(void) +{ + if (!kasan_enabled()) + return KASAN_TAG_KERNEL; + + return kasan_random_tag(); +} +EXPORT_SYMBOL(__hwasan_generate_tag); + bool kasan_check_range(const void *addr, size_t size, bool write, unsigned long ret_ip) { diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index 390658a2d5b7..f64c1aca3e97 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -73,6 +73,11 @@ ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y) CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1) endif +# RISC-V requires dynamically determining if stack tagging can be enabled. +ifdef CONFIG_RISCV +CFLAGS_KASAN += $(call cc-param,hwasan-generate-tags-with-calls=1) +endif + endif # CONFIG_KASAN_SW_TAGS export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE