linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -tip 0/2] x86/kprobes: Fix and cleanup can_boost()
@ 2021-03-25 10:08 Masami Hiramatsu
  2021-03-25 10:08 ` [PATCH -tip 1/2] x86/kprobes: Fix to check non boostable prefixes correctly Masami Hiramatsu
  2021-03-25 10:08 ` [PATCH -tip 2/2] x86/kprobes: Fix to identify indirect jmp and others using range case Masami Hiramatsu
  0 siblings, 2 replies; 5+ messages in thread
From: Masami Hiramatsu @ 2021-03-25 10:08 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Thomas Gleixner, Ingo Molnar, Andy Lutomirski, Borislav Petkov,
	x86, H . Peter Anvin, Masami Hiramatsu, linux-kernel,
	kernel-janitors, Colin Ian King, dan.carpenter,
	Muhammad Usama Anjum

Hi,

Here are 2 bugfixes for the x86/kprobes, [1/2] fixes an old bug which tries to find
instruction prefixes in insn->opcode (which should be never found) and misusing
insn->attr. [2/2] fixes a new bug found by Coverity in the recent commit 6256e668b7
("x86/kprobes: Use int3 instead of debug trap for single-step").
To fix the 2nd one, [2/2] also cleanup the switch-case with range cases, because
that is much more clear what is not boostable.

Thank you,

---

Masami Hiramatsu (2):
      x86/kprobes: Fix to check non boostable prefixes correctly
      x86/kprobes: Fix to identify indirect jmp and others using range case


 arch/x86/kernel/kprobes/core.c |   57 +++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

--
Masami Hiramatsu (Linaro) <mhiramat@kernel.org>

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

* [PATCH -tip 1/2] x86/kprobes: Fix to check non boostable prefixes correctly
  2021-03-25 10:08 [PATCH -tip 0/2] x86/kprobes: Fix and cleanup can_boost() Masami Hiramatsu
@ 2021-03-25 10:08 ` Masami Hiramatsu
  2021-03-25 11:08   ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
  2021-03-25 10:08 ` [PATCH -tip 2/2] x86/kprobes: Fix to identify indirect jmp and others using range case Masami Hiramatsu
  1 sibling, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2021-03-25 10:08 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Thomas Gleixner, Ingo Molnar, Andy Lutomirski, Borislav Petkov,
	x86, H . Peter Anvin, Masami Hiramatsu, linux-kernel,
	kernel-janitors, Colin Ian King, dan.carpenter,
	Muhammad Usama Anjum

There are 2 bugs in the can_boost() function because of using
x86 insn decoder. Since the insn->opcode never has a prefix byte,
it can not find CS override prefix in it. And the insn->attr is
the attribute of the opcode, thus inat_is_address_size_prefix(
insn->attr) always returns false.

Fix those by checking each prefix bytes with for_each_insn_prefix
loop and getting the correct attribute for each prefix byte.
Also, this removes unlikely, because this is a slow path.

Fixes: a8d11cd0714f ("kprobes/x86: Consolidate insn decoder users for copying code")
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/kernel/kprobes/core.c |   17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 89d9f26785c7..503958f15cf9 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -139,6 +139,8 @@ NOKPROBE_SYMBOL(synthesize_relcall);
 int can_boost(struct insn *insn, void *addr)
 {
 	kprobe_opcode_t opcode;
+	insn_byte_t prefix;
+	int i;
 
 	if (search_exception_tables((unsigned long)addr))
 		return 0;	/* Page fault may occur on this address. */
@@ -151,9 +153,14 @@ int can_boost(struct insn *insn, void *addr)
 	if (insn->opcode.nbytes != 1)
 		return 0;
 
-	/* Can't boost Address-size override prefix */
-	if (unlikely(inat_is_address_size_prefix(insn->attr)))
-		return 0;
+	for_each_insn_prefix(insn, i, prefix) {
+		insn_attr_t attr;
+
+		attr = inat_get_opcode_attribute(prefix);
+		/* Can't boost Address-size override prefix and CS override prefix */
+		if (prefix == 0x2e || inat_is_address_size_prefix(attr))
+			return 0;
+	}
 
 	opcode = insn->opcode.bytes[0];
 
@@ -181,8 +188,8 @@ int can_boost(struct insn *insn, void *addr)
 		/* indirect jmp is boostable */
 		return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
 	default:
-		/* CS override prefix and call are not boostable */
-		return (opcode != 0x2e && opcode != 0x9a);
+		/* call is not boostable */
+		return opcode != 0x9a;
 	}
 }
 


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

* [PATCH -tip 2/2] x86/kprobes: Fix to identify indirect jmp and others using range case
  2021-03-25 10:08 [PATCH -tip 0/2] x86/kprobes: Fix and cleanup can_boost() Masami Hiramatsu
  2021-03-25 10:08 ` [PATCH -tip 1/2] x86/kprobes: Fix to check non boostable prefixes correctly Masami Hiramatsu
@ 2021-03-25 10:08 ` Masami Hiramatsu
  2021-03-25 11:08   ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
  1 sibling, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2021-03-25 10:08 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Thomas Gleixner, Ingo Molnar, Andy Lutomirski, Borislav Petkov,
	x86, H . Peter Anvin, Masami Hiramatsu, linux-kernel,
	kernel-janitors, Colin Ian King, dan.carpenter,
	Muhammad Usama Anjum

Fix can_boost() to identify indirect jmp and others using range case
correctly.

Since the condition in switch statement is opcode & 0xf0, it can not
evaluate to 0xff case. This should be under the 0xf0 case. However,
there is no reason to use the conbinations of the bit-masked condition
and lower bit checking.

Use range case to clean up the switch statement too.

Fixes: 6256e668b7 ("x86/kprobes: Use int3 instead of debug trap for single-step")
Reported-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/kernel/kprobes/core.c |   44 ++++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 503958f15cf9..c1c763840d6e 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -164,32 +164,28 @@ int can_boost(struct insn *insn, void *addr)
 
 	opcode = insn->opcode.bytes[0];
 
-	switch (opcode & 0xf0) {
-	case 0x60:
-		/* can't boost "bound" */
-		return (opcode != 0x62);
-	case 0x70:
-		return 0; /* can't boost conditional jump */
-	case 0x90:
-		return opcode != 0x9a;	/* can't boost call far */
-	case 0xc0:
-		/* can't boost software-interruptions */
-		return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
-	case 0xd0:
-		/* can boost AA* and XLAT */
-		return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
-	case 0xe0:
-		/* can boost in/out and absolute jmps */
-		return ((opcode & 0x04) || opcode == 0xea);
-	case 0xf0:
-		/* clear and set flags are boostable */
-		return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
-	case 0xff:
-		/* indirect jmp is boostable */
+	switch (opcode) {
+	case 0x62:		/* bound */
+	case 0x70 ... 0x7f:	/* Conditional jumps */
+	case 0x9a:		/* Call far */
+	case 0xc0 ... 0xc1:	/* Grp2 */
+	case 0xcc ... 0xce:	/* software exceptions */
+	case 0xd0 ... 0xd3:	/* Grp2 */
+	case 0xd6:		/* (UD) */
+	case 0xd8 ... 0xdf:	/* ESC */
+	case 0xe0 ... 0xe3:	/* LOOP*, JCXZ */
+	case 0xe8 ... 0xe9:	/* near Call, JMP */
+	case 0xeb:		/* Short JMP */
+	case 0xf0 ... 0xf4:	/* LOCK/REP, HLT */
+	case 0xf6 ... 0xf7:	/* Grp3 */
+	case 0xfe:		/* Grp4 */
+		/* ... are not boostable */
+		return 0;
+	case 0xff:		/* Grp5 */
+		/* Only indirect jmp is boostable */
 		return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
 	default:
-		/* call is not boostable */
-		return opcode != 0x9a;
+		return 1;
 	}
 }
 


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

* [tip: x86/core] x86/kprobes: Fix to identify indirect jmp and others using range case
  2021-03-25 10:08 ` [PATCH -tip 2/2] x86/kprobes: Fix to identify indirect jmp and others using range case Masami Hiramatsu
@ 2021-03-25 11:08   ` tip-bot2 for Masami Hiramatsu
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Masami Hiramatsu @ 2021-03-25 11:08 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Colin Ian King, Masami Hiramatsu, Ingo Molnar, x86, linux-kernel

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     2f706e0e5e263c0d204e37ea496cbb0e98aac2d2
Gitweb:        https://git.kernel.org/tip/2f706e0e5e263c0d204e37ea496cbb0e98aac2d2
Author:        Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate:    Thu, 25 Mar 2021 19:08:43 +09:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Thu, 25 Mar 2021 11:37:22 +01:00

x86/kprobes: Fix to identify indirect jmp and others using range case

Fix can_boost() to identify indirect jmp and others using range case
correctly.

Since the condition in switch statement is opcode & 0xf0, it can not
evaluate to 0xff case. This should be under the 0xf0 case. However,
there is no reason to use the conbinations of the bit-masked condition
and lower bit checking.

Use range case to clean up the switch statement too.

Fixes: 6256e668b7 ("x86/kprobes: Use int3 instead of debug trap for single-step")
Reported-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/161666692308.1120877.4675552834049546493.stgit@devnote2
---
 arch/x86/kernel/kprobes/core.c | 44 +++++++++++++++------------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 81e1432..922a6e2 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -164,32 +164,28 @@ int can_boost(struct insn *insn, void *addr)
 
 	opcode = insn->opcode.bytes[0];
 
-	switch (opcode & 0xf0) {
-	case 0x60:
-		/* can't boost "bound" */
-		return (opcode != 0x62);
-	case 0x70:
-		return 0; /* can't boost conditional jump */
-	case 0x90:
-		return opcode != 0x9a;	/* can't boost call far */
-	case 0xc0:
-		/* can't boost software-interruptions */
-		return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
-	case 0xd0:
-		/* can boost AA* and XLAT */
-		return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
-	case 0xe0:
-		/* can boost in/out and absolute jmps */
-		return ((opcode & 0x04) || opcode == 0xea);
-	case 0xf0:
-		/* clear and set flags are boostable */
-		return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
-	case 0xff:
-		/* indirect jmp is boostable */
+	switch (opcode) {
+	case 0x62:		/* bound */
+	case 0x70 ... 0x7f:	/* Conditional jumps */
+	case 0x9a:		/* Call far */
+	case 0xc0 ... 0xc1:	/* Grp2 */
+	case 0xcc ... 0xce:	/* software exceptions */
+	case 0xd0 ... 0xd3:	/* Grp2 */
+	case 0xd6:		/* (UD) */
+	case 0xd8 ... 0xdf:	/* ESC */
+	case 0xe0 ... 0xe3:	/* LOOP*, JCXZ */
+	case 0xe8 ... 0xe9:	/* near Call, JMP */
+	case 0xeb:		/* Short JMP */
+	case 0xf0 ... 0xf4:	/* LOCK/REP, HLT */
+	case 0xf6 ... 0xf7:	/* Grp3 */
+	case 0xfe:		/* Grp4 */
+		/* ... are not boostable */
+		return 0;
+	case 0xff:		/* Grp5 */
+		/* Only indirect jmp is boostable */
 		return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
 	default:
-		/* call is not boostable */
-		return opcode != 0x9a;
+		return 1;
 	}
 }
 

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

* [tip: x86/core] x86/kprobes: Fix to check non boostable prefixes correctly
  2021-03-25 10:08 ` [PATCH -tip 1/2] x86/kprobes: Fix to check non boostable prefixes correctly Masami Hiramatsu
@ 2021-03-25 11:08   ` tip-bot2 for Masami Hiramatsu
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Masami Hiramatsu @ 2021-03-25 11:08 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Masami Hiramatsu, Ingo Molnar, x86, linux-kernel

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     6dd3b8c9f58816a1354be39559f630cd1bd12159
Gitweb:        https://git.kernel.org/tip/6dd3b8c9f58816a1354be39559f630cd1bd12159
Author:        Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate:    Thu, 25 Mar 2021 19:08:31 +09:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Thu, 25 Mar 2021 11:37:21 +01:00

x86/kprobes: Fix to check non boostable prefixes correctly

There are 2 bugs in the can_boost() function because of using
x86 insn decoder. Since the insn->opcode never has a prefix byte,
it can not find CS override prefix in it. And the insn->attr is
the attribute of the opcode, thus inat_is_address_size_prefix(
insn->attr) always returns false.

Fix those by checking each prefix bytes with for_each_insn_prefix
loop and getting the correct attribute for each prefix byte.
Also, this removes unlikely, because this is a slow path.

Fixes: a8d11cd0714f ("kprobes/x86: Consolidate insn decoder users for copying code")
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/161666691162.1120877.2808435205294352583.stgit@devnote2
---
 arch/x86/kernel/kprobes/core.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 3a14e8a..81e1432 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -139,6 +139,8 @@ NOKPROBE_SYMBOL(synthesize_relcall);
 int can_boost(struct insn *insn, void *addr)
 {
 	kprobe_opcode_t opcode;
+	insn_byte_t prefix;
+	int i;
 
 	if (search_exception_tables((unsigned long)addr))
 		return 0;	/* Page fault may occur on this address. */
@@ -151,9 +153,14 @@ int can_boost(struct insn *insn, void *addr)
 	if (insn->opcode.nbytes != 1)
 		return 0;
 
-	/* Can't boost Address-size override prefix */
-	if (unlikely(inat_is_address_size_prefix(insn->attr)))
-		return 0;
+	for_each_insn_prefix(insn, i, prefix) {
+		insn_attr_t attr;
+
+		attr = inat_get_opcode_attribute(prefix);
+		/* Can't boost Address-size override prefix and CS override prefix */
+		if (prefix == 0x2e || inat_is_address_size_prefix(attr))
+			return 0;
+	}
 
 	opcode = insn->opcode.bytes[0];
 
@@ -181,8 +188,8 @@ int can_boost(struct insn *insn, void *addr)
 		/* indirect jmp is boostable */
 		return X86_MODRM_REG(insn->modrm.bytes[0]) == 4;
 	default:
-		/* CS override prefix and call are not boostable */
-		return (opcode != 0x2e && opcode != 0x9a);
+		/* call is not boostable */
+		return opcode != 0x9a;
 	}
 }
 

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

end of thread, other threads:[~2021-03-25 11:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-25 10:08 [PATCH -tip 0/2] x86/kprobes: Fix and cleanup can_boost() Masami Hiramatsu
2021-03-25 10:08 ` [PATCH -tip 1/2] x86/kprobes: Fix to check non boostable prefixes correctly Masami Hiramatsu
2021-03-25 11:08   ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu
2021-03-25 10:08 ` [PATCH -tip 2/2] x86/kprobes: Fix to identify indirect jmp and others using range case Masami Hiramatsu
2021-03-25 11:08   ` [tip: x86/core] " tip-bot2 for Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).