Skip to contentSkip to conversation

9pfs: fix xattr issues

PATCH Superseded Li Qiang 3 participants 12 messages Oct 9, 2016 – Oct 13, 2016

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Qiang

author
#0 lore raw

cover 9pfs: fix xattr issues

From: Li Qiang <[email protected]>

Hello,

This series fix two security issues, the first issue is caused by uninitialized heap, and the other is caused by no considering free the buffer allocated previously.

Li Qiang (2):
9pfs: fix information leak in xattr read
9pfs: fix memory leak about xattr value

 hw/9pfs/9p.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 9pfs: fix information leak in xattr read

From: Li Qiang <[email protected]>

9pfs uses g_malloc() to allocate the xattr memory space, if the guest reads this memory before writing to it, this will leak host heap memory to the guest. This patch avoid this.

---
 hw/9pfs/9p.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/9pfs/9p.c +1 -1
index 119ee58..8751c19 100644
@@ -3282,7 +3282,7 @@ static void v9fs_xattrcreate(void *opaque)
     xattr_fidp->fs.xattr.flags = flags;
     v9fs_string_init(&xattr_fidp->fs.xattr.name);
     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
-    xattr_fidp->fs.xattr.value = g_malloc(size);
+    xattr_fidp->fs.xattr.value = g_malloc0(size);
     err = offset;
     put_fid(pdu, file_fidp);
 out_nofid:
Signature · 1 line
1.8.3.1
author
#8 lore raw

PATCH 2/2 9pfs: fix memory leak about xattr value

In reply to Li Qiang

From: Li Qiang <[email protected]>

The 'fs.xattr.value' field in V9fsFidState object doesn't consider the situation that this field has been allocated previously. Every time, it will be allocated directly. This leads a host memory leak issue. This patch fix this.

---
 hw/9pfs/9p.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +5 -2
index 8751c19..3af23f9 100644
@@ -350,6 +350,7 @@ free_out:
     v9fs_string_free(&fidp->fs.xattr.name);
 free_value:
     g_free(fidp->fs.xattr.value);
+    fidp->fs.xattr.value = NULL;
     return retval;
 }
 
@@ -3191,7 +3192,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
                                      xattr_fidp->fs.xattr.value,
                                      xattr_fidp->fs.xattr.len);
@@ -3224,7 +3226,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
                                     &name, xattr_fidp->fs.xattr.value,
                                     xattr_fidp->fs.xattr.len);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

In reply to Li Qiang
Quoted 8 lines · On Sat, 8 Oct 2016 22:26:51 -0700 Li Qiang <[email protected]> wrote:

From: Li Qiang <[email protected]>

9pfs uses g_malloc() to allocate the xattr memory space, if the guest reads this memory before writing to it, this will leak host heap memory to the guest. This patch avoid this.

---

I've looked again and we could theorically defer allocation until v9fs_xattr_write() is called, and only allow v9fs_xattr_read() to return bytes previously written by the client. But this would result in rather complex code to handle partial writes and reads.

So this patch looks like the way to go.

Quoted 16 lines
 hw/9pfs/9p.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/9pfs/9p.c +1 -1
index 119ee58..8751c19 100644
@@ -3282,7 +3282,7 @@ static void v9fs_xattrcreate(void *opaque)
     xattr_fidp->fs.xattr.flags = flags;
     v9fs_string_init(&xattr_fidp->fs.xattr.name);
     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
-    xattr_fidp->fs.xattr.value = g_malloc(size);
+    xattr_fidp->fs.xattr.value = g_malloc0(size);
     err = offset;
     put_fid(pdu, file_fidp);
 out_nofid:
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] 9pfs: fix memory leak about xattr value

In reply to Li Qiang
Quoted 7 lines · On Sat, 8 Oct 2016 22:27:08 -0700 Li Qiang <[email protected]> wrote:

From: Li Qiang <[email protected]>

The 'fs.xattr.value' field in V9fsFidState object doesn't consider the situation that this field has been allocated previously. Every time, it will be allocated directly. This leads a host memory leak issue. This patch fix this.

This cannot happen because v9fs_xattrwalk() always allocates this fid:

    xattr_fidp = alloc_fid(s, newfid);
    if (xattr_fidp == NULL) {
        err = -EINVAL;
        goto out;
    }
Quoted 37 lines
---
 hw/9pfs/9p.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +5 -2
index 8751c19..3af23f9 100644
@@ -350,6 +350,7 @@ free_out:
     v9fs_string_free(&fidp->fs.xattr.name);
 free_value:
     g_free(fidp->fs.xattr.value);
+    fidp->fs.xattr.value = NULL;
     return retval;
 }
 
@@ -3191,7 +3192,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
                                      xattr_fidp->fs.xattr.value,
                                      xattr_fidp->fs.xattr.len);
@@ -3224,7 +3226,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
                                     &name, xattr_fidp->fs.xattr.value,
                                     xattr_fidp->fs.xattr.len);
author
#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] 9pfs: fix memory leak about xattr value

Oops! I have made this patch to dress the memory leak in v9fs_xattrcreate but forget this and make another mistake.

I mean we should call g_free for xattr.value first in xattr create.

This avoid the memory leak.

static void v9fs_xattrcreate(void *opaque)
{
...

    g_free(xattr_fidp->fs.xattr.value);
    xattr_fidp->fs.xattr.value = g_malloc(size);
    err = offset;

}

Should I resend this path series?

2016-10-10 17:06 GMT+08:00 Greg Kurz <[email protected]>:

Quoted 58 lines
Quoted 7 lines · On Sat, 8 Oct 2016 22:27:08 -0700 Li Qiang <[email protected]> wrote:

From: Li Qiang <[email protected]>

The 'fs.xattr.value' field in V9fsFidState object doesn't consider the situation that this field has been allocated previously. Every time, it will be allocated directly. This leads a host memory leak issue. This patch fix this.

This cannot happen because v9fs_xattrwalk() always allocates this fid:

    xattr_fidp = alloc_fid(s, newfid);
    if (xattr_fidp == NULL) {
        err = -EINVAL;
        goto out;
    }
Quoted 37 lines
---
 hw/9pfs/9p.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +5 -2
index 8751c19..3af23f9 100644
@@ -350,6 +350,7 @@ free_out:
     v9fs_string_free(&fidp->fs.xattr.name);
 free_value:
     g_free(fidp->fs.xattr.value);
+    fidp->fs.xattr.value = NULL;
     return retval;
 }
 
@@ -3191,7 +3192,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
                                      xattr_fidp->fs.xattr.value,
                                      xattr_fidp->fs.xattr.len);
@@ -3224,7 +3226,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
                                     &name, xattr_fidp->fs.xattr.value,
                                     xattr_fidp->fs.xattr.len);
#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] 9pfs: fix memory leak about xattr value

Quoted 1 line · On Mon, 10 Oct 2016 17:15:10 +0800 Li Qiang <[email protected]> wrote:

Oops! I have made this patch to dress the memory leak in v9fs_xattrcreate

I guess you mean v9fs_xattrwalk().

There is no possible memory leak with v9fs_xattrwalk() because xattr_fidp comes from alloc_fid(). The leak can only occur when xattr_fidp comes from get_fid().

Quoted 6 lines

but forget this and make another mistake.

I mean we should call g_free for xattr.value first in xattr create.

This avoid the memory leak.

Quoted 9 lines

static void v9fs_xattrcreate(void *opaque)
{
...

    g_free(xattr_fidp->fs.xattr.value);
    xattr_fidp->fs.xattr.value = g_malloc(size);
    err = offset;

}

Indeed we have a potential leak with v9fs_xattrcreate().

Quoted 2 lines

Should I resend this path series?

No, you just need to send a patch that fixes the leak in v9fs_xattrcreate().

Quoted 60 lines

2016-10-10 17:06 GMT+08:00 Greg Kurz <[email protected]>:

Quoted 58 lines
Quoted 7 lines · On Sat, 8 Oct 2016 22:27:08 -0700 Li Qiang <[email protected]> wrote:

From: Li Qiang <[email protected]>

The 'fs.xattr.value' field in V9fsFidState object doesn't consider the situation that this field has been allocated previously. Every time, it will be allocated directly. This leads a host memory leak issue. This patch fix this.

This cannot happen because v9fs_xattrwalk() always allocates this fid:

    xattr_fidp = alloc_fid(s, newfid);
    if (xattr_fidp == NULL) {
        err = -EINVAL;
        goto out;
    }
Quoted 37 lines
---
 hw/9pfs/9p.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +5 -2
index 8751c19..3af23f9 100644
@@ -350,6 +350,7 @@ free_out:
     v9fs_string_free(&fidp->fs.xattr.name);
 free_value:
     g_free(fidp->fs.xattr.value);
+    fidp->fs.xattr.value = NULL;
     return retval;
 }
 
@@ -3191,7 +3192,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
                                      xattr_fidp->fs.xattr.value,
                                      xattr_fidp->fs.xattr.len);
@@ -3224,7 +3226,8 @@ static void v9fs_xattrwalk(void *opaque)
         xattr_fidp->fid_type = P9_FID_XATTR;
         xattr_fidp->fs.xattr.copied_len = -1;
         if (size) {
-            xattr_fidp->fs.xattr.value = g_malloc(size);
+            xattr_fidp->fs.xattr.value = g_realloc(
+                xattr_fidp->fs.xattr.value, size);
             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
                                     &name, xattr_fidp->fs.xattr.value,
                                     xattr_fidp->fs.xattr.len);
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

In reply to Greg Kurz
Quoted 12 lines · On Mon, 10 Oct 2016 10:56:03 +0200 Greg Kurz <[email protected]> wrote:
Quoted 8 lines · On Sat, 8 Oct 2016 22:26:51 -0700 Li Qiang <[email protected]> wrote:

From: Li Qiang <[email protected]>

9pfs uses g_malloc() to allocate the xattr memory space, if the guest reads this memory before writing to it, this will leak host heap memory to the guest. This patch avoid this.

---

Quoted 9 lines

I've looked again and we could theorically defer allocation until v9fs_xattr_write() is called, and only allow v9fs_xattr_read() to return bytes previously written by the client. But this would result in rather complex code to handle partial writes and reads.

So this patch looks like the way to go.

But in fact, I'm afraid we have a more serious problem here... size comes from the guest and could cause g_malloc() to abort if QEMU has reached some RLIMIT... we need to call g_try_malloc0() and return ENOMEM if the allocation fails.

Since this is yet another issue, I suggest you send another patch on top of this one... and maybe check other locations in the code where this could happen.

Cheers.

--
Greg

Quoted 18 lines
Quoted 16 lines
 hw/9pfs/9p.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/9pfs/9p.c +1 -1
index 119ee58..8751c19 100644
@@ -3282,7 +3282,7 @@ static void v9fs_xattrcreate(void *opaque)
     xattr_fidp->fs.xattr.flags = flags;
     v9fs_string_init(&xattr_fidp->fs.xattr.name);
     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
-    xattr_fidp->fs.xattr.value = g_malloc(size);
+    xattr_fidp->fs.xattr.value = g_malloc0(size);
     err = offset;
     put_fid(pdu, file_fidp);
 out_nofid:  
#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

Quoted 5 lines · Greg Kurz

But in fact, I'm afraid we have a more serious problem here... size comes from the guest and could cause g_malloc() to abort if QEMU has reached some RLIMIT... we need to call g_try_malloc0() and return ENOMEM if the allocation fails.

Even if it does not cause an ENOMEM failure right away, the guest can also use this to chew up lots of host resources. It may also be worth putting a reasonable cap at the maximum the guest can allocate, rather than just trying to malloc every possible size.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

Yes, I think the limit to apply to xattr size in 9pfs is the same as the Linux xattr size limit, I will try to find this limit.

Thanks.

Quoted 17 lines · On 2016-10-13 4:49 GMT+08:00 Eric Blake <[email protected]> wrote:
Quoted 5 lines · Greg Kurz

But in fact, I'm afraid we have a more serious problem here... size comes from the guest and could cause g_malloc() to abort if QEMU has reached some RLIMIT... we need to call g_try_malloc0() and return ENOMEM if the allocation fails.

Even if it does not cause an ENOMEM failure right away, the guest can also use this to chew up lots of host resources. It may also be worth putting a reasonable cap at the maximum the guest can allocate, rather than just trying to malloc every possible size.

--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org

#7 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

In reply to Eric Blake
Quoted 7 lines · On Wed, 12 Oct 2016 15:49:46 -0500 Eric Blake <[email protected]> wrote:
Quoted 5 lines · Greg Kurz

But in fact, I'm afraid we have a more serious problem here... size comes from the guest and could cause g_malloc() to abort if QEMU has reached some RLIMIT... we need to call g_try_malloc0() and return ENOMEM if the allocation fails.

Quoted 5 lines

Even if it does not cause an ENOMEM failure right away, the guest can also use this to chew up lots of host resources. It may also be worth putting a reasonable cap at the maximum the guest can allocate, rather than just trying to malloc every possible size.

In the case of v9fs_xattrcreate(), the allocation of the xattr value only happens if a fid with a specific id was created. This function alone cannot be used to chew up memory, but it can certainly be used to crash QEMU if the guest passes an insanely great value.

I fully agree that guest triggered allocations should be capped though, and the more I look the more I realize the 9p code is fragile on this matter... This will require more analysis and fixing, which goes far beyond the scope of preventing an immediate crash.

Cheers.

--
Greg

#6 lore raw

Re: [Qemu-devel] [PATCH 1/2] 9pfs: fix information leak in xattr read

In reply to Li Qiang
Quoted 3 lines · On Thu, 13 Oct 2016 11:30:08 +0800 Li Qiang <[email protected]> wrote:

Yes, I think the limit to apply to xattr size in 9pfs is the same as the Linux xattr size limit, I will try to find this limit.

/usr/include/linux/limits.h:#define XATTR_SIZE_MAX 65536 /* size of an extended attribute value (64k) */

Quoted 21 lines

Thanks.

Quoted 17 lines · On 2016-10-13 4:49 GMT+08:00 Eric Blake <[email protected]> wrote:
Quoted 5 lines · Greg Kurz

But in fact, I'm afraid we have a more serious problem here... size comes from the guest and could cause g_malloc() to abort if QEMU has reached some RLIMIT... we need to call g_try_malloc0() and return ENOMEM if the allocation fails.

Even if it does not cause an ENOMEM failure right away, the guest can also use this to chew up lots of host resources. It may also be worth putting a reasonable cap at the maximum the guest can allocate, rather than just trying to malloc every possible size.

--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org