From patchwork Tue Mar 12 12:52:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 13589948 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 2415DC54E60 for ; Tue, 12 Mar 2024 12:52:56 +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:Cc:To:MIME-Version:Message-Id:Date: Subject: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=MMiH7L5NO8fmutoNiC9R7fBLNdvjJRryZ0m4q8+6Jn0=; b=qZwGA0vRC8u3fe QTgndsKs560iMaYfg74Sro8E7nNSegvgWTZ6LytxhFnVVJ0Yo5kA104aYuaAF3zyXrFd+6585ZKXL BC0FSjiZ81JW0Q+USHIN1/HFdi5pJ2A/Y/OM/glFYFt1T+KWLandDb8s943lw2j/335OAlyFA/zk+ 9mKCZQGpbRd0g/Xg4O6RCinUiI/eLS20vUYxIKj9BwOtRqQwtlpGQk0Mz2MFsXwz95mezXJ2WpJh4 DGtOjmFMwg31xmddLHOle9V+sb5vtbUvRK0yDKr6UILdnWmnikr1LVOH68cMTtdRQVc0c9E+Z7Sg4 j8amMvpJv5o8rW4XP2tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rk1cX-00000005pFY-2qrU; Tue, 12 Mar 2024 12:52:41 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rk1cS-00000005pBd-0klD for linux-arm-kernel@lists.infradead.org; Tue, 12 Mar 2024 12:52:40 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a4499ef8b5aso402840266b.0 for ; Tue, 12 Mar 2024 05:52:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1710247948; x=1710852748; darn=lists.infradead.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=39j/B16jIWTVHwNxd6Zue8VqbUC1LIyI9dhM8Nu8yfU=; b=s+VCvk7ThzvCZYAfWEr4GjKv65kJkEvQUIuDMRS3pAu8Hn5iw8R7/jFh4Tjdn8iJNe uytK3ikWkMImcSO8sE1x/AONqoRw2FQalYCJ9ToJa2RWvjuHgeUvwCms0Sjy3ZFZucgy 3SfxA3XuDy5p0ZkJK2F7cYPwPtTIFBfbCRKdnIdowTGa2GpjzpO0k3tpGG7sES7jeoRd 2pRCMBIv/0Buiich+vZr6yz9PDOSxHvW6xjqlpaKoYYRlXJseijnELYaI/MjFV6IaAn6 PJSHaUtBox2ciSRGEsY03rAe0DG/npjAYjiunV7H6AjG28Ret/ijIRMtt0s6lOomzWAK S/ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710247948; x=1710852748; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=39j/B16jIWTVHwNxd6Zue8VqbUC1LIyI9dhM8Nu8yfU=; b=SSmQ5Gf9YRsgMr//tMiNJmspt6nrhzsits1wsD6E/p74hVPbmGmc2gtXJubg+yLcEh /hn4JuBSL7ALG/JJpym8Lo06MpfoyeeGblbhh0kvieXzxPeM0pXYuM/u3MA4uICzxalk v0drpg4Hn8zy+QIRE7EiKbZlokwmje94zJGrnRaSomEJkEgu4spS+P4jyMUJkWGwJ2j8 HHaQDzh5vSZh1Lyobec0AX4ffYNLfif/xBFVMqHL67ADA+/eGs+zYtVPdJhUt+yDiiNk UDqPxb4Q8UiN1+9SbVr3c2GCuMV9CTIEL3ssln3Xfx58FcPe1XX4R8GzLZGOaPrEEgU7 dmLQ== X-Gm-Message-State: AOJu0YwJRw9QTuruq6L2RpzEriJWhHkH6+OM83PleWLZg7onleeBsAYB 3wNghzWMcZb8bpXdbSe/CWF5zz3mok3iCzFsw1mH9KOqkBehpLqb5HP5EHuHz+YPOTwAVn8buA5 v X-Google-Smtp-Source: AGHT+IEr7GkY8+ymigGV1xKwC15tjcuZa7LRESdXrkKpnSqC0tKO33zhKgtqR0X8DgD5Yh2QsI/iDw== X-Received: by 2002:a17:906:6d0e:b0:a46:2aa1:1d with SMTP id m14-20020a1709066d0e00b00a462aa1001dmr3134661ejr.8.1710247948409; Tue, 12 Mar 2024 05:52:28 -0700 (PDT) Received: from [127.0.1.1] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id gc5-20020a170906c8c500b00a45a09e7e23sm3845088ejb.136.2024.03.12.05.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 05:52:28 -0700 (PDT) From: Linus Walleij Subject: [PATCH v3 0/4] PAN for ARM32 using LPAE Date: Tue, 12 Mar 2024 13:52:16 +0100 Message-Id: <20240312-arm32-lpae-pan-v3-0-532647afcd38@linaro.org> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAABQ8GUC/32OzQ6CMBAGX8X0bE13Syv15HsYDwus0ASBFEM0h He35eJf4nE2mfl2FiMHz6M4bGYRePKj77sIersRZUNdzdJXkQUq1IBgJYWrRtkOxHKgThoLaKi wurJaRGkIfPH3NXg6R278eOvDY+1PkK4plSlA/Z2aQCqJ5Cyb0rC16tj6jkK/60Od0n+9PZPLC ZWBrHz30g8TvnYR4cfH6DsHytmiIFPlH/6yLE/xAm0UJQEAAA== To: Russell King , Ard Biesheuvel , Arnd Bergmann , Stefan Wahren , Kees Cook , Geert Uytterhoeven Cc: linux-arm-kernel@lists.infradead.org, Linus Walleij , Catalin Marinas X-Mailer: b4 0.12.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240312_055236_314111_7DDC103F X-CRM114-Status: GOOD ( 19.78 ) 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 is a patch set from Catalin that ended up on the back burner. Since LPAE systems, i.e. ARM32 systems with a lot of physical memory, will be with us for a while more, this is a pretty straight-forward hardening measure that we should support. The last patch explains the mechanism: since PAN using CPU domains isn't available when using the LPAE MMU tables, we use the split between the two translation base tables instead: TTBR0 is for userspace pages and TTBR1 is for kernelspace tables. When executing in kernelspace: we protect userspace by simply disabling page walks in TTBR0. The simplest way to test a PAN crash: - Enable CONFIG_LKDTM - echo "ACCESS_USERSPACE" | cat >/sys/kernel/debug/provoke-crash/DIRECT - echo "EXEC_USERSPACE" | cat >/sys/kernel/debug/provoke-crash/DIRECT This was also tested by a simple hack in the ELF loader: create_elf_tables() + unsigned char *test; (...) if (copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes))) return -EFAULT; + /* Cause a kernelspace access to userspace memory */ + test = (char *)u_rand_bytes; + pr_info("Some byte: %02x\n", *test); This tries to read a byte from userspace memory right after the first unconditional copy_to_user(), a function that carefully switches access permissions if we're using PAN. Without LPAE PAN this will just happily print these bytes from userspace but with LPAE PAN it will cause a predictable crash: Run /init as init process Some byte: ac 8<--- cut here --- Unable to handle kernel paging request at virtual address 7ec59f6b when read [7ec59f6b] *pgd=82c3b003, *pmd=82863003, *pte=e00000882f6f5f Internal error: Oops: 206 [#1] SMP ARM CPU: 0 PID: 47 Comm: rc.init Not tainted 6.7.0-rc1+ #25 Hardware name: ARM-Versatile Express PC is at create_elf_tables+0x13c/0x608 Thus we can show that LPAE PAN does its job. Signed-off-by: Linus Walleij Tested-by: Florian Fainelli --- Changes in v3: - Drop leftover uaccess_disabled() stub. - Drop unnecesary volatile from the asm(mcr) call. - Move all changes of ifdef to if defined() to the last patch and keep the preceding patch to its subject. - Link to v2: https://lore.kernel.org/r/20240221-arm32-lpae-pan-v2-0-991096bba5d8@linaro.org Changes in v1 (from Catalins original patch set): - Use IS_ENABLED() to avoid some ifdefs Changes in v2: - Make the TTBCR a separate field in struct svc_pt_regs as requested by Russell. Adjust code accordingly. - Push the MM page fault permission check into a local function and avoid the too generic uaccess_disabled() as requested by Ard. - Link to v1: https://lore.kernel.org/r/20240123-arm32-lpae-pan-v1-0-7ea98a20514c@linaro.org --- Catalin Marinas (4): ARM: Add TTBCR_* definitions to pgtable-3level-hwdef.h ARM: Move asm statements accessing TTBCR into C functions ARM: Reduce the number of #ifdef CONFIG_CPU_SW_DOMAIN_PAN ARM: Implement PAN for LPAE by TTBR0 page table walks disablement arch/arm/Kconfig | 22 +++++++++-- arch/arm/include/asm/assembler.h | 1 + arch/arm/include/asm/pgtable-3level-hwdef.h | 26 +++++++++++++ arch/arm/include/asm/proc-fns.h | 12 ++++++ arch/arm/include/asm/ptrace.h | 1 + arch/arm/include/asm/uaccess-asm.h | 58 +++++++++++++++++++++++++++-- arch/arm/include/asm/uaccess.h | 45 +++++++++++++++++++--- arch/arm/kernel/asm-offsets.c | 1 + arch/arm/kernel/suspend.c | 8 ++++ arch/arm/lib/csumpartialcopyuser.S | 20 +++++++++- arch/arm/mm/fault.c | 29 +++++++++++++++ arch/arm/mm/mmu.c | 7 ++-- 12 files changed, 212 insertions(+), 18 deletions(-) --- base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d change-id: 20231216-arm32-lpae-pan-56125ab63d63 Best regards,