Skip to contentSkip to conversation

spapr-vscsi: Adding VSCSI capabilities

PATCH In review Alexey Kardashevskiy 5 participants 26 messages Aug 23, 2013 – Aug 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spapr-vscsi: Adding VSCSI capabilities

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
hw/scsi/spapr_vscsi.c +31 -0
index c5ff794..cc35b1b 100644
@@ -919,6 +919,34 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap, be16_to_cpu(vcap->common.length));
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+    rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(vcap->buffer),
+                             &cap, be16_to_cpu(vcap->common.length));
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
     union mad_iu *mad = &req->iu.mad;
@@ -939,6 +967,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
         mad->host_config.common.status = cpu_to_be16(1);
         vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
         break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
     default:
         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                 be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.4.rc4
#1 lore raw
Quoted 19 lines · Alexey Kardashevskiy

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
hw/scsi/spapr_vscsi.c | 31 +++++++++++++++++++++++++++++++

1 file changed, 31 insertions(+)
hw/scsi/spapr_vscsi.c +0 -0
index c5ff794..cc35b1b 100644
@@ -919,6 +919,34 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 930

Quoted 10 lines
hw/scsi/spapr_vscsi.c +9 -0
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap, be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

It's certainly better style (read: makes it easier to prove this doesn't happen when it really is important) to read the variables into local variables and reuse them there. In this case it mostly helps readability to make sure here and below are the same variables.

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 932

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");

This is a DMA read, no? Also this should be error_report.

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 939

Quoted 7 lines
hw/scsi/spapr_vscsi.c +7 -0
+    }
+
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;

Is this a memset(0)?

Alex

Quoted 26 lines
hw/scsi/spapr_vscsi.c +13 -0
+    rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(vcap->buffer),
+                             &cap, be16_to_cpu(vcap->common.length));
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
    union mad_iu *mad = &req->iu.mad;
@@ -939,6 +967,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
        mad->host_config.common.status = cpu_to_be16(1);
        vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
        break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
    default:
        fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.4.rc4
#2 lore raw
Quoted 10 lines · Alexander Graf

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

It's certainly better style (read: makes it easier to prove this doesn't happen when it really is important) to read the variables into local variables and reuse them there. In this case it mostly helps readability to make sure here and below are the same variables.

Ugh... It's not better style at all, it's also less efficient and the "attack" you talk about doesn't exist... All the guest can do is shoot itself in the foot.

Ben.

#3 lore raw

Il 25/08/2013 22:51, Benjamin Herrenschmidt ha scritto:

Quoted 7 lines · Alexander Graf
Quoted 6 lines

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

Quoted 8 lines
Quoted 4 lines

It's certainly better style (read: makes it easier to prove this doesn't happen when it really is important) to read the variables into local variables and reuse them there. In this case it mostly helps readability to make sure here and below are the same variables.

Ugh... It's not better style at all, it's also less efficient and the "attack" you talk about doesn't exist... All the guest can do is shoot itself in the foot.

There are certainly cases where time-of-check-to-time-of-use vulnerability could make QEMU access uninitialized memory (or worse, out-of-bounds arrays). For example, you could try racing the host on the length of a scatter/gather list.

Paolo

#4 lore raw
Quoted 4 lines · Paolo Bonzini

There are certainly cases where time-of-check-to-time-of-use vulnerability could make QEMU access uninitialized memory (or worse, out-of-bounds arrays). For example, you could try racing the host on the length of a scatter/gather list.

Sure, and I mentioned that too, the latest patch from Nikunj addresses it, I still think however that it's not a good practice to copy everything, then do the byteswaps on the result (and it defeats use of sparse for endian checking if we ever want to do that).

Ben.

#5 lore raw
In reply to Alexander Graf
Quoted 9 lines · Alexander Graf

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap,

be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

BTW. While I disagree with your initial comment ... is there any bound checking here ? That looks like potential stack corruption unless I miss something if the guest passes a too big length...

So at least the length should be read once, bound-checked, then the read done with the result (don't bound check and read again, that would be indeed racy).

Cheers,
Ben.

#6 lore raw
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 9 lines · Alexander Graf

Review

Quoted 3 lines
(quoted diff) +3 -0
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap,

be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

Quoted 7 lines

BTW. While I disagree with your initial comment ... is there any bound checking here ? That looks like potential stack corruption unless I miss something if the guest passes a too big length...

So at least the length should be read once, bound-checked, then the read done with the result (don't bound check and read again, that would be indeed racy).

How about the below patch:

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
hw/scsi/spapr_vscsi.c +37 -0
index e9090e5..fae3644 100644
@@ -858,6 +858,40 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len = 0;
+    int rc = true;
+
+    vcap = &req->iu.mad.capabilities;
+    len = be16_to_cpu(vcap->common.length);
+    if (len > sizeof(&cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        goto error_out;
+    }
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
+    }
+
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+    rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(vcap->buffer),
+                             &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
     union mad_iu *mad = &req->iu.mad;
@@ -878,6 +912,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
         mad->host_config.common.status = cpu_to_be16(1);
         vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
         break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
     default:
         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                 be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.3.1
#7 lore raw

Am 26.08.2013 um 05:32 schrieb Nikunj A Dadhania <[email protected]>:

Quoted 21 lines
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 9 lines · Alexander Graf

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap,

be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

Quoted 7 lines

BTW. While I disagree with your initial comment ... is there any bound checking here ? That looks like potential stack corruption unless I miss something if the guest passes a too big length...

So at least the length should be read once, bound-checked, then the read done with the result (don't bound check and read again, that would be indeed racy).

Quoted 6 lines

How about the below patch:

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client.

Client?

Quoted 13 lines

As at the moment no capability is supported, put zero flags everywhere and return.

---
hw/scsi/spapr_vscsi.c | 37 +++++++++++++++++++++++++++++++++++++

1 file changed, 37 insertions(+)
hw/scsi/spapr_vscsi.c +0 -0
index e9090e5..fae3644 100644

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 865

Quoted 8 lines
hw/scsi/spapr_vscsi.c +5 -0
@@ -858,6 +858,40 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len = 0;

No need for = 0.

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 866

Quoted 1 line
hw/scsi/spapr_vscsi.c +1 -0
+    int rc = true;

Better preassign it to a value that functions you call later would set as well. Or does spapr_vio_dma_xxx return true/false?

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 869

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+
+    vcap = &req->iu.mad.capabilities;
+    len = be16_to_cpu(vcap->common.length);

Good.

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 870

Quoted 1 line
hw/scsi/spapr_vscsi.c +1 -0
+    if (len > sizeof(&cap)) {

sizeof(&cap) == sizeof(long). Is this what you want here?

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 871

Quoted 1 line
hw/scsi/spapr_vscsi.c +1 -0
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");

Please use the error framework to report errors.

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 874

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        goto error_out;
+    }
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),

Just move the swizzled buffer pointer into its own variable too. Makes things more consistent (easier to read).

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 884

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+                            &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
+    }
+
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;

My question stands unanswered. Is this just memset(0)?

Alex

Quoted 26 lines
hw/scsi/spapr_vscsi.c +13 -0
+    rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(vcap->buffer),
+                             &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
    union mad_iu *mad = &req->iu.mad;
@@ -878,6 +912,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
        mad->host_config.common.status = cpu_to_be16(1);
        vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
        break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
    default:
        fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.3.1
#8 lore raw
Quoted 7 lines · Alexander Graf

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 884

Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;

My question stands unanswered. Is this just memset(0)?

If it is, then why bother reading it in the first place ? :-)

Note that even if it is, I'd rather keep it that way as we are eventually going to populate some stuff and so we have just the placeholders to do it.

Cheers,
Ben.

#9 lore raw

Am 26.08.2013 um 08:22 schrieb Benjamin Herrenschmidt <[email protected]>:

Quoted 10 lines · Alexander Graf
Quoted 7 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 884

Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;

My question stands unanswered. Is this just memset(0)?

If it is, then why bother reading it in the first place ? :-)

Because this is a revision that incorporates other feedback from that same review, so I'm quite sure he just missed it :)

Quoted 4 lines

Note that even if it is, I'd rather keep it that way as we are eventually going to populate some stuff and so we have just the placeholders to do it.

Do we ever need to preserve random fields in that struct? Or would it be clearer to just set the whole thing to 0 and then go from there?

It's definitely a case-by-case decision which way is better, but setting all fields by hand gives you potential for missing to set one.

Alex

Quoted 5 lines

Cheers,
Ben.

#10 lore raw
Quoted 9 lines · Alexander Graf

Am 26.08.2013 um 08:22 schrieb Benjamin Herrenschmidt <[email protected]>:

Quoted 7 lines · Alexander Graf
Quoted 6 lines
Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

My question stands unanswered. Is this just memset(0)?

If it is, then why bother reading it in the first place ? :-)

Because this is a revision that incorporates other feedback from that same review, so I'm quite sure he just missed it :)

Nope, i did not miss it.

Regards
Nikunj

#11 lore raw
Quoted 11 lines · Nikunj A Dadhania
Quoted 9 lines · Alexander Graf

Am 26.08.2013 um 08:22 schrieb Benjamin Herrenschmidt <[email protected]>:

Quoted 7 lines · Alexander Graf
Quoted 6 lines
Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

My question stands unanswered. Is this just memset(0)?

If it is, then why bother reading it in the first place ? :-)

Because this is a revision that incorporates other feedback from that same review, so I'm quite sure he just missed it :)

Nope, i did not miss it.

So you ignored it on purpose?

Alex

#12 lore raw
In reply to Alexander Graf
Quoted 2 lines · Alexander Graf

Do we ever need to preserve random fields in that struct? Or would it be clearer to just set the whole thing to 0 and then go from there?

Yeah sort of, we set/clear bits more/less based on what the guest put in the first place, it's a bit strange.

Quoted 2 lines

It's definitely a case-by-case decision which way is better, but setting all fields by hand gives you potential for missing to set one.

Right. I'll let Nikunj judge here as he looked at that part of the protocol more than I have.

Cheers,
Ben.

#14 lore raw
In reply to Alexander Graf
Quoted 23 lines · Alexander Graf

Am 26.08.2013 um 05:32 schrieb Nikunj A Dadhania <[email protected]>:

Quoted 21 lines
Quoted 18 lines · Benjamin Herrenschmidt
Quoted 9 lines · Alexander Graf

Review

Quoted 3 lines
(quoted diff) +3 -0
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap,

be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

BTW. While I disagree with your initial comment ... is there any bound checking here ? That looks like potential stack corruption unless I miss something if the guest passes a too big length...

So at least the length should be read once, bound-checked, then the read done with the result (don't bound check and read again, that would be indeed racy).

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
hw/scsi/spapr_vscsi.c +44 -0
index e9090e5..0758263 100644
@@ -858,6 +858,47 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len;
+    uint64_t buffer;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    len = be16_to_cpu(vcap->common.length);
+    buffer = be64_to_cpu(vcap->buffer);
+    if (len > sizeof(cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        rc = H_PARAMETER;
+        goto error_out;
+    }
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
     union mad_iu *mad = &req->iu.mad;
@@ -878,6 +919,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
         mad->host_config.common.status = cpu_to_be16(1);
         vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
         break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
     default:
         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                 be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.3.1
#15 lore raw
Quoted 53 lines · Nikunj A Dadhania
Quoted 23 lines · Alexander Graf

Am 26.08.2013 um 05:32 schrieb Nikunj A Dadhania <[email protected]>:

Quoted 21 lines
Quoted 18 lines · Benjamin Herrenschmidt
Quoted 9 lines · Alexander Graf

Review

Quoted 3 lines
(quoted diff) +3 -0
+    vcap = &req->iu.mad.capabilities;
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap,

be16_to_cpu(vcap->common.length));

While I don't think any harm could happen from it, this could lead to a potential timing attack where we read and write from different locations in memory if the guest swizzles the request while we're processing it.

BTW. While I disagree with your initial comment ... is there any bound checking here ? That looks like potential stack corruption unless I miss something if the guest passes a too big length...

So at least the length should be read once, bound-checked, then the read done with the result (don't bound check and read again, that would be indeed racy).

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
hw/scsi/spapr_vscsi.c | 44 ++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 44 insertions(+)
hw/scsi/spapr_vscsi.c +9 -0
index e9090e5..0758263 100644
@@ -858,6 +858,47 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len;
+    uint64_t buffer;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 879

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+    len = be16_to_cpu(vcap->common.length);
+    buffer = be64_to_cpu(vcap->buffer);
+    if (len > sizeof(cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        rc = H_PARAMETER;
+        goto error_out;
+    }
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");

At this point cap contains random host data, no?

Quoted 8 lines
hw/scsi/spapr_vscsi.c +8 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:

This is just "out" rather than "error_out", no? We also get here when we don't hit an error.

Alex

Quoted 20 lines
hw/scsi/spapr_vscsi.c +7 -0
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
    union mad_iu *mad = &req->iu.mad;
@@ -878,6 +919,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
        mad->host_config.common.status = cpu_to_be16(1);
        vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
        break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
    default:
        fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.3.1
#16 lore raw
Quoted 6 lines · Alexander Graf
Quoted 4 lines · Nikunj A Dadhania

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

Quoted 7 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 879

Quoted 5 lines

---

hw/scsi/spapr_vscsi.c +3 -0
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");

At this point cap contains random host data, no?

Yes, and we zero it out in this case.

Quoted 13 lines
Quoted 13 lines
hw/scsi/spapr_vscsi.c +12 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
Quoted 8 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 6 lines
hw/scsi/spapr_vscsi.c +6 -0
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:

This is just "out" rather than "error_out", no? We also get here when we don't hit an error.

Yes, the label is more readable at the goto, where we set the error return code. In case of no error return code is H_SUCCESS. So that we send a response back to the guest.

Regards
Nikunj

#17 lore raw
Quoted 14 lines · Nikunj A Dadhania
Quoted 12 lines · Alexander Graf
Quoted 9 lines · Nikunj A Dadhania

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---

hw/scsi/spapr_vscsi.c +3 -0
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
Quoted 3 lines
Quoted 1 line

At this point cap contains random host data, no?

Yes, and we zero it out in this case.

Then please make this obvious to the reader. Either memset(0) it or do cap = { };. But do something that doesn't make me look up the header file to check whether you really did catch all the fields there are in the struct.

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
hw/scsi/spapr_vscsi.c +12 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
Quoted 8 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 6 lines
hw/scsi/spapr_vscsi.c +6 -0
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:

This is just "out" rather than "error_out", no? We also get here when we don't hit an error.

Quoted 3 lines

Yes, the label is more readable at the goto, where we set the error return code. In case of no error return code is H_SUCCESS. So that we send a response back to the guest.

... which means we're not jumping into an error-only label.

Alex

Quoted 1 line
#18 lore raw
Quoted 4 lines · Alexander Graf
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

At this point cap contains random host data, no?

Yes, and we zero it out in this case.

Then please make this obvious to the reader. Either memset(0) it or do cap = { };. But do something that doesn't make me look up the header file to check whether you really did catch all the fields there are in the struct.

Sure

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 11 lines
hw/scsi/spapr_vscsi.c +8 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:

This is just "out" rather than "error_out", no? We also get here when we don't hit an error.

Quoted 5 lines
Quoted 3 lines

Yes, the label is more readable at the goto, where we set the error return code. In case of no error return code is H_SUCCESS. So that we send a response back to the guest.

... which means we're not jumping into an error-only label.

True.

Regards
Nikunj

#19 lore raw
In reply to Alexander Graf
Quoted 16 lines · Alexander Graf
Quoted 14 lines · Nikunj A Dadhania
Quoted 12 lines · Alexander Graf
Quoted 9 lines · Nikunj A Dadhania

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---

(quoted diff) +3 -0
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

At this point cap contains random host data, no?

Yes, and we zero it out in this case.

Then please make this obvious to the reader. Either memset(0) it or do cap = { };. But do something that doesn't make me look up the header file to check whether you really did catch all the fields there are in the struct.

Here is the patch incorporating the comments, moreover goto is not there anymore, we are being more accomodative and copying only the size(cap) structure even if guest is requesting for more.

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between vscsi host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
hw/scsi/spapr_vscsi.c +54 -0
index e9090e5..030b775 100644
@@ -858,6 +858,57 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap = { };
+    uint16_t len, req_len;
+    uint64_t buffer;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    req_len = len = be16_to_cpu(vcap->common.length);
+    buffer = be64_to_cpu(vcap->buffer);
+    if (len > sizeof(cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");
+
+        /*
+         * Just read and populate the structure that is known.
+         * Zero rest of the structure.
+         */
+        len = sizeof(cap);
+    }
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+    if (req_len > len) {
+        /*
+         * Being paranoid and lets not worry about the error code
+         * here. Actual write of the cap is done above.
+         */
+        spapr_vio_dma_set(&s->vdev, (buffer + len), 0, (req_len - len));
+    }
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
     union mad_iu *mad = &req->iu.mad;
@@ -878,6 +929,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
         mad->host_config.common.status = cpu_to_be16(1);
         vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
         break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
     default:
         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                 be32_to_cpu(mad->empty_iu.common.type));
Signature · 1 line
1.8.3.1
#20 lore raw
Quoted 63 lines · Nikunj A Dadhania
Quoted 16 lines · Alexander Graf
Quoted 14 lines · Nikunj A Dadhania
Quoted 12 lines · Alexander Graf
Quoted 9 lines · Nikunj A Dadhania

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---

hw/scsi/spapr_vscsi.c +3 -0
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

At this point cap contains random host data, no?

Yes, and we zero it out in this case.

Then please make this obvious to the reader. Either memset(0) it or do cap = { };. But do something that doesn't make me look up the header file to check whether you really did catch all the fields there are in the struct.

Here is the patch incorporating the comments, moreover goto is not there anymore, we are being more accomodative and copying only the size(cap) structure even if guest is requesting for more.

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between vscsi host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
hw/scsi/spapr_vscsi.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 54 insertions(+)
hw/scsi/spapr_vscsi.c +13 -0
index e9090e5..030b775 100644
@@ -858,6 +858,57 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
    return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap = { };
+    uint16_t len, req_len;
+    uint64_t buffer;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    req_len = len = be16_to_cpu(vcap->common.length);
+    buffer = be64_to_cpu(vcap->buffer);
+    if (len > sizeof(cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 883

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+
+        /*
+         * Just read and populate the structure that is known.
+         * Zero rest of the structure.
+         */
+        len = sizeof(cap);
+    }
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");

... if we fail reading

Quoted 4 lines
hw/scsi/spapr_vscsi.c +4 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);

... but then succeed writing back the empty structure we will report "success". Is this correct? It's fine with me if it's intended, just want to make sure we're making a conscious decision about it.

The rest looks good to me. But please post new patch versions as new top level emails, not as inline replies. My scripts use patchwork and that can't really handle this case overly well (it works, but is additional work). It also makes it hard for people who just follow the thread loosely to realize that a new version is there.

Alex

#21 lore raw
Quoted 36 lines · Alexander Graf
Quoted 13 lines
hw/scsi/spapr_vscsi.c +13 -0
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap = { };
+    uint16_t len, req_len;
+    uint64_t buffer;
+    int rc;
+
+    vcap = &req->iu.mad.capabilities;
+    req_len = len = be16_to_cpu(vcap->common.length);
+    buffer = be64_to_cpu(vcap->buffer);
+    if (len > sizeof(cap)) {
+        fprintf(stderr, "vscsi_send_capabilities: capabilities size mismatch !\n");

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 883

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+
+        /*
+         * Just read and populate the structure that is known.
+         * Zero rest of the structure.
+         */
+        len = sizeof(cap);
+    }
+    rc = spapr_vio_dma_read(&s->vdev, buffer, &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");

... if we fail reading

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+    }
+
+    /*
+     * Current implementation does not suppport any migration or
+     * reservation capabilities. Construct the response telling the
+     * guest not to use them.
+     */
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
Quoted 8 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 897

Quoted 4 lines
hw/scsi/spapr_vscsi.c +4 -0
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+
+    rc = spapr_vio_dma_write(&s->vdev, buffer, &cap, len);

... but then succeed writing back the empty structure we will report "success". Is this correct? It's fine with me if it's intended, just want to make sure we're making a conscious decision about it.

Yes, that should be fine if it succeeds writing it. If DMA read/write is an issue, it should fail writing as well and we report back failure.

Quoted 6 lines

The rest looks good to me. But please post new patch versions as new top level emails, not as inline replies. My scripts use patchwork and that can't really handle this case overly well (it works, but is additional work). It also makes it hard for people who just follow the thread loosely to realize that a new version is there.

Sure, will take care next time.

Regards
Nikunj

#22 lore raw
In reply to Nikunj A Dadhania
Quoted 17 lines · Nikunj A Dadhania

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
hw/scsi/spapr_vscsi.c +0 -0
index e9090e5..fae3644 100644

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 865

Quoted 8 lines
hw/scsi/spapr_vscsi.c +5 -0
@@ -858,6 +858,40 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len = 0;

The above initialization isn't useful

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 870

Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    int rc = true;
+
+    vcap = &req->iu.mad.capabilities;
+    len = be16_to_cpu(vcap->common.length);
+    if (len > sizeof(&cap)) {

^ Ugh ? Why the & here ?

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 873

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        goto error_out;
+    }

I am not 100% familiar with the protocol, could it be that we should just read sizeof(cap) instead of erroring out or is there no way it can be correct and have a len too long ?

Quoted 34 lines
hw/scsi/spapr_vscsi.c +24 -0
+    rc = spapr_vio_dma_read(&s->vdev, be64_to_cpu(vcap->buffer),
+                            &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA read failure !\n");
+    }
+
+    cap.flags = 0;
+    cap.migration.ecl = 0;
+    cap.reserve.type = 0;
+    cap.migration.common.server_support = 0;
+    cap.reserve.common.server_support = 0;
+    rc = spapr_vio_dma_write(&s->vdev, be64_to_cpu(vcap->buffer),
+                             &cap, len);
+    if (rc)  {
+        fprintf(stderr, "vscsi_send_capabilities: DMA write failure !\n");
+    }
+error_out:
+    vcap->common.status = rc ? cpu_to_be32(1) : 0;
+    return vscsi_send_iu(s, req, sizeof(*vcap), VIOSRP_MAD_FORMAT);
+}
+
 static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
 {
     union mad_iu *mad = &req->iu.mad;
@@ -878,6 +912,9 @@ static int vscsi_handle_mad_req(VSCSIState *s, vscsi_req *req)
         mad->host_config.common.status = cpu_to_be16(1);
         vscsi_send_iu(s, req, sizeof(mad->host_config), VIOSRP_MAD_FORMAT);
         break;
+    case VIOSRP_CAPABILITIES_TYPE:
+        vscsi_send_capabilities(s, req);
+        break;
     default:
         fprintf(stderr, "VSCSI: Unknown MAD type %02x\n",
                 be32_to_cpu(mad->empty_iu.common.type));
#23 lore raw
Quoted 30 lines · Benjamin Herrenschmidt
Quoted 17 lines · Nikunj A Dadhania

From: Nikunj A Dadhania <[email protected]>

This implements capabilities exchange between host and client. As at the moment no capability is supported, put zero flags everywhere and return.

---
 hw/scsi/spapr_vscsi.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
hw/scsi/spapr_vscsi.c +0 -0
index e9090e5..fae3644 100644

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 865

Quoted 8 lines
hw/scsi/spapr_vscsi.c +5 -0
@@ -858,6 +858,40 @@ static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req)
     return vscsi_send_iu(s, req, sizeof(*sinfo), VIOSRP_MAD_FORMAT);
 }
 
+static int vscsi_send_capabilities(VSCSIState *s, vscsi_req *req)
+{
+    struct viosrp_capabilities *vcap;
+    struct capabilities cap;
+    uint16_t len = 0;

The above initialization isn't useful

Quoted 6 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 870

Quoted 5 lines
hw/scsi/spapr_vscsi.c +5 -0
+    int rc = true;
+
+    vcap = &req->iu.mad.capabilities;
+    len = be16_to_cpu(vcap->common.length);
+    if (len > sizeof(&cap)) {

^ Ugh ? Why the & here ?

Oops, got that wrong.

Quoted 8 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 873

Quoted 4 lines
hw/scsi/spapr_vscsi.c +3 -0
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        goto error_out;
+    }

I am not 100% familiar with the protocol, could it be that we should just read sizeof(cap) instead of erroring out or is there no way it can be correct and have a len too long ?

If the length is incorrect, can we trust whether cap is correct or is of the type we are expecting?

#24 lore raw

Il 26/08/2013 11:06, Nikunj A Dadhania ha scritto:

Quoted 10 lines
Quoted 7 lines

hw/scsi/spapr_vscsi.c static int vscsi_send_adapter_info(VSCSIState *s, vscsi_req *req) · line 873

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        goto error_out;
+    }

I am not 100% familiar with the protocol, could it be that we should just read sizeof(cap) instead of erroring out or is there no way it can be correct and have a len too long ?

If the length is incorrect, can we trust whether cap is correct or is of the type we are expecting?

We shouldn't care, it'd be a guest bug.

If the guest is asking for say 1024 bytes, we do not have to fill all of them. It is in principle possible that a subsequent revision of vscsi will make the struct larger; perhaps a bit in the first part of the struct will tell the guest if the second part has been filled.

Unless the spec explicitly say the opposite, I would just zero the bytes between sizeof(cap) and len.

Paolo

#25 lore raw
Quoted 5 lines · Paolo Bonzini

Il 26/08/2013 11:06, Nikunj A Dadhania ha scritto:

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        fprintf(stderr, "vscsi_send_capabilities: size out of bound !\n");
+        goto error_out;
+    }
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am not 100% familiar with the protocol, could it be that we should just read sizeof(cap) instead of erroring out or is there no way it can be correct and have a len too long ?

If the length is incorrect, can we trust whether cap is correct or is of the type we are expecting?

We shouldn't care, it'd be a guest bug.

Then we can do a warning on the size and set only the parts supported.

This is a kind of negotiating capabilities, where the guest says that I can support following vscsi capabilities, hypervisor if it has implemented them should return back with affirmative for the capabilities supported. If not, tell the guest that hypervisor cannot support.

Quoted 8 lines

If the guest is asking for say 1024 bytes, we do not have to fill all of them. It is in principle possible that a subsequent revision of vscsi will make the struct larger; perhaps a bit in the first part of the struct will tell the guest if the second part has been filled.

Unless the spec explicitly say the opposite, I would just zero the bytes between sizeof(cap) and len.

Makes sense. I will change the patch accordingly.

Regards
Nikunj