Skip to contentSkip to conversation

Fix Arm off-by-one bugs

PATCH In review Michal Orzel 7 participants 21 messages Apr 9 – Apr 10

lore
Share & tools
lore
#0 lore raw

cover Fix Arm off-by-one bugs

Adding unrelated fix as the third patch that I found while preparing 1/3 for ease of merging.

Michal Orzel (3):
xen/arm: Fix off-by-one in iomem_deny_access() calls xen/common: llc-coloring: Fix off-by-one in parse_color_config() xen/arm: gic-v3: Use vsize instead of csize for virtual CPU interface

 xen/arch/arm/acpi/domain_build.c |  2 +-
 xen/arch/arm/gic-v2.c            |  8 ++++----
 xen/arch/arm/gic-v3-its.c        |  2 +-
 xen/arch/arm/gic-v3.c            | 10 +++++-----
 xen/common/llc-coloring.c        |  2 +-
 5 files changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 xen/arm: Fix off-by-one in iomem_deny_access() calls

iomem_deny_access() wraps rangeset_remove_range() which takes inclusive endpoints. All call sites in the GIC and ACPI code pass 'mfn + nr' (or 'mfn + 1' for single-page regions) as the end parameter, which causes one extra page beyond each region to be denied.

For single-page regions, use 'mfn' as the end (denying exactly one page). For all multi-page regions, use 'mfn + nr - 1'.

This matches the correct pattern used elsewhere, e.g. in device.c.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")
Fixes 66158be465 ("ARM: ITS: Deny hardware domain access to ITS")
Fixes 97e9875646 ("arm/acpi: Permit MMIO access of Xen unused devices for Dom0")
---
 xen/arch/arm/acpi/domain_build.c | 2 +-
 xen/arch/arm/gic-v2.c            | 8 ++++----
 xen/arch/arm/gic-v3-its.c        | 2 +-
 xen/arch/arm/gic-v3.c            | 8 ++++----
 4 files changed, 10 insertions(+), 10 deletions(-)
xen/arch/arm/acpi/domain_build.c +1 -1
index 5a117001ef11..249d899c3337 100644
@@ -48,7 +48,7 @@ static int __init acpi_iomem_deny_access(struct domain *d)
     {
         mfn = spcr->serial_port.address >> PAGE_SHIFT;
         /* Deny MMIO access for UART */
-        rc = iomem_deny_access(d, mfn, mfn + 1);
+        rc = iomem_deny_access(d, mfn, mfn);
         if ( rc )
             return rc;
     }
xen/arch/arm/gic-v2.c +4 -4
index b23e72a3d05d..014f9559673b 100644
@@ -1079,23 +1079,23 @@ static int gicv2_iomem_deny_access(struct domain *d)
     unsigned long mfn, nr;
 
     mfn = dbase >> PAGE_SHIFT;
-    rc = iomem_deny_access(d, mfn, mfn + 1);
+    rc = iomem_deny_access(d, mfn, mfn);
     if ( rc )
         return rc;
 
     mfn = hbase >> PAGE_SHIFT;
-    rc = iomem_deny_access(d, mfn, mfn + 1);
+    rc = iomem_deny_access(d, mfn, mfn);
     if ( rc )
         return rc;
 
     mfn = cbase >> PAGE_SHIFT;
     nr = DIV_ROUND_UP(csize, PAGE_SIZE);
-    rc = iomem_deny_access(d, mfn, mfn + nr);
+    rc = iomem_deny_access(d, mfn, mfn + nr - 1);
     if ( rc )
         return rc;
 
     mfn = vbase >> PAGE_SHIFT;
-    return iomem_deny_access(d, mfn, mfn + nr);
+    return iomem_deny_access(d, mfn, mfn + nr - 1);
 }
 
 #ifdef CONFIG_ACPI
xen/arch/arm/gic-v3-its.c +1 -1
index 9ba068c46fcb..e38aa8711744 100644
@@ -1009,7 +1009,7 @@ int gicv3_its_deny_access(struct domain *d)
     {
         mfn = paddr_to_pfn(its_data->addr);
         nr = PFN_UP(its_data->size);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
         if ( rc )
         {
             printk("iomem_deny_access failed for %lx:%lx \r\n", mfn, nr);
xen/arch/arm/gic-v3.c +4 -4
index bc07f97c16ab..b3e104ea4ad0 100644
@@ -1602,7 +1602,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
 
     mfn = dbase >> PAGE_SHIFT;
     nr = PFN_UP(SZ_64K);
-    rc = iomem_deny_access(d, mfn, mfn + nr);
+    rc = iomem_deny_access(d, mfn, mfn + nr - 1);
     if ( rc )
         return rc;
 
@@ -1614,7 +1614,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
     {
         mfn = gicv3.rdist_regions[i].base >> PAGE_SHIFT;
         nr = PFN_UP(gicv3.rdist_regions[i].size);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
         if ( rc )
             return rc;
     }
@@ -1623,7 +1623,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
     {
         mfn = cbase >> PAGE_SHIFT;
         nr = PFN_UP(csize);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
         if ( rc )
             return rc;
     }
@@ -1632,7 +1632,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
     {
         mfn = vbase >> PAGE_SHIFT;
         nr = PFN_UP(csize);
-        return iomem_deny_access(d, mfn, mfn + nr);
+        return iomem_deny_access(d, mfn, mfn + nr - 1);
     }
 
     return 0;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] xen/arm: Fix off-by-one in iomem_deny_access() calls

Hi Michal,

Apologies if my review is weird, I have been looking into too much of safety stuff.

Quoted 7 lines · Michal Orzel

iomem_deny_access() wraps rangeset_remove_range() which takes inclusive endpoints. All call sites in the GIC and ACPI code pass 'mfn + nr' (or 'mfn + 1' for single-page regions) as the end parameter, which causes one extra page beyond each region to be denied.

For single-page regions, use 'mfn' as the end (denying exactly one page). For all multi-page regions, use 'mfn + nr - 1'.

Just reading this and the change below, it seems that the issue was caught while doing some boundary value analysis. In this specific case, it seems the boundary values were set incorrectly.

Can you explain a bit more (the boundary/edge cases) in the commit message and give some reference to test (can be even a different repo or something) on how you caught this and verified it to be correct ?

We can keep this test somewhere (and tag it to the commit) even if such tests does not make sense to be upstreamed.

- Ayan

Quoted 112 lines

This matches the correct pattern used elsewhere, e.g. in device.c.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")
Fixes 66158be465 ("ARM: ITS: Deny hardware domain access to ITS")
Fixes 97e9875646 ("arm/acpi: Permit MMIO access of Xen unused devices for Dom0")
---
  xen/arch/arm/acpi/domain_build.c | 2 +-
  xen/arch/arm/gic-v2.c            | 8 ++++----
  xen/arch/arm/gic-v3-its.c        | 2 +-
  xen/arch/arm/gic-v3.c            | 8 ++++----
  4 files changed, 10 insertions(+), 10 deletions(-)
xen/arch/arm/acpi/domain_build.c +1 -1
index 5a117001ef11..249d899c3337 100644
@@ -48,7 +48,7 @@ static int __init acpi_iomem_deny_access(struct domain *d)
      {
          mfn = spcr->serial_port.address >> PAGE_SHIFT;
          /* Deny MMIO access for UART */
-        rc = iomem_deny_access(d, mfn, mfn + 1);
+        rc = iomem_deny_access(d, mfn, mfn);
          if ( rc )
              return rc;
      }
xen/arch/arm/gic-v2.c +4 -4
index b23e72a3d05d..014f9559673b 100644
@@ -1079,23 +1079,23 @@ static int gicv2_iomem_deny_access(struct domain *d)
      unsigned long mfn, nr;
 
      mfn = dbase >> PAGE_SHIFT;
-    rc = iomem_deny_access(d, mfn, mfn + 1);
+    rc = iomem_deny_access(d, mfn, mfn);
      if ( rc )
          return rc;
 
      mfn = hbase >> PAGE_SHIFT;
-    rc = iomem_deny_access(d, mfn, mfn + 1);
+    rc = iomem_deny_access(d, mfn, mfn);
      if ( rc )
          return rc;
 
      mfn = cbase >> PAGE_SHIFT;
      nr = DIV_ROUND_UP(csize, PAGE_SIZE);
-    rc = iomem_deny_access(d, mfn, mfn + nr);
+    rc = iomem_deny_access(d, mfn, mfn + nr - 1);
      if ( rc )
          return rc;
 
      mfn = vbase >> PAGE_SHIFT;
-    return iomem_deny_access(d, mfn, mfn + nr);
+    return iomem_deny_access(d, mfn, mfn + nr - 1);
  }
 
  #ifdef CONFIG_ACPI
xen/arch/arm/gic-v3-its.c +1 -1
index 9ba068c46fcb..e38aa8711744 100644
@@ -1009,7 +1009,7 @@ int gicv3_its_deny_access(struct domain *d)
      {
          mfn = paddr_to_pfn(its_data->addr);
          nr = PFN_UP(its_data->size);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
          if ( rc )
          {
              printk("iomem_deny_access failed for %lx:%lx \r\n", mfn, nr);
xen/arch/arm/gic-v3.c +4 -4
index bc07f97c16ab..b3e104ea4ad0 100644
@@ -1602,7 +1602,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
 
      mfn = dbase >> PAGE_SHIFT;
      nr = PFN_UP(SZ_64K);
-    rc = iomem_deny_access(d, mfn, mfn + nr);
+    rc = iomem_deny_access(d, mfn, mfn + nr - 1);
      if ( rc )
          return rc;
 
@@ -1614,7 +1614,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
      {
          mfn = gicv3.rdist_regions[i].base >> PAGE_SHIFT;
          nr = PFN_UP(gicv3.rdist_regions[i].size);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
          if ( rc )
              return rc;
      }
@@ -1623,7 +1623,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
      {
          mfn = cbase >> PAGE_SHIFT;
          nr = PFN_UP(csize);
-        rc = iomem_deny_access(d, mfn, mfn + nr);
+        rc = iomem_deny_access(d, mfn, mfn + nr - 1);
          if ( rc )
              return rc;
      }
@@ -1632,7 +1632,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
      {
          mfn = vbase >> PAGE_SHIFT;
          nr = PFN_UP(csize);
-        return iomem_deny_access(d, mfn, mfn + nr);
+        return iomem_deny_access(d, mfn, mfn + nr - 1);
      }
 
      return 0;
#3 lore raw

Re: [PATCH 1/3] xen/arm: Fix off-by-one in iomem_deny_access() calls

Quoted 4 lines · Ayan Kumar

Hi Michal,

Apologies if my review is weird, I have been looking into too much of safety stuff.

Yes, I know :)

Quoted 14 lines · Michal Orzel
Quoted 7 lines

iomem_deny_access() wraps rangeset_remove_range() which takes inclusive endpoints. All call sites in the GIC and ACPI code pass 'mfn + nr' (or 'mfn + 1' for single-page regions) as the end parameter, which causes one extra page beyond each region to be denied.

For single-page regions, use 'mfn' as the end (denying exactly one page). For all multi-page regions, use 'mfn + nr - 1'.

Just reading this and the change below, it seems that the issue was caught while doing some boundary value analysis. In this specific case, it seems the boundary values were set incorrectly.

Quoted 6 lines

Can you explain a bit more (the boundary/edge cases) in the commit message and give some reference to test (can be even a different repo or something) on how you caught this and verified it to be correct ?

We can keep this test somewhere (and tag it to the commit) even if such tests does not make sense to be upstreamed.

As much as it looks like an issue found during BVA, the truth is I found it by accident when debugging some issue where rangesets were involved. As always, whenever I see a function with parameters {start, end}, I need to dive deeper to verify whether end is inclusive or not. I also check the call-sites to validate my observations and that's how I found these bugs.

~Michal

Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] xen/arm: Fix off-by-one in iomem_deny_access() calls

In reply to Michal Orzel

Hi Michal,

Quoted 10 lines · Michal Orzel

iomem_deny_access() wraps rangeset_remove_range() which takes inclusive endpoints. All call sites in the GIC and ACPI code pass 'mfn + nr' (or 'mfn + 1' for single-page regions) as the end parameter, which causes one extra page beyond each region to be denied.

For single-page regions, use 'mfn' as the end (denying exactly one page). For all multi-page regions, use 'mfn + nr - 1'.

Quoted 8 lines

This matches the correct pattern used elsewhere, e.g. in device.c.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")
Fixes 66158be465 ("ARM: ITS: Deny hardware domain access to ITS")
Fixes 97e9875646 ("arm/acpi: Permit MMIO access of Xen unused devices for Dom0")

---

This looks ok to me.

Cheers,
Luca

Reviewed-by
#5 lore raw

Re: [PATCH 1/3] xen/arm: Fix off-by-one in iomem_deny_access() calls

Quoted 21 lines · Luca Fancellu

Hi Michal,

Quoted 10 lines · Michal Orzel

iomem_deny_access() wraps rangeset_remove_range() which takes inclusive endpoints. All call sites in the GIC and ACPI code pass 'mfn + nr' (or 'mfn + 1' for single-page regions) as the end parameter, which causes one extra page beyond each region to be denied.

For single-page regions, use 'mfn' as the end (denying exactly one page). For all multi-page regions, use 'mfn + nr - 1'.

Quoted 8 lines

This matches the correct pattern used elsewhere, e.g. in device.c.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")
Fixes 66158be465 ("ARM: ITS: Deny hardware domain access to ITS")
Fixes 97e9875646 ("arm/acpi: Permit MMIO access of Xen unused devices for Dom0")

---

Quoted 3 lines

This looks ok to me.

#6 lore raw

PATCH 2/3 xen/common: llc-coloring: Fix off-by-one in parse_color_config()

In reply to Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")
---
 xen/common/llc-coloring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/llc-coloring.c +1 -1
index eb7c72b24023..30c1594dac9f 100644
@@ -78,7 +78,7 @@ static int __init parse_color_config(const char *buf, unsigned int colors[],
 
         if ( end >= NR_LLC_COLORS || start > end ||
              (end - start) >= (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start + 1)) >= max_num_colors )
+             (*num_colors + (end - start + 1)) > max_num_colors )
             return -EINVAL;
 
         /* Colors are range checked in check_colors() */
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 20 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")
---
 xen/common/llc-coloring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/llc-coloring.c +0 -0
index eb7c72b24023..30c1594dac9f 100644

xen/common/llc-coloring.c static int __init parse_color_config(const char *buf, unsigned int colors[], · line 84

Quoted 8 lines
xen/common/llc-coloring.c +1 -1
@@ -78,7 +78,7 @@ static int __init parse_color_config(const char *buf, unsigned int colors[],
 
         if ( end >= NR_LLC_COLORS || start > end ||
              (end - start) >= (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start + 1)) >= max_num_colors )
+             (*num_colors + (end - start + 1)) > max_num_colors )
             return -EINVAL;
 
         /* Colors are range checked in check_colors() */

This boundary was changed by
https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=cba8a584de171c8c4510709c2edc9f1cf86b21ab
because it was off-by-one.

Are you saying that the analysis in that patch was wrong?

~Andrew

#8 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 28 lines · Andrew Cooper
Quoted 27 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")
---
 xen/common/llc-coloring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/llc-coloring.c +1 -1
index eb7c72b24023..30c1594dac9f 100644
@@ -78,7 +78,7 @@ static int __init parse_color_config(const char *buf, unsigned int colors[],
 
         if ( end >= NR_LLC_COLORS || start > end ||
              (end - start) >= (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start + 1)) >= max_num_colors )
+             (*num_colors + (end - start + 1)) > max_num_colors )
             return -EINVAL;
 
Quoted 7 lines

xen/common/llc-coloring.c static int __init parse_color_config(const char *buf, unsigned int colors[], · line 84

Quoted 1 line
xen/common/llc-coloring.c +0 -0
         /* Colors are range checked in check_colors() */

This boundary was changed by
https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=cba8a584de171c8c4510709c2edc9f1cf86b21ab
because it was off-by-one.

Are you saying that the analysis in that patch was wrong?

I examined the scenario that is a default for dom0 i.e. dom0 gets all the colors by default. This is equivalent to setting dom0-llc-colors=0-15. If I set this, I will get a message:
(XEN) parameter "dom0-llc-colors" has invalid value "0-15", rc=-22!

I admit that I added wrong example in commit msg.

~Michal

Quoted 2 lines

~Andrew

#9 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

In reply to Michal Orzel
Quoted 3 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

This seems related to BVA as well.

Quoted 3 lines

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Why does it fail ? Is it because the max number of colors can be 15.

- Ayan

Reviewed-by on v1 2/3
#10 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

In reply to Michal Orzel

Hi Michal,

Quoted 6 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

Quoted 8 lines

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Cheers,
Luca

#11 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Jan

#12 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 22 lines · Jan Beulich
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Quoted 7 lines

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Jan

Oh, no I didn’t see that! Thanks for pointing that out, I will have a closer look.

Cheers,
Luca

#13 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Hi Jan,

Quoted 30 lines · Luca Fancellu
Quoted 29 lines · Jan Beulich
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Quoted 3 lines
Quoted 1 line

Jan

Oh, no I didn’t see that! Thanks for pointing that out, I will have a closer look.

I had a closer look, I feel that the patch is ok and commit cba8a584de171c8c4510709c2edc9f1cf86b21ab was missing this corner case.

Let’s say max_num_colors = 8 (array capacity), *num_colors = 4 so we stored already 4 entries and the next parsed range gives start = 4, end = 7:

(*num_colors + (end - start + 1)) >= max_num_colors will compute as
(4 + (7 - 4 + 1)) >= 8 which will be
8 >= 8 that will be true and the input will be rejected, instead of being a valid entry.

Did I miss anything?

Cheers,
Luca

#14 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 26 lines · Luca Fancellu
Quoted 26 lines · Luca Fancellu
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Quoted 5 lines
Quoted 1 line

Oh, no I didn’t see that! Thanks for pointing that out, I will have a closer look.

I had a closer look, I feel that the patch is ok and commit cba8a584de171c8c4510709c2edc9f1cf86b21ab was missing this corner case.

If anything, that part of the change there was outright wrong (and hence, as said, a 2nd Fixes: tag [actually, see below, simply another one] is needed). With overflow excluded,

    (*num_colors + (end - start + 1)) > max_num_colors

is the same as

(*num_colors + (end - start)) >= max_num_colors

i.e. the state before that change, isn't it?

And yes, now that I look again I think I agree that I screwed up there. Yet then the (imo) better fix would be to undo that change, rather than switching from >= to > . That's one less calculation overall. Michal?

Jan

Quoted 12 lines

Let’s say max_num_colors = 8 (array capacity), *num_colors = 4 so we stored already 4 entries and the next parsed range gives start = 4, end = 7:

(*num_colors + (end - start + 1)) >= max_num_colors will compute as
(4 + (7 - 4 + 1)) >= 8 which will be
8 >= 8 that will be true and the input will be rejected, instead of being a valid entry.

Did I miss anything?

Cheers,
Luca

#15 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 41 lines · Jan Beulich
Quoted 26 lines · Luca Fancellu
Quoted 26 lines · Luca Fancellu
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Quoted 5 lines
Quoted 1 line

Oh, no I didn’t see that! Thanks for pointing that out, I will have a closer look.

I had a closer look, I feel that the patch is ok and commit cba8a584de171c8c4510709c2edc9f1cf86b21ab was missing this corner case.

If anything, that part of the change there was outright wrong (and hence, as said, a 2nd Fixes: tag [actually, see below, simply another one] is needed). With overflow excluded,

    (*num_colors + (end - start + 1)) > max_num_colors

is the same as

Quoted 7 lines
   (*num_colors + (end - start)) >= max_num_colors

i.e. the state before that change, isn't it?

And yes, now that I look again I think I agree that I screwed up there. Yet then the (imo) better fix would be to undo that change, rather than switching from >= to > . That's one less calculation overall. Michal?

Yes, I do agree. This patch can be modified to just do:

xen/common/llc-coloring.c +0 -0
index eb7c72b24023..6dc614739a98 100644
@@ -78,7 +78,7 @@ static int __init parse_color_config(const char *buf, unsigned

int colors[],

         if ( end >= NR_LLC_COLORS || start > end ||
              (end - start) >= (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start + 1)) >= max_num_colors )
+             (*num_colors + (end - start)) >= max_num_colors )
             return -EINVAL;

         /* Colors are range checked in check_colors() */

I'll do that later on.

~Michal

#16 lore raw

Re: [PATCH 2/3] xen/common: llc-coloring: Fix off-by-one in parse_color_config()

Quoted 72 lines · Michal
Quoted 41 lines · Jan Beulich
Quoted 26 lines · Luca Fancellu
Quoted 26 lines · Luca Fancellu
Quoted 24 lines · Jan Beulich
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Michal Orzel

The check uses >= to compare the total number of colors against max_num_colors (which is ARRAY_SIZE of the colors array). This incorrectly rejects input that would exactly fill the array.

For example, with NR_LLC_COLORS=16, specifying 1 color for Xen and 15 for dom0 would fail.

Quoted 7 lines
Quoted 5 lines

Change >= to > so that exactly filling the array is permitted.

Fixes 95ef5ddf8a ("xen/arm: add Dom0 cache coloring support")

---

Did you see Andrew's reply? If that earlier (recent) commit was wrong, I think a 2nd Fixes: tag may be needed here. For now I can't help the impression though that there might have been a re-basing mistake, where that re-base may have wanted to result in this patch dissolving into nothing. Yet of course I'm all ears to learn otherwise.

Quoted 5 lines
Quoted 1 line

Oh, no I didn’t see that! Thanks for pointing that out, I will have a closer look.

I had a closer look, I feel that the patch is ok and commit cba8a584de171c8c4510709c2edc9f1cf86b21ab was missing this corner case.

If anything, that part of the change there was outright wrong (and hence, as said, a 2nd Fixes: tag [actually, see below, simply another one] is needed). With overflow excluded,

   (*num_colors + (end - start + 1)) > max_num_colors

is the same as

Quoted 7 lines

(*num_colors + (end - start)) >= max_num_colors

i.e. the state before that change, isn't it?

And yes, now that I look again I think I agree that I screwed up there. Yet then the (imo) better fix would be to undo that change, rather than switching from >= to > . That's one less calculation overall. Michal?

Yes, I do agree. This patch can be modified to just do:

xen/common/llc-coloring.c +1 -1
index eb7c72b24023..6dc614739a98 100644
@@ -78,7 +78,7 @@ static int __init parse_color_config(const char *buf, unsigned
 int colors[],
 
        if ( end >= NR_LLC_COLORS || start > end ||
             (end - start) >= (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start + 1)) >= max_num_colors )
+             (*num_colors + (end - start)) >= max_num_colors )
            return -EINVAL;
 
        /* Colors are range checked in check_colors() */
Quoted 4 lines

I'll do that later on.

~Michal

Feel free to keep my R-by for this change.

Cheers,
Luca

#17 lore raw

PATCH 3/3 xen/arm: gic-v3: Use vsize instead of csize for virtual CPU interface

In reply to Michal Orzel

gicv3_iomem_deny_access() incorrectly uses csize (physical CPU interface size) instead of vsize (virtual CPU interface size) when calculating the number of pages to deny for the virtual CPU interface region at vbase.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")
---
 xen/arch/arm/gic-v3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/gic-v3.c +1 -1
index b3e104ea4ad0..5641ee870f23 100644
@@ -1631,7 +1631,7 @@ static int gicv3_iomem_deny_access(struct domain *d)
     if ( vbase != INVALID_PADDR )
     {
         mfn = vbase >> PAGE_SHIFT;
-        nr = PFN_UP(csize);
+        nr = PFN_UP(vsize);
         return iomem_deny_access(d, mfn, mfn + nr - 1);
     }
 
Signature · 1 line
2.43.0
Reviewed-by Ayan Kumar Halder on v1 3/3
#18 lore raw

Re: [PATCH 3/3] xen/arm: gic-v3: Use vsize instead of csize for virtual CPU interface

Quoted 4 lines · Michal Orzel

gicv3_iomem_deny_access() incorrectly uses csize (physical CPU interface size) instead of vsize (virtual CPU interface size) when calculating the number of pages to deny for the virtual CPU interface region at vbase.

good catch

Quoted 3 lines
Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")

- Ayan

Reviewed-by on v1 3/3
#19 lore raw

Re: [PATCH 3/3] xen/arm: gic-v3: Use vsize instead of csize for virtual CPU interface

In reply to Michal Orzel

Hi Michal,

Quoted 10 lines · Michal Orzel

gicv3_iomem_deny_access() incorrectly uses csize (physical CPU interface size) instead of vsize (virtual CPU interface size) when calculating the number of pages to deny for the virtual CPU interface region at vbase.

Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")

---

Cheers,
Luca

#20 lore raw

Re: [PATCH 3/3] xen/arm: gic-v3: Use vsize instead of csize for virtual CPU interface

Quoted 9 lines · Luca Fancellu

Hi Michal,

Quoted 7 lines · Michal Orzel

gicv3_iomem_deny_access() incorrectly uses csize (physical CPU interface size) instead of vsize (virtual CPU interface size) when calculating the number of pages to deny for the virtual CPU interface region at vbase.

Quoted 5 lines
Quoted 3 lines
Fixes 8300b3377e ("arm/gic: Add a new callback to deny Dom0 access to GIC regions")

---