Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/tdx: Fix "in-kernel MMIO" check

TDX only supports kernel-initiated MMIO operations. The handle_mmio()
function checks if the #VE exception occurred in the kernel and rejects
the operation if it did not.

However, userspace can deceive the kernel into performing MMIO on its
behalf. For example, if userspace can point a syscall to an MMIO address,
syscall does get_user() or put_user() on it, triggering MMIO #VE. The
kernel will treat the #VE as in-kernel MMIO.

Ensure that the target MMIO address is within the kernel before decoding
instruction.

Fixes: 31d58c4e557d ("x86/tdx: Handle in-kernel MMIO")
Signed-off-by: Alexey Gladkov (Intel) <legion@kernel.org>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc:stable@vger.kernel.org
Link: https://lore.kernel.org/all/565a804b80387970460a4ebc67c88d1380f61ad1.1726237595.git.legion%40kernel.org

authored by

Alexey Gladkov (Intel) and committed by
Dave Hansen
d4fc4d01 98f7e32f

+6
+6
arch/x86/coco/tdx/tdx.c
··· 16 #include <asm/insn-eval.h> 17 #include <asm/pgtable.h> 18 #include <asm/set_memory.h> 19 20 /* MMIO direction */ 21 #define EPT_READ 0 ··· 432 reg = insn_get_modrm_reg_ptr(&insn, regs); 433 if (!reg) 434 return -EINVAL; 435 } 436 437 /*
··· 16 #include <asm/insn-eval.h> 17 #include <asm/pgtable.h> 18 #include <asm/set_memory.h> 19 + #include <asm/traps.h> 20 21 /* MMIO direction */ 22 #define EPT_READ 0 ··· 431 reg = insn_get_modrm_reg_ptr(&insn, regs); 432 if (!reg) 433 return -EINVAL; 434 + } 435 + 436 + if (!fault_in_kernel_space(ve->gla)) { 437 + WARN_ONCE(1, "Access to userspace address is not supported"); 438 + return -EINVAL; 439 } 440 441 /*