mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, Masami Hiramatsu <mhiramat@kernel.org>,
	Oleg Nesterov <oleg@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Lukas Zobernig <lukas.zobernig@gmail.com>
Cc: "H . Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	Pablo Fraile Alonso <pablo@pablofraile.net>,
	Edgecombe Rick P <rick.p.edgecombe@intel.com>
Subject: [PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions
Date: Mon, 23 Nov 2026 11:48:06 +0900	[thread overview]
Message-ID: <179540208688.233843.9296024425945000350.stgit@devnote2> (raw)
In-Reply-To: <179540203033.233843.12951034848925983867.stgit@devnote2>

From: Masami Hiramatsu <mhiramat@kernel.org>

Commit 67fc809217dc ("uprobes/x86: Fix 1-byte opcode tables") set bit
0x0f in good_insns_32 and good_insns_64, assuming 0x0f would not appear
in OPCODE1(insn). Because the x86 instruction decoder stores 0x0f in
insn->opcode.bytes[0], uprobe_init_insn() returned 0 immediately for all
0x0f-escaped instructions, bypassing good_2byte_insns (including SYSCALL).

Furthermore, several places in arch/x86/kernel/uprobes.c assume that
OPCODE1(insn) always refers to a map-0 legacy 1-byte opcode, which breaks
for REX2 M0=1 (map 1) and VEX/EVEX/XOP instructions (all of which have
insn->opcode.nbytes == 1):

- uprobe_init_insn() checks REX2 M0=1 and VEX/EVEX/XOP opcodes against
  good_insns_64 (map 0) instead of their actual opcode maps, and does
  not reject unsupported REX2 JMPABS (0xa1) or EVEX map 4 (APX) opcodes.
- branch_setup_xol_ops() can misidentify VEX/EVEX/XOP or REX2 M0=1
  instructions whose opcode byte matches 0x70..0x7f, 0xe8, 0xe9, or 0xeb
  (e.g. VZEROUPPER 0xc5 0xf8 0x77, VPSHUFD, VPOR) as branches.
- arch_uprobe_analyze_insn() switches on OPCODE1(&insn) without checking
  the opcode map, misidentifying e.g. VCMPPS/VCMPPD (VEX.0F.C2) as RET
  (0xc2) and skipping UPROBE_FIX_IP.
- riprel_analyze() clears bit 0 of the first REX byte, which for a 2-byte
  REX2 prefix corrupts 0xd5 into 0xd4 instead of clearing B3/B4 in byte 1.

Clear bit 0x0f in good_insns_32/good_insns_64, use insn_opcode_map() and
insn_last_opcode() to dispatch opcode validation and analysis properly,
and fix REX2 handling in riprel_analyze().

Fixes: 67fc809217dc ("uprobes/x86: Fix 1-byte opcode tables")
Reported-by: Pablo Fraile Alonso <pablo@pablofraile.net>
Link: https://lore.kernel.org/r/20261116-x86-uprobes-two-byte-opcode-v1-1-2b6dbabead52@pablofraile.net
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 arch/x86/kernel/uprobes.c |   97 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 68 insertions(+), 29 deletions(-)

diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index 0f60c0d076b6..5ea8e9c92259 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -89,7 +89,7 @@
 static volatile u32 good_insns_32[256 / 32] = {
 	/*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f         */
 	/*      ----------------------------------------------         */
-	W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 00 */
+	W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */
 	W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
 	W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
 	W(0x30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
@@ -151,7 +151,7 @@ static volatile u32 good_insns_32[256 / 32] = {
 static volatile u32 good_insns_64[256 / 32] = {
 	/*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f         */
 	/*      ----------------------------------------------         */
-	W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* 00 */
+	W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */
 	W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
 	W(0x20, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 20 */
 	W(0x30, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 30 */
@@ -279,6 +279,7 @@ static bool is_prefix_bad(struct insn *insn)
 static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u32 volatile *good_insns;
+	int map;
 
 	if (is_prefix_bad(insn))
 		return -ENOTSUPP;
@@ -287,17 +288,44 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn)
 	if (insn_masking_exception(insn))
 		return -ENOTSUPP;
 
+	map = insn_opcode_map(insn);
+
+	if (insn_is_avx_or_xop(insn)) {
+		/*
+		 * EVEX map 4 (APX) includes control-flow and privileged
+		 * instructions (e.g. Grp5 indirect call/jmp, INVEPT/INVPCID)
+		 * that are not yet handled by uprobes.
+		 */
+		if (insn_is_evex(insn) && map == 4)
+			return -ENOTSUPP;
+		return 0;
+	}
+
 	if (insn->x86_64)
 		good_insns = good_insns_64;
 	else
 		good_insns = good_insns_32;
 
-	if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
-		return 0;
-
-	if (insn->opcode.nbytes == 2) {
-		if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
+	switch (map) {
+	case 0:
+		/* REX2 0xa1 is JMPABS, which is not yet supported by uprobes. */
+		if (insn_is_rex2(insn) && OPCODE1(insn) == 0xa1)
+			return -ENOTSUPP;
+		if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
 			return 0;
+		break;
+	case 1:
+		if (test_bit(insn_last_opcode(insn), (unsigned long *)good_2byte_insns))
+			return 0;
+		break;
+	case 2:
+	case 3:
+		/*
+		 * Preserve existing behavior for legacy three-byte opcodes
+		 * (0f 38 / 0f 3a), which have not yet been audited against
+		 * a dedicated allowlist.
+		 */
+		return 0;
 	}
 
 	return -ENOTSUPP;
@@ -470,10 +498,14 @@ static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Clear REX.b bit (extension of MODRM.rm field):
 	 * we want to encode low numbered reg, not r8+.
 	 */
-	if (insn->rex_prefix.nbytes) {
+	if (insn->rex_prefix.nbytes == 1) {
 		cursor = auprobe->insn + insn_offset_rex_prefix(insn);
 		/* REX byte has 0100wrxb layout, clearing REX.b bit */
 		*cursor &= 0xfe;
+	} else if (insn->rex_prefix.nbytes == 2) {
+		cursor = auprobe->insn + insn_offset_rex_prefix(insn) + 1;
+		/* REX2 payload has M0 R4 X4 B4 W R3 X3 B3 layout, clear B4 and B3 */
+		*cursor &= 0xee;
 	}
 	/*
 	 * Similar treatment for VEX3/EVEX prefix.
@@ -1493,6 +1525,9 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	if (insn_is_nop(insn))
 		goto setup;
 
+	if (insn_is_avx_or_xop(insn) || insn_is_rex2(insn))
+		return -ENOSYS;
+
 	switch (opc1) {
 	case 0xeb:	/* jmp 8 */
 	case 0xe9:	/* jmp 32 */
@@ -1658,31 +1693,35 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
 	 * Figure out which fixups default_post_xol_op() will need to perform,
 	 * and annotate defparam->fixups accordingly.
 	 */
-	switch (OPCODE1(&insn)) {
-	case 0x9d:		/* popf */
-		auprobe->defparam.fixups |= UPROBE_FIX_SETF;
-		break;
-	case 0xc3:		/* ret or lret -- ip is correct */
-	case 0xcb:
-	case 0xc2:
-	case 0xca:
-	case 0xea:		/* jmp absolute -- ip is correct */
-		fix_ip_or_call = 0;
-		break;
-	case 0x9a:		/* call absolute - Fix return addr, not ip */
-		fix_ip_or_call = UPROBE_FIX_CALL;
-		break;
-	case 0xff:
-		switch (MODRM_REG(&insn)) {
-		case 2: case 3:			/* call or lcall, indirect */
-			fix_ip_or_call = UPROBE_FIX_CALL;
+	if (insn_opcode_map(&insn) == 0) {
+		switch (OPCODE1(&insn)) {
+		case 0x9d:		/* popf */
+			auprobe->defparam.fixups |= UPROBE_FIX_SETF;
 			break;
-		case 4: case 5:			/* jmp or ljmp, indirect */
+		case 0xc3:		/* ret or lret -- ip is correct */
+		case 0xcb:
+		case 0xc2:
+		case 0xca:
+		case 0xea:		/* jmp absolute -- ip is correct */
 			fix_ip_or_call = 0;
 			break;
+		case 0x9a:		/* call absolute - Fix return addr, not ip */
+			fix_ip_or_call = UPROBE_FIX_CALL;
+			break;
+		case 0xff:
+			switch (MODRM_REG(&insn)) {
+			case 2: case 3:			/* call or lcall, indirect */
+				fix_ip_or_call = UPROBE_FIX_CALL;
+				break;
+			case 4: case 5:			/* jmp or ljmp, indirect */
+				fix_ip_or_call = 0;
+				break;
+			}
+			fallthrough;
+		default:
+			riprel_analyze(auprobe, &insn);
 		}
-		fallthrough;
-	default:
+	} else {
 		riprel_analyze(auprobe, &insn);
 	}
 


      parent reply	other threads:[~2026-11-23  2:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  2:47 [PATCH 0/5] x86/insn, uprobes: Fix opcode map handling " None
2026-11-23  2:47 ` [PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
2026-11-23  2:47 ` [PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT Masami Hiramatsu (Google)
2026-11-23  2:47 ` [PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode() Masami Hiramatsu (Google)
2026-11-23  2:47 ` [PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception() Masami Hiramatsu (Google)
2026-11-23  2:48 ` Masami Hiramatsu (Google) [this message]

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=179540208688.233843.9296024425945000350.stgit@devnote2 \
    --to=mhiramat@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=lukas.zobernig@gmail.com \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=pablo@pablofraile.net \
    --cc=peterz@infradead.org \
    --cc=rick.p.edgecombe@intel.com \
    --cc=tglx@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®