Skip to contentSkip to conversation

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

PATCH Superseded Masami Hiramatsu 4 participants 11 messages Dec 2, 2020 – Dec 3, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

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

Hi,

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

Kees Cook reported that this issue that there are 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/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes 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

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

PATCH 1/3 x86/sev-es: 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.

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
---
 arch/x86/boot/compressed/sev-es.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/compressed/sev-es.c +1 -1
index 954cb2702e23..6a7a3027c9ac 100644
@@ -36,7 +36,7 @@ static bool insn_has_rep_prefix(struct insn *insn)
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_byte_t p = insn->prefixes.bytes[i];
 
 		if (p == 0xf2 || p == 0xf3)
#2 lore raw

Re: [PATCH 1/3] x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 17 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.

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
---
  arch/x86/boot/compressed/sev-es.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/compressed/sev-es.c +0 -0
index 954cb2702e23..6a7a3027c9ac 100644

arch/x86/boot/compressed/sev-es.c static bool insn_has_rep_prefix(struct insn *insn) · line 39

Quoted 5 lines
arch/x86/boot/compressed/sev-es.c +1 -1
@@ -36,7 +36,7 @@ static bool insn_has_rep_prefix(struct insn *insn)
 
  	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {

Wouldn't it be better to create a #define for the size rather than hard coding 4 in the various files? That would protect everything should the bytes array size ever change in the future.

Thanks,
Tom

Quoted 4 lines
arch/x86/boot/compressed/sev-es.c +0 -0
  		insn_byte_t p = insn->prefixes.bytes[i];
 
  		if (p == 0xf2 || p == 0xf3)
#3 lore raw

Re: [PATCH 1/3] x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 13 lines · Tom Lendacky
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.

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
---
  arch/x86/boot/compressed/sev-es.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 8 lines

arch/x86/boot/compressed/sev-es.c static bool insn_has_rep_prefix(struct insn *insn) · line 37

Quoted 8 lines
arch/x86/boot/compressed/sev-es.c +1 -1
index 954cb2702e23..6a7a3027c9ac 100644
@@ -36,7 +36,7 @@ static bool insn_has_rep_prefix(struct insn *insn)
  	insn_get_prefixes(insn);
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {

You must test "i" before bytes[i] or you still do the out-of-bounds-read.

Quoted 4 lines

Wouldn't it be better to create a #define for the size rather than hard coding 4 in the various files? That would protect everything should the bytes array size ever change in the future.

Agreed, and perhaps instead of repeating the idiom in the for loop, add a helper like:

#define insn_prefix_valid(prefixes, i) (i >=0 && i < 4 && prefixes->bytes[i])

to be used like:

	for (i = 0; insn_prefix_valid(&insn->prefixes, i); i++) {
Quoted 7 lines

Thanks,
Tom

Quoted 3 lines
arch/x86/boot/compressed/sev-es.c +0 -0
  		insn_byte_t p = insn->prefixes.bytes[i];
  		if (p == 0xf2 || p == 0xf3)
 
Signature · 1 line
Kees Cook
author maintainer
#4 lore raw

Re: [PATCH 1/3] x86/sev-es: Fix not using prefixes.nbytes for loop over prefixes.bytes

Quoted 14 lines · On Wed, 2 Dec 2020 11:07:26 -0800 Kees Cook <[email protected]> wrote:
Quoted 13 lines · Tom Lendacky
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.

Fixes 25189d08e516 ("x86/sev-es: Add support for handling IOIO exceptions")
---
  arch/x86/boot/compressed/sev-es.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines
Quoted 8 lines

arch/x86/boot/compressed/sev-es.c static bool insn_has_rep_prefix(struct insn *insn) · line 37

Quoted 8 lines
arch/x86/boot/compressed/sev-es.c +1 -1
index 954cb2702e23..6a7a3027c9ac 100644
@@ -36,7 +36,7 @@ static bool insn_has_rep_prefix(struct insn *insn)
  	insn_get_prefixes(insn);
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {

You must test "i" before bytes[i] or you still do the out-of-bounds-read.

Oops, thanks.

Quoted 6 lines
Quoted 5 lines

Wouldn't it be better to create a #define for the size rather than hard coding 4 in the various files? That would protect everything should the bytes array size ever change in the future.

Quoted 8 lines

Agreed, and perhaps instead of repeating the idiom in the for loop, add a helper like:

#define insn_prefix_valid(prefixes, i) (i >=0 && i < 4 && prefixes->bytes[i])

to be used like:

	for (i = 0; insn_prefix_valid(&insn->prefixes, i); i++) {

Hm, for all of these usage, they are looping on the prefixes, so

for_each_insn_prefix(insn, idx, prefix) {
...
}

will be simpler.

Thank you,

Quoted 11 lines
Quoted 8 lines

Thanks,
Tom

Quoted 3 lines
arch/x86/boot/compressed/sev-es.c +0 -0
  		insn_byte_t p = insn->prefixes.bytes[i];
  		if (p == 0xf2 || p == 0xf3)
 
Signature · 1 line
Kees Cook
Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#5 lore raw

PATCH 2/3 x86/uprobes: 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 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
---
 arch/x86/kernel/uprobes.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
index 3fdaa042823d..bb3ea3705b99 100644
@@ -257,7 +257,7 @@ static bool is_prefix_bad(struct insn *insn)
 {
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
@@ -746,7 +746,7 @@ 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++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		if (insn->prefixes.bytes[i] == 0x66)
 			return -ENOTSUPP;
 	}
Reviewed-by on v1 2/3
#6 lore raw

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

Quoted 9 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.

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")

Looks good to me.

Quoted 27 lines
---
 arch/x86/kernel/uprobes.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
index 3fdaa042823d..bb3ea3705b99 100644
@@ -257,7 +257,7 @@ static bool is_prefix_bad(struct insn *insn)
 {
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
@@ -746,7 +746,7 @@ 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++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		if (insn->prefixes.bytes[i] == 0x66)
 			return -ENOTSUPP;
 	}
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
author maintainer
#7 lore raw

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

Quoted 7 lines · On Wed, 2 Dec 2020 20:21:35 +0530 Srikar Dronamraju…
Quoted 5 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.

Quoted 8 lines
Quoted 4 lines
Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")

Looks good to me.

Thanks Srikar!

Quoted 32 lines
Quoted 28 lines
---
 arch/x86/kernel/uprobes.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
index 3fdaa042823d..bb3ea3705b99 100644
@@ -257,7 +257,7 @@ static bool is_prefix_bad(struct insn *insn)
 {
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
@@ -746,7 +746,7 @@ 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++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		if (insn->prefixes.bytes[i] == 0x66)
 			return -ENOTSUPP;
 	}
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
Signature · 1 line
Masami Hiramatsu <[email protected]>
#8 lore raw

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

In reply to Masami Hiramatsu
Quoted 8 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.

Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")

This should probably be:

Quoted 28 lines
---
 arch/x86/kernel/uprobes.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
index 3fdaa042823d..bb3ea3705b99 100644
@@ -257,7 +257,7 @@ static bool is_prefix_bad(struct insn *insn)
 {
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
@@ -746,7 +746,7 @@ 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++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		if (insn->prefixes.bytes[i] == 0x66)
 			return -ENOTSUPP;
 	}
Signature · 1 line
Kees Cook
author maintainer
#9 lore raw

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

Quoted 6 lines · On Wed, 2 Dec 2020 11:04:41 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 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.

Quoted 8 lines
Quoted 3 lines
Fixes 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")

This should probably be:

OK, let me fix it.

Thank you,

Quoted 33 lines
Quoted 29 lines
---
 arch/x86/kernel/uprobes.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
index 3fdaa042823d..bb3ea3705b99 100644
@@ -257,7 +257,7 @@ static bool is_prefix_bad(struct insn *insn)
 {
 	int i;
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
@@ -746,7 +746,7 @@ 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++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		if (insn->prefixes.bytes[i] == 0x66)
 			return -ENOTSUPP;
 	}
Signature · 1 line
Kees Cook
Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#10 lore raw

PATCH 3/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")
---
 arch/x86/lib/insn-eval.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/lib/insn-eval.c +2 -2
index 58f7fb95c7f4..c52c91461f52 100644
@@ -67,7 +67,7 @@ bool insn_has_rep_prefix(struct insn *insn)
 
 	insn_get_prefixes(insn);
 
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_byte_t p = insn->prefixes.bytes[i];
 
 		if (p == 0xf2 || p == 0xf3)
@@ -99,7 +99,7 @@ static int get_seg_reg_override_idx(struct insn *insn)
 	insn_get_prefixes(insn);
 
 	/* Look for any segment override prefixes. */
-	for (i = 0; i < insn->prefixes.nbytes; i++) {
+	for (i = 0; insn->prefixes.bytes[i] && i < 4; i++) {
 		insn_attr_t attr;
 
 		attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);