From owner-svn-src-stable-12@freebsd.org Mon May 11 16:14:21 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0CE2B2E4F88; Mon, 11 May 2020 16:14:21 +0000 (UTC) (envelope-from jhb@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49LQx86bBpz3FyR; Mon, 11 May 2020 16:14:20 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id DD4EF211E8; Mon, 11 May 2020 16:14:20 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 04BGEKu7046042; Mon, 11 May 2020 16:14:20 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 04BGEKnL046041; Mon, 11 May 2020 16:14:20 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202005111614.04BGEKnL046041@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Mon, 11 May 2020 16:14:20 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r360901 - stable/12/sys/riscv/riscv X-SVN-Group: stable-12 X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: stable/12/sys/riscv/riscv X-SVN-Commit-Revision: 360901 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 May 2020 16:14:21 -0000 Author: jhb Date: Mon May 11 16:14:20 2020 New Revision: 360901 URL: https://svnweb.freebsd.org/changeset/base/360901 Log: MFC 356840,357344: Add stricter checks on user changes to SSTATUS. 356840: Check for invalid sstatus values in set_mcontext(). Previously, this check was only in sys_sigreturn() which meant that user applications could write invalid values to the register via setcontext() or swapcontext(). 357344: Add stricter checks on user changes to SSTATUS. Rather than trying to blacklist which bits userland can't write to via sigreturn() or setcontext(), only permit changes to whitelisted bits. - Permit arbitrary writes to bits in the user-writable USTATUS register that shadows SSTATUS. - Ignore changes in write-only bits maintained by the CPU. - Ignore the user-supplied value of the FS field used to track floating point state and instead set it to a value matching the actions taken by set_fpcontext(). Modified: stable/12/sys/riscv/riscv/machdep.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/riscv/riscv/machdep.c ============================================================================== --- stable/12/sys/riscv/riscv/machdep.c Mon May 11 15:38:44 2020 (r360900) +++ stable/12/sys/riscv/riscv/machdep.c Mon May 11 16:14:20 2020 (r360901) @@ -365,6 +365,19 @@ set_mcontext(struct thread *td, mcontext_t *mcp) tf = td->td_frame; + /* + * Permit changes to the USTATUS bits of SSTATUS. + * + * Ignore writes to read-only bits (SD, XS). + * + * Ignore writes to the FS field as set_fpcontext() will set + * it explicitly. + */ + if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) & + ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE | + SSTATUS_UIE)) != 0) + return (EINVAL); + memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t)); memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s)); memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a)); @@ -416,7 +429,12 @@ set_fpcontext(struct thread *td, mcontext_t *mcp) { #ifdef FPE struct pcb *curpcb; +#endif + td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK; + td->td_frame->tf_sstatus |= SSTATUS_FS_OFF; + +#ifdef FPE critical_enter(); if ((mcp->mc_flags & _MC_FP_VALID) != 0) { @@ -426,6 +444,7 @@ set_fpcontext(struct thread *td, mcontext_t *mcp) sizeof(mcp->mc_fpregs)); curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr; curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK; + td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN; } critical_exit(); @@ -520,21 +539,11 @@ struct sigreturn_args { int sys_sigreturn(struct thread *td, struct sigreturn_args *uap) { - uint64_t sstatus; ucontext_t uc; int error; if (copyin(uap->sigcntxp, &uc, sizeof(uc))) return (EFAULT); - - /* - * Make sure the processor mode has not been tampered with and - * interrupts have not been disabled. - * Supervisor interrupts in user mode are always enabled. - */ - sstatus = uc.uc_mcontext.mc_gpregs.gp_sstatus; - if ((sstatus & SSTATUS_SPP) != 0) - return (EINVAL); error = set_mcontext(td, &uc.uc_mcontext); if (error != 0)