Skip to content

Commit 63e44bc

Browse files
joergroedelbp3tk0v
authored andcommitted
x86/sev: Check for user-space IOIO pointing to kernel space
Check the memory operand of INS/OUTS before emulating the instruction. The #VC exception can get raised from user-space, but the memory operand can be manipulated to access kernel memory before the emulation actually begins and after the exception handler has run. [ bp: Massage commit message. ] Fixes: 597cfe4 ("x86/boot/compressed/64: Setup a GHCB-based VC Exception handler") Reported-by: Tom Dohrmann <[email protected]> Signed-off-by: Joerg Roedel <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Cc: <[email protected]>
1 parent b9cb9c4 commit 63e44bc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

arch/x86/boot/compressed/sev.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t si
108108
return ES_OK;
109109
}
110110

111+
static bool fault_in_kernel_space(unsigned long address)
112+
{
113+
return false;
114+
}
115+
111116
#undef __init
112117
#define __init
113118

arch/x86/kernel/sev-shared.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,36 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
592592
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
593593
}
594594

595+
static enum es_result vc_insn_string_check(struct es_em_ctxt *ctxt,
596+
unsigned long address,
597+
bool write)
598+
{
599+
if (user_mode(ctxt->regs) && fault_in_kernel_space(address)) {
600+
ctxt->fi.vector = X86_TRAP_PF;
601+
ctxt->fi.error_code = X86_PF_USER;
602+
ctxt->fi.cr2 = address;
603+
if (write)
604+
ctxt->fi.error_code |= X86_PF_WRITE;
605+
606+
return ES_EXCEPTION;
607+
}
608+
609+
return ES_OK;
610+
}
611+
595612
static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
596613
void *src, char *buf,
597614
unsigned int data_size,
598615
unsigned int count,
599616
bool backwards)
600617
{
601618
int i, b = backwards ? -1 : 1;
602-
enum es_result ret = ES_OK;
619+
unsigned long address = (unsigned long)src;
620+
enum es_result ret;
621+
622+
ret = vc_insn_string_check(ctxt, address, false);
623+
if (ret != ES_OK)
624+
return ret;
603625

604626
for (i = 0; i < count; i++) {
605627
void *s = src + (i * data_size * b);
@@ -620,7 +642,12 @@ static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
620642
bool backwards)
621643
{
622644
int i, s = backwards ? -1 : 1;
623-
enum es_result ret = ES_OK;
645+
unsigned long address = (unsigned long)dst;
646+
enum es_result ret;
647+
648+
ret = vc_insn_string_check(ctxt, address, true);
649+
if (ret != ES_OK)
650+
return ret;
624651

625652
for (i = 0; i < count; i++) {
626653
void *d = dst + (i * data_size * s);

0 commit comments

Comments
 (0)