[PATCH 1/5] x86/insn-eval: Do not mutate struct insn in insn_is_nop()

From: Masami Hiramatsu (Google)

Date: Sun Nov 22 2026 - 21:47:35 EST


From: Masami Hiramatsu <mhiramat@xxxxxxxxxx>

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@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
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 */