Skip to contentSkip to conversation

xen: Restore p2m_access_t enum order to allow bitmask semantics

PATCH In review Malcolm Crossley 7 participants 8 messages Mar 8, 2016 – Mar 14, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: Restore p2m_access_t enum order to allow bitmask semantics

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused functional problems for the nested hap code. As it may be error prone to audit and find all other p2m_access users assuming bitmask semantics, instead restore the previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

---
 xen/arch/x86/mm/hap/nested_hap.c |  2 +-
 xen/include/xen/p2m-common.h     | 17 +++++++++--------
 2 files changed, 10 insertions(+), 9 deletions(-)
xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 0dbae13..9cee5a0 100644
@@ -263,7 +263,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     switch ( p2ma_10 )
     {
-    case p2m_access_rwx ... p2m_access_n:
+    case p2m_access_n ... p2m_access_rwx:
         break;
     case p2m_access_rx2rw:
         p2ma_10 = p2m_access_rx;
xen/include/xen/p2m-common.h +9 -8
index 8b70459..6374a5b 100644
@@ -15,14 +15,15 @@
  * default.
  */
 typedef enum {
-    p2m_access_rwx   = 0, /* The default access type when not used. */
-    p2m_access_wx    = 1,
-    p2m_access_rx    = 2,
-    p2m_access_x     = 3,
-    p2m_access_rw    = 4,
-    p2m_access_w     = 5,
-    p2m_access_r     = 6,
-    p2m_access_n     = 7, /* No access allowed. */
+    /* Code uses bottom three bits with bitmask semantics */
+    p2m_access_n     = 0, /* No access allowed. */
+    p2m_access_r     = 1 << 0,
+    p2m_access_w     = 1 << 1,
+    p2m_access_x     = 1 << 2,
+    p2m_access_rw    = p2m_access_r | p2m_access_w,
+    p2m_access_rx    = p2m_access_r | p2m_access_x,
+    p2m_access_wx    = p2m_access_w | p2m_access_x,
+    p2m_access_rwx   = p2m_access_r | p2m_access_w | p2m_access_x,
 
     p2m_access_rx2rw = 8, /* Special: page goes from RX to RW on write */
     p2m_access_n2rwx = 9, /* Special: page goes from N to RWX on access, *
Signature · 7 lines
1.7.12.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Malcolm Crossley

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused functional problems for the nested hap code. As it may be error prone to audit and find all other p2m_access users assuming bitmask semantics, instead restore the previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

Specifically, the bug causes memory corruption in the L2 guest, because the code out of context in nestedhvm_hap_nested_page_fault() incorrectly calculates the permission bits for the nested p2m.

Quoted 36 lines
---
 xen/arch/x86/mm/hap/nested_hap.c |  2 +-
 xen/include/xen/p2m-common.h     | 17 +++++++++--------
 2 files changed, 10 insertions(+), 9 deletions(-)
xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 0dbae13..9cee5a0 100644
@@ -263,7 +263,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     switch ( p2ma_10 )
     {
-    case p2m_access_rwx ... p2m_access_n:
+    case p2m_access_n ... p2m_access_rwx:
         break;
     case p2m_access_rx2rw:
         p2ma_10 = p2m_access_rx;
xen/include/xen/p2m-common.h +2 -8
index 8b70459..6374a5b 100644
@@ -15,14 +15,15 @@
  * default.
  */
 typedef enum {
-    p2m_access_rwx   = 0, /* The default access type when not used. */
-    p2m_access_wx    = 1,
-    p2m_access_rx    = 2,
-    p2m_access_x     = 3,
-    p2m_access_rw    = 4,
-    p2m_access_w     = 5,
-    p2m_access_r     = 6,
-    p2m_access_n     = 7, /* No access allowed. */
+    /* Code uses bottom three bits with bitmask semantics */
+    p2m_access_n     = 0, /* No access allowed. */

xen/include/xen/p2m-common.h line 29

Quoted 10 lines
xen/include/xen/p2m-common.h +7 -0
+    p2m_access_r     = 1 << 0,
+    p2m_access_w     = 1 << 1,
+    p2m_access_x     = 1 << 2,
+    p2m_access_rw    = p2m_access_r | p2m_access_w,
+    p2m_access_rx    = p2m_access_r | p2m_access_x,
+    p2m_access_wx    = p2m_access_w | p2m_access_x,
+    p2m_access_rwx   = p2m_access_r | p2m_access_w | p2m_access_x,
 
     p2m_access_rx2rw = 8, /* Special: page goes from RX to RW on write */
     p2m_access_n2rwx = 9, /* Special: page goes from N to RWX on access, *

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

#2 lore raw
In reply to Malcolm Crossley
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.03.16 at 16:30, <[email protected]> wrote:

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused functional problems for the nested hap code. As it may be error prone to audit and find all other p2m_access users assuming bitmask semantics, instead restore the previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

Makes sense, but how certain are you that ...

xen/include/xen/p2m-common.h line 18

Quoted 7 lines
xen/include/xen/p2m-common.h +0 -1
@@ -15,14 +15,15 @@
  * default.
  */
 typedef enum {
-    p2m_access_rwx   = 0, /* The default access type when not used. */

... namely this has not meanwhile seen any implicit use somewhere?

Tamas, the original commit talked about this as an optimization only. Can you confirm that there indeed was no other intention than the one claimed in that commit's description?

Jan

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

#3 lore raw
Quoted 25 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.03.16 at 16:30, <[email protected]> wrote:

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused

functional

Quoted 1 line

problems for the nested hap code. As it may be error prone to audit and

find

Quoted 1 line

all other p2m_access users assuming bitmask semantics, instead restore

the

Quoted 2 lines

previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

Makes sense, but how certain are you that ...

xen/include/xen/p2m-common.h line 18

Quoted 7 lines
xen/include/xen/p2m-common.h +0 -1
@@ -15,14 +15,15 @@
  * default.
  */
 typedef enum {
-    p2m_access_rwx   = 0, /* The default access type when not used. */

... namely this has not meanwhile seen any implicit use somewhere?

Quoted 4 lines

Tamas, the original commit talked about this as an optimization only. Can you confirm that there indeed was no other intention than the one claimed in that commit's description?

That's the only reason I recall as well, so from my perspective it's fine to be reverted.

Tamas

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

#4 lore raw
In reply to Malcolm Crossley
Quoted 10 lines · Malcolm Crossley

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused functional problems for the nested hap code. As it may be error prone to audit and find all other p2m_access users assuming bitmask semantics, instead restore the previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

Looks good; but following up Jan's point, could you do a brief survey of the places where the p2m_access values are used, and confirm that none of them now implicitly assume that p2m_access_rwx is zero?

(Or Tamas, can you say that you're reasonably certain nothing has now come to depend on the value of p2m_access_rwx being zero?)

Thanks,
-George

Quoted 37 lines
---
 xen/arch/x86/mm/hap/nested_hap.c |  2 +-
 xen/include/xen/p2m-common.h     | 17 +++++++++--------
 2 files changed, 10 insertions(+), 9 deletions(-)
xen/arch/x86/mm/hap/nested_hap.c +1 -1
index 0dbae13..9cee5a0 100644
@@ -263,7 +263,7 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa,
 
     switch ( p2ma_10 )
     {
-    case p2m_access_rwx ... p2m_access_n:
+    case p2m_access_n ... p2m_access_rwx:
         break;
     case p2m_access_rx2rw:
         p2ma_10 = p2m_access_rx;
xen/include/xen/p2m-common.h +2 -8
index 8b70459..6374a5b 100644
@@ -15,14 +15,15 @@
  * default.
  */
 typedef enum {
-    p2m_access_rwx   = 0, /* The default access type when not used. */
-    p2m_access_wx    = 1,
-    p2m_access_rx    = 2,
-    p2m_access_x     = 3,
-    p2m_access_rw    = 4,
-    p2m_access_w     = 5,
-    p2m_access_r     = 6,
-    p2m_access_n     = 7, /* No access allowed. */
+    /* Code uses bottom three bits with bitmask semantics */
+    p2m_access_n     = 0, /* No access allowed. */

xen/include/xen/p2m-common.h line 29

Quoted 10 lines
xen/include/xen/p2m-common.h +7 -0
+    p2m_access_r     = 1 << 0,
+    p2m_access_w     = 1 << 1,
+    p2m_access_x     = 1 << 2,
+    p2m_access_rw    = p2m_access_r | p2m_access_w,
+    p2m_access_rx    = p2m_access_r | p2m_access_x,
+    p2m_access_wx    = p2m_access_w | p2m_access_x,
+    p2m_access_rwx   = p2m_access_r | p2m_access_w | p2m_access_x,
 
     p2m_access_rx2rw = 8, /* Special: page goes from RX to RW on write */
     p2m_access_n2rwx = 9, /* Special: page goes from N to RWX on access, *

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

#5 lore raw
Quoted 13 lines · George Dunlap
Quoted 4 lines · Malcolm Crossley

Nested hap code assumed implict bitmask semantics of the p2m_access_t enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c

The change to the enum ordering broke this assumption and caused

functional

Quoted 1 line

problems for the nested hap code. As it may be error prone to audit and

find

Quoted 1 line

all other p2m_access users assuming bitmask semantics, instead restore

the

Quoted 3 lines

previous enum order and make it explict that bitmask semantics are to be preserved for the read, write and execute access types.

Quoted 9 lines
Quoted 1 line

Looks good; but following up Jan's point, could you do a brief survey of the places where the p2m_access values are used, and confirm that none of them now implicitly assume that p2m_access_rwx is zero?

(Or Tamas, can you say that you're reasonably certain nothing has now come to depend on the value of p2m_access_rwx being zero?)

Yes, from my perspective it's all fine as checks of p2m_access values are done with the enum names and not the values directly.

Tamas

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

#6 lore raw
Quoted 21 lines · Tamas K Lengyel

On Wed, Mar 9, 2016 at 5:30 PM, George Dunlap <[email protected] <mailto:[email protected]>> wrote:

On 08/03/16 15:30, Malcolm Crossley wrote:
> Nested hap code assumed implict bitmask semantics of the p2m_access_t
> enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c
>
> The change to the enum ordering broke this assumption and caused functional
> problems for the nested hap code. As it may be error prone to audit and find
> all other p2m_access users assuming bitmask semantics, instead restore the
> previous enum order and make it explict that bitmask semantics are to be
> preserved for the read, write and execute access types.
>
> Signed-off-by: Malcolm Crossley <[email protected] <mailto:[email protected]>>

Looks good; but following up Jan's point, could you do a brief survey of the places where the p2m_access values are used, and confirm that none of them now implicitly assume that p2m_access_rwx is zero?

Quoted 6 lines

(Or Tamas, can you say that you're reasonably certain nothing has now come to depend on the value of p2m_access_rwx being zero?)

Yes, from my perspective it's all fine as checks of p2m_access values are done with the enum names and not the values directly.

I can't see any other usages of p2m_access_t without enum values either.

Malcolm

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

Reviewed-by
#7 lore raw
Quoted 22 lines · Malcolm Crossley
Quoted 21 lines · Tamas K Lengyel

On Wed, Mar 9, 2016 at 5:30 PM, George Dunlap <[email protected] <mailto:[email protected]>> wrote:

On 08/03/16 15:30, Malcolm Crossley wrote:
> Nested hap code assumed implict bitmask semantics of the p2m_access_t
> enum prior to C/S 4c63692d7c38c5ac414fe97f8ef37b66e05abe5c
>
> The change to the enum ordering broke this assumption and caused functional
> problems for the nested hap code. As it may be error prone to audit and find
> all other p2m_access users assuming bitmask semantics, instead restore the
> previous enum order and make it explict that bitmask semantics are to be
> preserved for the read, write and execute access types.
>
> Signed-off-by: Malcolm Crossley <[email protected] <mailto:[email protected]>>

Looks good; but following up Jan's point, could you do a brief survey of the places where the p2m_access values are used, and confirm that none of them now implicitly assume that p2m_access_rwx is zero?

Quoted 8 lines
Quoted 6 lines

(Or Tamas, can you say that you're reasonably certain nothing has now come to depend on the value of p2m_access_rwx being zero?)

Yes, from my perspective it's all fine as checks of p2m_access values are done with the enum names and not the values directly.

I can't see any other usages of p2m_access_t without enum values either.

Great, thanks:

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