Skip to contentSkip to conversation

v9fs_walk: As per 9p2000 RFC, MAXWELEM >= nwnames >= 0.

PATCH Superseded Harsh Prateek Bora 3 participants 4 messages Mar 21, 2011

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 v9fs_walk: As per 9p2000 RFC, MAXWELEM >= nwnames >= 0.

The nwnames field in TWALK message is assumed to be >=0 and <= MAXWELEM which is defined as macro P9_MAXWELEM (16) in virtio-9p.h as per 9p2000 RFC. Appropriate changes are required in V9fsWalkState and v9fs_walk.

---
 hw/9pfs/virtio-9p.c |    5 ++++-
 hw/9pfs/virtio-9p.h |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p.c +4 -1
index 9b44bd0..b782a19 100644
@@ -1805,7 +1805,7 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
     vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
                                             &newfid, &vs->nwnames);
 
-    if (vs->nwnames) {
+    if (vs->nwnames <= P9_MAXWELEM) {
         vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
 
         vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
@@ -1814,6 +1814,9 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
             vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
                                             &vs->wnames[i]);
         }
+    } else {
+        err = -EINVAL;
+        goto out;
     }
 
     vs->fidp = lookup_fid(s, fid);
hw/9pfs/virtio-9p.h +1 -1
index e7d2326..119b12c 100644
@@ -307,7 +307,7 @@ typedef struct V9fsStatStateDotl {
 typedef struct V9fsWalkState {
     V9fsPDU *pdu;
     size_t offset;
-    int16_t nwnames;
+    uint16_t nwnames;
     int name_idx;
     V9fsQID *qids;
     V9fsFidState *fidp;
Signature · 1 line
1.7.1.1
#1 lore raw
Quoted 24 lines · Harsh Prateek Bora

The nwnames field in TWALK message is assumed to be >=0 and <= MAXWELEM which is defined as macro P9_MAXWELEM (16) in virtio-9p.h as per 9p2000 RFC. Appropriate changes are required in V9fsWalkState and v9fs_walk.

---
 hw/9pfs/virtio-9p.c |    5 ++++-
 hw/9pfs/virtio-9p.h |    2 +-

 2 files changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p.c +1 -1
index 9b44bd0..b782a19 100644
@@ -1805,7 +1805,7 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
     vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
                                             &newfid, &vs->nwnames);
 
-    if (vs->nwnames) {
+    if (vs->nwnames <= P9_MAXWELEM) {
         vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
 
         vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);

hw/9pfs/virtio-9p.c static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) · line 1820

Quoted 7 lines
hw/9pfs/virtio-9p.c +3 -0
@@ -1814,6 +1814,9 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
             vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
                                             &vs->wnames[i]);
         }
+    } else {
+        err = -EINVAL;
+        goto out;
     }

v9fs_walk_complete() will attempt to free wnames, qids, and the wnames strings. Freeing the strings will crash because we're indexing into an array based off a NULL pointer.

It would be very handy to have a PDU-level test suite. You could then construct a PDU with an invalid nwnames field and exercise this code path. Perhaps a debug ioctl in Linux v9fs that allows a userspace tool to pass PDUs through will do?

Stefan

#2 lore raw
Quoted 24 lines · Stefan Hajnoczi
Quoted 22 lines · Harsh Prateek Bora

The nwnames field in TWALK message is assumed to be >=0 and <= MAXWELEM which is defined as macro P9_MAXWELEM (16) in virtio-9p.h as per 9p2000 RFC. Appropriate changes are required in V9fsWalkState and v9fs_walk.

---
 hw/9pfs/virtio-9p.c |    5 ++++-
 hw/9pfs/virtio-9p.h |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p.c +1 -1
index 9b44bd0..b782a19 100644
@@ -1805,7 +1805,7 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
                                            &newfid, &vs->nwnames);
 
-    if (vs->nwnames) {
+    if (vs->nwnames <= P9_MAXWELEM) {
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
 
Quoted 7 lines

hw/9pfs/virtio-9p.c static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) · line 1818

Quoted 7 lines
hw/9pfs/virtio-9p.c +2 -0
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
@@ -1814,6 +1814,9 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
                                            &vs->wnames[i]);
        }
+    } else {
+        err = -EINVAL;

vs->nwnames = 0; will take care of v9fs_walk_complete() issue Stefan brought up.

Quoted 11 lines

hw/9pfs/virtio-9p.c static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) · line 1820

Quoted 2 lines
hw/9pfs/virtio-9p.c +1 -0
+        goto out;
    }

v9fs_walk_complete() will attempt to free wnames, qids, and the wnames strings. Freeing the strings will crash because we're indexing into an array based off a NULL pointer.

It would be very handy to have a PDU-level test suite. You could then construct a PDU with an invalid nwnames field and exercise this code path. Perhaps a debug ioctl in Linux v9fs that allows a userspace tool to pass PDUs through will do?

Ioctl way was ruled out in the past discussion. But yes we have in plan on to introduce
a test suite of this kind. Malahal (who is on vacation) will start this work from early April.

Thanks,
JV

Quoted 2 lines

Stefan

#3 lore raw
Quoted 25 lines · Venkateswararao Jujjuri (JV)
Quoted 24 lines · Stefan Hajnoczi
Quoted 22 lines · Harsh Prateek Bora

The nwnames field in TWALK message is assumed to be >=0 and <= MAXWELEM which is defined as macro P9_MAXWELEM (16) in virtio-9p.h as per 9p2000 RFC. Appropriate changes are required in V9fsWalkState and v9fs_walk.

---
 hw/9pfs/virtio-9p.c |    5 ++++-
 hw/9pfs/virtio-9p.h |    2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/virtio-9p.c +1 -1
index 9b44bd0..b782a19 100644
@@ -1805,7 +1805,7 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
    vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "ddw", &fid,
                                            &newfid, &vs->nwnames);
 
-    if (vs->nwnames) {
+    if (vs->nwnames <= P9_MAXWELEM) {
        vs->wnames = qemu_mallocz(sizeof(vs->wnames[0]) * vs->nwnames);
 
Quoted 9 lines
Quoted 7 lines

hw/9pfs/virtio-9p.c static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) · line 1818

Quoted 7 lines
hw/9pfs/virtio-9p.c +2 -0
        vs->qids = qemu_mallocz(sizeof(vs->qids[0]) * vs->nwnames);
@@ -1814,6 +1814,9 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu)
            vs->offset += pdu_unmarshal(vs->pdu, vs->offset, "s",
                                            &vs->wnames[i]);
        }
+    } else {
+        err = -EINVAL;

vs->nwnames = 0; will take care of v9fs_walk_complete() issue Stefan brought up.

Quoted 1 line
Quoted 1 line
hw/9pfs/virtio-9p.c +1 -0
+        goto out;

Or you can have same check "if (vs->nwnames <= P9_MAXWELEM) {" in the v9fs_walk_complete() too; basically you need to be consistant with the checking.

- JV

Quoted 22 lines
Quoted 10 lines

hw/9pfs/virtio-9p.c static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) · line 1820

Quoted 1 line
hw/9pfs/virtio-9p.c +0 -0
    }

v9fs_walk_complete() will attempt to free wnames, qids, and the wnames strings. Freeing the strings will crash because we're indexing into an array based off a NULL pointer.

It would be very handy to have a PDU-level test suite. You could then construct a PDU with an invalid nwnames field and exercise this code path. Perhaps a debug ioctl in Linux v9fs that allows a userspace tool to pass PDUs through will do?

Ioctl way was ruled out in the past discussion. But yes we have in plan on to introduce
a test suite of this kind. Malahal (who is on vacation) will start this work from early April.

Thanks,
JV

Quoted 2 lines

Stefan