Skip to contentSkip to conversation

scsi: pvscsi: limit process IO loop to maximum page count

PATCH In review P J P 2 participants 6 messages Sep 7, 2016 – Sep 13, 2016

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 scsi: pvscsi: limit process IO loop to maximum page count

From: Prasad J Pandit <[email protected]>

Vmware Paravirtual SCSI emulator while processing IO requests could run into an infinite loop if 'pvscsi_ring_pop_req_descr' always returned positive value. Limit IO loop to the maximum page count.

---
 hw/scsi/vmw_pvscsi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/scsi/vmw_pvscsi.c +3 -1
index babac5a..3e77a08 100644
@@ -711,11 +711,13 @@ pvscsi_process_request_descriptor(PVSCSIState *s,
 static void
 pvscsi_process_io(PVSCSIState *s)
 {
+    int descr_pa_cnt = 0;
     PVSCSIRingReqDesc descr;
     hwaddr next_descr_pa;
 
     assert(s->rings_info_valid);
-    while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
+    while (((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0)
+            && descr_pa_cnt++ < PVSCSI_SETUP_RINGS_MAX_NUM_PAGES) {
 
         /* Only read after production index verification */
         smp_rmb();
Signature · 1 line
2.5.5
author
#1 lore raw
+-- On Wed, 7 Sep 2016, P J P wrote --+
| From: Prasad J Pandit <[email protected]>
| 
| Vmware Paravirtual SCSI emulator while processing IO requests
| could run into an infinite loop if 'pvscsi_ring_pop_req_descr'
| always returned positive value. Limit IO loop to the maximum
| page count.
| 
| Reported-by: Li Qiang <[email protected]>
| Signed-off-by: Prasad J Pandit <[email protected]>
| ---
|  hw/scsi/vmw_pvscsi.c | 4 +++-
|  1 file changed, 3 insertions(+), 1 deletion(-)
| 
| diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c
| index babac5a..3e77a08 100644
| --- a/hw/scsi/vmw_pvscsi.c
| +++ b/hw/scsi/vmw_pvscsi.c
| @@ -711,11 +711,13 @@ pvscsi_process_request_descriptor(PVSCSIState *s,
|  static void
|  pvscsi_process_io(PVSCSIState *s)
|  {
| +    int descr_pa_cnt = 0;
|      PVSCSIRingReqDesc descr;
|      hwaddr next_descr_pa;
|  
|      assert(s->rings_info_valid);
| -    while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
| +    while (((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0)
| +            && descr_pa_cnt++ < PVSCSI_SETUP_RINGS_MAX_NUM_PAGES) {
|  
|          /* Only read after production index verification */
|          smp_rmb();

Ping...!
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#2 lore raw

Hello Prasad,

Please see my questions inline.

Quoted 8 lines · P J P
+-- On Wed, 7 Sep 2016, P J P wrote --+
| From: Prasad J Pandit <[email protected]>
| 
| Vmware Paravirtual SCSI emulator while processing IO requests
| could run into an infinite loop if 'pvscsi_ring_pop_req_descr'
| always returned positive value. Limit IO loop to the maximum

Do you see any specific scenario why this might happen?

Quoted 16 lines
| page count.
| 
| Reported-by: Li Qiang <[email protected]>
| Signed-off-by: Prasad J Pandit <[email protected]>
| ---
|  hw/scsi/vmw_pvscsi.c | 4 +++-
|  1 file changed, 3 insertions(+), 1 deletion(-)
| 
| diff --git a/hw/scsi/vmw_pvscsi.c b/hw/scsi/vmw_pvscsi.c
| index babac5a..3e77a08 100644
| --- a/hw/scsi/vmw_pvscsi.c
| +++ b/hw/scsi/vmw_pvscsi.c
| @@ -711,11 +711,13 @@ pvscsi_process_request_descriptor(PVSCSIState *s,
|  static void
|  pvscsi_process_io(PVSCSIState *s)
|  {
Quoted 8 lines
| +    int descr_pa_cnt = 0;
|      PVSCSIRingReqDesc descr;
|      hwaddr next_descr_pa;
|  
|      assert(s->rings_info_valid);
| -    while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
| +    while (((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0)
| +            && descr_pa_cnt++ < PVSCSI_SETUP_RINGS_MAX_NUM_PAGES) {

Why do you limit number of processed descriptors by maximal number of pages in data exchange ring? What will happen to requests still waiting in the ring after this function exits?

Thanks,
Dmitry

Quoted 8 lines
|  
|          /* Only read after production index verification */
|          smp_rmb();

Ping...!
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#3 lore raw

Hello Dmitry,

+-- On Tue, 13 Sep 2016, Dmitry Fleytman wrote --+
| > On 13 Sep 2016, at 10:00 AM, P J P <[email protected]> wrote:
| > 
| > +-- On Wed, 7 Sep 2016, P J P wrote --+
| > | From: Prasad J Pandit <[email protected]>
| > | 
| > | Vmware Paravirtual SCSI emulator while processing IO requests
| > | could run into an infinite loop if 'pvscsi_ring_pop_req_descr'
| > | always returned positive value. Limit IO loop to the maximum
| 
| Do you see any specific scenario why this might happen?

A guest user could set the 'ready_ptr' and 'PVSCSIRingInfo *mgr' parameter
in 'pvscsi_ring_pop_req_descr', such that it always returns true.

| > | Reported-by: Li Qiang <[email protected]>
| > |  pvscsi_process_io(PVSCSIState *s)
| > |  {
| > | +    int descr_pa_cnt = 0;
| > |      PVSCSIRingReqDesc descr;
| > |      hwaddr next_descr_pa;
| > |  
| > |      assert(s->rings_info_valid);
| > | -    while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
| > | +    while (((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0)
| > | +            && descr_pa_cnt++ < PVSCSI_SETUP_RINGS_MAX_NUM_PAGES) {
| 
| Why do you limit number of processed descriptors by maximal number of pages 
| in data exchange ring? What will happen to requests still waiting in the 
| ring after this function exits?

I limit it to maximum page count thinking that the descriptor value returned
by pvscsi_ring_pop_req_descr() is derived from the mgr->req_ring_pages_pa[] array, which is of size 'PVSCSI_SETUP_RINGS_MAX_NUM_PAGES'. If pvscsi_process_io() was to go into an infinite loop, it'd continue processing the same set of req_ring_pages?

--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#4 lore raw
Quoted 17 lines · P J P

Hello Dmitry,

+-- On Tue, 13 Sep 2016, Dmitry Fleytman wrote --+
| > On 13 Sep 2016, at 10:00 AM, P J P <[email protected]> wrote:
| > 
| > +-- On Wed, 7 Sep 2016, P J P wrote --+
| > | From: Prasad J Pandit <[email protected]>
| > | 
| > | Vmware Paravirtual SCSI emulator while processing IO requests
| > | could run into an infinite loop if 'pvscsi_ring_pop_req_descr'
| > | always returned positive value. Limit IO loop to the maximum
| 
| Do you see any specific scenario why this might happen?
Quoted 2 lines

A guest user could set the 'ready_ptr' and 'PVSCSIRingInfo *mgr' parameter
in 'pvscsi_ring_pop_req_descr', such that it always returns true.

I see. The problematic code is if (ready_ptr != mgr->consumed_ptr) {…}

mgr->consumed_ptr is managed by device and not visible to the driver, but ready_ptr is managed by driver and may be set to some “big” number.

In this case it may take a lot of iterations for consumed_ptr
to become equal to ready_ptr and additionally some requests will be send multiple times.

The most straightforward way to fix this issue will be to check that ready_ptr - consumed_ptr is less than ring size.

Quoted 17 lines
| > | Reported-by: Li Qiang <[email protected]>
| > |  pvscsi_process_io(PVSCSIState *s)
| > |  {
| > | +    int descr_pa_cnt = 0;
| > |      PVSCSIRingReqDesc descr;
| > |      hwaddr next_descr_pa;
| > |  
| > |      assert(s->rings_info_valid);
| > | -    while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
| > | +    while (((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0)
| > | +            && descr_pa_cnt++ < PVSCSI_SETUP_RINGS_MAX_NUM_PAGES) {
| 
| Why do you limit number of processed descriptors by maximal number of pages 
| in data exchange ring? What will happen to requests still waiting in the 
| ring after this function exits?
Quoted 5 lines

I limit it to maximum page count thinking that the descriptor value returned
by pvscsi_ring_pop_req_descr() is derived from the mgr->req_ring_pages_pa[] array, which is of size 'PVSCSI_SETUP_RINGS_MAX_NUM_PAGES'. If pvscsi_process_io() was to go into an infinite loop, it'd continue processing the same set of req_ring_pages?

I think you’re mixing concepts of number of pages in the ring and number of requests in the ring.

Each page contains (much) more than one request.

Quoted 4 lines

--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#5 lore raw
+-- On Tue, 13 Sep 2016, Dmitry Fleytman wrote --+
| >  A guest user could set the 'ready_ptr' and 'PVSCSIRingInfo *mgr' parameter 
| > in 'pvscsi_ring_pop_req_descr', such that it always returns true.
| 
| I see. The problematic code is if (ready_ptr != mgr->consumed_ptr) {…}
| 
| mgr->consumed_ptr is managed by device and not visible to the driver,
| but ready_ptr is managed by driver and may be set to some “big” number.
| 
| In this case it may take a lot of iterations for consumed_ptr
| to become equal to ready_ptr and additionally some requests will be send multiple times.
| 
| The most straightforward way to fix this issue will be to
| check that ready_ptr - consumed_ptr is less than ring size.

I see.

| I think you’re mixing concepts of number of
| pages in the ring and number of requests in the ring.
| 
| Each page contains (much) more than one request.

I see, okay.

Thank you so much for the details. I'll send a revised patch.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F