Skip to content

Commit 42fea0a

Browse files
virtuosohansendc
authored andcommitted
x86/traps: Communicate a LASS violation in #GP message
A LASS violation typically results in a #GP. With LASS active, any invalid access to user memory (including the first page frame) would be reported as a #GP, instead of a #PF. Unfortunately, the #GP error messages provide limited information about the cause of the fault. This could be confusing for kernel developers and users who are accustomed to the friendly #PF messages. To make the transition easier, enhance the #GP Oops message to include a hint about LASS violations. Also, add a special hint for kernel NULL pointer dereferences to match with the existing #PF message. Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Sohil Mehta <sohil.mehta@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://patch.msgid.link/20251118182911.2983253-7-sohil.mehta%40intel.com
1 parent 731d437 commit 42fea0a

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

arch/x86/kernel/traps.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,23 @@ DEFINE_IDTENTRY(exc_bounds)
635635
enum kernel_gp_hint {
636636
GP_NO_HINT,
637637
GP_NON_CANONICAL,
638-
GP_CANONICAL
638+
GP_CANONICAL,
639+
GP_LASS_VIOLATION,
640+
GP_NULL_POINTER,
641+
};
642+
643+
static const char * const kernel_gp_hint_help[] = {
644+
[GP_NON_CANONICAL] = "probably for non-canonical address",
645+
[GP_CANONICAL] = "maybe for address",
646+
[GP_LASS_VIOLATION] = "probably LASS violation for address",
647+
[GP_NULL_POINTER] = "kernel NULL pointer dereference",
639648
};
640649

641650
/*
642651
* When an uncaught #GP occurs, try to determine the memory address accessed by
643652
* the instruction and return that address to the caller. Also, try to figure
644-
* out whether any part of the access to that address was non-canonical.
653+
* out whether any part of the access to that address was non-canonical or
654+
* across privilege levels.
645655
*/
646656
static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
647657
unsigned long *addr)
@@ -663,14 +673,28 @@ static enum kernel_gp_hint get_kernel_gp_address(struct pt_regs *regs,
663673
return GP_NO_HINT;
664674

665675
#ifdef CONFIG_X86_64
676+
/* Operand is in the kernel half */
677+
if (*addr >= ~__VIRTUAL_MASK)
678+
return GP_CANONICAL;
679+
680+
/* The last byte of the operand is not in the user canonical half */
681+
if (*addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
682+
return GP_NON_CANONICAL;
683+
666684
/*
667-
* Check that:
668-
* - the operand is not in the kernel half
669-
* - the last byte of the operand is not in the user canonical half
685+
* A NULL pointer dereference usually causes a #PF. However, it
686+
* can result in a #GP when LASS is active. Provide the same
687+
* hint in the rare case that the condition is hit without LASS.
670688
*/
671-
if (*addr < ~__VIRTUAL_MASK &&
672-
*addr + insn.opnd_bytes - 1 > __VIRTUAL_MASK)
673-
return GP_NON_CANONICAL;
689+
if (*addr < PAGE_SIZE)
690+
return GP_NULL_POINTER;
691+
692+
/*
693+
* Assume that LASS caused the exception, because the address is
694+
* canonical and in the user half.
695+
*/
696+
if (cpu_feature_enabled(X86_FEATURE_LASS))
697+
return GP_LASS_VIOLATION;
674698
#endif
675699

676700
return GP_CANONICAL;
@@ -833,9 +857,7 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)
833857

834858
if (hint != GP_NO_HINT)
835859
snprintf(desc, sizeof(desc), GPFSTR ", %s 0x%lx",
836-
(hint == GP_NON_CANONICAL) ? "probably for non-canonical address"
837-
: "maybe for address",
838-
gp_addr);
860+
kernel_gp_hint_help[hint], gp_addr);
839861

840862
/*
841863
* KASAN is interested only in the non-canonical case, clear it

0 commit comments

Comments
 (0)