Linux Trace Kernel
 help / color / mirror / Atom feed
* [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions
@ 2026-11-23 22:23 Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:23 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

[Resend with correct From tag.]

Hi,

Following up on Pablo Fraile Alonso's report and patch [1], as well as the
additional review points from Sashiko and Rick Edgecombe, this series fixes
the underlying x86 instruction decoder issues and the related opcode-map
assumptions in x86 uprobes.

Originally, the x86 instruction decoder was designed primarily for
instructions typically emitted in the kernel. Since uprobes uses it on
arbitrary user-space instruction encodings, several issues around multi-byte,
REX2, and VEX/EVEX/XOP encodings have surfaced:

1. insn_is_nop() in arch/x86/lib/insn-eval.c mutates insn->opcode.value
   in-place for REX2 M0=1 instructions without updating insn->opcode.nbytes,
   corrupting struct insn for subsequent callers (e.g. uprobe_init_insn()
   and branch_setup_xol_ops()).
2. gen-insn-attr-x86.awk skips emitting INAT_REX2_VARIANT for 0xa1 (JMPABS)
   because the check is placed after 'if (length(flags) == 0) continue'.
3. insn_get_opcode() never checks INAT_NO_REX2 / INAT_REX2_VARIANT or
   invalid REX2 encodings such as REX2 M0=0 + 0x0f or REX2 M0=1 + 0x38/0x3a.
4. insn->opcode.nbytes represents the physical opcode byte count (needed for
   field offset calculations such as insn_offset_modrm()), so REX2 M0=1 and
   VEX/EVEX/XOP instructions all have insn->opcode.nbytes == 1. Callers
   need a helper to determine the logical opcode map; insn_masking_exception()
   also lacked this check and could misidentify non-map-0 0x1f/0x8e opcodes.
5. In arch/x86/kernel/uprobes.c, bit 0x0f in good_insns_{32,64} bypassed
   good_2byte_insns, and uprobe_init_insn(), branch_setup_xol_ops(),
   arch_uprobe_analyze_insn(), and riprel_analyze() assumed map-0 or 1-byte
   REX encodings, causing misidentification of AVX/VEX instructions such as
   VZEROUPPER (c5 f8 77), VPSHUFD (c5 f9 70), VPOR (c5 f9 eb), and VCMPPS
   (c5 f8 c2), as well as REX2 prefix corruption in riprel_analyze().

Patch overview:
- Patch 1: Fix insn_is_nop() not to mutate struct insn.
- Patch 2: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT.
- Patch 3: Reject invalid REX2 encodings in insn_get_opcode().
- Patch 4: Add insn_opcode_map() / insn_last_opcode() helpers and fix
           insn_masking_exception().
- Patch 5: Fix opcode validation, branch/fixup analysis, and REX2 RIP-rel
           handling in arch/x86/kernel/uprobes.c.

Notes:
- checkpatch.pl reports warnings on patch 5 for using -ENOTSUPP and -ENOSYS
  in arch/x86/kernel/uprobes.c. Those match the existing error code
  conventions of uprobe_init_insn() and branch_setup_xol_ops() across
  uprobes; cleaning up error codes across uprobes should be done separately
  and is intentionally left out of scope for this fix series.
- Auditing whether Group 15 (0f ae: lfence/mfence/sfence, fxsave/fxrstor,
  CET instructions, etc.) should be enabled in good_2byte_insns (as raised
  by Rick Edgecombe) is also left for a separate follow-up discussion/patch.
- Uprobes should audit the three-byte opcodes and provide good_3bytes1_insns
  and good_3byte2_insns bit table if needed.

Testing:
- insn_decoder_test on vmlinux .text (7,926,007 instructions): 0 errors
- insn_sanity (64-bit and 32-bit, 1,000,000 random insns each): 0 errors
- Host unit tests covering REX2 (valid/invalid), JMPABS, VEX2, POP/MOV SS
- QEMU (KVM, -cpu host) uprobe runtime test verifying rejection of 8
  disallowed/invalid encodings (0f 05, 0f 0b, REX2 M0=0+0f/e8/a1,
  REX2 M0=1+05, 10-byte REX2 M0=1, EVEX map 4) and clean XOL/emulation
  execution + trace hits for 7 allowed instructions (bswap, nop10,
  vzeroupper, vcmpps, vpshufd, vpor, crc32).

[1] https://lore.kernel.org/r/20261116-x86-uprobes-two-byte-opcode-v1-1-2b6dbabead52@pablofraile.net

Thank you,

---
Masami Hiramatsu (5):
  x86/insn-eval: Do not mutate struct insn in insn_is_nop()
  x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT
  x86/insn: Reject invalid REX2 encodings in insn_get_opcode()
  x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception()
  uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions

 arch/x86/include/asm/inat.h                | 10 +++
 arch/x86/include/asm/insn.h                | 43 +++++++++++++
 arch/x86/kernel/uprobes.c                  | 97 +++++++++++++++++++++---------
 arch/x86/lib/insn-eval.c                   | 17 ++++--
 arch/x86/lib/insn.c                        | 16 +++++
 arch/x86/tools/gen-insn-attr-x86.awk       |  6 +-
 tools/arch/x86/include/asm/inat.h          | 10 +++
 tools/arch/x86/include/asm/insn.h          | 43 +++++++++++++
 tools/arch/x86/lib/insn.c                  | 16 +++++
 tools/arch/x86/tools/gen-insn-attr-x86.awk |  6 +-
 10 files changed, 225 insertions(+), 39 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [RESEND PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop()
  2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
@ 2026-11-23 22:23 ` Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT Masami Hiramatsu (Google)
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:23 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

From: Masami Hiramatsu <mhiramat@kernel.org>

For REX2 M0=1 (map 1) instructions, insn_is_nop() shifts insn->opcode.value
left by 8 bits and ORs in 0x0f in-place without updating
insn->opcode.nbytes.

This corrupts the decoded struct insn for subsequent callers. For example,
in uprobes arch_uprobe_analyze_insn(), can_optimize() ->
is_optimizable_nop10() calls insn_is_nop() on any 10-byte instruction
before uprobe_init_insn(), leaving OPCODE1(insn) == 0x0f with
opcode.nbytes == 1, and a second call from branch_setup_xol_ops() shifts
it a second time.

Use local variables for the opcode bytes instead of mutating insn->opcode.

Fixes: 8a5c6cbfe4a3 ("x86/insn,uprobes,alternative: Unify insn_is_nop()")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 arch/x86/lib/insn-eval.c |   17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/arch/x86/lib/insn-eval.c b/arch/x86/lib/insn-eval.c
index e03eeec55cfe..c67f8ac375c4 100644
--- a/arch/x86/lib/insn-eval.c
+++ b/arch/x86/lib/insn-eval.c
@@ -1700,6 +1700,7 @@ bool insn_is_nop(struct insn *insn)
 	u8 sib = 0, sib_scale, sib_index, sib_base;
 	u8 nrex, rex;
 	u8 p, rep = 0;
+	u8 op0, op1;
 
 	if ((nrex = insn->rex_prefix.nbytes)) {
 		rex = insn->rex_prefix.bytes[nrex-1];
@@ -1745,21 +1746,25 @@ bool insn_is_nop(struct insn *insn)
 	}
 
 	/*
-	 * Opcode map munging:
+	 * Opcode map handling:
 	 *
 	 * REX2: 0 - single byte opcode
 	 *       1 - 0f second byte opcode
 	 */
 	switch (m) {
-	case 0: break;
-	case 1: insn->opcode.value <<= 8;
-		insn->opcode.value |= 0x0f;
+	case 0:
+		op0 = insn->opcode.bytes[0];
+		op1 = insn->opcode.nbytes == 2 ? insn->opcode.bytes[1] : 0;
+		break;
+	case 1:
+		op0 = 0x0f;
+		op1 = insn->opcode.bytes[0];
 		break;
 	default:
 		return false;
 	}
 
-	switch (insn->opcode.bytes[0]) {
+	switch (op0) {
 	case 0x0f: /* 2nd byte */
 		break;
 
@@ -1811,7 +1816,7 @@ bool insn_is_nop(struct insn *insn)
 		return false;
 	}
 
-	switch (insn->opcode.bytes[1]) {
+	switch (op1) {
 	case 0x1f:
 		return modrm_reg == 0; /* 0f 1f /0 -- NOPL */
 


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RESEND PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT
  2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
@ 2026-11-23 22:23 ` Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode() Masami Hiramatsu (Google)
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:23 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

From: Masami Hiramatsu <mhiramat@kernel.org>

In gen-insn-attr-x86.awk, the check for the (REX2) superscript tag was
placed after 'if (length(flags) == 0) continue'. For opcode 0xa1
('MOV rAX,Ov (!REX2) | JMPABS O (REX2),(o64)'), the JMPABS alternative
has no flags set by convert_operands(), so the loop skipped before
emitting INAT_REX2_VARIANT and no entry in inat-tables.c ever received
INAT_REX2_VARIANT.

Move the rex2_expr check before the empty-flags skip so INAT_REX2_VARIANT
is properly added to flags and emitted in inat-tables.c.

Fixes: eada38d575a2 ("x86/insn: Add support for REX2 prefix to the instruction decoder logic")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 arch/x86/tools/gen-insn-attr-x86.awk       |    6 ++++--
 tools/arch/x86/tools/gen-insn-attr-x86.awk |    6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/arch/x86/tools/gen-insn-attr-x86.awk b/arch/x86/tools/gen-insn-attr-x86.awk
index 7ea1b75e59b7..186506a3db22 100644
--- a/arch/x86/tools/gen-insn-attr-x86.awk
+++ b/arch/x86/tools/gen-insn-attr-x86.awk
@@ -354,6 +354,10 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 		if (match(ext, no_rex2_expr))
 			flags = add_flags(flags, "INAT_NO_REX2")
 
+		# check REX2 variant
+		if (match(ext, rex2_expr))
+			flags = add_flags(flags, "INAT_REX2_VARIANT")
+
 		# check REX prefix
 		if (match(opcode, rex_expr))
 			flags = add_flags(flags, "INAT_MAKE_PREFIX(INAT_PFX_REX)")
@@ -395,8 +399,6 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 			lptable3[idx] = add_flags(lptable3[idx],flags)
 			variant = "INAT_VARIANT"
 		}
-		if (match(ext, rex2_expr))
-			table[idx] = add_flags(table[idx], "INAT_REX2_VARIANT")
 		if (!match(ext, lprefix_expr)){
 			table[idx] = add_flags(table[idx],flags)
 		}
diff --git a/tools/arch/x86/tools/gen-insn-attr-x86.awk b/tools/arch/x86/tools/gen-insn-attr-x86.awk
index 7ea1b75e59b7..186506a3db22 100644
--- a/tools/arch/x86/tools/gen-insn-attr-x86.awk
+++ b/tools/arch/x86/tools/gen-insn-attr-x86.awk
@@ -354,6 +354,10 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 		if (match(ext, no_rex2_expr))
 			flags = add_flags(flags, "INAT_NO_REX2")
 
+		# check REX2 variant
+		if (match(ext, rex2_expr))
+			flags = add_flags(flags, "INAT_REX2_VARIANT")
+
 		# check REX prefix
 		if (match(opcode, rex_expr))
 			flags = add_flags(flags, "INAT_MAKE_PREFIX(INAT_PFX_REX)")
@@ -395,8 +399,6 @@ function convert_operands(count,opnd,       i,j,imm,mod)
 			lptable3[idx] = add_flags(lptable3[idx],flags)
 			variant = "INAT_VARIANT"
 		}
-		if (match(ext, rex2_expr))
-			table[idx] = add_flags(table[idx], "INAT_REX2_VARIANT")
 		if (!match(ext, lprefix_expr)){
 			table[idx] = add_flags(table[idx],flags)
 		}


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RESEND PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode()
  2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
  2026-11-23 22:23 ` [RESEND PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT Masami Hiramatsu (Google)
@ 2026-11-23 22:23 ` Masami Hiramatsu (Google)
  2026-11-23 22:24 ` [RESEND PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception() Masami Hiramatsu (Google)
  2026-11-23 22:24 ` [RESEND PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
  4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:23 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

From: Masami Hiramatsu <mhiramat@kernel.org>

Although INAT_NO_REX2 and INAT_REX2_VARIANT attributes are generated from
x86-opcode-map.txt, insn_get_opcode() never checked them for REX2-prefixed
instructions. It also accepted REX2 M0=0 followed by an escape byte (0x0f),
legacy/REX/REX2/VEX prefix bytes, or 64-bit invalid opcodes, as well as
REX2 M0=1 followed by 3-byte escape opcodes (0x38/0x3a) or VEX-only
opcodes.

In particular, REX2 M0=0 + 0x0f (0xd5 0x00 0x0f) returned success with
opcode.bytes[0] = 0x0f and opcode.nbytes = 1, which can confuse callers
such as uprobes.

Add inat_is_no_rex2() and inat_is_rex2_variant() helpers and reject
invalid REX2 encodings with -EINVAL in insn_get_opcode().

Fixes: eada38d575a2 ("x86/insn: Add support for REX2 prefix to the instruction decoder logic")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 arch/x86/include/asm/inat.h       |   10 ++++++++++
 arch/x86/lib/insn.c               |   16 ++++++++++++++++
 tools/arch/x86/include/asm/inat.h |   10 ++++++++++
 tools/arch/x86/lib/insn.c         |   16 ++++++++++++++++
 4 files changed, 52 insertions(+)

diff --git a/arch/x86/include/asm/inat.h b/arch/x86/include/asm/inat.h
index 1b3060a3425c..26edd32dbabc 100644
--- a/arch/x86/include/asm/inat.h
+++ b/arch/x86/include/asm/inat.h
@@ -263,4 +263,14 @@ static inline int inat_is_invalid64(insn_attr_t attr)
 {
 	return attr & INAT_INV64;
 }
+
+static inline int inat_is_no_rex2(insn_attr_t attr)
+{
+	return attr & INAT_NO_REX2;
+}
+
+static inline int inat_is_rex2_variant(insn_attr_t attr)
+{
+	return attr & INAT_REX2_VARIANT;
+}
 #endif
diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c
index 225af1399c9d..36c477daf370 100644
--- a/arch/x86/lib/insn.c
+++ b/arch/x86/lib/insn.c
@@ -335,6 +335,22 @@ int insn_get_opcode(struct insn *insn)
 			insn->attr = inat_get_escape_attribute(op, pfx_id, esc_attr);
 		} else {
 			insn->attr = inat_get_opcode_attribute(op);
+			if (inat_is_legacy_prefix(insn->attr) ||
+			    inat_is_rex_prefix(insn->attr) ||
+			    inat_is_rex2_prefix(insn->attr) ||
+			    inat_is_vex_prefix(insn->attr) ||
+			    inat_is_invalid64(insn->attr)) {
+				insn->attr = 0;
+				return -EINVAL;
+			}
+		}
+		if (inat_is_escape(insn->attr) ||
+		    inat_must_vex(insn->attr) ||
+		    (inat_is_no_rex2(insn->attr) &&
+		     !inat_is_rex2_variant(insn->attr))) {
+			/* This instruction is bad */
+			insn->attr = 0;
+			return -EINVAL;
 		}
 		goto end;
 	}
diff --git a/tools/arch/x86/include/asm/inat.h b/tools/arch/x86/include/asm/inat.h
index 099e926595bd..5e7648e50c2b 100644
--- a/tools/arch/x86/include/asm/inat.h
+++ b/tools/arch/x86/include/asm/inat.h
@@ -263,4 +263,14 @@ static inline int inat_is_invalid64(insn_attr_t attr)
 {
 	return attr & INAT_INV64;
 }
+
+static inline int inat_is_no_rex2(insn_attr_t attr)
+{
+	return attr & INAT_NO_REX2;
+}
+
+static inline int inat_is_rex2_variant(insn_attr_t attr)
+{
+	return attr & INAT_REX2_VARIANT;
+}
 #endif
diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c
index 1d1c57c74d1f..9eb81232874a 100644
--- a/tools/arch/x86/lib/insn.c
+++ b/tools/arch/x86/lib/insn.c
@@ -335,6 +335,22 @@ int insn_get_opcode(struct insn *insn)
 			insn->attr = inat_get_escape_attribute(op, pfx_id, esc_attr);
 		} else {
 			insn->attr = inat_get_opcode_attribute(op);
+			if (inat_is_legacy_prefix(insn->attr) ||
+			    inat_is_rex_prefix(insn->attr) ||
+			    inat_is_rex2_prefix(insn->attr) ||
+			    inat_is_vex_prefix(insn->attr) ||
+			    inat_is_invalid64(insn->attr)) {
+				insn->attr = 0;
+				return -EINVAL;
+			}
+		}
+		if (inat_is_escape(insn->attr) ||
+		    inat_must_vex(insn->attr) ||
+		    (inat_is_no_rex2(insn->attr) &&
+		     !inat_is_rex2_variant(insn->attr))) {
+			/* This instruction is bad */
+			insn->attr = 0;
+			return -EINVAL;
 		}
 		goto end;
 	}


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RESEND PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception()
  2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
                   ` (2 preceding siblings ...)
  2026-11-23 22:23 ` [RESEND PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode() Masami Hiramatsu (Google)
@ 2026-11-23 22:24 ` Masami Hiramatsu (Google)
  2026-11-23 22:24 ` [RESEND PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
  4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:24 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

From: Masami Hiramatsu <mhiramat@kernel.org>

The x86 instruction decoder stores the physical opcode bytes in
insn->opcode.bytes[] and their physical byte count in insn->opcode.nbytes,
which is required for field offset calculations such as
insn_offset_modrm(). As a result, REX2 M0=1 (map 1) and VEX/EVEX/XOP
instructions all have insn->opcode.nbytes == 1 even though their opcodes
do not belong to map 0.

Add insn_opcode_map() and insn_last_opcode() helpers so callers can
reliably determine the logical opcode map and final opcode byte across
legacy, REX2, VEX, EVEX, and XOP encodings.

Use insn_opcode_map() in insn_masking_exception() so that non-map-0
instructions (e.g. VEX/EVEX/XOP or REX2 M0=1 instructions with opcode
0x1f or 0x8e) are not misidentified as POP SS or MOV SS.

Fixes: ee6a7354a362 ("kprobes/x86: Prohibit probing on exception masking instructions")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 arch/x86/include/asm/insn.h       |   43 +++++++++++++++++++++++++++++++++++++
 tools/arch/x86/include/asm/insn.h |   43 +++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)

diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h
index 8fb92da1a2c4..9861560efc36 100644
--- a/arch/x86/include/asm/insn.h
+++ b/arch/x86/include/asm/insn.h
@@ -326,6 +326,46 @@ static inline int insn_offset_immediate(struct insn *insn)
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
+/*
+ * Return the logical opcode map number for @insn:
+ *  0: 1-byte opcode map (legacy / REX / REX2 M0=0)
+ *  1: 2-byte opcode map (0x0f / REX2 M0=1 / VEX m=1 / EVEX m=1)
+ *  2: 3-byte opcode map (0x0f 0x38 / VEX m=2 / EVEX m=2)
+ *  3: 3-byte opcode map (0x0f 0x3a / VEX m=3 / EVEX m=3)
+ *  4..7: EVEX/VEX maps 4..7
+ *  8..31: XOP maps
+ */
+static inline int insn_opcode_map(struct insn *insn)
+{
+	if (insn_is_avx_or_xop(insn)) {
+		if (avx_insn_is_xop(insn))
+			return insn_xop_map_bits(insn);
+		return insn_vex_m_bits(insn);
+	}
+
+	if (insn_is_rex2(insn))
+		return insn_rex2_m_bit(insn) ? 1 : 0;
+
+	if (insn->opcode.nbytes == 1)
+		return 0;
+	if (insn->opcode.nbytes == 2)
+		return 1;
+	if (insn->opcode.nbytes == 3) {
+		if (insn->opcode.bytes[1] == 0x38)
+			return 2;
+		if (insn->opcode.bytes[1] == 0x3a)
+			return 3;
+	}
+	return -1;
+}
+
+static inline insn_byte_t insn_last_opcode(struct insn *insn)
+{
+	if (!insn->opcode.nbytes)
+		return 0;
+	return insn->opcode.bytes[insn->opcode.nbytes - 1];
+}
+
 /*
  * Intel SDM Vol.3A 6.8.3 states;
  * "Any single-step trap that would be delivered following the MOV to SS
@@ -336,6 +376,9 @@ static inline int insn_offset_immediate(struct insn *insn)
  */
 static inline int insn_masking_exception(struct insn *insn)
 {
+	if (insn_opcode_map(insn) != 0)
+		return 0;
+
 	return insn->opcode.bytes[0] == POP_SS_OPCODE ||
 		(insn->opcode.bytes[0] == MOV_SREG_OPCODE &&
 		 X86_MODRM_REG(insn->modrm.bytes[0]) == 2);
diff --git a/tools/arch/x86/include/asm/insn.h b/tools/arch/x86/include/asm/insn.h
index 1d2903508bbe..e7fba77f0b11 100644
--- a/tools/arch/x86/include/asm/insn.h
+++ b/tools/arch/x86/include/asm/insn.h
@@ -326,6 +326,46 @@ static inline int insn_offset_immediate(struct insn *insn)
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
+/*
+ * Return the logical opcode map number for @insn:
+ *  0: 1-byte opcode map (legacy / REX / REX2 M0=0)
+ *  1: 2-byte opcode map (0x0f / REX2 M0=1 / VEX m=1 / EVEX m=1)
+ *  2: 3-byte opcode map (0x0f 0x38 / VEX m=2 / EVEX m=2)
+ *  3: 3-byte opcode map (0x0f 0x3a / VEX m=3 / EVEX m=3)
+ *  4..7: EVEX/VEX maps 4..7
+ *  8..31: XOP maps
+ */
+static inline int insn_opcode_map(struct insn *insn)
+{
+	if (insn_is_avx_or_xop(insn)) {
+		if (avx_insn_is_xop(insn))
+			return insn_xop_map_bits(insn);
+		return insn_vex_m_bits(insn);
+	}
+
+	if (insn_is_rex2(insn))
+		return insn_rex2_m_bit(insn) ? 1 : 0;
+
+	if (insn->opcode.nbytes == 1)
+		return 0;
+	if (insn->opcode.nbytes == 2)
+		return 1;
+	if (insn->opcode.nbytes == 3) {
+		if (insn->opcode.bytes[1] == 0x38)
+			return 2;
+		if (insn->opcode.bytes[1] == 0x3a)
+			return 3;
+	}
+	return -1;
+}
+
+static inline insn_byte_t insn_last_opcode(struct insn *insn)
+{
+	if (!insn->opcode.nbytes)
+		return 0;
+	return insn->opcode.bytes[insn->opcode.nbytes - 1];
+}
+
 /*
  * Intel SDM Vol.3A 6.8.3 states;
  * "Any single-step trap that would be delivered following the MOV to SS
@@ -336,6 +376,9 @@ static inline int insn_offset_immediate(struct insn *insn)
  */
 static inline int insn_masking_exception(struct insn *insn)
 {
+	if (insn_opcode_map(insn) != 0)
+		return 0;
+
 	return insn->opcode.bytes[0] == POP_SS_OPCODE ||
 		(insn->opcode.bytes[0] == MOV_SREG_OPCODE &&
 		 X86_MODRM_REG(insn->modrm.bytes[0]) == 2);


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [RESEND PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions
  2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
                   ` (3 preceding siblings ...)
  2026-11-23 22:24 ` [RESEND PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception() Masami Hiramatsu (Google)
@ 2026-11-23 22:24 ` Masami Hiramatsu (Google)
  4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-23 22:24 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Lukas Zobernig
  Cc: H . Peter Anvin, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso, Edgecombe Rick P

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);
 	}
 


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-23 22:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 22:23 [RESEND PATCH 0/5] x86/insn, uprobes: Fix opcode map handling for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)
2026-11-23 22:23 ` [RESEND PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop() Masami Hiramatsu (Google)
2026-11-23 22:23 ` [RESEND PATCH 2/5] x86/insn: Fix gen-insn-attr-x86.awk to emit INAT_REX2_VARIANT Masami Hiramatsu (Google)
2026-11-23 22:23 ` [RESEND PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode() Masami Hiramatsu (Google)
2026-11-23 22:24 ` [RESEND PATCH 4/5] x86/insn: Add insn_opcode_map() helper and fix insn_masking_exception() Masami Hiramatsu (Google)
2026-11-23 22:24 ` [RESEND PATCH 5/5] uprobes/x86: Fix opcode validation and analysis for multi-byte, REX2, and VEX/EVEX/XOP instructions Masami Hiramatsu (Google)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox