x86/insn: Fix not using prefixes.nbytes for loop over prefixes.bytes
Masami Hiramatsu
maintainercover 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]>
Masami Hiramatsu
maintainerPATCH 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.
--- arch/x86/boot/compressed/sev-es.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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++) {insn_byte_t p = insn->prefixes.bytes[i];if (p ==0xf2 || p ==0xf3 )
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")Reported-by Kees Cook <[email protected]>Signed-off-by Masami Hiramatsu <[email protected]>--- arch/x86/boot/compressed/sev-es.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/boot/compressed/sev-es.c +0
-0
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
-0insn_byte_t p = insn->prefixes.bytes[i];if (p ==0xf2 || p ==0xf3 )
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")Reported-by Kees Cook <[email protected]>Signed-off-by Masami Hiramatsu <[email protected]>--- 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@@ -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,
TomQuoted 3 lines
arch/x86/boot/compressed/sev-es.c +0
-0insn_byte_t p = insn->prefixes.bytes[i];if (p ==0xf2 || p ==0xf3 )
Signature · 1 line
Kees Cook
Masami Hiramatsu
maintainerRe: [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")Reported-by Kees Cook <[email protected]>Signed-off-by Masami Hiramatsu <[email protected]>--- 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@@ -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,
TomQuoted 3 lines
arch/x86/boot/compressed/sev-es.c +0
-0insn_byte_t p = insn->prefixes.bytes[i];if (p ==0xf2 || p ==0xf3 )Signature · 1 line
Kees Cook
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerPATCH 2/3 x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes
In reply to Masami HiramatsuSince 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.
--- arch/x86/kernel/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/uprobes.c +2 -2
@@ -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 ; }
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")Reported-by Kees Cook <[email protected]>Signed-off-by Masami Hiramatsu <[email protected]>
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@@ -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
Masami Hiramatsu
maintainerRe: [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")Reported-by Kees Cook <[email protected]>Signed-off-by Masami Hiramatsu <[email protected]>Looks good to me.
Reviewed-by Srikar Dronamraju <[email protected]>
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@@ -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]>
Re: [PATCH 2/3] x86/uprobes: Fix not using prefixes.nbytes for loop over prefixes.bytes
In reply to Masami HiramatsuQuoted 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")Reported-by Kees Cook <[email protected]>
This should probably be:
Quoted 28 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- arch/x86/kernel/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/uprobes.c +2
-2@@ -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
Masami Hiramatsu
maintainerRe: [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")Reported-by Kees Cook <[email protected]>This should probably be:
Reported-by [email protected]Debugged-by Kees Cook <[email protected]>
OK, let me fix it.
Thank you,
Quoted 33 lines
Quoted 29 lines
Signed-off-by Masami Hiramatsu <[email protected]>--- arch/x86/kernel/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/uprobes.c +2
-2@@ -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]>
Masami Hiramatsu
maintainerPATCH 3/3 x86/insn-eval: Fix not using prefixes.nbytes for loop over prefixes.bytes
In reply to Masami HiramatsuSince 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.
--- arch/x86/lib/insn-eval.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/lib/insn-eval.c +2 -2
@@ -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]);