Re: CONFIG_UBSAN_TRAP #UD error message on x86 [was: Re: [CRASH][BISECTED] 6.4.1 crash in boot]

Peter Zijlstra
2023-07-05T21:31:26Z

On Wed, Jul 05, 2023 at 02:08:09PM -0700, Kees Cook wrote:

> > Even just a "WARNING: CONFIG_UBSAN_TRAP active, #UD might be caused by
> > that" on every #UD that does not come from a known BUG() location or
> > such might be better than nothing...
>
> I've considered it, but usually CONFIG_UBSAN_TRAP isn't accidentally
> set. Also, the crash info is something we can get help from on the
> compiler side, to mark up where the traps are, similar to what we do
> with KCFI, but it hasn't happened yet for x86. For example, arm64
> already encodes the details in the trap instruction itself:
> https://git.kernel.org/linus/25b84002afb9dc9a91a7ea67166879c13ad82422

Right, so you could easily use a different #UD instruction that has an
immediate, something like:

0f b9 40 ff ud1 -0x1(%rax),%rax

or even:

0f b9 80 00 ff ff ff ud1 -0x100(%rax),%rax

if you need a 32bit value.

It shouldn't be hard to fix up the #UD handler to decode the instruction
and obtain the displacement for a clue.

Typically we use ud2 because it's the smallest #UD instruction (2 bytes)
and that's enough, but if you want to provide additional clues, there's
options...