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