From nobody Fri Oct 15 15:26:15 2021 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 7DAE51816BFC; Fri, 15 Oct 2021 15:26:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HW98m1qVVz3PKf; Fri, 15 Oct 2021 15:26:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ED066D0F; Fri, 15 Oct 2021 15:26:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 19FFQFcA007388; Fri, 15 Oct 2021 15:26:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 19FFQFmH007387; Fri, 15 Oct 2021 15:26:15 GMT (envelope-from git) Date: Fri, 15 Oct 2021 15:26:15 GMT Message-Id: <202110151526.19FFQFmH007387@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mitchell Horne Subject: git: 43f5da70cc96 - stable/13 - riscv: handle page faults in the unmappable region List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 43f5da70cc96161c6f738b4e3a91fbdc44679760 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=43f5da70cc96161c6f738b4e3a91fbdc44679760 commit 43f5da70cc96161c6f738b4e3a91fbdc44679760 Author: Mitchell Horne AuthorDate: 2021-10-07 21:05:38 +0000 Commit: Mitchell Horne CommitDate: 2021-10-15 15:22:13 +0000 riscv: handle page faults in the unmappable region When handling a kernel page fault, check explicitly that stval resides in either the user or kernel address spaces, and make the page fault fatal if not. Otherwise, a properly crafted address may appear to pmap_fault() as a valid and present page in the kernel map, causing the page fault to be retried continuously. This is mainly due to the fact that the upper bits of virtual addresses are not validated by most of the pmap code. Faults of this nature should only occur due to some kind of bug in the kernel, but it is best to handle them gracefully when they do. Handle user page faults in the same way, sending a SIGSEGV immediately when a malformed address is encountered. Add an assertion to pmap_l1(), which should help catch other bugs of this kind that make it this far. Reviewed by: jrtc27, markj MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D31208 (cherry picked from commit 4a9f2f8b07c2d1a1c12f4aabdccd36f56b20cfda) --- sys/riscv/include/vmparam.h | 4 ++++ sys/riscv/riscv/pmap.c | 2 ++ sys/riscv/riscv/trap.c | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/sys/riscv/include/vmparam.h b/sys/riscv/include/vmparam.h index 94782da779f7..4ed95def2caa 100644 --- a/sys/riscv/include/vmparam.h +++ b/sys/riscv/include/vmparam.h @@ -186,6 +186,10 @@ #define VM_MINUSER_ADDRESS (VM_MIN_USER_ADDRESS) #define VM_MAXUSER_ADDRESS (VM_MAX_USER_ADDRESS) +/* Check if an address resides in a mappable region. */ +#define VIRT_IS_VALID(va) \ + (((va) < VM_MAX_USER_ADDRESS) || ((va) >= VM_MIN_KERNEL_ADDRESS)) + #define KERNBASE (VM_MIN_KERNEL_ADDRESS) #define SHAREDPAGE (VM_MAXUSER_ADDRESS - PAGE_SIZE) #define USRSTACK SHAREDPAGE diff --git a/sys/riscv/riscv/pmap.c b/sys/riscv/riscv/pmap.c index c596ce15bde5..eab61032fbcb 100644 --- a/sys/riscv/riscv/pmap.c +++ b/sys/riscv/riscv/pmap.c @@ -351,6 +351,8 @@ static __inline pd_entry_t * pmap_l1(pmap_t pmap, vm_offset_t va) { + KASSERT(VIRT_IS_VALID(va), + ("%s: malformed virtual address %#lx", __func__, va)); return (&pmap->pm_l1[pmap_l1_index(va)]); } diff --git a/sys/riscv/riscv/trap.c b/sys/riscv/riscv/trap.c index 69b43f598efd..abf527b2ef13 100644 --- a/sys/riscv/riscv/trap.c +++ b/sys/riscv/riscv/trap.c @@ -199,6 +199,11 @@ page_fault_handler(struct trapframe *frame, int usermode) goto fatal; if (usermode) { + if (!VIRT_IS_VALID(stval)) { + call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)stval, + frame->tf_scause & SCAUSE_CODE); + goto done; + } map = &td->td_proc->p_vmspace->vm_map; } else { /* @@ -207,6 +212,9 @@ page_fault_handler(struct trapframe *frame, int usermode) */ intr_enable(); + if (!VIRT_IS_VALID(stval)) + goto fatal; + if (stval >= VM_MAX_USER_ADDRESS) { map = kernel_map; } else {