Skip to contentSkip to conversation

SMMUV3: STE lookup fixes

PATCH Applied Eric Auger 3 participants 9 messages Jul 7 – Jul 9

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

v3 cover SMMUV3: STE lookup fixes

The current code fails to sanitize SMMU_S_STRTAB_BASE_CFG LOG2SIZE and SPLIT which are used in the STE lookup. This could potentially lead to wrong shifts/masks in smmu_find_ste() and does not fully comply with the spec.

Also the series fixes different issues related to strtab base address alignment computations: off-by-one mask, unchecked span, missing L2ptr base address alignment. Those issues were not visible because the guest kernel does what it should but better comply with the spec.

Best Regards

Eric

This series is available at:
https://github.com/eauger/qemu/tree/smmu_sanitize_v3

v2 -> v3:
- Introduced STE_SIZE and L1STD_SIZE
- Just cap strtab_size to 64

Eric Auger (5):
hw/arm/smmuv3: Fix off-by-one bug in alignment strtab mask hw/arm/smmuv3: Sanitize SMMU_S_STRTAB_BASE_CFG.SPLIT hw/arm/smmuv3: Fix possible overflow in strtab_base computation hw/arm/smmuv3: Check L1STD.SPAN
hw/arm/smmuv3: Enforce alignment of L2Ptr according to the span

 hw/arm/smmuv3-internal.h |  3 +++
 hw/arm/smmuv3.c          | 32 ++++++++++++++++++++++++++------
 2 files changed, 29 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v3 1/5 hw/arm/smmuv3: Fix off-by-one bug in alignment strtab mask

The stream table base address needs to be aligned to its size.

With FMT == 0 (linear stream table), the table size is log2size * STE_SIZE (2^6). So the spec says the base address
must have ADDR[LOG2SIZE + 5:0] = 0.

With FMT == 1 (2 level stream table), the table size is (log2size - split) * L1STD_SIZE (2^3) So the spec days the effective base address is aligned by the SMMU to the larger of 64 bytes or the first-level table size:
ADDR[MAX(5, (LOG2SIZE - SPLIT - 1 + 3)):0] = 0.

MAKE_64BIT_MASK() second argument is a size and not a shift, so fix this off-by-one computation.

Subsequent patches will fix the risk of overflow in MAKE_64BIT_MASK()

---
 hw/arm/smmuv3-internal.h |  3 +++
 hw/arm/smmuv3.c          | 10 +++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/arm/smmuv3-internal.h +3 -0
index eb482c7000f..0819a4b2e8d 100644
@@ -359,6 +359,9 @@ void smmuv3_record_event(SMMUv3State *s, SMMUEventInfo *event);
 void smmuv3_propagate_event(SMMUv3State *s, Evt *evt);
 int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event);
 
+#define STE_SIZE 6
+#define L1STD_SIZE 3
+
 static inline int oas2bits(int oas_field)
 {
     switch (oas_field) {
hw/arm/smmuv3.c +5 -5
index 5e5a6a960c9..10c67645a57 100644
@@ -664,7 +664,7 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
 {
     dma_addr_t addr, strtab_base;
     uint32_t log2size;
-    int strtab_size_shift;
+    int strtab_size;
     int ret;
 
     trace_smmuv3_find_ste(sid, s->features, s->sid_split);
@@ -685,9 +685,9 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
          * Align strtab base address to table size. For this purpose, assume it
          * is not bounded by SMMU_IDR1_SIDSIZE.
          */
-        strtab_size_shift = MAX(5, (int)log2size - s->sid_split - 1 + 3);
+        strtab_size = MAX(6, (int)log2size - s->sid_split + L1STD_SIZE);
         strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK &
-                      ~MAKE_64BIT_MASK(0, strtab_size_shift);
+                      ~MAKE_64BIT_MASK(0, strtab_size);
         l1_ste_offset = sid >> s->sid_split;
         l2_ste_offset = sid & ((1 << s->sid_split) - 1);
         l1ptr = (dma_addr_t)(strtab_base + l1_ste_offset * sizeof(l1std));
@@ -729,9 +729,9 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
         }
         addr = l2ptr + l2_ste_offset * sizeof(*ste);
     } else {
-        strtab_size_shift = log2size + 5;
+        strtab_size = log2size + STE_SIZE;
         strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK &
-                      ~MAKE_64BIT_MASK(0, strtab_size_shift);
+                      ~MAKE_64BIT_MASK(0, strtab_size);
         addr = strtab_base + sid * sizeof(*ste);
     }
 
Signature · 1 line
2.53.0
Reviewed-by on v3 1/5
#2 lore raw

RE: [PATCH v3 1/5] hw/arm/smmuv3: Fix off-by-one bug in alignment strtab mask

Quoted 28 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Sent: 07 July 2026 09:50
To: [email protected]; [email protected]; qemu- [email protected]; [email protected]; [email protected]; [email protected]; Shameer Kolothum Thodi <[email protected]>; Nicolin Chen <[email protected]>; Nathan Chen <[email protected]>; [email protected]
Subject: [PATCH v3 1/5] hw/arm/smmuv3: Fix off-by-one bug in alignment strtab mask

External email: Use caution opening links or attachments

The stream table base address needs to be aligned to its size.

With FMT == 0 (linear stream table), the table size is log2size * STE_SIZE (2^6). So the spec says the base address
must have ADDR[LOG2SIZE + 5:0] = 0.

With FMT == 1 (2 level stream table), the table size is (log2size - split) * L1STD_SIZE (2^3) So the spec days the effective base address is aligned by the SMMU to the larger of 64 bytes or the first-level table size:
ADDR[MAX(5, (LOG2SIZE - SPLIT - 1 + 3)):0] = 0.

Quoted 6 lines

MAKE_64BIT_MASK() second argument is a size and not a shift, so fix this off-by-one computation.

Subsequent patches will fix the risk of overflow in MAKE_64BIT_MASK()

Thanks,
Shameer

author
#3 lore raw

PATCH v3 2/5 hw/arm/smmuv3: Sanitize SMMU_S_STRTAB_BASE_CFG.SPLIT

In reply to Eric Auger

Currently the guest value for the SPLIT field is not checked. Also the spec says that values different from 6, 8, 10, respectively meaning 4KB, 16kB and 64kB leaf tables are reserved and behave as 6.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3632

---
 hw/arm/smmuv3.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/arm/smmuv3.c +7 -0
index 10c67645a57..c2946d8d23c 100644
@@ -1688,6 +1688,13 @@ static MemTxResult smmu_writel(SMMUv3State *s, hwaddr offset,
         s->strtab_base_cfg = data;
         if (FIELD_EX32(data, STRTAB_BASE_CFG, FMT) == 1) {
             s->sid_split = FIELD_EX32(data, STRTAB_BASE_CFG, SPLIT);
+            if (s->sid_split != 6 && s->sid_split != 8 && s->sid_split != 10) {
+                /* Other values are reserved, behave as 6 */
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "Invalid STRTAB_BASE_CFG.SPLIT=%u, use 6 instead\n",
+                              s->sid_split);
+                s->sid_split = 6;
+            }
             s->features |= SMMU_FEATURE_2LVL_STE;
         }
         break;
Signature · 1 line
2.53.0
author
#4 lore raw

PATCH v3 3/5 hw/arm/smmuv3: Fix possible overflow in strtab_base computation

In reply to Eric Auger

In the linear stream table mode (FMT = 0), if the guest programs SMMU_STRTAB_BASE_CFG.LOG2SIZE (8 bits) to something bigger than 58, MAKE_64BIT_MASK() will overflow. So cap the mask length to 64. We still comply with the spec ADDR alignment computation:

ADDR[LOG2SIZE + 5:0] = 0.

In the 2 level stream table mode (FMT = 1), we don't have this risk anymore since SPLIT is sanitized and equals to 6 at minimum.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3632

---
 hw/arm/smmuv3.c | 1 +
 1 file changed, 1 insertion(+)
hw/arm/smmuv3.c +1 -0
index c2946d8d23c..3fd1dd7cf81 100644
@@ -730,6 +730,7 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
         addr = l2ptr + l2_ste_offset * sizeof(*ste);
     } else {
         strtab_size = log2size + STE_SIZE;
+        strtab_size = MIN(64, strtab_size);
         strtab_base = s->strtab_base & SMMU_BASE_ADDR_MASK &
                       ~MAKE_64BIT_MASK(0, strtab_size);
         addr = strtab_base + sid * sizeof(*ste);
Signature · 1 line
2.53.0
Reviewed-by on v3 3/5
#5 lore raw

RE: [PATCH v3 3/5] hw/arm/smmuv3: Fix possible overflow in strtab_base computation

Quoted 21 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Sent: 07 July 2026 09:50
To: [email protected]; [email protected]; qemu- [email protected]; [email protected]; [email protected]; [email protected]; Shameer Kolothum Thodi <[email protected]>; Nicolin Chen <[email protected]>; Nathan Chen <[email protected]>; [email protected]
Subject: [PATCH v3 3/5] hw/arm/smmuv3: Fix possible overflow in strtab_base computation

External email: Use caution opening links or attachments

In the linear stream table mode (FMT = 0), if the guest programs SMMU_STRTAB_BASE_CFG.LOG2SIZE (8 bits) to something bigger than 58, MAKE_64BIT_MASK() will overflow. So cap the mask length to 64. We still comply with the spec ADDR alignment computation:

Quoted 7 lines

ADDR[LOG2SIZE + 5:0] = 0.

In the 2 level stream table mode (FMT = 1), we don't have this risk anymore since SPLIT is sanitized and equals to 6 at minimum.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3632

Thanks,
Shameer

author
#6 lore raw

PATCH v3 4/5 hw/arm/smmuv3: Check L1STD.SPAN

In reply to Eric Auger

Span values above 11 are reserved and behave as 0.

Also span must be within the range of 0 to (SMMU_STRTAB_BASE_CFG.SPLIT + 1), ie. it must stay within the bounds of the stream table split point.

---
 hw/arm/smmuv3.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/arm/smmuv3.c +11 -1
index 3fd1dd7cf81..9008ba7acd9 100644
@@ -707,7 +707,7 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
 
         span = L1STD_SPAN(&l1std);
 
-        if (!span) {
+        if (!span || span > 11) {
             /* l2ptr is not valid */
             if (!event->inval_ste_allowed) {
                 qemu_log_mask(LOG_GUEST_ERROR,
@@ -716,6 +716,16 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
             event->type = SMMU_EVT_C_BAD_STREAMID;
             return -EINVAL;
         }
+
+        if (span > s->sid_split + 1) {
+            if (!event->inval_ste_allowed) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "invalid span (0x%x)\n", span);
+            }
+            event->type = SMMU_EVT_C_BAD_STREAMID;
+            return -EINVAL;
+        }
+
         max_l2_ste = (1 << span) - 1;
         l2ptr = l1std_l2ptr(&l1std);
         trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset,
Signature · 1 line
2.53.0
author
#7 lore raw

PATCH v3 5/5 hw/arm/smmuv3: Enforce alignment of L2Ptr according to the span

In reply to Eric Auger

Spec says: Bits L2Ptr[N:0] are treated as 0 by the SMMU, where N == 5 + (Span - 1).

Let's enforce this alignment.

---
 hw/arm/smmuv3.c | 2 ++
 1 file changed, 2 insertions(+)
hw/arm/smmuv3.c +2 -0
index 9008ba7acd9..ed19536a4dc 100644
@@ -728,6 +728,8 @@ int smmu_find_ste(SMMUv3State *s, uint32_t sid, STE *ste, SMMUEventInfo *event)
 
         max_l2_ste = (1 << span) - 1;
         l2ptr = l1std_l2ptr(&l1std);
+
+        l2ptr &= ~MAKE_64BIT_MASK(0, 6 + (span - 1));
         trace_smmuv3_find_ste_2lvl(s->strtab_base, l1ptr, l1_ste_offset,
                                    l2ptr, l2_ste_offset, max_l2_ste);
         if (l2_ste_offset > max_l2_ste) {
Signature · 1 line
2.53.0
#8 lore raw
In reply to Eric Auger
Quoted 14 lines · Eric Auger

The current code fails to sanitize SMMU_S_STRTAB_BASE_CFG LOG2SIZE and SPLIT which are used in the STE lookup. This could potentially lead to wrong shifts/masks in smmu_find_ste() and does not fully comply with the spec.

Also the series fixes different issues related to strtab base address alignment computations: off-by-one mask, unchecked span, missing L2ptr base address alignment. Those issues were not visible because the guest kernel does what it should but better comply with the spec.

Best Regards

Quoted 8 lines

Eric

This series is available at:
https://github.com/eauger/qemu/tree/smmu_sanitize_v3

v2 -> v3:
- Introduced STE_SIZE and L1STD_SIZE
- Just cap strtab_size to 64

Applied to target-arm.next, thanks.

-- PMM