Skip to contentSkip to conversation

hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

PATCH In review Kane Chen 4 participants 12 messages Apr 28 – May 19 Arm/Aspeed machine support

lore
Share & tools
lore

Kane Chen

author
#0 lore raw

cover hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

This series fixes a bounds issue in Aspeed OTP programming through the Secure Boot Controller path.

The guest-provided OTP address is word-indexed in the SBC model, but the OTP device write path operates on byte offsets. Passing the value through without validation/conversion could lead to out-of-range writes.

The patch adds bounds checking in aspeed_sbc_otp_prog() before converting the address to a byte offset, and aligns the OTP write helper interfaces with byte-offset semantics.

The patch has been validated by a functional test and by the boundary test documented at:
https://gitlab.com/qemu-project/qemu/-/work_items/3436

Kane-Chen-AS (1):
hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

 hw/misc/aspeed_sbc.c  | 14 +++++++++++---
 hw/nvram/aspeed_otp.c | 13 ++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/1 hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
 hw/misc/aspeed_sbc.c  | 14 +++++++++++---
 hw/nvram/aspeed_otp.c | 13 ++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +11 -3
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
     MemTxResult ret;
     AspeedOTPState *otp = &s->otp;
     uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
     if (ret != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);
         return false;
     }
 
hw/nvram/aspeed_otp.c +6 -7
index a60289000c..1a9d3841b8 100644
@@ -57,12 +57,12 @@ static bool valid_program_data(uint32_t otp_addr,
     return has_programmable_bits != 0;
 }
 
-static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
+static bool program_otpmem_data(void *opaque, hwaddr otp_offset,
                              uint32_t prog_bit, uint32_t *value)
 {
     AspeedOTPState *s = opaque;
+    uint32_t otp_addr = otp_offset >> 2;
     bool is_odd = otp_addr & 1;
-    uint32_t otp_offset = otp_addr << 2;
 
     memcpy(value, s->storage + otp_offset, sizeof(uint32_t));
 
@@ -79,26 +79,25 @@ static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
     return true;
 }
 
-static void aspeed_otp_write(void *opaque, hwaddr otp_addr,
+static void aspeed_otp_write(void *opaque, hwaddr otp_offset,
                                 uint64_t val, unsigned size)
 {
     AspeedOTPState *s = opaque;
-    uint32_t otp_offset, value;
+    uint32_t value;
 
-    if (!program_otpmem_data(s, otp_addr, val, &value)) {
+    if (!program_otpmem_data(s, otp_offset, val, &value)) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Failed to program data, value = %x, bit = %"PRIx64"\n",
                       __func__, value, val);
         return;
     }
 
-    otp_offset = otp_addr << 2;
     memcpy(s->storage + otp_offset, &value, size);
 
     if (s->blk) {
         if (blk_pwrite(s->blk, otp_offset, size, &value, 0) < 0) {
             qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: Failed to write %x to %x\n",
+                          "%s: Failed to write %x to %"HWADDR_PRIx"\n",
                           __func__, value, otp_offset);
 
             return;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
Quoted 42 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
 hw/misc/aspeed_sbc.c  | 14 +++++++++++---
 hw/nvram/aspeed_otp.c | 13 ++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +5 -2
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
     MemTxResult ret;
     AspeedOTPState *otp = &s->otp;
     uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 176

Quoted 10 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
     if (ret != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);

I would stick to reporting otp_addr here:
- it's the value the guest actually writes
- it matches the logging for "invalid OTP addr above"
- it matches what aspeed_sbc_otp_read() logs in the equivalent case

Otherwise

thanks
-- PMM

author Reviewed-by Peter Maydell
#3 lore raw

Hi Peter,

Thanks for your review and feedback. I will update the logging accordingly and submit a v2 patch.

Best Regards,
Kane
________________________________
寄件者: Peter Maydell <[email protected]>
寄件日期: 2026年4月30日 下午 07:03
收件者: Kane Chen <[email protected]>
副本: Cédric Le Goater <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>; Troy Lee <[email protected]>; [email protected] <[email protected]>; Cédric Le Goater <[email protected]> 主旨: Re: [PATCH v1 1/1] hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

Quoted 42 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
 hw/misc/aspeed_sbc.c  | 14 +++++++++++---
 hw/nvram/aspeed_otp.c | 13 ++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +5 -2
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
     MemTxResult ret;
     AspeedOTPState *otp = &s->otp;
     uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 176

Quoted 10 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
     if (ret != MEMTX_OK) {
         qemu_log_mask(LOG_GUEST_ERROR,
                       "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);

I would stick to reporting otp_addr here:
- it's the value the guest actually writes
- it matches the logging for "invalid OTP addr above"
- it matches what aspeed_sbc_otp_read() logs in the equivalent case

Otherwise

thanks
-- PMM

#6 lore raw
In reply to Kane Chen
Quoted 31 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
  hw/misc/aspeed_sbc.c  | 14 +++++++++++---
  hw/nvram/aspeed_otp.c | 13 ++++++-------
  2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +0 -0
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
      MemTxResult ret;

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 167

Quoted 10 lines
hw/misc/aspeed_sbc.c +5 -2
      AspeedOTPState *otp = &s->otp;
      uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

This log event could be on one line.

Quoted 18 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
      if (ret != MEMTX_OK) {
          qemu_log_mask(LOG_GUEST_ERROR,
                        "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);
          return false;
      }
 
hw/nvram/aspeed_otp.c +0 -0
index a60289000c..1a9d3841b8 100644

hw/nvram/aspeed_otp.c static bool valid_program_data(uint32_t otp_addr, · line 64

Quoted 9 lines
hw/nvram/aspeed_otp.c +2 -1
@@ -57,12 +57,12 @@ static bool valid_program_data(uint32_t otp_addr,
      return has_programmable_bits != 0;
  }
 
-static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
+static bool program_otpmem_data(void *opaque, hwaddr otp_offset,
                               uint32_t prog_bit, uint32_t *value)
  {
      AspeedOTPState *s = opaque;
+    uint32_t otp_addr = otp_offset >> 2;

I think 'opt_addr' is a bit confusing. It's a word offset.

valid_program_data() could also use an 'otp_offset' arg and I would introduce a small inline helper to replace variable 'is_odd'

Thanks,

C.

Quoted 36 lines
hw/nvram/aspeed_otp.c +4 -6
      bool is_odd = otp_addr & 1;
-    uint32_t otp_offset = otp_addr << 2;
 
      memcpy(value, s->storage + otp_offset, sizeof(uint32_t));
 
@@ -79,26 +79,25 @@ static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
      return true;
  }
 
-static void aspeed_otp_write(void *opaque, hwaddr otp_addr,
+static void aspeed_otp_write(void *opaque, hwaddr otp_offset,
                                  uint64_t val, unsigned size)
  {
      AspeedOTPState *s = opaque;
-    uint32_t otp_offset, value;
+    uint32_t value;
 
-    if (!program_otpmem_data(s, otp_addr, val, &value)) {
+    if (!program_otpmem_data(s, otp_offset, val, &value)) {
          qemu_log_mask(LOG_GUEST_ERROR,
                        "%s: Failed to program data, value = %x, bit = %"PRIx64"\n",
                        __func__, value, val);
          return;
      }
 
-    otp_offset = otp_addr << 2;
      memcpy(s->storage + otp_offset, &value, size);
 
      if (s->blk) {
          if (blk_pwrite(s->blk, otp_offset, size, &value, 0) < 0) {
              qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: Failed to write %x to %x\n",
+                          "%s: Failed to write %x to %"HWADDR_PRIx"\n",
                            __func__, value, otp_offset);
 
              return;
#7 lore raw
Quoted 67 lines · Cédric Le Goater
Quoted 31 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
  hw/misc/aspeed_sbc.c  | 14 +++++++++++---
  hw/nvram/aspeed_otp.c | 13 ++++++-------
  2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +0 -0
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
      MemTxResult ret;

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 167

Quoted 10 lines
hw/misc/aspeed_sbc.c +5 -2
      AspeedOTPState *otp = &s->otp;
      uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

This log event could be on one line.

Quoted 21 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
      if (ret != MEMTX_OK) {
          qemu_log_mask(LOG_GUEST_ERROR,
                        "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);
          return false;
      }
 
hw/nvram/aspeed_otp.c +0 -0
index a60289000c..1a9d3841b8 100644
@@ -57,12 +57,12 @@ static bool valid_program_data(uint32_t otp_addr,
      return has_programmable_bits != 0;
  }
 
Quoted 8 lines

hw/nvram/aspeed_otp.c static bool valid_program_data(uint32_t otp_addr, · line 64

Quoted 6 lines
hw/nvram/aspeed_otp.c +2 -1
-static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
+static bool program_otpmem_data(void *opaque, hwaddr otp_offset,
                               uint32_t prog_bit, uint32_t *value)
  {
      AspeedOTPState *s = opaque;
+    uint32_t otp_addr = otp_offset >> 2;

I think 'opt_addr' is a bit confusing. It's a word offset.

It's the variable name we're currently using for that meaning everywhere in these two devices, though. It might be better to keep that cleanup in a separate patch from this one.

-- PMM

#8 lore raw
Quoted 77 lines · Peter Maydell
Quoted 67 lines · Cédric Le Goater
Quoted 31 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
   hw/misc/aspeed_sbc.c  | 14 +++++++++++---
   hw/nvram/aspeed_otp.c | 13 ++++++-------
   2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +0 -0
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
       MemTxResult ret;

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 167

Quoted 10 lines
hw/misc/aspeed_sbc.c +5 -2
       AspeedOTPState *otp = &s->otp;
       uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

This log event could be on one line.

Quoted 21 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
       if (ret != MEMTX_OK) {
           qemu_log_mask(LOG_GUEST_ERROR,
                         "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);
           return false;
       }
 
hw/nvram/aspeed_otp.c +0 -0
index a60289000c..1a9d3841b8 100644
@@ -57,12 +57,12 @@ static bool valid_program_data(uint32_t otp_addr,
       return has_programmable_bits != 0;
   }
 
Quoted 8 lines

hw/nvram/aspeed_otp.c static bool valid_program_data(uint32_t otp_addr, · line 64

Quoted 6 lines
hw/nvram/aspeed_otp.c +2 -1
-static bool program_otpmem_data(void *opaque, uint32_t otp_addr,
+static bool program_otpmem_data(void *opaque, hwaddr otp_offset,
                                uint32_t prog_bit, uint32_t *value)
   {
       AspeedOTPState *s = opaque;
+    uint32_t otp_addr = otp_offset >> 2;

I think 'opt_addr' is a bit confusing. It's a word offset.

Quoted 3 lines

It's the variable name we're currently using for that meaning everywhere in these two devices, though. It might be better to keep that cleanup in a separate patch from this one.

OK. Let's do that.

C.

#4 lore raw
In reply to Peter Maydell
Quoted 59 lines · Peter Maydell
Quoted 42 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

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

---
  hw/misc/aspeed_sbc.c  | 14 +++++++++++---
  hw/nvram/aspeed_otp.c | 13 ++++++-------
  2 files changed, 17 insertions(+), 10 deletions(-)
hw/misc/aspeed_sbc.c +5 -2
index 065e822e70..8b74dca13c 100644
@@ -159,13 +159,21 @@ static bool aspeed_sbc_otp_prog(AspeedSBCState *s,
      MemTxResult ret;
      AspeedOTPState *otp = &s->otp;
      uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 176

Quoted 10 lines
hw/misc/aspeed_sbc.c +6 -1
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                              &value, sizeof(value));
      if (ret != MEMTX_OK) {
          qemu_log_mask(LOG_GUEST_ERROR,
                        "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);

I would stick to reporting otp_addr here:
- it's the value the guest actually writes
- it matches the logging for "invalid OTP addr above"
- it matches what aspeed_sbc_otp_read() logs in the equivalent case

Quoted 2 lines

Otherwise

Applied to

https://github.com/legoater/qemu aspeed-next

with this change.

Peter,

Feel free to pick the change if needed.

Thanks,

C.

author
#5 lore raw
Quoted 90 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Friday, May 1, 2026 1:57 AM
To: Peter Maydell <[email protected]>; Kane Chen <[email protected]>
Cc: Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>; Troy Lee <[email protected]>; [email protected]; Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v1 1/1] hw/misc/aspeed_sbc: Add bounds checking for OTP write operations

Quoted 1 line · Peter Maydell

On Tue, 28 Apr 2026 at 06:53, Kane Chen <[email protected]>

Quoted 27 lines · wrote:
Quoted 27 lines

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller

model")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3436

---
  hw/misc/aspeed_sbc.c  | 14 +++++++++++---
  hw/nvram/aspeed_otp.c | 13 ++++++-------
  2 files changed, 17 insertions(+), 10 deletions(-)
Quoted 5 lines

hw/misc/aspeed_sbc.c static bool

Quoted 5 lines
hw/misc/aspeed_sbc.c index renamed from hw/misc/aspeed_sbc.c +0 -0

065e822e70..8b74dca13c 100644

hw/misc/aspeed_sbc.c +0 -0
@@ -159,13 +159,21 @@ static bool

aspeed_sbc_otp_prog(AspeedSBCState *s,

Quoted 6 lines

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s,

Quoted 6 lines
hw/misc/aspeed_sbc.c +1 -1
      MemTxResult ret;
      AspeedOTPState *otp = &s->otp;
      uint32_t value = s->regs[R_CAMP1];
+    uint32_t otp_offset = otp_addr << 2;
 
-    ret = address_space_write(&otp->as, otp_addr,

MEMTXATTRS_UNSPECIFIED,

Quoted 9 lines

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s,

Quoted 9 lines
hw/misc/aspeed_sbc.c +8 -1
-                        &value, sizeof(value));
+    if (otp_addr >= OTP_TOTAL_DWORD_COUNT) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Invalid OTP addr 0x%x\n",
+                      otp_addr);
+        return false;
+    }
+
+    ret = address_space_write(&otp->as, otp_offset,

MEMTXATTRS_UNSPECIFIED,

Quoted 7 lines

hw/misc/aspeed_sbc.c static bool aspeed_sbc_otp_prog(AspeedSBCState *s, · line 176

Quoted 6 lines
hw/misc/aspeed_sbc.c +2 -1
+                              &value, sizeof(value));
      if (ret != MEMTX_OK) {
          qemu_log_mask(LOG_GUEST_ERROR,
                        "Failed to write OTP memory, addr = %x\n",
-                      otp_addr);
+                      otp_offset);
Quoted 7 lines

I would stick to reporting otp_addr here:
- it's the value the guest actually writes
- it matches the logging for "invalid OTP addr above"
- it matches what aspeed_sbc_otp_read() logs in the equivalent case

Otherwise

Applied to

https://github.com/legoater/qemu aspeed-next

with this change.

Quoted 7 lines

Peter,

Feel free to pick the change if needed.

Thanks,

C.

Hi Cédric/Peter,

Thanks for your review and comments.

Currently, the naming of otp_addr and otp_offset is inconsistent, as both are used to represent the user-provided address. For example, when implementing the OTP module, I followed the naming convention used in the write operation (see the link below for reference):
https://github.com/AspeedTech-BMC/zephyr/blob/aspeed-main-v3.7.0/drivers/misc/aspeed/otp_shell.c#L239

However, in the read path, the code uses the term offset instead (see below):
https://github.com/AspeedTech-BMC/zephyr/blob/aspeed-main-v3.7.0/drivers/misc/aspeed/otp_shell.c#L150

To improve consistency, we could align the naming with otp_offset, following the read path convention. However, I'm not certain if this would be more intuitive. Please let me know if you have any further suggestions regarding the otp_addr / otp_offset naming.

Best Regards,
Kane

#9 lore raw
In reply to Kane Chen
Quoted 9 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Quoted 7 lines

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

FWIW (this change has already landed in the master branch, so it's cast in stone), I can't find this commit in my git clone of qemu repository. Neither hash ID nor the name.

Is it this one maybe? https://marc.info/?l=qemu-arm&m=164500409921734,
which is commit e1acf581c9 in qemu? It has a different commit message though.

And this patch does not apply to 10.0.x, - in there, there's no hw/nvram/aspeed_otp.c (688a3dae78 hw/nvram/aspeed_otp: Add ASPEED OTP memory device model). I assume it's not needed for 10.0.x, but in that case, the Fixes: tag isn't even commit e1acf581c9.

Thanks,

/mjt

#10 lore raw
Quoted 14 lines · Michael Tokarev
Quoted 13 lines · Kane Chen

There is a mismatch between the Aspeed OTP model and the Aspeed SBC model in how the guest-provided address is handled. aspeed_sbc_otp_prog() passes a word-indexed address directly to address_space_write() without converting it to a byte offset, whereas aspeed_otp_write() expects a byte offset and applies an additional shift (otp_addr << 2). This double-shift confusion means that an out-of-range word address can lead to a write beyond the allocated storage.

Fix this by adding bounds checking on the word offset before converting to byte offset and passing to address_space_write(). This matches the existing bounds check in aspeed_sbc_otp_read().

Quoted 8 lines
Quoted 3 lines
Cc Kane-Chen-AS <[email protected]>
Fixes 1a00754ccf15 ("hw/misc: Add Aspeed Secure Boot Controller model")

FWIW (this change has already landed in the master branch, so it's cast in stone), I can't find this commit in my git clone of qemu repository. Neither hash ID nor the name.

My bad :/ I should improve my flow to check for bogus commit ids.

Quoted 3 lines

Is it this one maybe? https://marc.info/?l=qemu-arm&m=164500409921734,
which is commit e1acf581c9 in qemu?  It has a different commit message though.

It should be :

Fixes 9f58dd0a8c30 ("hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC")
Quoted 4 lines

And this patch does not apply to 10.0.x, - in there, there's no hw/nvram/aspeed_otp.c (688a3dae78 hw/nvram/aspeed_otp: Add ASPEED OTP memory device model).  I assume it's not needed for 10.0.x, but in that case, the Fixes: tag isn't even commit e1acf581c9.

It should apply on stable-10.2

Thanks,

C.

#11 lore raw
Quoted 1 line · Cédric Le Goater

On 5/14/26 20:48, Michael Tokarev wrote:

...

Quoted 7 lines
Quoted 3 lines

Is it this one maybe? https://marc.info/?l=qemu-arm&m=164500409921734,
which is commit e1acf581c9 in qemu?  It has a different commit message though.

It should be  :

Fixes 9f58dd0a8c30 ("hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC")

Aha!

Quoted 6 lines
Quoted 4 lines

And this patch does not apply to 10.0.x, - in there, there's no hw/nvram/aspeed_otp.c (688a3dae78 hw/nvram/aspeed_otp: Add ASPEED OTP memory device model).  I assume it's not needed for 10.0.x, but in that case, the Fixes: tag isn't even commit e1acf581c9.

It should apply on stable-10.2

Yeah, with this Fixes: tag it becomes obvious.

Thank you for the update!

/mjt