x86/sev-es: Handle instruction fetches from user-space
authorJoerg Roedel <jroedel@suse.de>
Mon, 7 Sep 2020 13:15:49 +0000 (15:15 +0200)
committerBorislav Petkov <bp@suse.de>
Wed, 9 Sep 2020 09:33:19 +0000 (11:33 +0200)
When a #VC exception is triggered by user-space, the instruction decoder
needs to read the instruction bytes from user addresses. Enhance
vc_decode_insn() to safely fetch kernel and user instructions.

Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20200907131613.12703-49-joro@8bytes.org
arch/x86/kernel/sev-es.c

index b10a62a78c07722c84c9d2d9d971e808a3a61323..6c30dbc1144aa2dd7e10dc295f29d85b6af56aef 100644 (file)
@@ -232,17 +232,30 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
        enum es_result ret;
        int res;
 
-       res = vc_fetch_insn_kernel(ctxt, buffer);
-       if (unlikely(res == -EFAULT)) {
-               ctxt->fi.vector     = X86_TRAP_PF;
-               ctxt->fi.error_code = 0;
-               ctxt->fi.cr2        = ctxt->regs->ip;
-               return ES_EXCEPTION;
+       if (user_mode(ctxt->regs)) {
+               res = insn_fetch_from_user(ctxt->regs, buffer);
+               if (!res) {
+                       ctxt->fi.vector     = X86_TRAP_PF;
+                       ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
+                       ctxt->fi.cr2        = ctxt->regs->ip;
+                       return ES_EXCEPTION;
+               }
+
+               if (!insn_decode(&ctxt->insn, ctxt->regs, buffer, res))
+                       return ES_DECODE_FAILED;
+       } else {
+               res = vc_fetch_insn_kernel(ctxt, buffer);
+               if (res) {
+                       ctxt->fi.vector     = X86_TRAP_PF;
+                       ctxt->fi.error_code = X86_PF_INSTR;
+                       ctxt->fi.cr2        = ctxt->regs->ip;
+                       return ES_EXCEPTION;
+               }
+
+               insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1);
+               insn_get_length(&ctxt->insn);
        }
 
-       insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1);
-       insn_get_length(&ctxt->insn);
-
        ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
 
        return ret;