Don't directly access userspace memory.

Rather then using the racy useracc() followed by direct access to
userspace memory, perform a copyin() and use the result if it succeeds.

Reviewed by:	jhb
MFC after:	3 weeks
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D24410
This commit is contained in:
Brooks Davis 2020-04-15 16:33:27 +00:00
parent 3e58608634
commit d0fa673e4d

View File

@ -1402,7 +1402,7 @@ log_illegal_instruction(const char *msg, struct trapframe *frame)
{ {
pt_entry_t *ptep; pt_entry_t *ptep;
pd_entry_t *pdep; pd_entry_t *pdep;
unsigned int *addr; unsigned int *addr, instr[4];
struct thread *td; struct thread *td;
struct proc *p; struct proc *p;
register_t pc; register_t pc;
@ -1429,17 +1429,16 @@ log_illegal_instruction(const char *msg, struct trapframe *frame)
* Dump a few words around faulting instruction, if the addres is * Dump a few words around faulting instruction, if the addres is
* valid. * valid.
*/ */
if (!(pc & 3) && addr = (unsigned int *)(intptr_t)pc;
useracc((caddr_t)(intptr_t)pc, sizeof(int) * 4, VM_PROT_READ)) { if ((pc & 3) == 0 && copyin(addr, instr, sizeof(instr)) == 0) {
/* dump page table entry for faulting instruction */ /* dump page table entry for faulting instruction */
log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n", log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n",
(intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0)); (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
addr = (unsigned int *)(intptr_t)pc;
log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n", log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n",
addr); addr);
log(LOG_ERR, "%08x %08x %08x %08x\n", log(LOG_ERR, "%08x %08x %08x %08x\n",
addr[0], addr[1], addr[2], addr[3]); instr[0], instr[1], instr[2], instr[3]);
} else { } else {
log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n", log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n",
(intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0)); (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
@ -1451,7 +1450,7 @@ log_bad_page_fault(char *msg, struct trapframe *frame, int trap_type)
{ {
pt_entry_t *ptep; pt_entry_t *ptep;
pd_entry_t *pdep; pd_entry_t *pdep;
unsigned int *addr; unsigned int *addr, instr[4];
struct thread *td; struct thread *td;
struct proc *p; struct proc *p;
char *read_or_write; char *read_or_write;
@ -1499,18 +1498,18 @@ log_bad_page_fault(char *msg, struct trapframe *frame, int trap_type)
* Dump a few words around faulting instruction, if the addres is * Dump a few words around faulting instruction, if the addres is
* valid. * valid.
*/ */
if (!(pc & 3) && (pc != frame->badvaddr) && addr = (unsigned int *)(intptr_t)pc;
(trap_type != T_BUS_ERR_IFETCH) && if ((pc & 3) == 0 && pc != frame->badvaddr &&
useracc((caddr_t)(intptr_t)pc, sizeof(int) * 4, VM_PROT_READ)) { trap_type != T_BUS_ERR_IFETCH &&
copyin((caddr_t)(intptr_t)pc, instr, sizeof(instr)) == 0) {
/* dump page table entry for faulting instruction */ /* dump page table entry for faulting instruction */
log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n", log(LOG_ERR, "Page table info for pc address %#jx: pde = %p, pte = %#jx\n",
(intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0)); (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));
addr = (unsigned int *)(intptr_t)pc;
log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n", log(LOG_ERR, "Dumping 4 words starting at pc address %p: \n",
addr); addr);
log(LOG_ERR, "%08x %08x %08x %08x\n", log(LOG_ERR, "%08x %08x %08x %08x\n",
addr[0], addr[1], addr[2], addr[3]); instr[0], instr[1], instr[2], instr[3]);
} else { } else {
log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n", log(LOG_ERR, "pc address %#jx is inaccessible, pde = %p, pte = %#jx\n",
(intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0)); (intmax_t)pc, (void *)(intptr_t)*pdep, (uintmax_t)(ptep ? *ptep : 0));