From patchwork Tue Oct 22 01:57:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844973 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 10CE3D17157 for ; Tue, 22 Oct 2024 01:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=VZUvlfvCUE6q+xvxhH+NDkvfHCGo6CbX0Xg5vLWAwBo=; b=QpIAC2EV3Edjtp J0IPw7aLQObNPaXQRRfBOh4nk8jdEhOdu4yg2/D6o9fnPDiiSaIxl4jCuSpnFTMJMlAS8nsmRkPsy 6zvcZrb8qqqaKqp+5FZMbSq3CV1+1gNngyivRjCAXiokP8rnzA4W+/KppCn+PGeW06U83o1gY7YrN Ko7JLja5/bFoyKoHft9yZLCDqsWETcG1SqmrC7kPD2v7YmUihTrmXXcwWOn1+uTrSzK+udT5afsLe YcLL2TqBT8mrY9sxkDDQ7Z+sB+O4ZxTL6BjGZ++4XTfqlxCgU70uEuwU7Ius0BIwyuOe5eKscvYP9 3Fw6628mGFJYKsvxDKNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t34B6-00000009Flo-3LE7; Tue, 22 Oct 2024 01:59:20 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t34B3-00000009Fk4-1DSd for linux-riscv@lists.infradead.org; Tue, 22 Oct 2024 01:59:18 +0000 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-2e3686088c3so3733194a91.0 for ; Mon, 21 Oct 2024 18:59:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562356; x=1730167156; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=e65ANAnizaofFjgpIDXOaUasil0HVOGTLixFGnIEx4s=; b=PxSleaQwkQmVEAMHbV745Hx3a4at9U6ZArzvlC4Ej/ktO8HjBiH1HOvSkX7B/pQp6y +IvUJ7wCvttULXoYplwuLTHBiw/orpTuwerrWRaKz564HdT2Amzjw0tajCFDmIDSfXYr /8ilfaiPo5gcHZbV7G8RIgdhxHD7bAeTUZGlxvC2FrH3dBRV5BWXgtsnel605J5ce+BF zTw7C4LiGh/fuMA8S6r6GJQKnDp9bu065OGtmj6pK0cvaipEDfjVecWUUX27mw4zpqoT CCZoMeEiCwEpbaeeIfHUbo+s6wF32n0GhaHkgrUGo8RDpnLGoicEGkMsYtlHl9xie3hW Du7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562356; x=1730167156; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e65ANAnizaofFjgpIDXOaUasil0HVOGTLixFGnIEx4s=; b=kfrGz7Gd070kKErQmqtngKYWC0SikeSYE3r0+BeEnOrs9Q4YLDgh48NqASulU1m7yu nd+0R/q3eD8Pp+ZVNyW8BNWBWPI3MCxHh0/+qvIvJprXRTDC0pqtNlkeO0tgaH0+eRLg FX+ljBaMQ/yMS8RIcyLlFdaKUHiEQUsfriQMSd4ym18Ro6ntlra2m+STQHJ7J+gOeLfL 9ZcC47hOuwivKMuQ5iZpoa8HMbIUzzsVHMRj6008BLbPxFXxkd+xQ33Fs/cKNKTIIfCO ce9J7S4qW+JziYw9q0jiSlmncYVAb9bTst901cnUrD7DjVyKV6jILRqb4F17Qtr+mVEt I1LA== X-Forwarded-Encrypted: i=1; AJvYcCXn/CQB/AD7/T4ZMR2ewqAw/4VxHjQCC9tXqI1Xm00u3wxFA5rMA/JuSN3WN/LbNdA3RPk+4Ry1aURbTg==@lists.infradead.org X-Gm-Message-State: AOJu0YyA634Hy+Rc3t/Vl6x/LaugfHh+L7na5b+UrOIkLTKC0t+nWy30 1vDSGGEVAfe3ZL1QogpwssdG/yQIeWS55SL9Nfi755o9RgcXAl/sApQn2A0q7ko= X-Google-Smtp-Source: AGHT+IEXsyYj+KgjHmYUKoNKrCcxY4nPfOvA/Yp1hV7DCU8lwbKfMca2QJF0T6KgMVKIPzFYrhGr9A== X-Received: by 2002:a05:6a20:d81b:b0:1d9:20cf:2c24 with SMTP id adf61e73a8af0-1d96b71566fmr2363833637.29.1729562356331; Mon, 21 Oct 2024 18:59:16 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:15 -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, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 0/9] kasan: RISC-V support for KASAN_SW_TAGS using pointer masking Date: Mon, 21 Oct 2024 18:57:08 -0700 Message-ID: <20241022015913.3524425-1-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_185917_373988_66AB83FE X-CRM114-Status: GOOD ( 22.35 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This series implements support for software tag-based KASAN using the RISC-V pointer masking extension[1], which supports 7 and/or 16-bit tags. This implementation uses 7-bit tags, so it is compatible with either hardware mode. Patch 4 adds supports for KASAN_SW_TAGS with tag widths other than 8 bits. Pointer masking is an optional ISA extension, and it must be enabled using an SBI call to firmware on each CPU. If the SBI call fails on the boot CPU, KASAN is globally disabled. Patch 2 adds support for boot-time disabling of KASAN_SW_TAGS, and patch 3 adds support for runtime control of stack tagging. Patch 1 is an optimization that could be applied separately. It is included here because it affects the selection of KASAN_SHADOW_OFFSET. This implementation currently passes the KASAN KUnit test suite: # kasan: pass:64 fail:0 skip:9 total:73 # Totals: pass:64 fail:0 skip:9 total:73 ok 1 kasan One workaround is required to pass the vmalloc_percpu test. I have to shrink the initial percpu area to force the use of a KASAN-tagged percpu area in the test (depending on .config, this workaround is also needed on arm64 without this series applied, so it is not a new issue): masking, the kernel still boots successfully: kasan: test: Can't run KASAN tests with KASAN disabled # kasan: # failed to initialize (-1) not ok 1 kasan This series can be tested by applying patch series to LLVM[2] and QEMU[3], and using the master branch of OpenSBI[4]. [1]: https://github.com/riscv/riscv-j-extension/raw/d70011dde6c2/zjpm-spec.pdf [2]: https://github.com/SiFiveHolland/llvm-project/commits/up/riscv64-kernel-hwasan [3]: https://lore.kernel.org/qemu-devel/20240511101053.1875596-1-me@deliversmonkey.space/ [4]: https://github.com/riscv-software-src/opensbi/commit/1cb234b1c9ed Changes in v2: - Improve the explanation for how KASAN_SHADOW_END is derived - Update the range check in kasan_non_canonical_hook() - Split the generic and RISC-V parts of stack tag generation control to avoid breaking bisectability - Add a patch to call kasan_non_canonical_hook() on riscv - Fix build error with KASAN_GENERIC - Use symbolic definitons for SBI firmware features call - Update indentation in scripts/Makefile.kasan - Use kasan_params to set hwasan-generate-tags-with-calls=1 Clément Léger (1): riscv: Add SBI Firmware Features extension definitions Samuel Holland (8): kasan: sw_tags: Use arithmetic shift for shadow computation kasan: sw_tags: Check kasan_flag_enabled at runtime kasan: sw_tags: Support outline stack tag generation kasan: sw_tags: Support tag widths less than 8 bits riscv: mm: Log potential KASAN shadow alias riscv: Do not rely on KASAN to define the memory layout riscv: Align the sv39 linear map to 16 GiB riscv: Implement KASAN_SW_TAGS Documentation/arch/riscv/vm-layout.rst | 10 ++--- Documentation/dev-tools/kasan.rst | 14 +++--- arch/arm64/Kconfig | 10 ++--- arch/arm64/include/asm/kasan.h | 6 ++- arch/arm64/include/asm/memory.h | 17 ++++++- arch/arm64/include/asm/uaccess.h | 1 + arch/arm64/mm/kasan_init.c | 7 ++- arch/riscv/Kconfig | 4 +- arch/riscv/include/asm/cache.h | 4 ++ arch/riscv/include/asm/kasan.h | 29 +++++++++++- arch/riscv/include/asm/page.h | 21 +++++++-- arch/riscv/include/asm/pgtable.h | 6 +++ arch/riscv/include/asm/sbi.h | 28 ++++++++++++ arch/riscv/include/asm/tlbflush.h | 4 +- arch/riscv/kernel/setup.c | 6 +++ arch/riscv/kernel/smpboot.c | 8 +++- arch/riscv/lib/Makefile | 2 + arch/riscv/lib/kasan_sw_tags.S | 61 ++++++++++++++++++++++++++ arch/riscv/mm/fault.c | 3 ++ arch/riscv/mm/init.c | 2 +- arch/riscv/mm/kasan_init.c | 32 +++++++++++++- arch/riscv/mm/physaddr.c | 4 ++ include/linux/kasan-enabled.h | 15 +++---- include/linux/kasan-tags.h | 13 +++--- include/linux/kasan.h | 10 ++++- mm/kasan/hw_tags.c | 10 ----- mm/kasan/kasan.h | 2 + mm/kasan/report.c | 22 ++++++++-- mm/kasan/sw_tags.c | 9 ++++ mm/kasan/tags.c | 10 +++++ scripts/Makefile.kasan | 5 +++ scripts/gdb/linux/mm.py | 5 ++- 32 files changed, 313 insertions(+), 67 deletions(-) create mode 100644 arch/riscv/lib/kasan_sw_tags.S diff --git a/include/linux/percpu.h b/include/linux/percpu.h index b6321fc49159..26b97c79ad7c 100644 --- a/include/linux/percpu.h +++ b/include/linux/percpu.h @@ -43,7 +43,7 @@ #ifdef CONFIG_RANDOM_KMALLOC_CACHES #define PERCPU_DYNAMIC_SIZE_SHIFT 12 #else -#define PERCPU_DYNAMIC_SIZE_SHIFT 10 +#define PERCPU_DYNAMIC_SIZE_SHIFT 8 #endif When running with hardware or firmware that doesn't support pointer