[PATCH v5] xen/arm: validate non-common page-table ranges

Gabriel Quintáns Souto posted 1 patch 14 hours ago
xen/arch/arm/mmu/pt.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
[PATCH v5] xen/arm: validate non-common page-table ranges
Posted by Gabriel Quintáns Souto 14 hours ago
On arm32, all the Xen mappings are common to all the CPUs, except for the
domheap mapping, which is per-CPU.

The xen_pt_update function assumed that only the common mappings would be
modified, but did not check this assumption.

Add a check to reject modifications to the domheap mapping.

Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
---
v5:
 - Moved the check back to pt.c under #ifdef CONFIG_ARM_32.
 - Only check the domheap area.
 - Added a comment explaining why only Arm32 needs it.

v4:
 - Rebased onto staging (v3 was diffed against v2).

v3:
 - Dropped accidentally tracked patch file from commit.

v2:
- Inverted polarity of xen_pt_range_is_non_common() as suggested.
- Moved xen_pt_range_is_common() implementation to arch-specific headers
(arm32 vs arm64) instead of keeping #ifdef CONFIG_ARM_32 in pt.c (tried
IS_ENABLED() for DCE, but ARM64 lacks definitions for
DOMHEAP_VIRT_START/TEMPORARY_AREA_ADDR, breaking the build).
- Added ASSERT_UNREACHABLE() on failure path in xen_pt_update().
---
 xen/arch/arm/mmu/pt.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/xen/arch/arm/mmu/pt.c b/xen/arch/arm/mmu/pt.c
index 81fe121..b463c85 100644
--- a/xen/arch/arm/mmu/pt.c
+++ b/xen/arch/arm/mmu/pt.c
@@ -13,6 +13,7 @@
 
 #include <asm/current.h>
 #include <asm/fixmap.h>
+#include <asm/mmu/layout.h>
 
 #ifdef NDEBUG
 static inline void
@@ -601,14 +602,21 @@ static int xen_pt_update(unsigned long virt,
     unsigned long vfn = virt >> PAGE_SHIFT;
     unsigned long left = nr_mfns;
 
+    const mfn_t root = maddr_to_mfn(READ_SYSREG64(TTBR0_EL2));
+
+#ifdef CONFIG_ARM_32
     /*
-     * For arm32, page-tables are different on each CPUs. Yet, they share
-     * some common mappings. It is assumed that only common mappings
-     * will be modified with this function.
-     *
-     * XXX: Add a check.
+     * Only Arm32 needs this check, as updating the domheap mapping here
+     * would only affect the current CPU. On Arm64, all the CPUs share the
+     * same page-tables.
      */
-    const mfn_t root = maddr_to_mfn(READ_SYSREG64(TTBR0_EL2));
+    if ( virt >= DOMHEAP_VIRT_START ||
+         nr_mfns * PAGE_SIZE > DOMHEAP_VIRT_START - virt )
+    {
+        ASSERT_UNREACHABLE();
+        return -EINVAL;
+    }
+#endif
 
     if ( flags_has_rwx(flags) )
     {
-- 
2.56.0


Re: [PATCH v5] xen/arm: validate non-common page-table ranges
Posted by Orzel, Michal 3 hours ago

On 25-Nov-26 22:06, Gabriel Quintáns Souto wrote:
> On arm32, all the Xen mappings are common to all the CPUs, except for the
> domheap mapping, which is per-CPU.
> 
> The xen_pt_update function assumed that only the common mappings would be
> modified, but did not check this assumption.
> 
> Add a check to reject modifications to the domheap mapping.
> 
> Signed-off-by: Gabriel Quintáns Souto <gabi.qs.mail@gmail.com>
> ---
> v5:
>  - Moved the check back to pt.c under #ifdef CONFIG_ARM_32.
>  - Only check the domheap area.
>  - Added a comment explaining why only Arm32 needs it.
> 
> v4:
>  - Rebased onto staging (v3 was diffed against v2).
> 
> v3:
>  - Dropped accidentally tracked patch file from commit.
> 
> v2:
> - Inverted polarity of xen_pt_range_is_non_common() as suggested.
> - Moved xen_pt_range_is_common() implementation to arch-specific headers
> (arm32 vs arm64) instead of keeping #ifdef CONFIG_ARM_32 in pt.c (tried
> IS_ENABLED() for DCE, but ARM64 lacks definitions for
> DOMHEAP_VIRT_START/TEMPORARY_AREA_ADDR, breaking the build).
> - Added ASSERT_UNREACHABLE() on failure path in xen_pt_update().
> ---
>  xen/arch/arm/mmu/pt.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/xen/arch/arm/mmu/pt.c b/xen/arch/arm/mmu/pt.c
> index 81fe121..b463c85 100644
> --- a/xen/arch/arm/mmu/pt.c
> +++ b/xen/arch/arm/mmu/pt.c
> @@ -13,6 +13,7 @@
>  
>  #include <asm/current.h>
>  #include <asm/fixmap.h>
> +#include <asm/mmu/layout.h>
This was supposed to be dropped.

>  
>  #ifdef NDEBUG
>  static inline void
> @@ -601,14 +602,21 @@ static int xen_pt_update(unsigned long virt,
>      unsigned long vfn = virt >> PAGE_SHIFT;
>      unsigned long left = nr_mfns;
>  
> +    const mfn_t root = maddr_to_mfn(READ_SYSREG64(TTBR0_EL2));
> +
> +#ifdef CONFIG_ARM_32
>      /*
> -     * For arm32, page-tables are different on each CPUs. Yet, they share
> -     * some common mappings. It is assumed that only common mappings
> -     * will be modified with this function.
> -     *
> -     * XXX: Add a check.
> +     * Only Arm32 needs this check, as updating the domheap mapping here
> +     * would only affect the current CPU. On Arm64, all the CPUs share the
> +     * same page-tables.
>       */
> -    const mfn_t root = maddr_to_mfn(READ_SYSREG64(TTBR0_EL2));
> +    if ( virt >= DOMHEAP_VIRT_START ||
> +         nr_mfns * PAGE_SIZE > DOMHEAP_VIRT_START - virt )
On arm32, nr_mfns * PAGE_SIZE could overflow.
nr_mfns > PFN_DOWN(DOMHEAP_VIRT_START - virt) fixes that.

I will fix those issues on commit. Thanks.

Reviewed-by: Michal Orzel <michal.orzel@amd.com>

~Michal