Received: by 2002:a5b:f51:0:0:0:0:0 with SMTP id y17csp129900ybr; Fri, 22 Nov 2019 03:20:00 -0800 (PST) X-Google-Smtp-Source: APXvYqwNEYGH7k1kwh7HH1g0w0DTqkpZzSiWJuR+yeQO/LCxwIPBK1bT6mfyZ1/XRrm3zUjaMm7I X-Received: by 2002:a17:906:a28d:: with SMTP id i13mr5081103ejz.154.1574421565293; Fri, 22 Nov 2019 03:19:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574421565; cv=none; d=google.com; s=arc-20160816; b=cC2+e8g7Q2hT0Uq6z/gEJqzpJV3G47DkoKXeoqGqfqkq8YteUs9H/y8BIHuenh9lGB eMraFuNFDU/oJYrPeATu/JnjgW+zTT3Lxt7iCudfqV3eT8IriaEYNy14J982zMY4zDF6 FrnRL0Bkko6bivB88k/kfSs7SmRSLIbQqLocbUVosHE26Of7LQ13m6pGzmdGVtKk/Ojr voN3eAG9nRJfpAxWhQIGBobx7cqI6YvEyhxTdbr4/hyvWbax7D2X62q6f2QUeBL1Jrpe Y0sKq8qEyIL1HFNd5fytJzdiiwbj3vG3HsqHSDR5cbOF3IU6kMnWL6i2/MkYRiUoeFyU /Ymg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bmdmWVoP8ZQB/rTcretptCJdbtVrnnnXWLARcLfUW1M=; b=UJeG7zoiZYYkVIjE2f+0ZhidWbITZhOiqPAqVRYWof58weS8ErW61fViGBEuynzHdL G+YYIj06mdgi1MCdOTim3pMz44j4xyjpBSkaMveLJ8jbcE21x93kBVOL3UZZDGofP8Fh ZArkTe6PkZwg+k7fJnqBv4KJa9avpff+TjhTcrLCS5X6NxIh1AZo/gtMso7j8Z587lj8 prrz0svXq++R9Kz+dOD6gfUOVUqG7EFbE+JaMAe0gQ/KKz0pHQPSqr5Du4SiVVBU9ssH DpZDhMWF2OLOxyoHLzY6rEKHFhoyBshCY01UAHUVabZzBcOD3OWOgDvQU/72fzJZkTki uwiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xsYyK0l6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r25si3858032eji.283.2019.11.22.03.19.01; Fri, 22 Nov 2019 03:19:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xsYyK0l6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729257AbfKVKu7 (ORCPT + 99 others); Fri, 22 Nov 2019 05:50:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:33138 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728482AbfKVKu5 (ORCPT ); Fri, 22 Nov 2019 05:50:57 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0BC232073B; Fri, 22 Nov 2019 10:50:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574419856; bh=le8YsAdSof++XonceReapTGQ5IqL5e0lvkiY/PXkKr4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xsYyK0l6fiGPyF6Mn8pF+k+f/oQgPxgTQWiO6HYh12GauaMeMLdHc4W7tIS1B3Bwz 1YVK90DN8RBWlsCRjoeHSRlp+5Ou3uCwWsby1Uqqgizi/kcmYWjbK6iBgDE8ev9B1l Rh/vdHNteu0Rvuz+Eug5FRvTJaXKp7ivh3FkW7sM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Catalin Marinas , Mark Rutland , Pavel Tatashin , Will Deacon Subject: [PATCH 4.14 008/122] arm64: uaccess: Ensure PAN is re-enabled after unhandled uaccess fault Date: Fri, 22 Nov 2019 11:27:41 +0100 Message-Id: <20191122100728.780935539@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191122100722.177052205@linuxfoundation.org> References: <20191122100722.177052205@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Tatashin commit 94bb804e1e6f0a9a77acf20d7c70ea141c6c821e upstream. A number of our uaccess routines ('__arch_clear_user()' and '__arch_copy_{in,from,to}_user()') fail to re-enable PAN if they encounter an unhandled fault whilst accessing userspace. For CPUs implementing both hardware PAN and UAO, this bug has no effect when both extensions are in use by the kernel. For CPUs implementing hardware PAN but not UAO, this means that a kernel using hardware PAN may execute portions of code with PAN inadvertently disabled, opening us up to potential security vulnerabilities that rely on userspace access from within the kernel which would usually be prevented by this mechanism. In other words, parts of the kernel run the same way as they would on a CPU without PAN implemented/emulated at all. For CPUs not implementing hardware PAN and instead relying on software emulation via 'CONFIG_ARM64_SW_TTBR0_PAN=y', the impact is unfortunately much worse. Calling 'schedule()' with software PAN disabled means that the next task will execute in the kernel using the page-table and ASID of the previous process even after 'switch_mm()', since the actual hardware switch is deferred until return to userspace. At this point, or if there is a intermediate call to 'uaccess_enable()', the page-table and ASID of the new process are installed. Sadly, due to the changes introduced by KPTI, this is not an atomic operation and there is a very small window (two instructions) where the CPU is configured with the page-table of the old task and the ASID of the new task; a speculative access in this state is disastrous because it would corrupt the TLB entries for the new task with mappings from the previous address space. As Pavel explains: | I was able to reproduce memory corruption problem on Broadcom's SoC | ARMv8-A like this: | | Enable software perf-events with PERF_SAMPLE_CALLCHAIN so userland's | stack is accessed and copied. | | The test program performed the following on every CPU and forking | many processes: | | unsigned long *map = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, | MAP_SHARED | MAP_ANONYMOUS, -1, 0); | map[0] = getpid(); | sched_yield(); | if (map[0] != getpid()) { | fprintf(stderr, "Corruption detected!"); | } | munmap(map, PAGE_SIZE); | | From time to time I was getting map[0] to contain pid for a | different process. Ensure that PAN is re-enabled when returning after an unhandled user fault from our uaccess routines. Cc: Catalin Marinas Reviewed-by: Mark Rutland Tested-by: Mark Rutland Cc: Fixes: 338d4f49d6f7 ("arm64: kernel: Add support for Privileged Access Never") Signed-off-by: Pavel Tatashin [will: rewrote commit message] Signed-off-by: Will Deacon Signed-off-by: Greg Kroah-Hartman --- arch/arm64/lib/clear_user.S | 1 + arch/arm64/lib/copy_from_user.S | 1 + arch/arm64/lib/copy_in_user.S | 1 + arch/arm64/lib/copy_to_user.S | 1 + 4 files changed, 4 insertions(+) --- a/arch/arm64/lib/clear_user.S +++ b/arch/arm64/lib/clear_user.S @@ -57,5 +57,6 @@ ENDPROC(__arch_clear_user) .section .fixup,"ax" .align 2 9: mov x0, x2 // return the original size + uaccess_disable_not_uao x2, x3 ret .previous --- a/arch/arm64/lib/copy_from_user.S +++ b/arch/arm64/lib/copy_from_user.S @@ -75,5 +75,6 @@ ENDPROC(__arch_copy_from_user) .section .fixup,"ax" .align 2 9998: sub x0, end, dst // bytes not copied + uaccess_disable_not_uao x3, x4 ret .previous --- a/arch/arm64/lib/copy_in_user.S +++ b/arch/arm64/lib/copy_in_user.S @@ -77,5 +77,6 @@ ENDPROC(__arch_copy_in_user) .section .fixup,"ax" .align 2 9998: sub x0, end, dst // bytes not copied + uaccess_disable_not_uao x3, x4 ret .previous --- a/arch/arm64/lib/copy_to_user.S +++ b/arch/arm64/lib/copy_to_user.S @@ -74,5 +74,6 @@ ENDPROC(__arch_copy_to_user) .section .fixup,"ax" .align 2 9998: sub x0, end, dst // bytes not copied + uaccess_disable_not_uao x3, x4 ret .previous