Skip to contentSkip to conversation

x86/insn: Fix not using prefixes.nbytes for loop over prefixes.bytes

PATCH Applied Masami Hiramatsu 6 participants 32 messages Dec 3, 2020 – Dec 10, 2020

Subsystems (3)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v2 cover x86/insn: Fix not using prefixes.nbytes for loop over prefixes.bytes

Hi,

Here are the 2nd version of patches to fix the wrong loop boundary check on insn.prefixes.bytes[] array.

The previous version is here;

https://lkml.kernel.org/r/160689905099.3084105.7880450206184269465.stgit@devnote2

In this version, I introduced for_each_insn_prefix() macro to for looping on the prefixes in the given instruction and fixed out-of-bounds-read issue by checking index first. Also, I sorted the patches so that the oldest commit fix becomes the first patch because it will go into the older stable kernel and that introduces the new iteration macro.

Kees Cook got a syzbot warning and found this issue and there were similar wrong boundary check patterns in the x86 code.

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 (*) and i < 4 instead of insn.prefixes.nbytes.

(*) Note that insn.prefixes.bytes[] should be zeroed in insn_init() before decoding, and 0x00 is not a legacy prefix. So if you see 0 on insn.prefix.bytes[], it indicates the end of the array. Or, if the prefixes.bytes[] is filled with prefix bytes, we can check the index is less than 4.

Thank you,

---

Masami Hiramatsu (3):
x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes x86/insn-eval: Fix not using prefixes.nbytes for loop over prefixes.bytes x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes

 arch/x86/boot/compressed/sev-es.c |    5 ++---
 arch/x86/include/asm/insn.h       |   15 +++++++++++++++
 arch/x86/kernel/uprobes.c         |   10 ++++++----
 arch/x86/lib/insn-eval.c          |   10 +++++-----
 4 files changed, 28 insertions(+), 12 deletions(-)
Signature · 1 line
Masami Hiramatsu (Linaro) <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/3 x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead of insn.prefixes.nbytes.
This introduces for_each_insn_prefix() macro for this purpose.

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]

---
Changes in v2:
- Add for_each_insn_prefix() macro and fix to check index first.

---
 arch/x86/include/asm/insn.h |   15 +++++++++++++++
 arch/x86/kernel/uprobes.c   |   10 ++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
arch/x86/include/asm/insn.h +15 -0
index 5c1ae3eff9d4..e6b38ebd3a1c 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes are repeated,
+ * we can not use it for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < 4 && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa042823d..138bdb1fd136 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
maintainer
#2 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 12 lines · Masami Hiramatsu

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead of insn.prefixes.nbytes.
This introduces for_each_insn_prefix() macro for this purpose.

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
Quoted 7 lines

---
Changes in v2:
- Add for_each_insn_prefix() macro and fix to check index first.

---
 arch/x86/include/asm/insn.h |   15 +++++++++++++++
 arch/x86/kernel/uprobes.c   |   10 ++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)

Warning: Kernel ABI header at 'tools/arch/x86/include/asm/insn.h' differs from latest version at 'arch/x86/include/asm/insn.h'

Quoted 9 lines
arch/x86/include/asm/insn.h +1 -0
index 5c1ae3eff9d4..e6b38ebd3a1c 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 214

Quoted 10 lines
arch/x86/include/asm/insn.h +10 -0
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes are repeated,
+ * we can not use it for looping over the prefixes.

Please use passive voice: no "we" or "I", etc,

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 217

Quoted 3 lines
arch/x86/include/asm/insn.h +3 -0
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < 4 && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)

Btw, looking at the struct insn definition, that prefixes member should have a comment above it that those are the legacy prefixes which can be <= 4. But that's minor.

In any case, I'll fix up the minor issues now but pls remember to do them in the future.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#3 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 3 lines · Borislav Petkov

Btw, looking at the struct insn definition, that prefixes member should have a comment above it that those are the legacy prefixes which can be <= 4. But that's minor.

And that naked 4 is poking my eye too - It'd be better if it were NUM_LEGACY_PREFIXES.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer Reviewed-by Srikar Dronamraju
#4 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

So it ended up like this:

---
From 5014e4e902778d63ce392f864b3654baa4b72384 Mon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Thu, 3 Dec 2020 13:50:37 +0900
Subject: [PATCH] x86/uprobes: Do not use prefixes.nbytes when looping over
prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, add NUM_LEGACY_PREFIXES, sync with the
respective header in tools/ and drop "we". ]

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
---
 arch/x86/include/asm/insn.h       | 16 ++++++++++++++++
 arch/x86/kernel/uprobes.c         | 10 ++++++----
 tools/arch/x86/include/asm/insn.h | 18 +++++++++++++++++-
 3 files changed, 39 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +16 -0
index 5c1ae3eff9d4..fe8e862004d3 100644
@@ -58,6 +58,7 @@ struct insn {
 };
 
 #define MAX_INSN_SIZE	15
+#define NUM_LEGACY_PREFIXES 4
 
 #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
 #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
@@ -201,6 +202,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than NUM_LEGACY_PREFIXES if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < NUM_LEGACY_PREFIXES && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa042823d..138bdb1fd136 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
tools/arch/x86/include/asm/insn.h +17 -1
index 568854b14d0a..fe8e862004d3 100644
@@ -8,7 +8,7 @@
  */
 
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include <asm/inat.h>
 
 struct insn_field {
 	union {
@@ -58,6 +58,7 @@ struct insn {
 };
 
 #define MAX_INSN_SIZE	15
+#define NUM_LEGACY_PREFIXES 4
 
 #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
 #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
@@ -201,6 +202,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than NUM_LEGACY_PREFIXES if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < NUM_LEGACY_PREFIXES && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
Signature · 8 lines
2.21.0


-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#5 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 56 lines · Borislav Petkov

So it ended up like this:

---
From 5014e4e902778d63ce392f864b3654baa4b72384 Mon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Thu, 3 Dec 2020 13:50:37 +0900
Subject: [PATCH] x86/uprobes: Do not use prefixes.nbytes when looping over
prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

   insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, add NUM_LEGACY_PREFIXES, sync with the
respective header in tools/ and drop "we". ]

---
  arch/x86/include/asm/insn.h       | 16 ++++++++++++++++
  arch/x86/kernel/uprobes.c         | 10 ++++++----
  tools/arch/x86/include/asm/insn.h | 18 +++++++++++++++++-
  3 files changed, 39 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +5 -0
index 5c1ae3eff9d4..fe8e862004d3 100644
@@ -58,6 +58,7 @@ struct insn {
  };
 
  #define MAX_INSN_SIZE	15
+#define NUM_LEGACY_PREFIXES 4
 
  #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
  #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
@@ -201,6 +202,21 @@ static inline int insn_offset_immediate(struct insn *insn)
  	return insn_offset_displacement(insn) + insn->displacement.nbytes;
  }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 218

Quoted 10 lines
arch/x86/include/asm/insn.h +10 -0
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than NUM_LEGACY_PREFIXES if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < NUM_LEGACY_PREFIXES && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)

Since this is based on the array size, can

idx < NUM_LEGACY_PREFIXES

be replaced with:

	idx < ARRAY_SIZE(insn->prefixes.bytes)

?

Thanks,
Tom

Quoted 4 lines
arch/x86/include/asm/insn.h +1 -0
+
  #define POP_SS_OPCODE 0x1f
  #define MOV_SREG_OPCODE 0x8e
 
maintainer
#6 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 7 lines · Tom Lendacky

Since this is based on the array size, can

idx < NUM_LEGACY_PREFIXES

be replaced with:

	idx < ARRAY_SIZE(insn->prefixes.bytes)

Actually, this needs another change:

struct insn_field {
        union {
                insn_value_t value;
                insn_byte_t bytes[NUM_LEGACY_PREFIXES];

because you can have max. 4 legacy prefixes and then we can do either of the checks above.

Mine is shorter tho. :-)

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#7 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Tom Lendacky

Since this is based on the array size, can

idx < NUM_LEGACY_PREFIXES

be replaced with:

Quoted 8 lines
Quoted 1 line
	idx < ARRAY_SIZE(insn->prefixes.bytes)

Actually, this needs another change:

struct insn_field {
        union {
                insn_value_t value;
                insn_byte_t bytes[NUM_LEGACY_PREFIXES];

Blergh, spoke too soon. All those struct insn members are struct insn_field.

insn.prefixes should probably be a separate array of explicit size NUM_LEGACY_PREFIXES, not that insn_byte_t bytes[] gets enlarged in the future for whatever reason, while the max legacy prefixes count will remain 4.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 17 lines · Borislav Petkov
Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Tom Lendacky

Since this is based on the array size, can

idx < NUM_LEGACY_PREFIXES

be replaced with:

Quoted 8 lines
Quoted 1 line
	idx < ARRAY_SIZE(insn->prefixes.bytes)

Actually, this needs another change:

struct insn_field {
         union {
                 insn_value_t value;
                 insn_byte_t bytes[NUM_LEGACY_PREFIXES];
Quoted 7 lines

Blergh, spoke too soon. All those struct insn members are struct insn_field.

insn.prefixes should probably be a separate array of explicit size NUM_LEGACY_PREFIXES, not that insn_byte_t bytes[] gets enlarged in the future for whatever reason, while the max legacy prefixes count will remain 4.

Since that struct is used in multiple places, I think basing it on the array size is the best way to go. The main point of the check is just to be sure you don't read outside of the array.

Thanks,
Tom

Quoted 1 line
maintainer
#9 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 3 lines · Tom Lendacky

Since that struct is used in multiple places, I think basing it on the array size is the best way to go. The main point of the check is just to be sure you don't read outside of the array.

Well, what happens if someone increases the array size of:

struct insn_field {
	union {
		insn_byte_t bytes[4];
				^^^^

?

That's why a separate array only for legacy prefixes would be better in the long run. The array size check is good as a short-term fix for stable.

I'd say.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#10 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Tom Lendacky

Since that struct is used in multiple places, I think basing it on the array size is the best way to go. The main point of the check is just to be sure you don't read outside of the array.

Quoted 8 lines

Well, what happens if someone increases the array size of:

struct insn_field {
	union {
		insn_byte_t bytes[4];
				^^^^

?

I think we need to keep the parsing of the instruction separate from accessing the prefixes after (successfully) parsing it. This fix is merely making sure that we don't read outside the bounds of the array that currently holds the legacy prefixes.

Quoted 6 lines

That's why a separate array only for legacy prefixes would be better in the long run. The array size check is good as a short-term fix for stable.

I'd say.

According to Volume 3 of the AMD APM (Figure 1-2 on page 5), there could be as many as 5 legacy prefixes and it says that more than one prefix from each group is undefined behavior. The instruction parsing code doesn't seem to take into account the different prefix groups. So I agree with you that short term the array size check works, and long term, the legacy prefix support probably needs a closer look.

Thanks,
Tom

Quoted 1 line
author maintainer
#11 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 25 lines · On Thu, 3 Dec 2020 12:49:46 -0600 Tom Lendacky <[email protected]> wrote:
Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Tom Lendacky

Since that struct is used in multiple places, I think basing it on the array size is the best way to go. The main point of the check is just to be sure you don't read outside of the array.

Quoted 8 lines

Well, what happens if someone increases the array size of:

struct insn_field {
	union {
		insn_byte_t bytes[4];
				^^^^

?

I think we need to keep the parsing of the instruction separate from accessing the prefixes after (successfully) parsing it. This fix is merely making sure that we don't read outside the bounds of the array that currently holds the legacy prefixes.

Quoted 5 lines

That's why a separate array only for legacy prefixes would be better in the long run. The array size check is good as a short-term fix for stable.

Quoted 8 lines
Quoted 1 line

I'd say.

According to Volume 3 of the AMD APM (Figure 1-2 on page 5), there could be as many as 5 legacy prefixes and it says that more than one prefix from each group is undefined behavior. The instruction parsing code doesn't seem to take into account the different prefix groups. So I agree with you that short term the array size check works, and long term, the legacy prefix support probably needs a closer look.

Hmm, there is a difference between Intel SDM and AMD APM.

Intel SDM vol.2

2.1.1 Instruction Prefixes
Instruction prefixes are divided into four groups, each with a set of allowable prefix codes. For each instruction, it is only useful to include up to one prefix code from each of the four groups (Groups 1, 2, 3, 4).

AMD APM vol.3

1.2.1 Summary of Legacy Prefixes
Table 1-1 on page 7 shows the legacy prefixes. The legacy prefixes are organized into five groups, as shown in the left-most column of Table 1-1. An instruction encoding may include a maximum of one prefix from each of the five groups.

So, Intel CPU doesn't accept LOCK-REP because those are in a same prefix group, but AMD says it is acceptable. Actually, insn.c only accepts the prefix up to 4, so if there is any instruction which has 5 prefixes, it will fail to parse.

Note that anyway the same prefix can be repeated, we can see a good example in K8_NOP*.

/* Opteron 64bit nops
1: nop
2: osp nop
3: osp osp nop
4: osp osp osp nop
*/

In this case, insn.c just store the 1 osp in the prefixes.bytes[], and just increment prefixes.nbytes for the repeated prefixes.

Anyway, if there is LOCK-REP prefix combination, I have to introduce new insn_field for legacy prefix.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#12 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 52 lines · On Fri, 4 Dec 2020 09:56:53 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 25 lines · On Thu, 3 Dec 2020 12:49:46 -0600 Tom Lendacky <[email protected]> wrote:
Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Tom Lendacky

Since that struct is used in multiple places, I think basing it on the array size is the best way to go. The main point of the check is just to be sure you don't read outside of the array.

Quoted 8 lines

Well, what happens if someone increases the array size of:

struct insn_field {
	union {
		insn_byte_t bytes[4];
				^^^^

?

I think we need to keep the parsing of the instruction separate from accessing the prefixes after (successfully) parsing it. This fix is merely making sure that we don't read outside the bounds of the array that currently holds the legacy prefixes.

Quoted 5 lines

That's why a separate array only for legacy prefixes would be better in the long run. The array size check is good as a short-term fix for stable.

Quoted 8 lines
Quoted 1 line

I'd say.

According to Volume 3 of the AMD APM (Figure 1-2 on page 5), there could be as many as 5 legacy prefixes and it says that more than one prefix from each group is undefined behavior. The instruction parsing code doesn't seem to take into account the different prefix groups. So I agree with you that short term the array size check works, and long term, the legacy prefix support probably needs a closer look.

Hmm, there is a difference between Intel SDM and AMD APM.

Intel SDM vol.2

2.1.1 Instruction Prefixes
Instruction prefixes are divided into four groups, each with a set of allowable prefix codes. For each instruction, it is only useful to include up to one prefix code from each of the four groups (Groups 1, 2, 3, 4).

AMD APM vol.3

1.2.1 Summary of Legacy Prefixes
Table 1-1 on page 7 shows the legacy prefixes. The legacy prefixes are organized into five groups, as shown in the left-most column of Table 1-1. An instruction encoding may include a maximum of one prefix from each of the five groups.

Quoted 4 lines

So, Intel CPU doesn't accept LOCK-REP because those are in a same prefix group, but AMD says it is acceptable. Actually, insn.c only accepts the prefix up to 4, so if there is any instruction which has 5 prefixes, it will fail to parse.

OK, I got it. AMD APM's explanation is misleading. Intel SDM groups the legacy prefixes in 1) Lock/Repeat/Bound 2) Segment override/branch hints,
3) Operand-size override 4) Address-size override. On the other hand, AMD APM makes it 5 groups(See Table 1-1), 1) Operand-Size Override
2) Address-Size Override 3) Segment Override 4) Lock 5) Repeat.

So the difference is whether the Lock and Repeat is a group or not.

However, I found we must not see LOCK-REP prefix in the same instruction, because the available instruction for LOCK and REP are different.

AMD APM vol.3
---
1.2.5 Lock Prefix
...
The LOCK prefix can only be used with forms of the following instructions that write a memory
operand: ADC, ADD, AND, BTC, BTR, BTS, CMPXCHG, CMPXCHG8B, CMPXCHG16B, DEC, INC, NEG, NOT, OR, SBB, SUB, XADD, XCHG, and XOR. An invalid-opcode exception occurs if the LOCK prefix is used with any other instruction.

1.2.6 Repeat Prefixes
The repeat prefixes cause repetition of certain instructions that load, store, move, input, or output strings. The prefixes should only be used with such string instructions.
...
REP. ... The prefix can be used with the INS, LODS, MOVS, OUTS, and STOS instructions.
...
The REPE and REPZ prefixes can be used with the CMPS, CMPSB, CMPSD, CMPSW, SCAS, SCASB, SCASD, and SCASW instructions.
...
The REPNE and REPNZ prefixes can be used with the CMPS, CMPSB, CMPSD, CMPSW, SCAS, SCASB, SCASD, and SCASW instructions.
---

Thus, I think the current expectation -- legacy prefix will consist of 4 different groups -- is good. No need to take care of LOCK-REP combination.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#13 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

In reply to Masami Hiramatsu
Quoted 10 lines · Masami Hiramatsu

Hmm, there is a difference between Intel SDM and AMD APM.

Intel SDM vol.2

2.1.1 Instruction Prefixes
Instruction prefixes are divided into four groups, each with a set of allowable prefix codes. For each instruction, it is only useful to include up to one prefix code from each of the four groups (Groups 1, 2, 3, 4).

AMD APM vol.3

Quoted 7 lines

1.2.1 Summary of Legacy Prefixes
Table 1-1 on page 7 shows the legacy prefixes. The legacy prefixes are organized into five groups, as shown in the left-most column of Table 1-1. An instruction encoding may include a maximum of one prefix from each of the five groups.

So, Intel CPU doesn't accept LOCK-REP because those are in a same prefix group, but AMD says it is acceptable.

That would be a huge problem for code if both vendors would behave differently wrt prefixes.

Quoted 2 lines

Actually, insn.c only accepts the prefix up to 4, so if there is any instruction which has 5 prefixes, it will fail to parse.

Well, actually it looks more like a difference in how both vendors group things:

AMD has 5 groups and Intel 4 by putting LOCK and REP together.

The most important aspect, however, is that you can have as many prefixes as you want and there's no hardware limitation on the number - I'm being told - just that you can overflow the instruction limit of 15 and then get a #GP for invalid insn. See here:

https://sandpile.org/x86/opc_enc.htm

note #1

with examples how you can overflow the 15 bytes limit even with a valid insn.

Quoted 2 lines

Note that anyway the same prefix can be repeated, we can see a good example in K8_NOP*.

Yap.

Quoted 5 lines

In this case, insn.c just store the 1 osp in the prefixes.bytes[], and just increment prefixes.nbytes for the repeated prefixes.

Anyway, if there is LOCK-REP prefix combination, I have to introduce new insn_field for legacy prefix.

Well, the legacy prefixes field needs to be of 4 fields because REP and LOCK really are two separate but mutually exclusive groups. Why?

They're used by a disjoint set of instructions, see the AMD doc for both REP and LOCK prefixes.

Which means, you can either have a REP (exclusive or) LOCK but not both.

Which means, as a stable@ fix I can use Tom's ARRAY_SIZE() suggestion and then later on we can make the legacy prefixes a separate struct. Maybe even a struct with a bitfield:

struct legacy_prefixes {
        /* operand-size override: 0x66 */
        u8 os_over: 1,
        /* address-size override: 0x67 */
           as_over: 1,
        /*
         * segment override: 0x2e(CS), 0x3e(DS), 0x26(ES), 0x64(FS), 0x65(GS),
         * 0x36(SS)
         */
           s_over: 1,
        /* lock prefix: 0xf0 */
           lock:   1,
        /* repeat prefixes: 0xf2: REPNx, 0xf3: REPx */
           rep:    1,
	   __resv: 3;
};

or so which you can set to denote when you've seen the respective prefixes.

But that we can discuss later.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#14 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 53 lines · On Fri, 4 Dec 2020 12:06:44 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 10 lines · Masami Hiramatsu

Hmm, there is a difference between Intel SDM and AMD APM.

Intel SDM vol.2

2.1.1 Instruction Prefixes
Instruction prefixes are divided into four groups, each with a set of allowable prefix codes. For each instruction, it is only useful to include up to one prefix code from each of the four groups (Groups 1, 2, 3, 4).

AMD APM vol.3

Quoted 7 lines

1.2.1 Summary of Legacy Prefixes
Table 1-1 on page 7 shows the legacy prefixes. The legacy prefixes are organized into five groups, as shown in the left-most column of Table 1-1. An instruction encoding may include a maximum of one prefix from each of the five groups.

So, Intel CPU doesn't accept LOCK-REP because those are in a same prefix group, but AMD says it is acceptable.

That would be a huge problem for code if both vendors would behave differently wrt prefixes.

Quoted 2 lines

Actually, insn.c only accepts the prefix up to 4, so if there is any instruction which has 5 prefixes, it will fail to parse.

Well, actually it looks more like a difference in how both vendors group things:

AMD has 5 groups and Intel 4 by putting LOCK and REP together.

The most important aspect, however, is that you can have as many prefixes as you want and there's no hardware limitation on the number - I'm being told - just that you can overflow the instruction limit of 15 and then get a #GP for invalid insn. See here:

https://sandpile.org/x86/opc_enc.htm

note #1

with examples how you can overflow the 15 bytes limit even with a valid insn.

Quoted 2 lines

Note that anyway the same prefix can be repeated, we can see a good example in K8_NOP*.

Yap.

Quoted 5 lines

In this case, insn.c just store the 1 osp in the prefixes.bytes[], and just increment prefixes.nbytes for the repeated prefixes.

Anyway, if there is LOCK-REP prefix combination, I have to introduce new insn_field for legacy prefix.

Quoted 7 lines

Well, the legacy prefixes field needs to be of 4 fields because REP and LOCK really are two separate but mutually exclusive groups. Why?

They're used by a disjoint set of instructions, see the AMD doc for both REP and LOCK prefixes.

Which means, you can either have a REP (exclusive or) LOCK but not both.

Yeah, I found that. So I think the "max number of legacy groups on one instruction" is 4.

Quoted 3 lines

Which means, as a stable@ fix I can use Tom's ARRAY_SIZE() suggestion and then later on we can make the legacy prefixes a separate struct. Maybe even a struct with a bitfield:

Sorry, but I don't think we need such optimization. It seems over- optimized the code for me. Moreover, the last-prefix is meaningful for switching the opcode, so we need to keep it.

Thank you,

Quoted 28 lines
struct legacy_prefixes {
        /* operand-size override: 0x66 */
        u8 os_over: 1,
        /* address-size override: 0x67 */
           as_over: 1,
        /*
         * segment override: 0x2e(CS), 0x3e(DS), 0x26(ES), 0x64(FS), 0x65(GS),
         * 0x36(SS)
         */
           s_over: 1,
        /* lock prefix: 0xf0 */
           lock:   1,
        /* repeat prefixes: 0xf2: REPNx, 0xf3: REPx */
           rep:    1,
	   __resv: 3;
};

or so which you can set to denote when you've seen the respective prefixes.

But that we can discuss later.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#15 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

In reply to Tom Lendacky
Quoted 70 lines · On Thu, 3 Dec 2020 10:45:48 -0600 Tom Lendacky <[email protected]> wrote:
Quoted 56 lines · Borislav Petkov

So it ended up like this:

---
From 5014e4e902778d63ce392f864b3654baa4b72384 Mon Sep 17 00:00:00 2001
From: Masami Hiramatsu <[email protected]>
Date: Thu, 3 Dec 2020 13:50:37 +0900
Subject: [PATCH] x86/uprobes: Do not use prefixes.nbytes when looping over
prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

   insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, add NUM_LEGACY_PREFIXES, sync with the
respective header in tools/ and drop "we". ]

---
  arch/x86/include/asm/insn.h       | 16 ++++++++++++++++
  arch/x86/kernel/uprobes.c         | 10 ++++++----
  tools/arch/x86/include/asm/insn.h | 18 +++++++++++++++++-
  3 files changed, 39 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +5 -0
index 5c1ae3eff9d4..fe8e862004d3 100644
@@ -58,6 +58,7 @@ struct insn {
  };
 
  #define MAX_INSN_SIZE	15
+#define NUM_LEGACY_PREFIXES 4
 
  #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
  #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
@@ -201,6 +202,21 @@ static inline int insn_offset_immediate(struct insn *insn)
  	return insn_offset_displacement(insn) + insn->displacement.nbytes;
  }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 218

Quoted 10 lines
arch/x86/include/asm/insn.h +10 -0
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than NUM_LEGACY_PREFIXES if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < NUM_LEGACY_PREFIXES && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)

Since this is based on the array size, can

Quoted 5 lines

idx < NUM_LEGACY_PREFIXES

be replaced with:

	idx < ARRAY_SIZE(insn->prefixes.bytes)

You're right.
There are 2 issues are mixed in this solution.

- 4 bytes limitation comes from the prefixes.bytes size, because it is mapped to insn_value_t.

struct insn_field {
        union {
                insn_value_t value;
                insn_byte_t bytes[4];
        };

- The restriction that one instruction can have up to four types of prefixes comes from Intel's specification.

Intel SDM Vol.2
----
2.1.1 Instruction Prefixes
Instruction prefixes are divided into four groups, each with a set of allowable prefix codes. For each instruction, it is only useful to include up to one prefix code from each of the four groups (Groups 1, 2, 3, 4).
----

So, we need 2 macros to fix this.

#define NUM_INSN_FIELD_BYTES (sizeof(insn_value_t) / sizeof(insn_byte_t))
#define NUM_LEGACY_PREFIX_GROUPS 4 /* See Intel SDM Vol.2 2.2.1 Instruction Prefixes */

Thank you,

Quoted 10 lines

?

Thanks,
Tom

Quoted 4 lines
arch/x86/include/asm/insn.h +1 -0
+
  #define POP_SS_OPCODE 0x1f
  #define MOV_SREG_OPCODE 0x8e
 
Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#16 lore raw

Re: [PATCH v2 1/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes

In reply to Borislav Petkov
Quoted 21 lines · On Thu, 3 Dec 2020 13:37:57 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 12 lines · Masami Hiramatsu

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead of insn.prefixes.nbytes.
This introduces for_each_insn_prefix() macro for this purpose.

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
Quoted 7 lines

---
Changes in v2:
- Add for_each_insn_prefix() macro and fix to check index first.

---
 arch/x86/include/asm/insn.h |   15 +++++++++++++++
 arch/x86/kernel/uprobes.c   |   10 ++++++----
 2 files changed, 21 insertions(+), 4 deletions(-)
Quoted 1 line

Warning: Kernel ABI header at 'tools/arch/x86/include/asm/insn.h' differs from latest version at 'arch/x86/include/asm/insn.h'

Oops.

Quoted 9 lines
Quoted 9 lines
arch/x86/include/asm/insn.h +0 -0
index 5c1ae3eff9d4..e6b38ebd3a1c 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
Quoted 13 lines

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 214

Quoted 11 lines
arch/x86/include/asm/insn.h +11 -0
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes are repeated,
+ * we can not use it for looping over the prefixes.

Please use passive voice: no "we" or "I", etc,

OK.

Quoted 11 lines

arch/x86/include/asm/insn.h static inline int insn_offset_immediate(struct insn *insn) · line 217

Quoted 4 lines
arch/x86/include/asm/insn.h +3 -0
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < 4 && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)

Btw, looking at the struct insn definition, that prefixes member should have a comment above it that those are the legacy prefixes which can be <= 4. But that's minor.

In any case, I'll fix up the minor issues now but pls remember to do them in the future.

OK, I will add a macro with comment for it.

Thank you,

Quoted 8 lines

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 1 line
Masami Hiramatsu <[email protected]>
#17 lore raw

[tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

In reply to Masami Hiramatsu
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     9dc23f960adb9ce410ef835b32a2398fdb09c828
Gitweb:        https://git.kernel.org/tip/9dc23f960adb9ce410ef835b32a2398fdb09c828
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:37 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 04 Dec 2020 14:32:57 +01:00

x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, sync with the respective header in tools/
and drop "we". ]

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
---
 arch/x86/include/asm/insn.h       | 15 +++++++++++++++
 arch/x86/kernel/uprobes.c         | 10 ++++++----
 tools/arch/x86/include/asm/insn.h | 17 ++++++++++++++++-
 3 files changed, 37 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +15 -0
index 5c1ae3e..a8c3d28 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa04..138bdb1 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
tools/arch/x86/include/asm/insn.h +16 -1
index 568854b..a8c3d28 100644
@@ -8,7 +8,7 @@
  */
 
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include <asm/inat.h>
 
 struct insn_field {
 	union {
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
author maintainer
#18 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Quoted 108 lines · On Fri, 04 Dec 2020 15:04:03 -0000 "tip-bot2 for Masami Hiramatsu"…

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

Commit-ID:     9dc23f960adb9ce410ef835b32a2398fdb09c828
Gitweb:        https://git.kernel.org/tip/9dc23f960adb9ce410ef835b32a2398fdb09c828
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:37 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 04 Dec 2020 14:32:57 +01:00

x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, sync with the respective header in tools/
and drop "we". ]

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
---
 arch/x86/include/asm/insn.h       | 15 +++++++++++++++
 arch/x86/kernel/uprobes.c         | 10 ++++++----
 tools/arch/x86/include/asm/insn.h | 17 ++++++++++++++++-
 3 files changed, 37 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +15 -0
index 5c1ae3e..a8c3d28 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa04..138bdb1 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
tools/arch/x86/include/asm/insn.h +0 -0
index 568854b..a8c3d28 100644

tools/arch/x86/include/asm/insn.h line 11

Quoted 5 lines
tools/arch/x86/include/asm/insn.h +1 -1
@@ -8,7 +8,7 @@
  */
 
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include <asm/inat.h>

This may break tools/objtool build. Please keep "inat.h".

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#19 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Quoted 1 line · Masami Hiramatsu

This may break tools/objtool build. Please keep "inat.h".

How? Please elaborate.

Build tests are fine here.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#20 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Quoted 6 lines · On Sat, 5 Dec 2020 11:17:04 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 1 line · Masami Hiramatsu

This may break tools/objtool build. Please keep "inat.h".

How? Please elaborate.

Build tests are fine here.

Oops, sorry, it was for perf build.

Please refer commit 00a263902ac3 ("perf intel-pt: Use shared x86 insn decoder").

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#21 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

( drop stable@ )

Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Sat, 5 Dec 2020 11:17:04 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 1 line · Masami Hiramatsu

This may break tools/objtool build. Please keep "inat.h".

Quoted 7 lines
Quoted 3 lines

How? Please elaborate.

Build tests are fine here.

Oops, sorry, it was for perf build.

Please refer commit 00a263902ac3 ("perf intel-pt: Use shared x86 insn decoder").

Oh wow:

"This way we continue to be able to process perf.data files with Intel PT
traces in distros other than x86."

acme, why is that? Can you explain pls?

It probably would be better to fix this so that copying insn.h to keep it in sync won't cause any future breakages. Or the diffing check should verify whether header paths are wrong in the tools/ version and fail if so, so that we don't break it.

Hmmm.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#22 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Em Sun, Dec 06, 2020 at 10:02:50AM +0100, Borislav Petkov escreveu:

Quoted 22 lines

( drop stable@ )

Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Sat, 5 Dec 2020 11:17:04 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 1 line · Masami Hiramatsu

This may break tools/objtool build. Please keep "inat.h".

Quoted 7 lines
Quoted 3 lines

How? Please elaborate.

Build tests are fine here.

Oops, sorry, it was for perf build.

Please refer commit 00a263902ac3 ("perf intel-pt: Use shared x86 insn decoder").

Oh wow:

"This way we continue to be able to process perf.data files with Intel PT
traces in distros other than x86."

Quoted 6 lines

acme, why is that? Can you explain pls?

It probably would be better to fix this so that copying insn.h to keep it in sync won't cause any future breakages. Or the diffing check should verify whether header paths are wrong in the tools/ version and fail if so, so that we don't break it.

Trying to swap this back into my brain...

Humm, if I'm building this on, say, aarch64 then asm/ will not be pointing to x86, right? Intel PT needs the x86 instruction decoder, right?

I should've have wrote in the cset comment log if this was related to cross build failures I encountered, can't remember now :-\

- Arnaldo

#23 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Quoted 1 line · Arnaldo Carvalho de Melo

Trying to swap this back into my brain...

I know *exactly* what you mean. :)

Quoted 4 lines

Humm, if I'm building this on, say, aarch64 then asm/ will not be pointing to x86, right? Intel PT needs the x86 instruction decoder, right?

Yeah.

Quoted 2 lines

I should've have wrote in the cset comment log if this was related to cross build failures I encountered, can't remember now :-\

I think that is it. There's inat.h in tools/arch/x86/include/asm/ too so it needs to be exactly that one that gets included on other arches.

Quoted 6 lines

And also it would be interesting to avoid updating both the kernel and the tools/ copy, otherwise one would have to test the tools build, which may break with such updates.

The whole point of the copy is to avoid that, otherwise we could just use the kernel files directly.

Well, there's this diff -u thing which makes sure both copies are in sync.

Why did we ever copy the insn decoder to tools/?

There must've been some reason because otherwise we could probably use the one in arch/x86/lib/, in tools/.

Yeah, this whole copying of headers back'n'forth is turning out to be kinda hairy...

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
#24 lore raw

Re: [tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

In reply to Masami Hiramatsu

Em Sat, Dec 05, 2020 at 09:12:56AM +0900, Masami Hiramatsu escreveu:

Quoted 113 lines
Quoted 110 lines · On Fri, 04 Dec 2020 15:04:03 -0000 "tip-bot2 for Masami Hiramatsu"…

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

Commit-ID:     9dc23f960adb9ce410ef835b32a2398fdb09c828
Gitweb:        https://git.kernel.org/tip/9dc23f960adb9ce410ef835b32a2398fdb09c828
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:37 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 04 Dec 2020 14:32:57 +01:00

x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, sync with the respective header in tools/
and drop "we". ]

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
---
 arch/x86/include/asm/insn.h       | 15 +++++++++++++++
 arch/x86/kernel/uprobes.c         | 10 ++++++----
 tools/arch/x86/include/asm/insn.h | 17 ++++++++++++++++-
 3 files changed, 37 insertions(+), 5 deletions(-)
arch/x86/include/asm/insn.h +15 -0
index 5c1ae3e..a8c3d28 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa04..138bdb1 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
tools/arch/x86/include/asm/insn.h +0 -0
index 568854b..a8c3d28 100644
@@ -8,7 +8,7 @@
  */
 
Quoted 5 lines

tools/arch/x86/include/asm/insn.h line 11

Quoted 3 lines
tools/arch/x86/include/asm/insn.h +1 -1
 /* insn_attr_t is defined in inat.h */
-#include "inat.h"
+#include <asm/inat.h>

This may break tools/objtool build. Please keep "inat.h".

And also it would be interesting to avoid updating both the kernel and the tools/ copy, otherwise one would have to test the tools build, which may break with such updates.

The whole point of the copy is to avoid that, otherwise we could just use the kernel files directly.

- Arnaldo

#25 lore raw

[tip: x86/urgent] x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

In reply to Masami Hiramatsu
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     4e9a5ae8df5b3365183150f6df49e49dece80d8c
Gitweb:        https://git.kernel.org/tip/4e9a5ae8df5b3365183150f6df49e49dece80d8c
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:37 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Sun, 06 Dec 2020 09:58:13 +01:00

x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes.

Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message, sync with the respective header in tools/
and drop "we". ]

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Reported-by [email protected]
---
 arch/x86/include/asm/insn.h       | 15 +++++++++++++++
 arch/x86/kernel/uprobes.c         | 10 ++++++----
 tools/arch/x86/include/asm/insn.h | 15 +++++++++++++++
 3 files changed, 36 insertions(+), 4 deletions(-)
arch/x86/include/asm/insn.h +15 -0
index 5c1ae3e..a8c3d28 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
arch/x86/kernel/uprobes.c +6 -4
index 3fdaa04..138bdb1 100644
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
 
 static bool is_prefix_bad(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_ES):
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
 static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 {
 	u8 opc1 = OPCODE1(insn);
+	insn_byte_t p;
 	int i;
 
 	switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
 	 * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
 	 * No one uses these insns, reject any branch insns with such prefix.
 	 */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		if (insn->prefixes.bytes[i] == 0x66)
+	for_each_insn_prefix(insn, i, p) {
+		if (p == 0x66)
 			return -ENOTSUPP;
 	}
 
tools/arch/x86/include/asm/insn.h +15 -0
index 568854b..52c6262 100644
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
 	return insn_offset_displacement(insn) + insn->displacement.nbytes;
 }
 
+/**
+ * for_each_insn_prefix() -- Iterate prefixes in the instruction
+ * @insn: Pointer to struct insn.
+ * @idx:  Index storage.
+ * @prefix: Prefix byte.
+ *
+ * Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
+ * and the index is stored in @idx (note that this @idx is just for a cursor,
+ * do not change it.)
+ * Since prefixes.nbytes can be bigger than 4 if some prefixes
+ * are repeated, it cannot be used for looping over the prefixes.
+ */
+#define for_each_insn_prefix(insn, idx, prefix)	\
+	for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
+
 #define POP_SS_OPCODE 0x1f
 #define MOV_SREG_OPCODE 0x8e
 
author maintainer
#26 lore raw

PATCH v2 2/3 x86/insn-eval: Fix not using prefixes.nbytes for loop over prefixes.bytes

In reply to Masami Hiramatsu

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the insn.prefixes.bytes[i] != 0 and i < 4 instead of insn.prefixes.nbytes.

Fixes 32d0b95300db ("x86/insn-eval: Add utility functions to get segment selector")
Reported-by [email protected]
---
 arch/x86/lib/insn-eval.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/lib/insn-eval.c +5 -5
index 58f7fb95c7f4..4229950a5d78 100644
@@ -63,13 +63,12 @@ static bool is_string_insn(struct insn *insn)
  */
 bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}
@@ -95,14 +94,15 @@ static int get_seg_reg_override_idx(struct insn *insn)
 {
 	int idx = INAT_SEG_REG_DEFAULT;
 	int num_overrides = 0, i;
+	insn_byte_t p;
 
 	insn_get_prefixes(insn);
 
 	/* Look for any segment override prefixes. */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
 			idx = INAT_SEG_REG_CS;
#27 lore raw

[tip: x86/urgent] x86/insn-eval: Use new for_each_insn_prefix() macro to loop over prefixes bytes

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

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

Commit-ID:     2d7896c24ec977e91af1ff93c823032a27212700
Gitweb:        https://git.kernel.org/tip/2d7896c24ec977e91af1ff93c823032a27212700
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:50 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 04 Dec 2020 14:33:51 +01:00

x86/insn-eval: Use new for_each_insn_prefix() macro to loop over prefixes bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes. Use the new for_each_insn_prefix() macro which does it correctly.

Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message. ]

---
 arch/x86/lib/insn-eval.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/lib/insn-eval.c +5 -5
index 58f7fb9..4229950 100644
@@ -63,13 +63,12 @@ static bool is_string_insn(struct insn *insn)
  */
 bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}
@@ -95,14 +94,15 @@ static int get_seg_reg_override_idx(struct insn *insn)
 {
 	int idx = INAT_SEG_REG_DEFAULT;
 	int num_overrides = 0, i;
+	insn_byte_t p;
 
 	insn_get_prefixes(insn);
 
 	/* Look for any segment override prefixes. */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
 			idx = INAT_SEG_REG_CS;
#28 lore raw

[tip: x86/urgent] x86/insn-eval: Use new for_each_insn_prefix() macro to loop over prefixes bytes

In reply to Masami Hiramatsu
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     12cb908a11b2544b5f53e9af856e6b6a90ed5533
Gitweb:        https://git.kernel.org/tip/12cb908a11b2544b5f53e9af856e6b6a90ed5533
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:50:50 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Sun, 06 Dec 2020 10:03:08 +01:00

x86/insn-eval: Use new for_each_insn_prefix() macro to loop over prefixes bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes. Use the new for_each_insn_prefix() macro which does it correctly.

Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message. ]

---
 arch/x86/lib/insn-eval.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/lib/insn-eval.c +5 -5
index 58f7fb9..4229950 100644
@@ -63,13 +63,12 @@ static bool is_string_insn(struct insn *insn)
  */
 bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}
@@ -95,14 +94,15 @@ static int get_seg_reg_override_idx(struct insn *insn)
 {
 	int idx = INAT_SEG_REG_DEFAULT;
 	int num_overrides = 0, i;
+	insn_byte_t p;
 
 	insn_get_prefixes(insn);
 
 	/* Look for any segment override prefixes. */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for_each_insn_prefix(insn, i, p) {
 		insn_attr_t attr;
 
-		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
+		attr = inat_get_opcode_attribute(p);
 		switch (attr) {
 		case INAT_MAKE_PREFIX(INAT_PFX_CS):
 			idx = INAT_SEG_REG_CS;
author maintainer
#29 lore raw

PATCH v2 3/3 x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes

In reply to Masami Hiramatsu

Since the insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a same prefix is repeated, we have to check whether the i < 4 and insn.prefixes.bytes[i] != 0 instead of insn.prefixes.nbytes.

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
Reported-by [email protected]
---
 arch/x86/boot/compressed/sev-es.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev-es.c +2 -3
index 954cb2702e23..27826c265aab 100644
@@ -32,13 +32,12 @@ struct ghcb *boot_ghcb;
  */
 static bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}
#30 lore raw

[tip: x86/urgent] x86/sev-es: Use new for_each_insn_prefix() macro to loop over prefixes bytes

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

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

Commit-ID:     46a4ad7814fa39971aa6549b30c1a08d5c2ec65f
Gitweb:        https://git.kernel.org/tip/46a4ad7814fa39971aa6549b30c1a08d5c2ec65f
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:51:01 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 04 Dec 2020 14:45:15 +01:00

x86/sev-es: Use new for_each_insn_prefix() macro to loop over prefixes bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be:

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes. Use the new for_each_insn_prefix() macro which does it correctly.

Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message. ]

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
Reported-by [email protected]
---
 arch/x86/boot/compressed/sev-es.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev-es.c +2 -3
index 954cb27..27826c2 100644
@@ -32,13 +32,12 @@ struct ghcb *boot_ghcb;
  */
 static bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}
#31 lore raw

[tip: x86/urgent] x86/sev-es: Use new for_each_insn_prefix() macro to loop over prefixes bytes

In reply to Masami Hiramatsu
The following commit has been merged into the x86/urgent branch of tip:

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

Commit-ID:     84da009f06e60cf59d5e861f8e2101d2d3885517
Gitweb:        https://git.kernel.org/tip/84da009f06e60cf59d5e861f8e2101d2d3885517
Author:        Masami Hiramatsu <[email protected]>
AuthorDate:    Thu, 03 Dec 2020 13:51:01 +09:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Sun, 06 Dec 2020 10:03:08 +01:00

x86/sev-es: Use new for_each_insn_prefix() macro to loop over prefixes bytes

Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be:

insn.prefixes.bytes[i] != 0 and i < 4

instead of using insn.prefixes.nbytes. Use the new for_each_insn_prefix() macro which does it correctly.

Debugged by Kees Cook <[email protected]>.

[ bp: Massage commit message. ]

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
Reported-by [email protected]
---
 arch/x86/boot/compressed/sev-es.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev-es.c +2 -3
index 954cb27..27826c2 100644
@@ -32,13 +32,12 @@ struct ghcb *boot_ghcb;
  */
 static bool insn_has_rep_prefix(struct insn *insn)
 {
+	insn_byte_t p;
 	int i;
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
-		insn_byte_t p = insn->prefixes.bytes[i];
-
+	for_each_insn_prefix(insn, i, p) {
 		if (p == 0xf2 || p == 0xf3)
 			return true;
 	}