x86/svm: Adjust ModRM Mode check in is_invlpg()
PATCH 1/1 x86/svm: Adjust ModRM Mode check in is_invlpg()
Coverity points out that x86_insn_modrm() returns -EINVAL for instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.
In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
Use an unsigned < comparison to exclude the -EINVAL case, guaranteeing that ext is only read if it was filled in by x86_insn_modrm(), which should placate Coverity.
---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
RFC. I haven't actually checked that this fixes the issue.
An alternative would be to ASSERT() that x86_insn_modrm() is non-negative, but I can't nice way of integrating that into the existing logic (without using the comma operator, and that isn't nice to read).
--- xen/arch/x86/hvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -2162,7 +2162,7 @@ static bool is_invlpg(const struct x86_emulate_state *state,unsigned int ext;return ctxt->opcode ==X86EMUL_OPC (0x0f ,0x01 ) && -x86_insn_modrm (state,NULL , &ext) !=3 && + (unsigned int )x86_insn_modrm (state,NULL , &ext) <3 && (ext &7 ) ==7 ; }
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 17 lines · Andrew Cooper
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
Use an unsigned < comparison to exclude the
-EINVALcase, guaranteeing that ext is only read if it was filled in byx86_insn_modrm(), which should placate Coverity.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
Quoted 5 lines
RFC. I haven't actually checked that this fixes the issue.
An alternative would be to
ASSERT()thatx86_insn_modrm()is non-negative, but I can't nice way of integrating that into the existing logic (without using the comma operator, and that isn't nice to read).
how about
return ctxt->opcode == X86EMUL_OPC(0x0f, 0x01) &&
- x86_insn_modrm(state, NULL, &ext) != 3 &&
+ (mod = x86_insn_modrm(state, NULL, &ext)) >= 0 && mod != 3 &&
(ext & 7) == 7;(in case x86_insn_modrm decides to return some other errors in the future.)
-boris
Quoted 9 lines
--- xen/arch/x86/hvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/hvm/svm/svm.c +0
-0
xen/arch/x86/hvm/svm/svm.c static bool is_invlpg(const struct x86_emulate_state *state, · line 2168
Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +1
-1@@ -2162,7 +2162,7 @@ static bool is_invlpg(const struct x86_emulate_state *state,unsigned int ext;return ctxt->opcode ==X86EMUL_OPC (0x0f ,0x01 ) && -x86_insn_modrm (state,NULL , &ext) !=3 && + (unsigned int )x86_insn_modrm (state,NULL , &ext) <3 && (ext &7 ) ==7 ; }
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 11.01.17 at 21:29, <[email protected]> wrote:
Quoted 17 lines · Andrew Cooper
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
Use an unsigned < comparison to exclude the
-EINVALcase, guaranteeing that ext is only read if it was filled in byx86_insn_modrm(), which should placate Coverity.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>Quoted 5 lines
RFC. I haven't actually checked that this fixes the issue.
An alternative would be to
ASSERT()thatx86_insn_modrm()is non-negative, but I can't nice way of integrating that into the existing logic (without using the comma operator, and that isn't nice to read).how about
return ctxt->opcode == X86EMUL_OPC(0x0f, 0x01) &&xen/arch/x86/hvm/svm/svm.c +1
-1-x86_insn_modrm (state,NULL , &ext) !=3 && + (mod =x86_insn_modrm (state,NULL , &ext)) >=0 && mod !=3 && (ext &7 ) ==7 ;
Quoted 1 line
(in case
x86_insn_modrmdecides to return some other errors in the future.)
I had specifically avoided introduction of a "mod" local variable.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.01.17 at 18:33, <[email protected]> wrote:
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
I think there may be one or two more such instances elsewhere.
Quoted 11 lines
Use an unsigned < comparison to exclude the
-EINVALcase, guaranteeing that ext is only read if it was filled in byx86_insn_modrm(), which should placate Coverity.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>RFC. I haven't actually checked that this fixes the issue.
Provided it does,
I'd also be fine with putting it in without double checking, going the revert or amend route if it doesn't help.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.01.17 at 18:33, <[email protected]> wrote:
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
I think there may be one or two more such instances elsewhere.
I did audit the other callsites, and thought I found them to be safe.
However, thinking again, I think the gate_op case isn't
case 0xff:
if ( x86_insn_modrm(state, NULL, &modrm_345) >= 3 )
break;
switch ( modrm_345 & 7 )This also needs to be an unsigned comparison to catch the -EINVAL case.
As for the other uses, __get_instruction_length_from_list() checks against an explicit field while searching the array, priv_op_validate() breaks on != 3, while x86_insn_is_cr_access() checks >= 0. All of these are safe.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 12.01.17 at 12:31, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.01.17 at 18:33, <[email protected]> wrote:
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
I think there may be one or two more such instances elsewhere.
I did audit the other callsites, and thought I found them to be safe.
Quoted 8 lines
However, thinking again, I think the
gate_opcase isn'tcase 0xff: if ( x86_insn_modrm(state, NULL, &modrm_345) >= 3 ) break; switch ( modrm_345 & 7 )This also needs to be an unsigned comparison to catch the
-EINVALcase.
Right. However, how about using 3U instead of a cast to achieve this?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 12.01.17 at 12:31, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 11.01.17 at 18:33, <[email protected]> wrote:
Coverity points out that
x86_insn_modrm()returns-EINVALfor instructions not encoded with a ModRM byte. A consequence is that checking != 3 is insufficient to confirm that &ext was actually written to.In practice, this check is only used after decode has been successful, and 0f01 will have a ModRM byte.
I think there may be one or two more such instances elsewhere.
I did audit the other callsites, and thought I found them to be safe.
However, thinking again, I think the
gate_opcase isn't
Quoted 7 lines
Quoted 6 lines
case 0xff: if ( x86_insn_modrm(state, NULL, &modrm_345) >= 3 ) break; switch ( modrm_345 & 7 )This also needs to be an unsigned comparison to catch the
-EINVALcase.Right. However, how about using 3U instead of a cast to achieve this?
That is very subtle, and will go wrong silently if x86_insn_modrm() changes return type, e.g. to long.
In this case, the explicit cast is much clearer, as it shows an obvious intent.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel