Skip to contentSkip to conversation

9pfs: fix potential segfault during walk

PATCH In review Greg Kurz 2 participants 5 messages Sep 15, 2016 – Sep 16, 2016

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 9pfs: fix potential segfault during walk

If the call to fid_to_qid() returns an error, we will call v9fs_path_free() on uninitialized paths.

Let's fix this by initializing dpath and path before calling fid_to_qid().

---

Thanks Paolo (and Coverity) for spotting this.

Cc'ing stable as this is a regression introduced in 2.7. It is also present in Michael's stable-2.6-staging branch.

 hw/9pfs/9p.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +3 -2
index dfe293d11d1c..91a497079acb 100644
@@ -1320,13 +1320,14 @@ static void v9fs_walk(void *opaque)
         goto out_nofid;
     }
 
+    v9fs_path_init(&dpath);
+    v9fs_path_init(&path);
+
     err = fid_to_qid(pdu, fidp, &qid);
     if (err < 0) {
         goto out;
     }
 
-    v9fs_path_init(&dpath);
-    v9fs_path_init(&path);
     /*
      * Both dpath and path initially poin to fidp.
      * Needed to handle request with nwnames == 0
author
#1 lore raw
Quoted 3 lines · On Fri, 16 Sep 2016 01:05:11 +0200 Greg Kurz <[email protected]> wrote:

If the call to fid_to_qid() returns an error, we will call v9fs_path_free() on uninitialized paths.

I'll add this to the changelog:

It is a regression introduced by the following commit:

56f101ecce0e 9pfs: handle walk of ".." in the root directory

Quoted 35 lines

Let's fix this by initializing dpath and path before calling fid_to_qid().

---

Thanks Paolo (and Coverity) for spotting this.

Cc'ing stable as this is a regression introduced in 2.7. It is also present in Michael's stable-2.6-staging branch.

 hw/9pfs/9p.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +3 -2
index dfe293d11d1c..91a497079acb 100644
@@ -1320,13 +1320,14 @@ static void v9fs_walk(void *opaque)
         goto out_nofid;
     }
 
+    v9fs_path_init(&dpath);
+    v9fs_path_init(&path);
+
     err = fid_to_qid(pdu, fidp, &qid);
     if (err < 0) {
         goto out;
     }
 
-    v9fs_path_init(&dpath);
-    v9fs_path_init(&path);
     /*
      * Both dpath and path initially poin to fidp.
      * Needed to handle request with nwnames == 0
#2 lore raw
Quoted 42 lines · Greg Kurz
Quoted 3 lines · On Fri, 16 Sep 2016 01:05:11 +0200 Greg Kurz <[email protected]> wrote:

If the call to fid_to_qid() returns an error, we will call v9fs_path_free() on uninitialized paths.

I'll add this to the changelog:

It is a regression introduced by the following commit:

56f101ecce0e 9pfs: handle walk of ".." in the root directory

Quoted 29 lines

Let's fix this by initializing dpath and path before calling fid_to_qid().

---

Thanks Paolo (and Coverity) for spotting this.

Cc'ing stable as this is a regression introduced in 2.7. It is also present in Michael's stable-2.6-staging branch.

 hw/9pfs/9p.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +3 -0
index dfe293d11d1c..91a497079acb 100644
@@ -1320,13 +1320,14 @@ static void v9fs_walk(void *opaque)
         goto out_nofid;
     }
 
+    v9fs_path_init(&dpath);
+    v9fs_path_init(&path);
+
     err = fid_to_qid(pdu, fidp, &qid);
     if (err < 0) {
         goto out;
     }
 
Quoted 8 lines

hw/9pfs/9p.c static void v9fs_walk(void *opaque) · line 1333

Quoted 6 lines
hw/9pfs/9p.c +0 -2
-    v9fs_path_init(&dpath);
-    v9fs_path_init(&path);
     /*
      * Both dpath and path initially poin to fidp.
      * Needed to handle request with nwnames == 0

There is still a possible segv I think, in out_nofid :

    if (nwnames && nwnames <= P9_MAXWELEM) {
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
            v9fs_string_free(&wnames[name_idx]);

&wnames[name_idx] could NULL if pdu_unmarshal() fails.

C.

author
#3 lore raw
Quoted 53 lines · On Fri, 16 Sep 2016 09:37:48 +0200 Cédric Le Goater <[email protected]> wrote:
Quoted 42 lines · Greg Kurz
Quoted 3 lines · On Fri, 16 Sep 2016 01:05:11 +0200 Greg Kurz <[email protected]> wrote:

If the call to fid_to_qid() returns an error, we will call v9fs_path_free() on uninitialized paths.

I'll add this to the changelog:

It is a regression introduced by the following commit:

56f101ecce0e 9pfs: handle walk of ".." in the root directory

Quoted 29 lines

Let's fix this by initializing dpath and path before calling fid_to_qid().

---

Thanks Paolo (and Coverity) for spotting this.

Cc'ing stable as this is a regression introduced in 2.7. It is also present in Michael's stable-2.6-staging branch.

 hw/9pfs/9p.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +3 -0
index dfe293d11d1c..91a497079acb 100644
@@ -1320,13 +1320,14 @@ static void v9fs_walk(void *opaque)
         goto out_nofid;
     }
 
+    v9fs_path_init(&dpath);
+    v9fs_path_init(&path);
+
     err = fid_to_qid(pdu, fidp, &qid);
     if (err < 0) {
         goto out;
     }
 
Quoted 8 lines

hw/9pfs/9p.c static void v9fs_walk(void *opaque) · line 1333

Quoted 6 lines
hw/9pfs/9p.c +0 -2
-    v9fs_path_init(&dpath);
-    v9fs_path_init(&path);
     /*
      * Both dpath and path initially poin to fidp.
      * Needed to handle request with nwnames == 0
Quoted 8 lines

There is still a possible segv I think, in out_nofid :

    if (nwnames && nwnames <= P9_MAXWELEM) {
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
            v9fs_string_free(&wnames[name_idx]);

&wnames[name_idx] could NULL if pdu_unmarshal() fails.

We're safe because wnames[] is fully allocated and zeroed:

    if (nwnames && nwnames <= P9_MAXWELEM) {
        wnames = g_malloc0(sizeof(wnames[0]) * nwnames); <--- here
        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
        for (i = 0; i < nwnames; i++) {
            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
            if (err < 0) {
                goto out_nofid;
            }

But I agree that the calls to v9fs_string_free() without the corresponding calls to v9fs_string_init(), as it is done everywhere else, is disturbing and should be addressed.

Thanks.

--
Greg

Quoted 7 lines

C.

Reviewed-by
#4 lore raw
Quoted 58 lines · Greg Kurz
Quoted 55 lines · On Fri, 16 Sep 2016 09:37:48 +0200 Cédric Le Goater <[email protected]> wrote:
Quoted 42 lines · Greg Kurz
Quoted 3 lines · On Fri, 16 Sep 2016 01:05:11 +0200 Greg Kurz <[email protected]> wrote:

If the call to fid_to_qid() returns an error, we will call v9fs_path_free() on uninitialized paths.

I'll add this to the changelog:

It is a regression introduced by the following commit:

56f101ecce0e 9pfs: handle walk of ".." in the root directory

Quoted 29 lines

Let's fix this by initializing dpath and path before calling fid_to_qid().

---

Thanks Paolo (and Coverity) for spotting this.

Cc'ing stable as this is a regression introduced in 2.7. It is also present in Michael's stable-2.6-staging branch.

 hw/9pfs/9p.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +3 -0
index dfe293d11d1c..91a497079acb 100644
@@ -1320,13 +1320,14 @@ static void v9fs_walk(void *opaque)
         goto out_nofid;
     }
 
+    v9fs_path_init(&dpath);
+    v9fs_path_init(&path);
+
     err = fid_to_qid(pdu, fidp, &qid);
     if (err < 0) {
         goto out;
     }
 
Quoted 8 lines

hw/9pfs/9p.c static void v9fs_walk(void *opaque) · line 1333

Quoted 6 lines
hw/9pfs/9p.c +0 -2
-    v9fs_path_init(&dpath);
-    v9fs_path_init(&path);
     /*
      * Both dpath and path initially poin to fidp.
      * Needed to handle request with nwnames == 0

There is still a possible segv I think, in out_nofid :

Quoted 8 lines
Quoted 6 lines
    if (nwnames && nwnames <= P9_MAXWELEM) {
        for (name_idx = 0; name_idx < nwnames; name_idx++) {
            v9fs_string_free(&wnames[name_idx]);

&wnames[name_idx] could NULL if pdu_unmarshal() fails.

We're safe because wnames[] is fully allocated and zeroed:

ah yes. I got confused by the structures.

C.

Quoted 26 lines
    if (nwnames && nwnames <= P9_MAXWELEM) {
        wnames = g_malloc0(sizeof(wnames[0]) * nwnames); <--- here
        qids   = g_malloc0(sizeof(qids[0]) * nwnames);
        for (i = 0; i < nwnames; i++) {
            err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
            if (err < 0) {
                goto out_nofid;
            }

But I agree that the calls to v9fs_string_free() without the corresponding calls to v9fs_string_init(), as it is done everywhere else, is disturbing and should be addressed.

Thanks.

--
Greg

Quoted 7 lines

C.