Skip to contentSkip to conversation

xen-4.3.1:hvm.c: 2 * possible bad if tests ?

PATCH In review David Binderman 5 participants 14 messages Nov 21, 2013 – Nov 22, 2013

lore
Share & tools
lore
#0 lore raw

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) ) goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) ) goto unmap_and_fail;

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) ) goto unmap_and_fail;

Duplicate.

Regards

David Binderman

#1 lore raw
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

~Andrew

#2 lore raw
Quoted 34 lines · Andrew Cooper
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
#3 lore raw
Quoted 51 lines · Tim Deegan
Quoted 33 lines · Andrew Cooper
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Also, the commit should probably have CID 1055180 referenced ?

~Andrew

#4 lore raw
Quoted 64 lines · Andrew Cooper
Quoted 51 lines · Tim Deegan
Quoted 33 lines · Andrew Cooper
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Quoted 1 line

Also, the commit should probably have CID 1055180 referenced ?

Sure. here's v2:

commit 22c2226e799787ec444ab480db95369d18972cd8

Author: Tim Deegan <[email protected]>
Date:   Thu Nov 21 15:11:39 2013 +0000

x86/hvm: fix test for non-conforming segments.

Also Coverity CID 1055180

xen/arch/x86/hvm/hvm.c +2 -2
index 3b353ec..bbeef53 100644
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2298,7 +2298,7 @@ static int hvm_load_segment_selector(
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            if ( !(desc.b & (1u<<10)) && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }
#6 lore raw
In reply to Tim Deegan
Quoted 65 lines · Tim Deegan
Quoted 64 lines · Andrew Cooper
Quoted 51 lines · Tim Deegan
Quoted 33 lines · Andrew Cooper
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Quoted 3 lines
Quoted 1 line

Also, the commit should probably have CID 1055180 referenced ?

Sure. here's v2:

...which was buggy: one path needs to handle data segments too. v3:

commit 8f8b746cfdcc11197c91efea2b4414045e846fa3

Author: Tim Deegan <[email protected]>
Date:   Thu Nov 21 15:11:39 2013 +0000

x86/hvm: fix test for non-conforming segments.

Also Coverity CID 1055180

xen/arch/x86/hvm/hvm.c +3 -2
index 3b353ec..d64f635 100644
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2298,7 +2298,8 @@ static int hvm_load_segment_selector(
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            if ( ((desc.b & (3u<<10)) != (3u<<10))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }
#7 lore raw
Quoted 94 lines · Tim Deegan
Quoted 64 lines · Tim Deegan
Quoted 63 lines · Andrew Cooper
Quoted 51 lines · Tim Deegan
Quoted 33 lines · Andrew Cooper
Quoted 20 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser "cppcheck".

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Quoted 2 lines
Quoted 1 line

Also, the commit should probably have CID 1055180 referenced ?

Sure. here's v2:

...which was buggy: one path needs to handle data segments too. v3:

commit 8f8b746cfdcc11197c91efea2b4414045e846fa3

Author: Tim Deegan <[email protected]>
Date:   Thu Nov 21 15:11:39 2013 +0000

x86/hvm: fix test for non-conforming segments.

Also Coverity CID 1055180

xen/arch/x86/hvm/hvm.c +1 -1
index 3b353ec..d64f635 100644
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2305

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2298,7 +2298,8 @@ static int hvm_load_segment_selector(
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            if ( ((desc.b & (3u<<10)) != (3u<<10))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }

Can you fix the comment to /* Data or non-conforming segment: check DPL against RPL and CPL. */ to match the new logic?

~Andrew

#8 lore raw
Quoted 101 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 8 lines · Tim Deegan
Quoted 7 lines · Tim Deegan
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Tim Deegan
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser

"cppcheck".

Quoted 90 lines
Quoted 57 lines
Quoted 57 lines
Quoted 46 lines
Quoted 29 lines
Quoted 17 lines

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Quoted 2 lines
Quoted 1 line

Also, the commit should probably have CID 1055180 referenced ?

Sure. here's v2:

...which was buggy: one path needs to handle data segments too. v3:

commit 8f8b746cfdcc11197c91efea2b4414045e846fa3

Author: Tim Deegan <[email protected]>
Date:   Thu Nov 21 15:11:39 2013 +0000

x86/hvm: fix test for non-conforming segments.

Also Coverity CID 1055180

xen/arch/x86/hvm/hvm.c +1 -2
index 3b353ec..d64f635 100644
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2298,7 +2298,8 @@ static int hvm_load_segment_selector(
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2305

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ((desc.b & (3u<<10)) != (3u<<10))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }

Can you fix the comment to /* Data or non-conforming segment: check DPL against RPL and CPL. */ to match the new logic?

And ideally use _SEGMENT_* instead of raw numbers...

Jan

#9 lore raw
Quoted 101 lines · Jan Beulich
Quoted 101 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 8 lines · Tim Deegan
Quoted 7 lines · Tim Deegan
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Tim Deegan
Quoted 4 lines · Andrew Cooper
Quoted 3 lines · David Binderman

Hello there,

I just ran the source code of xen-4.3.1 through the static analyser

"cppcheck".

Quoted 90 lines
Quoted 57 lines
Quoted 57 lines
Quoted 46 lines
Quoted 29 lines
Quoted 17 lines

It said

1.

[hvm.c:2190]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
                goto unmap_and_fail;

You might be better off with

            if ( ((desc.b & (6u<<9))) && (dpl != rpl) )
                goto unmap_and_fail;
Quoted 8 lines

2.

[hvm.c:2210]: (style) Expression '(X & 0xc00) != 0x6' is always true.

Source code is

            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
                goto unmap_and_fail;

These have both been flagged up by our Coverity scanning, but I haven't had enough time to pour over the manuals workout out the correct expression should be.

Quoted 6 lines

The prevailing style for all other checks in this area is "(X & (6u<<9)) != (6u<<9)" , which is rather different to the result you came up with.

As this is the security checks for segment selectors in the emulation code, leaving it in its current "too many operations are failed" is safer than being uncertain with the fix and introducing a vulnerability.

Looking at the manual and the comment, I think the right change is:

x86/hvm: fix test for non-conforming segments.

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2284

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:

There is another example higher in the switch statement for the code segment selector.

Quoted 2 lines
Quoted 1 line

Also, the commit should probably have CID 1055180 referenced ?

Sure. here's v2:

...which was buggy: one path needs to handle data segments too. v3:

commit 8f8b746cfdcc11197c91efea2b4414045e846fa3

Author: Tim Deegan <[email protected]>
Date:   Thu Nov 21 15:11:39 2013 +0000

x86/hvm: fix test for non-conforming segments.

Also Coverity CID 1055180

xen/arch/x86/hvm/hvm.c +1 -2
index 3b353ec..d64f635 100644
@@ -2278,7 +2278,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & (1u<<10)) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2298,7 +2298,8 @@ static int hvm_load_segment_selector(
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_segment_selector( · line 2305

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ((desc.b & (3u<<10)) != (3u<<10))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }

Can you fix the comment to /* Data or non-conforming segment: check DPL against RPL and CPL. */ to match the new logic?

Yes.

Quoted 1 line

And ideally use _SEGMENT_* instead of raw numbers...

Eh, OK. It'll be next week, then, with a followup to convert the surrounding code too.

Tim.

#10 lore raw

PATCH v4 1/2 x86/hvm: fix segment validation

Also Coverity CID 1055180.

Use _SEGMENT_* instead of plain numbers and adjust a comment.

xen/arch/x86/hvm/hvm.c +8 -3
@@ -2280,7 +2280,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2299,8 +2299,13 @@ static int hvm_load_segment_selector(
             /* Readable code or data segment? */
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
-            /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            /*
+             * Data or non-conforming code segment:
+             * check DPL against RPL and CPL.
+             */
+            if ( ((desc.b & (_SEGMENT_EC|_SEGMENT_CODE)) !=
+                  (_SEGMENT_EC|_SEGMENT_CODE))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }

x86/hvm: fix segment validation

Also Coverity CID 1055180.

Use _SEGMENT_* instead of plain numbers and adjust a comment.

xen/arch/x86/hvm/hvm.c +8 -3
@@ -2280,7 +2280,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2299,8 +2299,13 @@ static int hvm_load_segment_selector(
             /* Readable code or data segment? */
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
-            /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            /*
+             * Data or non-conforming code segment:
+             * check DPL against RPL and CPL.
+             */
+            if ( ((desc.b & (_SEGMENT_EC|_SEGMENT_CODE)) !=
+                  (_SEGMENT_EC|_SEGMENT_CODE))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v4 1/2
#11 lore raw

Re: [PATCH v4 1/2] x86/hvm: fix segment validation

Quoted 8 lines · Jan Beulich

Also Coverity CID 1055180.

Use _SEGMENT_* instead of plain numbers and adjust a comment.

Quoted 31 lines
xen/arch/x86/hvm/hvm.c +8 -3
@@ -2280,7 +2280,7 @@ static int hvm_load_segment_selector(
             if ( !(desc.b & (1u<<11)) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && (dpl != rpl) )
+            if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ss:
@@ -2299,8 +2299,13 @@ static int hvm_load_segment_selector(
             /* Readable code or data segment? */
             if ( (desc.b & (5u<<9)) == (4u<<9) )
                 goto unmap_and_fail;
-            /* Non-conforming segment: check DPL against RPL and CPL. */
-            if ( ((desc.b & (6u<<9)) != 6) && ((dpl < cpl) || (dpl < rpl)) )
+            /*
+             * Data or non-conforming code segment:
+             * check DPL against RPL and CPL.
+             */
+            if ( ((desc.b & (_SEGMENT_EC|_SEGMENT_CODE)) !=
+                  (_SEGMENT_EC|_SEGMENT_CODE))
+                 && ((dpl < cpl) || (dpl < rpl)) )
                 goto unmap_and_fail;
             break;
         }
#12 lore raw

PATCH v4 2/2 x86/hvm: clean up segment validation

In reply to Tim Deegan

Use _SEGMENT_* instead of plain numbers where feasible.

xen/arch/x86/hvm/hvm.c +5 -5
@@ -2259,7 +2259,7 @@ static int hvm_load_segment_selector(
         desc = *pdesc;
 
         /* Segment present in memory? */
-        if ( !(desc.b & (1u<<15)) )
+        if ( !(desc.b & _SEGMENT_P) )
         {
             fault_type = TRAP_no_segment;
             goto unmap_and_fail;
@@ -2277,7 +2277,7 @@ static int hvm_load_segment_selector(
         {
         case x86_seg_cs:
             /* Code segment? */
-            if ( !(desc.b & (1u<<11)) )
+            if ( !(desc.b & _SEGMENT_CODE) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
             if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
@@ -2285,19 +2285,19 @@ static int hvm_load_segment_selector(
             break;
         case x86_seg_ss:
             /* Writable data segment? */
-            if ( (desc.b & (5u<<9)) != (1u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) != _SEGMENT_WR )
                 goto unmap_and_fail;
             if ( (dpl != cpl) || (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ldtr:
             /* LDT system segment? */
-            if ( (desc.b & (15u<<8)) != (2u<<8) )
+            if ( (desc.b & _SEGMENT_TYPE) != (2u<<8) )
                 goto unmap_and_fail;
             goto skip_accessed_flag;
         default:
             /* Readable code or data segment? */
-            if ( (desc.b & (5u<<9)) == (4u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) == _SEGMENT_CODE )
                 goto unmap_and_fail;
             /*
              * Data or non-conforming code segment:

x86/hvm: clean up segment validation

Use _SEGMENT_* instead of plain numbers where feasible.

xen/arch/x86/hvm/hvm.c +5 -5
@@ -2259,7 +2259,7 @@ static int hvm_load_segment_selector(
         desc = *pdesc;
 
         /* Segment present in memory? */
-        if ( !(desc.b & (1u<<15)) )
+        if ( !(desc.b & _SEGMENT_P) )
         {
             fault_type = TRAP_no_segment;
             goto unmap_and_fail;
@@ -2277,7 +2277,7 @@ static int hvm_load_segment_selector(
         {
         case x86_seg_cs:
             /* Code segment? */
-            if ( !(desc.b & (1u<<11)) )
+            if ( !(desc.b & _SEGMENT_CODE) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
             if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
@@ -2285,19 +2285,19 @@ static int hvm_load_segment_selector(
             break;
         case x86_seg_ss:
             /* Writable data segment? */
-            if ( (desc.b & (5u<<9)) != (1u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) != _SEGMENT_WR )
                 goto unmap_and_fail;
             if ( (dpl != cpl) || (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ldtr:
             /* LDT system segment? */
-            if ( (desc.b & (15u<<8)) != (2u<<8) )
+            if ( (desc.b & _SEGMENT_TYPE) != (2u<<8) )
                 goto unmap_and_fail;
             goto skip_accessed_flag;
         default:
             /* Readable code or data segment? */
-            if ( (desc.b & (5u<<9)) == (4u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) == _SEGMENT_CODE )
                 goto unmap_and_fail;
             /*
              * Data or non-conforming code segment:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v4 2/2
#13 lore raw

Re: [PATCH v4 2/2] x86/hvm: clean up segment validation

Quoted 3 lines · Jan Beulich

Use _SEGMENT_* instead of plain numbers where feasible.

Quoted 47 lines
xen/arch/x86/hvm/hvm.c +5 -5
@@ -2259,7 +2259,7 @@ static int hvm_load_segment_selector(
         desc = *pdesc;
 
         /* Segment present in memory? */
-        if ( !(desc.b & (1u<<15)) )
+        if ( !(desc.b & _SEGMENT_P) )
         {
             fault_type = TRAP_no_segment;
             goto unmap_and_fail;
@@ -2277,7 +2277,7 @@ static int hvm_load_segment_selector(
         {
         case x86_seg_cs:
             /* Code segment? */
-            if ( !(desc.b & (1u<<11)) )
+            if ( !(desc.b & _SEGMENT_CODE) )
                 goto unmap_and_fail;
             /* Non-conforming segment: check DPL against RPL. */
             if ( !(desc.b & _SEGMENT_EC) && (dpl != rpl) )
@@ -2285,19 +2285,19 @@ static int hvm_load_segment_selector(
             break;
         case x86_seg_ss:
             /* Writable data segment? */
-            if ( (desc.b & (5u<<9)) != (1u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) != _SEGMENT_WR )
                 goto unmap_and_fail;
             if ( (dpl != cpl) || (dpl != rpl) )
                 goto unmap_and_fail;
             break;
         case x86_seg_ldtr:
             /* LDT system segment? */
-            if ( (desc.b & (15u<<8)) != (2u<<8) )
+            if ( (desc.b & _SEGMENT_TYPE) != (2u<<8) )
                 goto unmap_and_fail;
             goto skip_accessed_flag;
         default:
             /* Readable code or data segment? */
-            if ( (desc.b & (5u<<9)) == (4u<<9) )
+            if ( (desc.b & (_SEGMENT_CODE|_SEGMENT_WR)) == _SEGMENT_CODE )
                 goto unmap_and_fail;
             /*
              * Data or non-conforming code segment: