6e0c7aa
From ba42ebb863ab7d40adc79298422ed9596df8f73a Mon Sep 17 00:00:00 2001
6e0c7aa
From: Li Qiang <liqiang6-s@360.cn>
6e0c7aa
Date: Mon, 17 Oct 2016 14:13:58 +0200
6e0c7aa
Subject: [PATCH] 9pfs: allocate space for guest originated empty strings
6e0c7aa
6e0c7aa
If a guest sends an empty string paramater to any 9P operation, the current
6e0c7aa
code unmarshals it into a V9fsString equal to { .size = 0, .data = NULL }.
6e0c7aa
6e0c7aa
This is unfortunate because it can cause NULL pointer dereference to happen
6e0c7aa
at various locations in the 9pfs code. And we don't want to check str->data
6e0c7aa
everywhere we pass it to strcmp() or any other function which expects a
6e0c7aa
dereferenceable pointer.
6e0c7aa
6e0c7aa
This patch enforces the allocation of genuine C empty strings instead, so
6e0c7aa
callers don't have to bother.
6e0c7aa
6e0c7aa
Out of all v9fs_iov_vunmarshal() users, only v9fs_xattrwalk() checks if
6e0c7aa
the returned string is empty. It now uses v9fs_string_size() since
6e0c7aa
name.data cannot be NULL anymore.
6e0c7aa
6e0c7aa
Signed-off-by: Li Qiang <liqiang6-s@360.cn>
6e0c7aa
[groug, rewritten title and changelog,
6e0c7aa
 fix empty string check in v9fs_xattrwalk()]
6e0c7aa
Signed-off-by: Greg Kurz <groug@kaod.org>
6e0c7aa
---
6e0c7aa
 fsdev/9p-iov-marshal.c |    2 +-
6e0c7aa
 hw/9pfs/9p.c           |    2 +-
6e0c7aa
 2 files changed, 2 insertions(+), 2 deletions(-)
6e0c7aa
6e0c7aa
diff --git a/fsdev/virtio-9p-marshal.c b/fsdev/9p-iov-marshal.c
6e0c7aa
index 663cad5..1d16f8d 100644
6e0c7aa
--- a/fsdev/virtio-9p-marshal.c
6e0c7aa
+++ b/fsdev/virtio-9p-marshal.c
6e0c7aa
@@ -125,7 +125,7 @@ ssize_t v9fs_iov_vunmarshal(struct iovec *out_sg, int out_num, size_t offset,
6e0c7aa
                 str->data = g_malloc(str->size + 1);
6e0c7aa
                 copied = v9fs_unpack(str->data, out_sg, out_num, offset,
6e0c7aa
                                      str->size);
6e0c7aa
-                if (copied > 0) {
6e0c7aa
+                if (copied >= 0) {
6e0c7aa
                     str->data[str->size] = 0;
6e0c7aa
                 } else {
6e0c7aa
                     v9fs_string_free(str);
6e0c7aa
diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/9p.c
6e0c7aa
index 119ee58..39a7e1d 100644
6e0c7aa
--- a/hw/9pfs/virtio-9p.c
6e0c7aa
+++ b/hw/9pfs/virtio-9p.c
6e0c7aa
@@ -3174,7 +3174,7 @@ static void v9fs_xattrwalk(void *opaque)
6e0c7aa
         goto out;
6e0c7aa
     }
6e0c7aa
     v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
6e0c7aa
-    if (name.data == NULL) {
6e0c7aa
+    if (!v9fs_string_size(&name)) {
6e0c7aa
         /*
6e0c7aa
          * listxattr request. Get the size first
6e0c7aa
          */
6e0c7aa
-- 
6e0c7aa
1.7.0.4
6e0c7aa