Skip to contentSkip to conversation

Recursion in cpu_physical_memory_rw

Qemu-devel Herbert Xu 4 participants 12 messages Nov 15, 2006 – Nov 16, 2006

lore
Share & tools
lore

Herbert Xu

author
#0 lore raw

Hi:

A number of qemu driver backends (such as rtl8139) call the function cpu_physical_memory_rw to read/write guest memory. The target guest memory address is often supplied by the guest. This opens up the possibility of a guest giving an address which happens to be an MMIO address which can potentially lead to infinite recursion involving cpu_physical_memory_rw.

Since these driver backends really only need to access system memory, we could simply provide a new access interface that does not allow MMIO addresses.

Any comments on this problem?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#1 lore raw
Quoted 9 lines · Herbert Xu

Hi:

A number of qemu driver backends (such as rtl8139) call the function cpu_physical_memory_rw to read/write guest memory. The target guest memory address is often supplied by the guest. This opens up the possibility of a guest giving an address which happens to be an MMIO address which can potentially lead to infinite recursion involving cpu_physical_memory_rw.

Quoted 3 lines

Since these driver backends really only need to access system memory, we could simply provide a new access interface that does not allow MMIO addresses.

It isn't always system memory. Some DMA controllers deliberately write to device FIFOs. There are also several devices which map areas of onboard RAM. At minimum you need to make those to use RAM mappings rather than MMIO.

If a device is recursively writing to itself I'd take this as sign that the guest OS is already pretty screwed. I'm not sure what happens in this situation on real hardware, but I wouldn't be surprised if it caused similar effects by flooding the bus.

Paul

author
#2 lore raw
Quoted 4 lines · Paul Brook

It isn't always system memory. Some DMA controllers deliberately write to device FIFOs. There are also several devices which map areas of onboard RAM. At minimum you need to make those to use RAM mappings rather than MMIO.

I'm not suggesting that we change all existing users of cpu_physical_* to a new interface that only accessed RAM. However, for cases where it is obvious that only system RAM is intended (e.g., rtl8139), it makes sense to bypass MMIO handlers.

Quoted 4 lines

If a device is recursively writing to itself I'd take this as sign that the guest OS is already pretty screwed. I'm not sure what happens in this situation on real hardware, but I wouldn't be surprised if it caused similar effects by flooding the bus.

The scenario here is a compromised guest attempting to harm a host such as Xen.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines

It isn't always system memory. Some DMA controllers deliberately write to device FIFOs. There are also several devices which map areas of onboard RAM. At minimum you need to make those to use RAM mappings rather than MMIO.

I'm not suggesting that we change all existing users of cpu_physical_* to a new interface that only accessed RAM. However, for cases where it is obvious that only system RAM is intended (e.g., rtl8139), it makes sense to bypass MMIO handlers.

Could we add a recursion counter to the memory-access functions, and bail if it reaches some limit?

-- Keir

author
#4 lore raw
Quoted 8 lines · Keir Fraser
Quoted 5 lines

I'm not suggesting that we change all existing users of cpu_physical_* to a new interface that only accessed RAM. However, for cases where it is obvious that only system RAM is intended (e.g., rtl8139), it makes sense to bypass MMIO handlers.

Could we add a recursion counter to the memory-access functions, and bail if it reaches some limit?

Yes that would work too. However, chips such as rtl8139 should never do MMIO in this case (the real hardware would never allow that to occur) so we should do that accordingly.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw
Quoted 6 lines · Herbert Xu
Quoted 2 lines

Could we add a recursion counter to the memory-access functions, and bail if it reaches some limit?

Yes that would work too. However, chips such as rtl8139 should never do MMIO in this case (the real hardware would never allow that to occur) so we should do that accordingly.

We'd take a patch for Xen to do it the 'proper way' with an extended memory-access API if that is also acceptable for the upstream Qemu maintainers.

-- Keir

#6 lore raw
In reply to Herbert Xu
Quoted 2 lines

However, chips such as rtl8139 should never
do MMIO in this case (the real hardware would never allow that to occur)

Really? Why wouldn't it work on real hardware?

Paul

author
#7 lore raw
Quoted 4 lines · Paul Brook
Quoted 2 lines

However, chips such as rtl8139 should never
do MMIO in this case (the real hardware would never allow that to occur)

Really? Why wouldn't it work on real hardware?

For rtl8139 it would cause a Master Abort.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#8 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu
Quoted 1 line · Paul Brook

It isn't always system memory. Some DMA controllers deliberately write to device FIFOs. There are also several devices which map areas of onboard RAM. At minimum you need to make those to use RAM mappings rather than MMIO.

I'm not suggesting that we change all existing users of cpu_physical_* to a new interface that only accessed RAM. However, for cases where it is obvious that only system RAM is intended (e.g., rtl8139), it makes sense to bypass MMIO handlers.

Quoted 4 lines
Quoted 1 line

If a device is recursively writing to itself I'd take this as sign that the guest OS is already pretty screwed. I'm not sure what happens in this situation on real hardware, but I wouldn't be surprised if it caused similar effects by flooding the bus.

The scenario here is a compromised guest attempting to harm a host such as Xen.

The only "harm" done to a host is that the process will take as much CPU as it can get. This is really only a problem in Xen because the device model is in Domain-0. Once the device model is in a different domain, it doesn't matter anymore as the normal scheduler parameters can be used to ensure that no other hosts are harmed.

Regards,

Anthony Liguori

Quoted 1 line

Cheers,

author
#9 lore raw
Quoted 9 lines · Anthony Liguori
Quoted 3 lines

The scenario here is a compromised guest attempting to harm a host such as Xen.

The only "harm" done to a host is that the process will take as much CPU as it can get. This is really only a problem in Xen because the device model is in Domain-0. Once the device model is in a different domain, it doesn't matter anymore as the normal scheduler parameters can be used to ensure that no other hosts are harmed.

Actually it'll still be a problem in a driver domain unless it (and the hardware) is dedicated to a single guest.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#10 lore raw
Quoted 8 lines · Herbert Xu
Quoted 5 lines

The only "harm" done to a host is that the process will take as much CPU as it can get. This is really only a problem in Xen because the device model is in Domain-0. Once the device model is in a different domain, it doesn't matter anymore as the normal scheduler parameters can be used to ensure that no other hosts are harmed.

Actually it'll still be a problem in a driver domain unless it (and the hardware) is dedicated to a single guest.

Each qemu 'stub domain' will be dedicated to a single guest. Adding a recursion counter to the memory access functions sounds possibly useful even just from a debugging p.o.v. though.

-- Keir

author
#11 lore raw
Quoted 2 lines · Keir Fraser

Each qemu 'stub domain' will be dedicated to a single guest. Adding a

You're right of course. Somehow I was thinking of having the physical NIC in the qemu domain which obviously isn't the case.

Quoted 2 lines

recursion counter to the memory access functions sounds possibly useful even just from a debugging p.o.v. though.

After thinking about it for a while this does seem to be the safest and easiest strategy.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt