From patchwork Wed Sep 11 14:33:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fares Mehanna X-Patchwork-Id: 13800754 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 71F42EE49BC for ; Wed, 11 Sep 2024 14:53:08 +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: Content-Type: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=OYjq6pbgVwn9b81T7cFK3VzB+SX95+WNYatfu9MFnys=; b=u66xvdBGeKtd0E9lQ0SbvPqD8X qAm5HRvVUAWVwQwIxqb9YDwXpgdKEuqTIO+CxseneRVtK8ojS8qxFzfcbjGJ4mFGKj4eJQAEwdoxK EMRfGGUtzaJETIvWHVvd+4q8ZLhEXm9gIQftuu4jKIboKvfG9WH6+u61K65VGPzJZljOhh067IE1w VLEfK1G0YqSjtDXcAix5MyjgLkkkKWm91kTEI99a33PBluTPIfeGrVBlGVlNn93cof0muQMzEhHGy Suh/6nOpAUgNufh6oVerHUcE4Iuc/ccTgM17+NlNDzDn3MVB7YdI4D55Ru73t0pizkt+FwEoizmS5 TSmdqhmA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1soOiH-0000000A1Tq-0y6j; Wed, 11 Sep 2024 14:52:57 +0000 Received: from smtp-fw-80007.amazon.com ([99.78.197.218]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1soORK-00000009wC0-37dx for linux-arm-kernel@lists.infradead.org; Wed, 11 Sep 2024 14:35:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1726065327; x=1757601327; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=OYjq6pbgVwn9b81T7cFK3VzB+SX95+WNYatfu9MFnys=; b=axIMxfzAcT8XSB+pmM+KFz6KqisyR++L5Dx5eDtvhp8DoE+rde1ZWj62 7mmZ/+afekxxKG9lTSpCFcGsbbJFDchcnk68tkSY4t4QsH/zCZvvT+BzG JdUxIYfMlgjFk96t3bGmu9lziQeauZIAUoEn3koveJMFF+TIa04PeYwFi 8=; X-IronPort-AV: E=Sophos;i="6.10,220,1719878400"; d="scan'208";a="329955903" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Sep 2024 14:35:20 +0000 Received: from EX19MTAEUA002.ant.amazon.com [10.0.10.100:6114] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.5.52:2525] with esmtp (Farcaster) id 509ab7d1-9dbf-4d4d-b643-5a7e7b0c884b; Wed, 11 Sep 2024 14:35:18 +0000 (UTC) X-Farcaster-Flow-ID: 509ab7d1-9dbf-4d4d-b643-5a7e7b0c884b Received: from EX19D007EUB001.ant.amazon.com (10.252.51.82) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 11 Sep 2024 14:35:17 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D007EUB001.ant.amazon.com (10.252.51.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Wed, 11 Sep 2024 14:35:17 +0000 Received: from dev-dsk-faresx-1b-27755bf1.eu-west-1.amazon.com (10.253.79.181) by mail-relay.amazon.com (10.252.135.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 11 Sep 2024 14:35:14 +0000 From: Fares Mehanna To: CC: , Fares Mehanna , "Marc Zyngier" , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , "Will Deacon" , Andrew Morton , "Kemeng Shi" , =?utf-8?q?Pierre-Cl=C3=A9ment_Tos?= =?utf-8?q?i?= , Ard Biesheuvel , Mark Rutland , Javier Martinez Canillas , "Arnd Bergmann" , Fuad Tabba , Mark Brown , Joey Gouly , Kristina Martsenko , Randy Dunlap , "Bjorn Helgaas" , Jean-Philippe Brucker , "Mike Rapoport (IBM)" , "David Hildenbrand" , Roman Kagan , "moderated list:KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64)" , "open list:KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64)" , open list , "open list:MEMORY MANAGEMENT" Subject: [RFC PATCH 0/7] support for mm-local memory allocations and use it Date: Wed, 11 Sep 2024 14:33:59 +0000 Message-ID: <20240911143421.85612-1-faresx@amazon.de> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240911_073527_283062_C5FFC47F X-CRM114-Status: GOOD ( 19.58 ) 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 In a series posted a few years ago [1], a proposal was put forward to allow the kernel to allocate memory local to a mm and thus push it out of reach for current and future speculation-based cross-process attacks. We still believe this is a nice thing to have. However, in the time passed since that post Linux mm has grown quite a few new goodies, so we'd like to explore possibilities to implement this functionality with less effort and churn leveraging the now available facilities. An RFC was posted few months back [2] to show the proof of concept and a simple test driver. In this RFC, we're using the same approach of implementing mm-local allocations piggy-backing on memfd_secret(), using regular user addresses but pinning the pages and flipping the user/supervisor flag on the respective PTEs to make them directly accessible from kernel. In addition to that we are submitting 5 patches to use the secret memory to hide the vCPU gp-regs and fp-regs on arm64 VHE systems. The generic drawbacks of using user virtual addresses mentioned in the previous RFC [2] still hold, in addition to a more specific one: - While the user virtual addresses allocated for kernel secret memory are not directly accessible by userspace as the PTEs restrict that, copy_from_user() and copy_to_user() can operate on those ranges, so that e.g. the usermode can guess the address and pass it as the target buffer for read(), making the kernel overwrite it with the user-controlled content. Effectively making the secret memory in the current implementation missing confidentiality and integrity guarantees. In the specific case of vCPU registers, this is fine because the owner process can read and write to them using KVM IOCTLs anyway. But in the general case this represents a security concern and needs to be addressed. A possible way forward for the arch-agnostic implementation is to limit the user virtual addresses used for kernel to specific range that can be checked against in copy_from_user() and copy_to_user(). For arch specific implementation, using separate PGD is the way to go. [1] https://lore.kernel.org/lkml/20190612170834.14855-1-mhillenb@amazon.de/ [2] https://lore.kernel.org/lkml/20240621201501.1059948-1-rkagan@amazon.de/ Fares Mehanna / Roman Kagan (2): mseal: expose interface to seal / unseal user memory ranges mm/secretmem: implement mm-local kernel allocations Fares Mehanna (5): arm64: KVM: Refactor C-code to access vCPU gp-registers through macros KVM: Refactor Assembly-code to access vCPU gp-registers through a macro arm64: KVM: Allocate vCPU gp-regs dynamically on VHE and KERNEL_SECRETMEM enabled systems arm64: KVM: Refactor C-code to access vCPU fp-registers through macros arm64: KVM: Allocate vCPU fp-regs dynamically on VHE and KERNEL_SECRETMEM enabled systems arch/arm64/include/asm/kvm_asm.h | 50 ++-- arch/arm64/include/asm/kvm_emulate.h | 2 +- arch/arm64/include/asm/kvm_host.h | 41 +++- arch/arm64/kernel/asm-offsets.c | 1 + arch/arm64/kernel/image-vars.h | 2 + arch/arm64/kvm/arm.c | 90 +++++++- arch/arm64/kvm/fpsimd.c | 2 +- arch/arm64/kvm/guest.c | 14 +- arch/arm64/kvm/hyp/entry.S | 15 ++ arch/arm64/kvm/hyp/include/hyp/switch.h | 6 +- arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +- .../arm64/kvm/hyp/include/nvhe/trap_handler.h | 2 +- arch/arm64/kvm/hyp/nvhe/host.S | 20 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +- arch/arm64/kvm/reset.c | 2 +- arch/arm64/kvm/va_layout.c | 38 ++++ include/linux/secretmem.h | 29 +++ mm/Kconfig | 10 + mm/gup.c | 4 +- mm/internal.h | 7 + mm/mseal.c | 81 ++++--- mm/secretmem.c | 213 ++++++++++++++++++ 22 files changed, 559 insertions(+), 84 deletions(-)