Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] uprobes/x86: Fix bypass of two-byte opcode validation
@ 2026-11-16 15:40 Pablo Fraile Alonso
  2026-11-16 16:01 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Pablo Fraile Alonso @ 2026-11-16 15:40 UTC (permalink / raw)
  To: Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
	Denys Vlasenko
  Cc: Ingo Molnar, linux-kernel, linux-trace-kernel,
	Pablo Fraile Alonso

Commit 67fc809217dc ("uprobes/x86: Fix 1-byte opcode tables") set
bit 0x0f in both good_insns_32 and good_insns_64, assuming that
the opcode escape byte would not be reported by OPCODE1(insn).

However, unlike ordinary instruction prefixes, 0x0f is an opcode
escape byte retained in insn->opcode.bytes[0] by the x86 instruction
decoder. Subsequent opcode bytes are stored in the following positions.

For example, SYSCALL (0F 05) is decoded as:

    insn->opcode.bytes[0] = 0x0f;
    insn->opcode.bytes[1] = 0x05;

Consequently, OPCODE1(insn) returns 0x0f, causing the first
test_bit() in uprobe_init_insn() to succeed and return 0 before
good_2byte_insns is consulted.

This bypasses the secondary opcode allowlist for all legacy
two-byte instructions, including SYSCALL (0F 05), which is
explicitly disallowed by good_2byte_insns.

Three-byte opcodes (0F 38 and 0F 3A) are also affected by the
early return, since their decoded opcodes begin with 0x0f.

Fix the validation logic so that legacy two-byte instructions
are checked against good_2byte_insns. Preserve the existing
behavior for three-byte opcodes, which are not currently checked
against a dedicated allowlist (in my opinion, this should be
audited on another patch, instead of returning -ENOTSUPP).

Fixes: 67fc809217dc ("uprobes/x86: Fix 1-byte opcode tables")

Signed-off-by: Pablo Fraile Alonso <pablo@pablofraile.net>
---
I created two reproducers (and a brief explanation of them). One it's a simple one that demonstrates what I told (that you can attach to a non-allowed instruction), an another one that freezes a system by attaching a "poisoned" uprobe to glibc.

- Dummy: https://gist.github.com/Pablito2020/077fc8d8b6d7a75c23c702c7021d68e6
- Poisoned Glibc: https://gist.github.com/Pablito2020/bdda586ab023ec4760716b3be7cff102

After the fix, registration of the uprobe should fail because
the instruction is rejected by the secondary opcode bitmap.
---
 arch/x86/kernel/uprobes.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index 0f60c0d076b6..1cf901d18688 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -292,14 +292,24 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn)
 	else
 		good_insns = good_insns_32;
 
-	if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
-		return 0;
-
-	if (insn->opcode.nbytes == 2) {
+	switch (insn->opcode.nbytes) {
+	case 1:
+		if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
+			return 0;
+		break;
+	case 2:
 		if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
 			return 0;
+		break;
+	case 3:
+		/*
+		 * As of today, allow three-byte opcodes, although
+		 * one day  we should investigate which instructions may
+		 * not be  compatible with XOL and reject specific
+		 * problematic encodings.
+		 */
+		return 0;
 	}
-
 	return -ENOTSUPP;
 }
 

---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261116-x86-uprobes-two-byte-opcode-7fa743830931

Best regards,
--  
Pablo Fraile Alonso <pablo@pablofraile.net>


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

* Re: [PATCH] uprobes/x86: Fix bypass of two-byte opcode validation
  2026-11-16 15:40 [PATCH] uprobes/x86: Fix bypass of two-byte opcode validation Pablo Fraile Alonso
@ 2026-11-16 16:01 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16 16:01 UTC (permalink / raw)
  To: Pablo Fraile Alonso; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] uprobes/x86: REX2 map-1 opcodes bypass good_2byte_insns in
  uprobe_init_insn()
--

commit 4da9c45b309734e7a1e4eb07255d608c16d67ca3
Author: Pablo Fraile Alonso <pablo@pablofraile.net>

uprobes/x86: Fix bypass of two-byte opcode validation

This commit updates uprobe_init_insn() to switch on insn->opcode.nbytes so
that two-byte instructions starting with 0x0f are checked against
good_2byte_insns instead of matching bit 0x0f in good_insns.

> diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
> index 0f60c0d076b62..1cf901d186884 100644
> --- a/arch/x86/kernel/uprobes.c
> +++ b/arch/x86/kernel/uprobes.c
> @@ -292,14 +292,24 @@ static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn)
>  	else
>  		good_insns = good_insns_32;
>  
> -	if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
> -		return 0;
> -
> -	if (insn->opcode.nbytes == 2) {
> +	switch (insn->opcode.nbytes) {
> +	case 1:
> +		if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
> +			return 0;
> +		break;
> +	case 2:
>  		if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
>  			return 0;
> +		break;

[Severity: Medium]
Does switching solely on insn->opcode.nbytes still allow REX2-prefixed
map-1 (0x0f) instructions to bypass good_2byte_insns?

For a REX2 prefix with M0=1, insn_get_opcode() stores the map-1 opcode
byte in insn->opcode.bytes[0] (OPCODE1(insn)) and leaves
insn->opcode.nbytes == 1:

arch/x86/lib/insn.c:insn_get_opcode() {
    ...
	if (insn_is_rex2(insn)) {
		if (insn_rex2_m_bit(insn)) {
			/* map 1 is escape 0x0f */
			insn_attr_t esc_attr = inat_get_opcode_attribute(0x0f);

			pfx_id = insn_last_prefix_id(insn);
			insn->attr = inat_get_escape_attribute(op, pfx_id, esc_attr);
		} else {
			insn->attr = inat_get_opcode_attribute(op);
		}
		goto end;
	}
    ...
}

In uprobe_init_insn(), those instructions enter case 1 and test their
map-1 opcode against good_insns_64 (map 0) instead of good_2byte_insns,
allowing disallowed 2-byte instructions such as syscall (0xd5 0x80 0x05)
to pass validation while rejecting valid map-1 instructions such as
bswap (0x0f 0xc8..0xcf) whose opcode byte is 0 in good_insns_64.

In addition, because bit 0x0f remains set in good_insns_64, a 10-byte
REX2 M0=1 instruction has its insn->opcode.value shifted and ORed with
0x0f in-place by can_optimize() -> is_optimizable_nop10() ->
insn_is_nop() without updating insn->opcode.nbytes, so case 1 tests bit
0x0f and accepts any 10-byte REX2 M0=1 instruction regardless of opcode
(as well as an invalid REX2 M0=0 instruction with opcode byte 0x0f).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-x86-uprobes-two-byte-opcode-v1-1-2b6dbabead52@pablofraile.net?part=1

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

end of thread, other threads:[~2026-11-16 16:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 15:40 [PATCH] uprobes/x86: Fix bypass of two-byte opcode validation Pablo Fraile Alonso
2026-11-16 16:01 ` sashiko-bot

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