Skip to contentSkip to conversation

x86/hvm/viridian: zero and check vcpu context __pad field

PATCH Superseded Paul Durrant 2 participants 8 messages Mar 30, 2016

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm/viridian: zero and check vcpu context __pad field

Commit 57844631 "save APIC assist vector" added an extra field to the viridian vcpu context save record. This field was only a uint8_t and so an extra __pad field was also added to pad up to the next 64-bit boundary.

This patch makes sure that __pad field is zeroed on save and checked for zero on restore. This prevents a potential leak of information from the stack and a compatibility check against future use of the space occupied by the __pad field.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/viridian.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
xen/arch/x86/hvm/viridian.c +14 -0
index 5c76c1a..b85b55b 100644
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));
+
         ctxt.apic_assist_msr = v->arch.hvm_vcpu.viridian.apic_assist.msr.raw;
         ctxt.apic_assist_vector = v->arch.hvm_vcpu.viridian.apic_assist.vector;
 
@@ -834,6 +836,15 @@ static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     return 0;
 }
 
+static bool_t is_zero(void *p, size_t size)
+{
+    while ( size-- )
+        if ( *(uint8_t *)p++ != 0 )
+            return 0;
+
+    return 1;
+}
+
 static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
 {
     int vcpuid;
@@ -851,6 +862,9 @@ static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     if ( hvm_load_entry_zeroextend(VIRIDIAN_VCPU, h, &ctxt) != 0 )
         return -EINVAL;
 
+    if ( !is_zero(&ctxt._pad, sizeof(ctxt._pad)) )
+        return -EINVAL;
+
     v->arch.hvm_vcpu.viridian.apic_assist.msr.raw = ctxt.apic_assist_msr;
     if ( v->arch.hvm_vcpu.viridian.apic_assist.msr.fields.enabled )
         initialize_apic_assist(v);
Signature · 7 lines
2.1.4


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

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 827

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +1 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 839

Quoted 5 lines
xen/arch/x86/hvm/viridian.c +1 -0
@@ -834,6 +836,15 @@ static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     return 0;
 }
 
+static bool_t is_zero(void *p, size_t size)

At the very least this wants to be a pointer to const.

Quoted 12 lines
xen/arch/x86/hvm/viridian.c +8 -0
+{
+    while ( size-- )
+        if ( *(uint8_t *)p++ != 0 )
+            return 0;
+
+    return 1;
+}
+
 static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
 {
     int vcpuid;

xen/arch/x86/hvm/viridian.c static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 866

Quoted 5 lines
xen/arch/x86/hvm/viridian.c +2 -0
@@ -851,6 +862,9 @@ static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     if ( hvm_load_entry_zeroextend(VIRIDIAN_VCPU, h, &ctxt) != 0 )
         return -EINVAL;
 
+    if ( !is_zero(&ctxt._pad, sizeof(ctxt._pad)) )
+        return -EINVAL;

"memcmp(&ctx._pad, zero_page, sizeof(ctxt._pad))" would be an alternative not requiring any new helper function.

Jan

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

#2 lore raw
Quoted 13 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: Re: [PATCH] x86/hvm/viridian: zero and check vcpu context __pad field

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain *d,

hvm_domain_context_t *h)

Quoted 7 lines

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 827

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

I think having a 'zero the entire struct' call at the start is better as it will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar call into the domain context save function would probably be worthwhile.

Quoted 23 lines

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain

Quoted 1 line
xen/arch/x86/hvm/viridian.c +0 -0
@@ -834,6 +836,15 @@ static int viridian_save_vcpu_ctxt(struct domain

*d, hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 839

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     return 0;
 }
 
+static bool_t is_zero(void *p, size_t size)

At the very least this wants to be a pointer to const.

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h)

Quoted 9 lines
xen/arch/x86/hvm/viridian.c +8 -0
+{
+    while ( size-- )
+        if ( *(uint8_t *)p++ != 0 )
+            return 0;
+
+    return 1;
+}
+
 static int viridian_load_vcpu_ctxt(struct domain *d,

hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_load_vcpu_ctxt(struct domain *d,

Quoted 3 lines
xen/arch/x86/hvm/viridian.c +0 -0
 {
     int vcpuid;
@@ -851,6 +862,9 @@ static int viridian_load_vcpu_ctxt(struct domain *d,

hvm_domain_context_t *h)

Quoted 9 lines

xen/arch/x86/hvm/viridian.c static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 866

Quoted 5 lines
xen/arch/x86/hvm/viridian.c +2 -0
     if ( hvm_load_entry_zeroextend(VIRIDIAN_VCPU, h, &ctxt) != 0 )
         return -EINVAL;
 
+    if ( !is_zero(&ctxt._pad, sizeof(ctxt._pad)) )
+        return -EINVAL;

"memcmp(&ctx._pad, zero_page, sizeof(ctxt._pad))" would be an alternative not requiring any new helper function.

Ah, I didn't know about the zero_page definition. I'll use that and drop the helper.

Paul

Quoted 1 line

Jan

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

#3 lore raw
Quoted 13 lines
Quoted 13 lines
Quoted 1 line · On 30.03.16 at 13:26, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain *d,

hvm_domain_context_t *h)

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));
Quoted 7 lines
Quoted 2 lines

How about just adding an empty initializer to the declaration?

I think having a 'zero the entire struct' call at the start is better as it will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar call into the domain context save function would probably be worthwhile.

And how does the initializer approach not fulfill that intention?

Jan

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

#4 lore raw
Quoted 25 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 14:17
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context __pad field

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 13:26, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain

*d,

Quoted 9 lines
Quoted 8 lines

hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 827

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

Quoted 7 lines
Quoted 4 lines

I think having a 'zero the entire struct' call at the start is better as it will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar call into the domain context save function would probably be worthwhile.

And how does the initializer approach not fulfill that intention?

Because any time anyone adds another field they have to remember to add another initializer, which is what I forgot to do. This approach OTOH is failsafe.

Paul

Quoted 1 line

Jan

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

#5 lore raw
Quoted 31 lines
Quoted 31 lines
Quoted 1 line · On 30.03.16 at 15:19, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 14:17
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context __pad field

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 13:26, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct domain

*d,

Quoted 13 lines
Quoted 8 lines

hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 827

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

I think having a 'zero the entire struct' call at the start is better as it will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar call into the domain context save function would probably be worthwhile.

Quoted 6 lines
Quoted 2 lines

And how does the initializer approach not fulfill that intention?

Because any time anyone adds another field they have to remember to add another initializer, which is what I forgot to do. This approach OTOH is failsafe.

But note how I said "an empty initializer": When there is an initializer at all, all fields not mentioned in the initializer will get default initialized (i.e. zeroed). Hence an empty initializer clears the entire structure.

Jan

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

#6 lore raw
Quoted 45 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 15:22
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context __pad field

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 15:19, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 14:17
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context

__pad

Quoted 9 lines
Quoted 9 lines

field

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 13:26, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct

domain

Quoted 15 lines
Quoted 15 lines

*d,

Quoted 13 lines
Quoted 8 lines

hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct domain *d, hvm_domain_context_t *h) · line 827

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

I think having a 'zero the entire struct' call at the start is better as it will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar call into the domain context save function would probably be worthwhile.

Quoted 4 lines
Quoted 2 lines

And how does the initializer approach not fulfill that intention?

Because any time anyone adds another field they have to remember to

Quoted 9 lines

add

Quoted 2 lines

another initializer, which is what I forgot to do. This approach OTOH is failsafe.

But note how I said "an empty initializer": When there is an initializer at all, all fields not mentioned in the initializer will get default initialized (i.e. zeroed). Hence an empty initializer clears the entire structure.

Ah, you mean C99 initializer style. That would be neater.

Paul

Quoted 1 line

Jan

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

#7 lore raw
Quoted 52 lines
Quoted 19 lines
Quoted 1 line · On 30.03.16 at 17:16, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 15:22
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context __pad field

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 15:19, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 14:17
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH] x86/hvm/viridian: zero and check vcpu context

__pad

Quoted 2 lines
Quoted 2 lines

field

Quoted 19 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 30.03.16 at 13:26, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 30 March 2016 12:23

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.16 at 12:32, <[email protected]> wrote:
xen/arch/x86/hvm/viridian.c +0 -0
@@ -824,6 +824,8 @@ static int viridian_save_vcpu_ctxt(struct

domain

Quoted 11 lines
Quoted 11 lines

*d,

Quoted 10 lines
Quoted 8 lines

hvm_domain_context_t *h)

xen/arch/x86/hvm/viridian.c static int viridian_save_vcpu_ctxt(struct · line 827

Quoted 4 lines
xen/arch/x86/hvm/viridian.c +1 -0
     for_each_vcpu( d, v ) {
         struct hvm_viridian_vcpu_context ctxt;
 
+        memset(&ctxt, 0, sizeof(ctxt));

How about just adding an empty initializer to the declaration?

I think having a 'zero the entire struct' call at the start is better as

it

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

will cover any additions made to the struct in future. It's what I had mistakenly assumed was already there. In fact I think adding a similar

call

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

into the domain context save function would probably be worthwhile.

And how does the initializer approach not fulfill that intention?

Because any time anyone adds another field they have to remember to

add

Quoted 2 lines

another initializer, which is what I forgot to do. This approach OTOH is failsafe.

Quoted 7 lines
Quoted 5 lines

But note how I said "an empty initializer": When there is an initializer at all, all fields not mentioned in the initializer will get default initialized (i.e. zeroed). Hence an empty initializer clears the entire structure.

Ah, you mean C99 initializer style. That would be neater.

Not really - "static struct s s = {};" was allowed in C89 too iirc.

Jan

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