Skip to contentSkip to conversation

x86/svm: Adjust ModRM Mode check in is_invlpg()

PATCH In review Andrew Cooper 3 participants 7 messages Jan 11, 2017 – Jan 12, 2017

lore
Share & tools
lore
#0 lore raw

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
index ae8e2c4..ff134a5 100644
@@ -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
#1 lore raw
Quoted 17 lines · Andrew Cooper

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]>

Quoted 5 lines

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).

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
index ae8e2c4..ff134a5 100644

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

#2 lore raw
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 -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]>

Quoted 5 lines

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).

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_modrm decides 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

Acked-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
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 -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.

I think there may be one or two more such instances elsewhere.

Quoted 11 lines

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.

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

#4 lore raw
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 -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.

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

#5 lore raw
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 -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.

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_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.

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

#6 lore raw
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 -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.

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

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 -EINVAL case.

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