Re: git: 95f773e59482 - main - i386 copyout_fast: improve detection of a fault on accessing userspace

From: Tijl Coosemans <tijl_at_FreeBSD.org>
Date: Thu, 08 Sep 2022 17:00:09 UTC
On Thu, 8 Sep 2022 14:49:09 +0200 Tijl Coosemans <tijl@FreeBSD.org> wrote:
> On Wed, 7 Sep 2022 23:18:32 +0300 Konstantin Belousov
> <kostikbel@gmail.com> wrote:
> > On Wed, Sep 07, 2022 at 06:38:04PM +0200, Tijl Coosemans wrote:  
> >> So interrupts must have been reenabled somehow, probably by the page
> >> fault handler, and this allows context switches and then another process
> >> can call copyout or copyin and corrupt the trampoline stack and
> >> copyout_buf.    
> > I do not see where the interrupts could be reenabled in copyout_fast path,
> > without or with page fault on the userspace access.  
> 
> The problem is not with userspace page faults.  Those are treated
> specially by the page fault handler in exception.s causing copyout_fast
> and copyin_fast to return immediately with EFAULT so copyout and copyin
> fall back to doing a slow copy.
> 
> The problem is with page faults on the kernel space accesses.  Before
> this commit they were also treated specially, and now they are not.  Now 
> the page fault handler in exception.s calls trap() which calls
> trap_pfault() etc.

And trap() contains this:

	if (trap_enable_intr(type) && td->td_md.md_spinlock_count == 0 &&
	    frame->tf_eip != (int)cpu_switch_load_gs)
		enable_intr();

Where trap_enable_intr(T_PAGEFLT) returns true and enable_intr() is sti.