All of lore.kernel.org
 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: [RESEND PATCH 3/5] x86/insn: Reject invalid REX2 encodings in insn_get_opcode()
Date: Tue, 24 Nov 2026 07:23:55 +0900	[thread overview]
Message-ID: <179547263496.326766.3048022446114454399.stgit@devnote2> (raw)
In-Reply-To: <179547260053.326766.11540305553612301074.stgit@devnote2>

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


  parent reply	other threads:[~2026-11-23 22:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]
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)

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=179547263496.326766.3048022446114454399.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 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.