Skip to contentSkip to conversation

hvm: refactor set param

PATCH Superseded Norbert Manthey 2 participants 15 messages Feb 5, 2021 – Feb 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hvm: refactor set param

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains as late as possible.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

This is part of the speculative hardening effort.

---
v2: Add another speculative blocker, which protects the return code check
of the function hvm_allow_set_param.

 xen/arch/x86/hvm/hvm.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +15 -4
@@ -4060,7 +4060,7 @@ static int hvm_allow_set_param(struct domain *d,
                                uint32_t index,
                                uint64_t new_value)
 {
-    uint64_t value = d->arch.hvm.params[index];
+    uint64_t value;
     int rc;
 
     rc = xsm_hvm_param(XSM_TARGET, d, HVMOP_set_param);
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:
@@ -4388,6 +4398,10 @@ int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
     if ( rc )
         return rc;
 
+    /* Make sure the index bound check in hvm_get_param is respected, as well as
+       the above domain permissions. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_ACPI_S_STATE:
@@ -4428,9 +4442,6 @@ static int hvmop_get_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;
Signature · 12 lines
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#1 lore raw
Quoted 11 lines · Norbert Manthey

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains as late as possible.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

This is part of the speculative hardening effort.

Did you lose Ian's release-ack, or did you drop it for a specific reason?

Quoted 15 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -4060,7 +4060,7 @@ static int hvm_allow_set_param(struct domain *d,
                                uint32_t index,
                                uint64_t new_value)
 {
-    uint64_t value = d->arch.hvm.params[index];
+    uint64_t value;
     int rc;
 
     rc = xsm_hvm_param(XSM_TARGET, d, HVMOP_set_param);
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Furthermore the first switch() doesn't use "value" at all, so you could move the access even further down. This may have the downside of adding latency, so may not be worth it, but in this case at least the description should say half a word, especially seeing it say "as late as possible" right now.

xen/arch/x86/hvm/hvm.c static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value) · line 4156

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:

Like you do for the "get" path I think this similarly renders pointless the use in hvmop_set_param() (and - see below - the same consideration wrt is_hvm_domain() applies).

xen/arch/x86/hvm/hvm.c int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value) · line 4403

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -4388,6 +4398,10 @@ int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
     if ( rc )
         return rc;
 
+    /* Make sure the index bound check in hvm_get_param is respected, as well as
+       the above domain permissions. */
+    block_speculation();

Nit: Please fix comment style here.

xen/arch/x86/hvm/hvm.c static int hvmop_get_param( · line 4447

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +0 -3
@@ -4428,9 +4442,6 @@ static int hvmop_get_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;

This one really was pointless anyway, as is_hvm_domain() (used down from here) already contains a suitable barrier.

Jan

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Norbert Manthey

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains as late as possible.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

Quoted 6 lines
Quoted 4 lines

This is part of the speculative hardening effort.

Did you lose Ian's release-ack, or did you drop it for a specific reason?

That happened by accident, similarly to not chaining this v2 to the former v1. I'll add it to the next revision.

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/hvm/hvm.c +1 -1
@@ -4060,7 +4060,7 @@ static int hvm_allow_set_param(struct domain *d,
                                uint32_t index,
                                uint64_t new_value)
 {
-    uint64_t value = d->arch.hvm.params[index];
+    uint64_t value;
     int rc;
 
     rc = xsm_hvm_param(XSM_TARGET, d, HVMOP_set_param);
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
Quoted 14 lines

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Quoted 6 lines

Furthermore the first switch() doesn't use "value" at all, so you could move the access even further down. This may have the downside of adding latency, so may not be worth it, but in this case at least the description should say half a word, especially seeing it say "as late as possible" right now.

Agreed, I can move this further down, or adapt the wording. The initial intention was to move it only below the first possible speculative blocker. Hence, let me adapt the wording.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
Quoted 9 lines

xen/arch/x86/hvm/hvm.c static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value) · line 4156

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +3 -0
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:

Like you do for the "get" path I think this similarly renders pointless the use in hvmop_set_param() (and - see below - the same consideration wrt is_hvm_domain() applies).

Can you please be more specific why this is pointless? I understand that the is_hvm_domain check comes with a barrier that can be used to not add another barrier. However, I did not find such a barrier here, which comes between the 'if (rc)' just above, and the potential next access based on the value of 'index'. At least the access behind the switch statement cannot be optimized and replaced with a constant value easily.

Quoted 9 lines

xen/arch/x86/hvm/hvm.c int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value) · line 4403

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -4388,6 +4398,10 @@ int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
     if ( rc )
         return rc;
 
+    /* Make sure the index bound check in hvm_get_param is respected, as well as
+       the above domain permissions. */
+    block_speculation();

Nit: Please fix comment style here.

Will do.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4428,9 +4442,6 @@ static int hvmop_get_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static int hvmop_get_param( · line 4447

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -3
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;

This one really was pointless anyway, as is_hvm_domain() (used down from here) already contains a suitable barrier.

Yes, agreed.

Best,
Norbert

Quoted 2 lines

Jan

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#3 lore raw

PATCH v3 1/1 hvm: refactor set param

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains after performing all relevant checks, and blocking speculative execution.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

This is part of the speculative hardening effort.

---
v3: * rephrased commit message to better explain code relocation
* added release-acked

 xen/arch/x86/hvm/hvm.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +15 -4
@@ -4060,7 +4060,7 @@ static int hvm_allow_set_param(struct domain *d,
                                uint32_t index,
                                uint64_t new_value)
 {
-    uint64_t value = d->arch.hvm.params[index];
+    uint64_t value;
     int rc;
 
     rc = xsm_hvm_param(XSM_TARGET, d, HVMOP_set_param);
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:
@@ -4388,6 +4398,10 @@ int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
     if ( rc )
         return rc;
 
+    /* Make sure the index bound check in hvm_get_param is respected, as well as
+       the above domain permissions. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_ACPI_S_STATE:
@@ -4428,9 +4442,6 @@ static int hvmop_get_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;
Signature · 12 lines
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#4 lore raw
Quoted 3 lines · Norbert Manthey

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains after performing all relevant checks, and blocking speculative execution.

I'd like to suggest "..., let's load values of domains only after ...".

But there are other points open from v2; I'd like to further suggest that you allow discussion on a prior version to first settle, before sending a new one. Unless of course discussion appears to have stalled.

Jan

#5 lore raw
In reply to Norbert Manthey
Quoted 20 lines · Norbert Manthey
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
Quoted 14 lines

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
Quoted 9 lines

xen/arch/x86/hvm/hvm.c static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value) · line 4156

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +3 -0
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:

Like you do for the "get" path I think this similarly renders pointless the use in hvmop_set_param() (and - see below - the same consideration wrt is_hvm_domain() applies).

Quoted 6 lines

Can you please be more specific why this is pointless? I understand that the is_hvm_domain check comes with a barrier that can be used to not add another barrier. However, I did not find such a barrier here, which comes between the 'if (rc)' just above, and the potential next access based on the value of 'index'. At least the access behind the switch statement cannot be optimized and replaced with a constant value easily.

I'm suspecting a misunderstanding (the more that further down you did agree to what I've said for hvmop_get_param()): I'm not saying your addition is pointless. Instead I'm saying that your addition should be accompanied by removal of the barrier from hvmop_set_param(), paralleling what you do to hvmop_get_param(). And additionally I'm saying that just like in hvmop_get_param() the barrier there was already previously redundant with that inside is_hvm_domain().

Jan

#6 lore raw
Quoted 25 lines · Jan Beulich

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 20 lines · Norbert Manthey
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 6 lines
Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

I can drop that bound check again. This was added to make sure other callers would be save as well. Thinking about this a little more, the check could actually be moved into the hvm_allow_set_param function, above the first index access in that function. Are there good reasons to not move the index check into the allow function?

Quoted 20 lines
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4141,6 +4148,9 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     if ( rc )
         return rc;
 
Quoted 9 lines

xen/arch/x86/hvm/hvm.c static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value) · line 4156

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +3 -0
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:

Like you do for the "get" path I think this similarly renders pointless the use in hvmop_set_param() (and - see below - the same consideration wrt is_hvm_domain() applies).

Quoted 6 lines

Can you please be more specific why this is pointless? I understand that the is_hvm_domain check comes with a barrier that can be used to not add another barrier. However, I did not find such a barrier here, which comes between the 'if (rc)' just above, and the potential next access based on the value of 'index'. At least the access behind the switch statement cannot be optimized and replaced with a constant value easily.

Quoted 8 lines

I'm suspecting a misunderstanding (the more that further down you did agree to what I've said for hvmop_get_param()): I'm not saying your addition is pointless. Instead I'm saying that your addition should be accompanied by removal of the barrier from hvmop_set_param(), paralleling what you do to hvmop_get_param(). And additionally I'm saying that just like in hvmop_get_param() the barrier there was already previously redundant with that inside is_hvm_domain().

I now understand, thank you. I agree, the already existing barrier in the hvmop_set_param function can be dropped as well. I will update the diff accordingly, after we concluded where to put the index check.

Best,
Norbert

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#7 lore raw
Quoted 28 lines · Norbert Manthey
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Norbert Manthey
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 6 lines
Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

Quoted 5 lines

I can drop that bound check again. This was added to make sure other callers would be save as well. Thinking about this a little more, the check could actually be moved into the hvm_allow_set_param function, above the first index access in that function. Are there good reasons to not move the index check into the allow function?

I guess I'm confused: We're talking about dropping the check you add to hvm_allow_set_param() and you suggest to "move" it right there?

Jan

#8 lore raw
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Norbert Manthey
Quoted 27 lines · Jan Beulich
Quoted 20 lines · Norbert Manthey
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
Quoted 14 lines

xen/arch/x86/hvm/hvm.c static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

Quoted 8 lines
Quoted 5 lines

I can drop that bound check again. This was added to make sure other callers would be save as well. Thinking about this a little more, the check could actually be moved into the hvm_allow_set_param function, above the first index access in that function. Are there good reasons to not move the index check into the allow function?

I guess I'm confused: We're talking about dropping the check you add to hvm_allow_set_param() and you suggest to "move" it right there?

Yes. I can either just no introduce that check in that function (which is what you suggested). As an alternative, to have all checks in one function, I proposed to instead move it into hvm_allow_set_param. If you do not like this proposal, I'll follow your suggestion and will simply not introduce the additional bound check as part of this patch.

Best,
Norbert

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#9 lore raw
Quoted 35 lines · Norbert Manthey
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Norbert Manthey
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Norbert Manthey
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
(quoted diff) +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 

Review static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
(quoted diff) +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 6 lines
Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

I can drop that bound check again. This was added to make sure other callers would be save as well. Thinking about this a little more, the check could actually be moved into the hvm_allow_set_param function, above the first index access in that function. Are there good reasons to not move the index check into the allow function?

Quoted 7 lines
Quoted 3 lines

I guess I'm confused: We're talking about dropping the check you add to hvm_allow_set_param() and you suggest to "move" it right there?

Yes. I can either just no introduce that check in that function (which is what you suggested). As an alternative, to have all checks in one function, I proposed to instead move it into hvm_allow_set_param.

Oh, I see. What I'd like to be the result of your re-arrangement is symmetry between "get" and "set" where possible in this regard, and asymmetry having a clear reason. Seeing that hvm_{get,set}_param() are the non-static functions here, I think it would be quite desirable for the bounds checking to live there. Since hvm_allow_{get,set}_param() are specifically helpers of the two earlier named functions, checks consistently living there would as well be fine with me.

Jan

#10 lore raw
Quoted 42 lines · Jan Beulich
Quoted 35 lines · Norbert Manthey
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Norbert Manthey
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Norbert Manthey
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Norbert Manthey
(quoted diff) +0 -0
@@ -4108,6 +4108,13 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 

Review static int hvm_allow_set_param(struct domain *d, · line 4120

Quoted 10 lines
(quoted diff) +7 -0
+    if ( index >= HVM_NR_PARAMS )
+        return -EINVAL;
+
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */

I don't see the need for the heavier block_speculation() here; afaict array_access_nospec() should do fine. The switch() in context above as well as the switch() further down in the function don't have any speculation susceptible code.

Quoted 6 lines
Quoted 2 lines

The reason to block speculation instead of just using the hardened index access is to not allow to speculatively load data from another domain.

Okay, looks like I got mislead by the added bounds check. Why do you add that, when the sole caller already has one? It'll suffice since you move the array access past the barrier, won't it?

I can drop that bound check again. This was added to make sure other callers would be save as well. Thinking about this a little more, the check could actually be moved into the hvm_allow_set_param function, above the first index access in that function. Are there good reasons to not move the index check into the allow function?

Quoted 6 lines
Quoted 3 lines

I guess I'm confused: We're talking about dropping the check you add to hvm_allow_set_param() and you suggest to "move" it right there?

Yes. I can either just no introduce that check in that function (which is what you suggested). As an alternative, to have all checks in one function, I proposed to instead move it into hvm_allow_set_param.

Quoted 8 lines

Oh, I see. What I'd like to be the result of your re-arrangement is symmetry between "get" and "set" where possible in this regard, and asymmetry having a clear reason. Seeing that hvm_{get,set}_param() are the non-static functions here, I think it would be quite desirable for the bounds checking to live there. Since hvm_allow_{get,set}_param() are specifically helpers of the two earlier named functions, checks consistently living there would as well be fine with me.

I agree with the symmetry for get and set. This is what I'd aim for:

 1. hvmop_set_param and hvmop_get_param (static) both check for the index, and afterwards use the is_hvm_domain(d) function with its barrier
 2. hvm_set_param (static) and hvm_get_param both call their allow helper function, evaluate the return code, and afterwards block speculation. 2.1. hvm_get_param is declared in a public header, and cannot be turned into a static function, hence needs the index check
 2.2. hvm_set_param is only called from hvmop_set_param, and index is already checked there, hence, do not add check
 3. hvm_allow_set_param (static) and hvm_allow_get_param (static) do not validate the index parameter
 3.1. hvm_allow_set_param blocks speculative execution with a barrier after domain permissions have been evaluated, before accessing the parameters of the domain. hvm_allow_get_param does not access the params member of the domain, and hence does not require additional protection.

To simplify the code, I propose to furthermore make the hvmop_set_param function static as well.

Please let me know whether the above would is acceptable.

Best,
Norbert

Quoted 2 lines

Jan

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#11 lore raw
Quoted 8 lines · Norbert Manthey

I agree with the symmetry for get and set. This is what I'd aim for:

 1. hvmop_set_param and hvmop_get_param (static) both check for the index, and afterwards use the is_hvm_domain(d) function with its barrier
 2. hvm_set_param (static) and hvm_get_param both call their allow helper function, evaluate the return code, and afterwards block speculation. 2.1. hvm_get_param is declared in a public header, and cannot be turned into a static function, hence needs the index check

But both further call sites are in bounded loops, with the bounds not guest controlled. It can rely on the callers just as much as ...

Quoted 2 lines

 2.2. hvm_set_param is only called from hvmop_set_param, and index is already checked there, hence, do not add check

... this.

Quoted 9 lines

 3. hvm_allow_set_param (static) and hvm_allow_get_param (static) do not validate the index parameter
 3.1. hvm_allow_set_param blocks speculative execution with a barrier after domain permissions have been evaluated, before accessing the parameters of the domain. hvm_allow_get_param does not access the params member of the domain, and hence does not require additional protection.

To simplify the code, I propose to furthermore make the hvmop_set_param function static as well.

Yes - this not being so already is likely simply an oversight, supported by the fact that there's no declaration in any header.

Jan

#12 lore raw
Quoted 7 lines · Jan Beulich

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 2 lines · Norbert Manthey

I agree with the symmetry for get and set. This is what I'd aim for:

Quoted 8 lines
Quoted 6 lines

1. hvmop_set_param and hvmop_get_param (static) both check for the
index, and afterwards use the is_hvm_domain(d) function with its barrier
2. hvm_set_param (static) and hvm_get_param both call their allow
helper function, evaluate the return code, and afterwards block speculation.
2.1. hvm_get_param is declared in a public header, and cannot be turned
into a static function, hence needs the index check

But both further call sites are in bounded loops, with the bounds not guest controlled. It can rely on the callers just as much as ...

Okay, so I will not add the check there either. I thought about future modifications that allow to call that function from other places, or modified call environments with eventually guest control - but I am fine to not consider these.

Quoted 12 lines
Quoted 3 lines

2.2. hvm_set_param is only called from hvmop_set_param, and index is
already checked there, hence, do not add check

... this.

Quoted 7 lines

3. hvm_allow_set_param (static) and hvm_allow_get_param (static) do not
validate the index parameter
3.1. hvm_allow_set_param blocks speculative execution with a barrier
after domain permissions have been evaluated, before accessing the parameters of the domain. hvm_allow_get_param does not access the params member of the domain, and hence does not require additional protection.

Quoted 4 lines
Quoted 2 lines

To simplify the code, I propose to furthermore make the hvmop_set_param function static as well.

Yes - this not being so already is likely simply an oversight, supported by the fact that there's no declaration in any header.

Okay.

Best,
Norbert

Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879

#13 lore raw

PATCH v4 1/1 hvm: refactor set param

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains only after performing all relevant checks, and blocking speculative execution.

For both get and set, the value of the index is already checked in the outer calling function. The block_speculation calls in hvmop_get_param and hvmop_set_param are removed, because is_hvm_domain already blocks speculation.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

To improve symmetry between the get and set operations, function hvmop_set_param is made static.

This is part of the speculative hardening effort.

---
v4: * add 'static' attribute to hvmop_set_param
* drop introduced bound checks, e.g. in hvm_allow_set_param
* drop existing bound check from hvm_set_param
* do not introduce block_speculation in hvmop_set_param, as is_hvm_domain already blocks speculation
* fix comments

 xen/arch/x86/hvm/hvm.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
xen/arch/x86/hvm/hvm.c +12 -11
@@ -4060,7 +4060,7 @@ static int hvm_allow_set_param(struct domain *d,
                                uint32_t index,
                                uint64_t new_value)
 {
-    uint64_t value = d->arch.hvm.params[index];
+    uint64_t value;
     int rc;
 
     rc = xsm_hvm_param(XSM_TARGET, d, HVMOP_set_param);
@@ -4108,6 +4108,10 @@ static int hvm_allow_set_param(struct domain *d,
     if ( rc )
         return rc;
 
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
+    value = d->arch.hvm.params[index];
     switch ( index )
     {
     /* The following parameters should only be changed once. */
@@ -4134,13 +4138,13 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     struct vcpu *v;
     int rc;
 
-    if ( index >= HVM_NR_PARAMS )
-        return -EINVAL;
-
     rc = hvm_allow_set_param(d, index, value);
     if ( rc )
         return rc;
 
+    /* Make sure we evaluate permissions before loading data of domains. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_CALLBACK_IRQ:
@@ -4305,7 +4309,7 @@ static int hvm_set_param(struct domain *d, uint32_t index, uint64_t value)
     return rc;
 }
 
-int hvmop_set_param(
+static int hvmop_set_param(
     XEN_GUEST_HANDLE_PARAM(xen_hvm_param_t) arg)
 {
     struct xen_hvm_param a;
@@ -4318,9 +4322,6 @@ int hvmop_set_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;
@@ -4388,6 +4389,9 @@ int hvm_get_param(struct domain *d, uint32_t index, uint64_t *value)
     if ( rc )
         return rc;
 
+    /* Make sure the above domain permissions check is respected. */
+    block_speculation();
+
     switch ( index )
     {
     case HVM_PARAM_ACPI_S_STATE:
@@ -4428,9 +4432,6 @@ static int hvmop_get_param(
     if ( a.index >= HVM_NR_PARAMS )
         return -EINVAL;
 
-    /* Make sure the above bound check is not bypassed during speculation. */
-    block_speculation();
-
     d = rcu_lock_domain_by_any_id(a.domid);
     if ( d == NULL )
         return -ESRCH;
Signature · 12 lines
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
Reviewed-by on v4 1/1
#14 lore raw
Quoted 13 lines · Norbert Manthey

To prevent leaking HVM params via L1TF and similar issues on a hyperthread pair, let's load values of domains only after performing all relevant checks, and blocking speculative execution.

For both get and set, the value of the index is already checked in the outer calling function. The block_speculation calls in hvmop_get_param and hvmop_set_param are removed, because is_hvm_domain already blocks speculation.

Furthermore, speculative barriers are re-arranged to make sure we do not allow guests running on co-located VCPUs to leak hvm parameter values of other domains.

Quoted 8 lines

To improve symmetry between the get and set operations, function hvmop_set_param is made static.

This is part of the speculative hardening effort.