Skip to contentSkip to conversation

xen/gnttab: XSA-519/520 followup

PATCH In review Andrew Cooper 3 participants 13 messages Nov 3 – Nov 4 RISC-V

lore
Share & tools
lore
#0 lore raw

cover xen/gnttab: XSA-519/520 followup

Andrew Cooper (6):
xen/gnttab: Drop owner->grant_table check in _cache_flush() xen/gnttab: Disallow GNTTABOP_cache_flush from non-direct-mapped
domains
xen/gnttab: Remove explicit rcu_lock_current_domain() xen/gnttab: Document how _cache_flush() works xen/gnttab: Fold error handling in _cache_flush()
treewide: Make {invalidate/clean}_dcahce_va_range() void

 xen/arch/arm/include/asm/page.h   | 20 +++++--------
 xen/arch/riscv/include/asm/page.h | 10 ++-----
 xen/arch/riscv/mm.c               |  4 +--
 xen/common/grant_table.c          | 48 ++++++++++++++++---------------
 4 files changed, 36 insertions(+), 46 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/6 xen/gnttab: Drop owner->grant_table check in _cache_flush()

The "owner && !owner->grant_table" case needs a put_page() in it's error handling.

In Xen 4.20 and earlier, this was addressed by XSA-520. Since the addition of CONFIG_HAS_GRANT_CACHE_FLUSH in Xen 4.21, this path is only reachable on ARM, and system domains on ARM do not own any memory.

Therefore delete the check, rather than inserting unreachable error handling.

No functional change.

This issue was also independently reported by Yehyeong Lee as the security team was already working on XSA-519/520.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Baptiste Le Duc <[email protected]>

---
 xen/common/grant_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/grant_table.c +1 -1
index 5e79c012b695..2285c4aee8d6 100644
@@ -3633,7 +3633,7 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
 
     page = mfn_to_page(mfn);
     owner = page_get_owner_and_reference(page);
-    if ( !owner || !owner->grant_table )
+    if ( !owner )
     {
         rcu_unlock_domain(d);
         return -EPERM;
Signature · 1 line
2.39.5
#2 lore raw

PATCH 2/6 xen/gnttab: Disallow GNTTABOP_cache_flush from non-direct-mapped domains

In reply to Andrew Cooper

cache_flush() opereates on maddr_to_mfn(cflush->a.dev_bus_addr), which is an address space layering violation.

Restrict the hypercall to direct-mapped domains.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

It also malfunctions when an SMMU is using non-identity mappings.

Linux never checks for errors, which hides all the ways in which this hypercall is broken.

---
 xen/common/grant_table.c | 3 +++
 1 file changed, 3 insertions(+)
xen/common/grant_table.c +3 -0
index 2285c4aee8d6..d42bd5fb83fd 100644
@@ -3704,6 +3704,9 @@ gnttab_cache_flush(XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) uop,
     unsigned int i;
     gnttab_cache_flush_t op;
 
+    if ( !is_domain_direct_mapped(current->domain) )
+        return -EOPNOTSUPP;
+
     for ( i = 0; i < count; i++ )
     {
         if ( i && hypercall_preempt_check() )
Signature · 1 line
2.39.5
#3 lore raw

PATCH 3/6 xen/gnttab: Remove explicit rcu_lock_current_domain()

In reply to Andrew Cooper

rcu_lock_current_domain() is always a nop, while rcu_unlock_domain() is not.

GNTTAB ops swap_grant_ref and cache_flush are the only two paths in Xen which explicitly RCU lock themselves. Drop this behaviour.

No functional change.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Baptiste Le Duc <[email protected]>

---
 xen/common/grant_table.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
xen/common/grant_table.c +2 -12
index d42bd5fb83fd..f4ec7524140a 100644
@@ -3498,7 +3498,7 @@ gnttab_get_version(XEN_GUEST_HANDLE_PARAM(gnttab_get_version_t) uop)
 static int16_t
 swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b)
 {
-    struct domain *d = rcu_lock_current_domain();
+    struct domain *d = current->domain;
     struct grant_table *gt = d->grant_table;
     struct active_grant_entry *act_a = NULL;
     struct active_grant_entry *act_b = NULL;
@@ -3573,8 +3573,6 @@ swap_grant_ref(grant_ref_t ref_a, grant_ref_t ref_b)
         active_entry_release(act_a);
     grant_write_unlock(gt);
 
-    rcu_unlock_domain(d);
-
     return rc;
 }
 
@@ -3622,22 +3620,16 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
     if ( cflush->op & GNTTAB_CACHE_SOURCE_GREF )
         return -EOPNOTSUPP;
 
-    d = rcu_lock_current_domain();
+    d = current->domain;
     mfn = maddr_to_mfn(cflush->a.dev_bus_addr);
 
     if ( !mfn_valid(mfn) )
-    {
-        rcu_unlock_domain(d);
         return -EINVAL;
-    }
 
     page = mfn_to_page(mfn);
     owner = page_get_owner_and_reference(page);
     if ( !owner )
-    {
-        rcu_unlock_domain(d);
         return -EPERM;
-    }
 
     if ( d != owner )
     {
@@ -3647,7 +3639,6 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
         if ( IS_ERR_OR_NULL(act) )
         {
             grant_read_unlock(owner->grant_table);
-            rcu_unlock_domain(d);
             put_page(page);
             return act ? PTR_ERR(act) : 1;
         }
@@ -3691,7 +3682,6 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
 
     unmap_domain_page(v);
     put_page(page);
-    rcu_unlock_domain(d);
 
     return ret;
 }
Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH 3/6] xen/gnttab: Remove explicit rcu_lock_current_domain()

Quoted 4 lines · Andrew Cooper

rcu_lock_current_domain() is always a nop, while rcu_unlock_domain() is not.

GNTTAB ops swap_grant_ref and cache_flush are the only two paths in Xen which explicitly RCU lock themselves. Drop this behaviour.

Arm's do_platform_op() has similar logic. From the description I infer you consider this bad behavior. It's not becoming quite clear why, though. Plus if it was bad behavior, why would we keep rcu_lock_current_domain() around at all?

Jan

#5 lore raw

PATCH 4/6 xen/gnttab: Document how _cache_flush() works

In reply to Andrew Cooper

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Baptiste Le Duc <[email protected]>

---
 xen/common/grant_table.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/grant_table.c +9 -0
index f4ec7524140a..4cc23f9231e6 100644
@@ -3598,6 +3598,15 @@ gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop,
 }
 
 #ifdef CONFIG_HAS_GRANT_CACHE_FLUSH
+/*
+ * Issue a cache flush operation on memory owned by the domain, or a mapped
+ * grant from another domain.  The caller must pass in cur_ref = 0 initially,
+ * and preserve the value across continuations.
+ *
+ * Returns: 0 for success
+ *          1 for a continuation while searching the mapped grants
+ *     -errno for error
+ */
 static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
 {
     struct domain *d, *owner;
Signature · 1 line
2.39.5
#7 lore raw

PATCH 5/6 xen/gnttab: Fold error handling in _cache_flush()

In reply to Andrew Cooper

No functional change.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Baptiste Le Duc <[email protected]>

---
 xen/common/grant_table.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
xen/common/grant_table.c +5 -4
index 4cc23f9231e6..d453c38556fc 100644
@@ -3647,9 +3647,8 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
         act = grant_map_exists(d, owner->grant_table, mfn, cur_ref);
         if ( IS_ERR_OR_NULL(act) )
         {
-            grant_read_unlock(owner->grant_table);
-            put_page(page);
-            return act ? PTR_ERR(act) : 1;
+            ret = act ? PTR_ERR(act) : 1;
+            goto gnt_unlock;
         }
 
         /*
@@ -3683,13 +3682,15 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
     else
         ret = 0;
 
+    unmap_domain_page(v);
+
     if ( d != owner )
     {
         active_entry_release(act);
+    gnt_unlock:
         grant_read_unlock(owner->grant_table);
     }
 
-    unmap_domain_page(v);
     put_page(page);
 
     return ret;
Signature · 1 line
2.39.5
Acked-by on v1 5/6
#8 lore raw

Re: [PATCH 5/6] xen/gnttab: Fold error handling in _cache_flush()

Quoted 3 lines · Andrew Cooper

No functional change.

xen/common/grant_table.c static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref) · line 3689

Quoted 9 lines
xen/common/grant_table.c +2 -0
@@ -3683,13 +3682,15 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
     else
         ret = 0;
 
+    unmap_domain_page(v);
+
     if ( d != owner )
     {
         active_entry_release(act);

I was first meaning to complain about the unnecessary new lock nesting here, but the earlier map_domain_page() also is issued with the active entry lock held. You move things to a more consistent pattern this way, which is good. Yet at the same time avoiding that nesting might also be a good idea. Something for a later change, I guess.

Jan

Quoted 8 lines
xen/common/grant_table.c +1 -1
+    gnt_unlock:
         grant_read_unlock(owner->grant_table);
     }
 
-    unmap_domain_page(v);
     put_page(page);
 
     return ret;
#9 lore raw

Re: [PATCH 5/6] xen/gnttab: Fold error handling in _cache_flush()

Quoted 5 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper

No functional change.

Thanks.

Quoted 5 lines
Quoted 5 lines
xen/common/grant_table.c +0 -0
@@ -3683,13 +3682,15 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
     else
         ret = 0;
 
Quoted 10 lines

xen/common/grant_table.c static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref) · line 3689

Quoted 5 lines
xen/common/grant_table.c +2 -0
+    unmap_domain_page(v);
+
     if ( d != owner )
     {
         active_entry_release(act);

I was first meaning to complain about the unnecessary new lock nesting here, but the earlier map_domain_page() also is issued with the active entry lock held. You move things to a more consistent pattern this way, which is good. Yet at the same time avoiding that nesting might also be a good idea. Something for a later change, I guess.

I don't see how you'd manage that.

We do have the proposed MFN in our hands, but until we've confirmed there's a valid map for it, it's an arbitrary piece of memory belonging to an arbitrary other domain.

With an ASI hat on, we absolutely mustn't speculatively map it.

~Andrew

#10 lore raw

PATCH 6/6 treewide: Make {invalidate/clean}_dcahce_va_range() void

In reply to Andrew Cooper

Precisely two functions - RISC-V's flush_page_to_ram() and ARM's _cache_flush() - check the return value of these functions, yet all implementations return success, and ARM documents this as a requirement.

Make them void and simply the callers which care.

No functional change.

---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Baptiste Le Duc <[email protected]>

---
 xen/arch/arm/include/asm/page.h   | 20 +++++++-------------
 xen/arch/riscv/include/asm/page.h | 10 +++-------
 xen/arch/riscv/mm.c               |  4 +---
 xen/common/grant_table.c          | 11 +++++------
 4 files changed, 16 insertions(+), 29 deletions(-)
xen/arch/arm/include/asm/page.h +7 -13
index 6da4745736a2..b719c2ed4c12 100644
@@ -165,13 +165,13 @@ static inline size_t read_dcache_line_bytes(void)
  * if 'range' is large enough we might want to use model-specific
  * full-cache flushes. */
 
-static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
+static inline void invalidate_dcache_va_range(const void *p, unsigned long size)
 {
     size_t cacheline_mask = dcache_line_bytes - 1;
     unsigned long partial_head;
 
     if ( !size )
-        return 0;
+        return;
 
     /* Passing a region that wraps around is illegal */
     ASSERT(((uintptr_t)p + size - 1) >= (uintptr_t)p);
@@ -208,17 +208,15 @@ static inline int invalidate_dcache_va_range(const void *p, unsigned long size)
 
  done:
     dsb(sy);           /* So we know the flushes happen before continuing */
-
-    return 0;
 }
 
-static inline int clean_dcache_va_range(const void *p, unsigned long size)
+static inline void clean_dcache_va_range(const void *p, unsigned long size)
 {
     size_t cacheline_mask = dcache_line_bytes - 1;
     unsigned long idx = 0;
 
     if ( !size )
-        return 0;
+        return;
 
     /* Passing a region that wraps around is illegal */
     ASSERT(((uintptr_t)p + size - 1) >= (uintptr_t)p);
@@ -231,18 +229,16 @@ static inline int clean_dcache_va_range(const void *p, unsigned long size)
             idx += dcache_line_bytes, size -= dcache_line_bytes )
         asm_inline volatile ( __clean_dcache_one(0) : : "r" (p + idx) );
     dsb(sy);           /* So we know the flushes happen before continuing */
-    /* ARM callers assume that dcache_* functions cannot fail. */
-    return 0;
 }
 
-static inline int clean_and_invalidate_dcache_va_range
-    (const void *p, unsigned long size)
+static inline void
+clean_and_invalidate_dcache_va_range(const void *p, unsigned long size)
 {
     size_t cacheline_mask = dcache_line_bytes - 1;
     unsigned long idx = 0;
 
     if ( !size )
-        return 0;
+        return;
 
     /* Passing a region that wraps around is illegal */
     ASSERT(((uintptr_t)p + size - 1) >= (uintptr_t)p);
@@ -255,8 +251,6 @@ static inline int clean_and_invalidate_dcache_va_range
             idx += dcache_line_bytes, size -= dcache_line_bytes )
         asm volatile (__clean_and_invalidate_dcache_one(0) : : "r" (p + idx));
     dsb(sy);         /* So we know the flushes happen before continuing */
-    /* ARM callers assume that dcache_* functions cannot fail. */
-    return 0;
 }
 
 /* Macros for flushing a single small item.  The predicate is always
xen/arch/riscv/include/asm/page.h +3 -7
index f9e59e61de30..770410085fd9 100644
@@ -196,23 +196,19 @@ static inline bool pte_is_superpage(pte_t p, unsigned int level)
     return (level > 0) && pte_is_mapping(p);
 }
 
-static inline int clean_and_invalidate_dcache_va_range(const void *p,
-                                                       unsigned long size)
+static inline void
+clean_and_invalidate_dcache_va_range(const void *p, unsigned long size)
 {
 #ifndef CONFIG_QEMU_PLATFORM
 # error "should clean_and_invalidate_dcache_va_range() be updated?"
 #endif
-
-    return 0;
 }
 
-static inline int clean_dcache_va_range(const void *p, unsigned long size)
+static inline void clean_dcache_va_range(const void *p, unsigned long size)
 {
 #ifndef CONFIG_QEMU_PLATFORM
 # error "should clean_dcache_va_range() be updated?"
 #endif
-
-    return 0;
 }
 
 static inline void invalidate_icache(void)
xen/arch/riscv/mm.c +1 -3
index 6f7dcd10b2c6..78f23392cc35 100644
@@ -618,9 +618,7 @@ void flush_page_to_ram(unsigned long mfn, bool sync_icache)
 {
     const void *v = map_domain_page(_mfn(mfn));
 
-    if ( clean_and_invalidate_dcache_va_range(v, PAGE_SIZE) )
-        BUG();
-
+    clean_and_invalidate_dcache_va_range(v, PAGE_SIZE);
     unmap_domain_page(v);
 
     if ( sync_icache )
xen/common/grant_table.c +5 -6
index d453c38556fc..eebf8850d062 100644
@@ -3673,14 +3673,13 @@ static int _cache_flush(gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
     v = map_domain_page(mfn);
     v += cflush->offset;
 
+    ret = 0;
     if ( (cflush->op & GNTTAB_CACHE_INVAL) && (cflush->op & GNTTAB_CACHE_CLEAN) )
-        ret = clean_and_invalidate_dcache_va_range(v, cflush->length);
+        clean_and_invalidate_dcache_va_range(v, cflush->length);
     else if ( cflush->op & GNTTAB_CACHE_INVAL )
-        ret = invalidate_dcache_va_range(v, cflush->length);
-    else if ( cflush->op & GNTTAB_CACHE_CLEAN )
-        ret = clean_dcache_va_range(v, cflush->length);
-    else
-        ret = 0;
+        invalidate_dcache_va_range(v, cflush->length);
+    else /* cflush->op & GNTTAB_CACHE_CLEAN */
+        clean_dcache_va_range(v, cflush->length);
 
     unmap_domain_page(v);
 
Signature · 1 line
2.39.5
Reviewed-by on v1 6/6
#11 lore raw

Re: [PATCH 6/6] treewide: Make {invalidate/clean}_dcahce_va_range() void

Quoted 9 lines · Andrew Cooper

Precisely two functions - RISC-V's flush_page_to_ram() and ARM's _cache_flush() - check the return value of these functions, yet all implementations return success, and ARM documents this as a requirement.

Make them void and simply the callers which care.

No functional change.

Reviewed-by on v1 6/6
#12 lore raw

Re: [PATCH 6/6] treewide: Make {invalidate/clean}_dcahce_va_range() void

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

Precisely two functions - RISC-V's flush_page_to_ram() and ARM's _cache_flush() - check the return value of these functions, yet all implementations return success, and ARM documents this as a requirement.

Make them void and simply the callers which care.

No functional change.

~Michal