Skip to contentSkip to conversation

softmmu/memory: Validate {read, write}_with_attrs before calling

PATCH In review Bin Meng 4 participants 8 messages Sep 5, 2021 – Sep 6, 2021

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 softmmu/memory: Validate {read, write}_with_attrs before calling

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

---

 softmmu/memory.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
softmmu/memory.c +6 -2
index bfedaf9c4d..b97ffd4ba7 100644
@@ -1426,12 +1426,14 @@ static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
                                          mr->ops->impl.max_access_size,
                                          memory_region_read_accessor,
                                          mr, attrs);
-    } else {
+    } else if (mr->ops->read_with_attrs) {
         return access_with_adjusted_size(addr, pval, size,
                                          mr->ops->impl.min_access_size,
                                          mr->ops->impl.max_access_size,
                                          memory_region_read_with_attrs_accessor,
                                          mr, attrs);
+    } else {
+        return MEMTX_ERROR;
     }
 }
 
@@ -1506,13 +1508,15 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
                                          mr->ops->impl.max_access_size,
                                          memory_region_write_accessor, mr,
                                          attrs);
-    } else {
+    } else if (mr->ops->write_with_attrs) {
         return
             access_with_adjusted_size(addr, &data, size,
                                       mr->ops->impl.min_access_size,
                                       mr->ops->impl.max_access_size,
                                       memory_region_write_with_attrs_accessor,
                                       mr, attrs);
+    } else {
+        return MEMTX_ERROR;
     }
 }
 
Signature · 1 line
2.25.1
#1 lore raw

Cc'ing PJP for https://www.mail-archive.com/[email protected]/msg730311.html

Quoted 51 lines · Bin Meng

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

---

 softmmu/memory.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
softmmu/memory.c +6 -3
index bfedaf9c4d..b97ffd4ba7 100644
@@ -1426,12 +1426,14 @@ static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
                                          mr->ops->impl.max_access_size,
                                          memory_region_read_accessor,
                                          mr, attrs);
-    } else {
+    } else if (mr->ops->read_with_attrs) {
         return access_with_adjusted_size(addr, pval, size,
                                          mr->ops->impl.min_access_size,
                                          mr->ops->impl.max_access_size,
                                          memory_region_read_with_attrs_accessor,
                                          mr, attrs);
+    } else {
+        return MEMTX_ERROR;
     }
 }
 
@@ -1506,13 +1508,15 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
                                          mr->ops->impl.max_access_size,
                                          memory_region_write_accessor, mr,
                                          attrs);
-    } else {
+    } else if (mr->ops->write_with_attrs) {
         return
             access_with_adjusted_size(addr, &data, size,
                                       mr->ops->impl.min_access_size,
                                       mr->ops->impl.max_access_size,
                                       memory_region_write_with_attrs_accessor,
                                       mr, attrs);
+    } else {
+        return MEMTX_ERROR;
     }
 }
 
--

2.25.1

#2 lore raw
In reply to Bin Meng
Quoted 6 lines · Bin Meng

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

This 'fixes' tag doesn't seem quite right. Before that commit 62a0db942dec, we still required that a MemoryRegionOps provided some form of both read and write. It was never valid to leave all of the possible read function pointers NULL.

What are the examples of devices which are deliberately leaving these pointers set to NULL?

Last time this came up, we discussed the other option, which is to have memory_region_init assert that the MemoryRegionOps defines at least one valid read and one valid write pointer, so that these bugs get caught quickly rather than only if the guest accesses the device in the wrong way. I tend to think that that is better, because for any particular device it's not necessarily the right thing to return MEMTX_ERROR (maybe the right behaviour is "return 0 for reads, ignore writes" -- the point is that there is no single default behaviour that works for every device and architecture). Forcing the device model author to explicitly write the code means they have to think about what the behaviour they want is.

If we do choose to support allowing MemoryRegionOps structs to leave all the pointers NULL, we should document that, including what the default behaviour is.

thanks
-- PMM

author
#3 lore raw
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Bin Meng

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Quoted 6 lines
Quoted 2 lines
Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

This 'fixes' tag doesn't seem quite right. Before that commit 62a0db942dec, we still required that a MemoryRegionOps provided some form of both read and write.

Did you mean before commit 62a0db942dec leaving MemoryRegionOps NULL was not allowed, but things changed so that now it's possible to have NULL MemoryRegionOps? If yes, then the commit id of "Fixes" should probably go to the changes that allowed NULL memory ops. If not, I don't think "Fixes" tag is wrong.

Quoted 5 lines

It was never
valid to leave all of the possible read function pointers NULL.

What are the examples of devices which are deliberately leaving these pointers set to NULL?

No in-tree real examples. I just read the codes and found no protection against the {read,write}_with_attrs ops.

Quoted 5 lines

Last time this came up, we discussed the other option, which is to have memory_region_init assert that the MemoryRegionOps defines at least one valid read and one valid write pointer, so that these bugs get caught quickly rather than only if the guest accesses the device in the wrong way. I tend to think

Quoted 8 lines

that that is better, because for any particular device it's not necessarily the right thing to return MEMTX_ERROR (maybe the right behaviour is "return 0 for reads, ignore writes" -- the point is that there is no single default behaviour that works for every device and architecture). Forcing the device model author to explicitly write the code means they have to think about what the behaviour they want is.

Yes, either way can prevent the NULL dereferencing. So it's time to revisit this topic :)

Quoted 4 lines

If we do choose to support allowing MemoryRegionOps structs to leave all the pointers NULL, we should document that, including what the default behaviour is.

Regards,
Bin

#4 lore raw
Quoted 15 lines · Bin Meng
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Bin Meng

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Quoted 6 lines
Quoted 2 lines
Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

This 'fixes' tag doesn't seem quite right. Before that commit 62a0db942dec, we still required that a MemoryRegionOps provided some form of both read and write.

Quoted 5 lines

Did you mean before commit 62a0db942dec leaving MemoryRegionOps NULL was not allowed, but things changed so that now it's possible to have NULL MemoryRegionOps? If yes, then the commit id of "Fixes" should probably go to the changes that allowed NULL memory ops. If not, I don't think "Fixes" tag is wrong.

I mean that before commit 62a0db942dec leaving the pointers all NULL was not allowed, and after that commit leaving the pointers all NULL was still not allowed. It's been a requirement that you provide at least one function pointer for read and one for write ever since the MemoryRegion APIs were introduced in 2012. You're proposing an API change; it might be a good one, but it isn't a 'Fixes' to anything.

-- PMM

author
#5 lore raw
Quoted 23 lines · Peter Maydell
Quoted 15 lines · Bin Meng
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Bin Meng

{read,write}_with_attrs might be missing, and the codes currently do not validate them before calling, which will cause segment fault.

Quoted 6 lines
Quoted 2 lines
Fixes 62a0db942dec ("memory: Remove old_mmio accessors")

This 'fixes' tag doesn't seem quite right. Before that commit 62a0db942dec, we still required that a MemoryRegionOps provided some form of both read and write.

Quoted 5 lines

Did you mean before commit 62a0db942dec leaving MemoryRegionOps NULL was not allowed, but things changed so that now it's possible to have NULL MemoryRegionOps? If yes, then the commit id of "Fixes" should probably go to the changes that allowed NULL memory ops. If not, I don't think "Fixes" tag is wrong.

Quoted 7 lines

I mean that before commit 62a0db942dec leaving the pointers all NULL was not allowed, and after that commit leaving the pointers all NULL was still not allowed. It's been a requirement that you provide at least one function pointer for read and one for write ever since the MemoryRegion APIs were introduced in 2012. You're proposing an API change; it might be a good one, but it isn't a 'Fixes' to anything.

Where is the requirement documented? I don't see anything mentioned in docs/devel/memory.rst

If it's a requirement since 2012, then I agree "Fixes" can be dropped. But a doc fix should be made to document the "requirement".

Regards,
Bin

#6 lore raw
Quoted 10 lines · Bin Meng
Quoted 7 lines · Peter Maydell

I mean that before commit 62a0db942dec leaving the pointers all NULL was not allowed, and after that commit leaving the pointers all NULL was still not allowed. It's been a requirement that you provide at least one function pointer for read and one for write ever since the MemoryRegion APIs were introduced in 2012. You're proposing an API change; it might be a good one, but it isn't a 'Fixes' to anything.

Quoted 2 lines

Where is the requirement documented? I don't see anything mentioned in docs/devel/memory.rst

It's not documented, but lots of fiddly details of QEMU functions aren't documented...

Quoted 2 lines

If it's a requirement since 2012, then I agree "Fixes" can be dropped. But a doc fix should be made to document the "requirement".

Agreed.

-- PMM

#7 lore raw

Re: [PATCH] softmmu/memory: Validate {read,write}_with_attrs before calling

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Last time this came up, we discussed the other option, which is to have memory_region_init assert that the MemoryRegionOps defines at least one valid read and one valid write pointer, so that these bugs get caught quickly rather than only if the guest accesses the device in the wrong way. I tend to think that that is better

I agree, and Prasad had even posted a patch for that, but it failed some qtests and I ended up never checking why that was.

Paolo