All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Peter Zijlstra <peterz@infradead.org>, x86@kernel.org
Cc: linux-kernel@vger.kernel.org, alyssa.milburn@intel.com,
	scott.d.constable@intel.com, joao@overdrivepizza.com,
	jpoimboe@kernel.org, alexei.starovoitov@gmail.com,
	ebiggers@kernel.org, samitolvanen@google.com, kees@kernel.org
Subject: Re: [PATCH 5/8] x86/traps: Cleanup and robustify decode_bug()
Date: Tue, 5 Nov 2024 15:19:23 +0000	[thread overview]
Message-ID: <ea88e0d2-2ada-40e6-9bca-06a598dba70d@citrix.com> (raw)
In-Reply-To: <20241105114522.285032152@infradead.org>

On 05/11/2024 11:39 am, Peter Zijlstra wrote:
> --- a/arch/x86/kernel/traps.c
> +++ b/arch/x86/kernel/traps.c
> @@ -110,24 +117,37 @@ __always_inline int decode_bug(unsigned
>  		return BUG_NONE;
>  
>  	v = *(u8 *)(addr++);
> -	if (v == SECOND_BYTE_OPCODE_UD2)
> +	if (v == SECOND_BYTE_OPCODE_UD2) {
> +		*len = addr - start;
>  		return BUG_UD2;
> +	}
>  
> -	if (!IS_ENABLED(CONFIG_UBSAN_TRAP) || v != SECOND_BYTE_OPCODE_UD1)
> +	if (v != SECOND_BYTE_OPCODE_UD1)
>  		return BUG_NONE;
>  
> -	/* Retrieve the immediate (type value) for the UBSAN UD1 */
> -	v = *(u8 *)(addr++);
> -	if (X86_MODRM_RM(v) == 4)
> -		addr++;
> -
>  	*imm = 0;
> -	if (X86_MODRM_MOD(v) == 1)
> -		*imm = *(u8 *)addr;
> -	else if (X86_MODRM_MOD(v) == 2)
> -		*imm = *(u32 *)addr;
> -	else
> -		WARN_ONCE(1, "Unexpected MODRM_MOD: %u\n", X86_MODRM_MOD(v));
> +	v = *(u8 *)(addr++);		/* ModRM */
> +
> +	/* Decode immediate, if present */
> +	if (X86_MODRM_MOD(v) != 3) {
> +		if (X86_MODRM_RM(v) == 4)
> +			addr++;		/* Skip SIB byte */
> +
> +		if (X86_MODRM_MOD(v) == 1) {
> +			*imm = *(s8 *)addr;
> +			addr += 1;
> +
> +		} else if (X86_MODRM_MOD(v) == 2) {
> +			*imm = *(s32 *)addr;
> +			addr += 4;
> +		}
> +	}
> +
> +	/* record instruction length */
> +	*len = addr - start;

`ud1 0(%rip),%eax` has something to say about this length calculation[1].

You need the Mod = 0, RM = 5 case wired into addr += 4 without filling
in imm.

~Andrew

[1] or maybe you've got something rude to say about those of us who
encode instructions like that...[2]
[2] It's perhaps fortunate that decode_bug() doesn't know what a REX
prefix is.

  reply	other threads:[~2024-11-05 15:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-05 11:39 [PATCH 0/8] x86: kCFI and IBT cleanups Peter Zijlstra
2024-11-05 11:39 ` [PATCH 1/8] x86,kcfi: Fix EXPORT_SYMBOL vs kCFI Peter Zijlstra
2024-11-05 14:16   ` Christoph Hellwig
2024-11-05 14:27     ` Peter Zijlstra
2024-11-05 14:32       ` Christoph Hellwig
2024-11-05 14:58         ` Peter Zijlstra
2024-11-05 15:41           ` Christoph Hellwig
2024-11-05 15:47             ` Peter Zijlstra
2024-11-05 16:11               ` Peter Zijlstra
2024-11-09  9:14           ` David Laight
2024-11-05 11:39 ` [PATCH 2/8] x86/cfi: Clean up linkage Peter Zijlstra
2024-11-05 11:39 ` [PATCH 3/8] x86/boot: Mark start_secondary() with __noendbr Peter Zijlstra
2024-11-05 11:39 ` [PATCH 4/8] x86/alternative: Simplify callthunk patching Peter Zijlstra
2024-11-05 11:39 ` [PATCH 5/8] x86/traps: Cleanup and robustify decode_bug() Peter Zijlstra
2024-11-05 15:19   ` Andrew Cooper [this message]
2024-11-05 11:39 ` [PATCH 6/8] x86/ibt: Clean up is_endbr() Peter Zijlstra
2024-11-05 11:39 ` [PATCH 7/8] x86/ibt: Clean up poison_endbr() Peter Zijlstra
2024-11-05 11:39 ` [PATCH 8/8] x86/early_printk: Harden early_serial Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ea88e0d2-2ada-40e6-9bca-06a598dba70d@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=alyssa.milburn@intel.com \
    --cc=ebiggers@kernel.org \
    --cc=joao@overdrivepizza.com \
    --cc=jpoimboe@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=samitolvanen@google.com \
    --cc=scott.d.constable@intel.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.