Skip to contentSkip to conversation

virtiofsd: fix some accessing NULL pointer problem

PATCH In review Haotian Li 2 participants 8 messages Nov 11, 2020 – Nov 12, 2020

lore
Share & tools
lore

Haotian Li

author
#0 lore raw

v3 cover virtiofsd: fix some accessing NULL pointer problem

Hi,
We find some potential NULL pointer bugs on tools/virtiofsd.
Three patches are made to fix them

Haotian Li (3):
tools/virtiofsd/buffer.c: check whether buf is NULL in
fuse_bufvec_advance func
virtiofsd: check whether lo_map_reserve returns NULL in main func
virtiofsd: check whether strdup lo.source return NULL in main func.

 tools/virtiofsd/buffer.c         |  4 ++++
 tools/virtiofsd/passthrough_ll.c | 16 +++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
author
#1 lore raw

PATCH v3 1/3 tools/virtiofsd/buffer.c: check whether buf is NULL in fuse_bufvec_advance func

In fuse_bufvec_advance func, calling fuse_bufvec_current func may return NULL, so we should check whether buf is NULL before using it.

---
 tools/virtiofsd/buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/virtiofsd/buffer.c +4 -0
index 27c1377f22..bdc608c221 100644
@@ -246,6 +246,10 @@ static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
 {
     const struct fuse_buf *buf = fuse_bufvec_current(bufv);
 
+    if (!buf) {
+        return 0;
+    }
+
     bufv->off += len;
     assert(bufv->off <= buf->size);
     if (bufv->off == buf->size) {
Reviewed-by on v3 1/3
#2 lore raw

Re: [Virtio-fs] [PATCH 1/3] tools/virtiofsd/buffer.c: check whether buf is NULL in fuse_bufvec_advance func

Quoted 15 lines · * Haotian Li ([email protected]) wrote:

In fuse_bufvec_advance func, calling fuse_bufvec_current func may return NULL, so we should check whether buf is NULL before using it.

---
 tools/virtiofsd/buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/virtiofsd/buffer.c +0 -0
index 27c1377f22..bdc608c221 100644

tools/virtiofsd/buffer.c static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len) · line 252

Quoted 7 lines
tools/virtiofsd/buffer.c +4 -0
@@ -246,6 +246,10 @@ static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
 {
     const struct fuse_buf *buf = fuse_bufvec_current(bufv);
 
+    if (!buf) {
+        return 0;
+    }
+
Quoted 9 lines
tools/virtiofsd/buffer.c +0 -0
     bufv->off += len;
     assert(bufv->off <= buf->size);
     if (bufv->off == buf->size) {
Signature · 4 lines
_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#3 lore raw

PATCH v3 2/3 virtiofsd: check whether lo_map_reserve returns NULL in, main func

In reply to Haotian Li

In main func, func lo_map_reserve is called without NULL check. If reallocing new_elems fails in func lo_map_grow, the func lo_map_reserve may return NULL. We should check whether lo_map_reserve returns NULL before using it.

---
 tools/virtiofsd/passthrough_ll.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/virtiofsd/passthrough_ll.c +11 -1
index ec1008bceb..3e9bbc7a04 100644
@@ -3433,6 +3433,7 @@ int main(int argc, char *argv[])
         .proc_self_fd = -1,
     };
     struct lo_map_elem *root_elem;
+    struct lo_map_elem *reserve_elem;
     int ret = -1;
 
     /* Don't mask creation mode, kernel already did that */
@@ -3452,8 +3453,17 @@ int main(int argc, char *argv[])
      * [1] Root inode
      */
     lo_map_init(&lo.ino_map);
-    lo_map_reserve(&lo.ino_map, 0)->in_use = false;
+    reserve_elem = lo_map_reserve(&lo.ino_map, 0);
+    if (!reserve_elem) {
+        fuse_log(FUSE_LOG_ERR, "failed to alloc reserve_elem.\n");
+        goto err_out1;
+    }
+    reserve_elem->in_use = false;
     root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
+    if (!root_elem) {
+        fuse_log(FUSE_LOG_ERR, "failed to alloc root_elem.\n");
+        goto err_out1;
+    }
     root_elem->inode = &lo.root;
 
     lo_map_init(&lo.dirp_map);
Reviewed-by on v3 2/3
#4 lore raw

Re: [Virtio-fs] [PATCH 2/3] virtiofsd: check whether lo_map_reserve returns NULL in, main func

Quoted 7 lines · * Haotian Li ([email protected]) wrote:

In main func, func lo_map_reserve is called without NULL check. If reallocing new_elems fails in func lo_map_grow, the func lo_map_reserve may return NULL. We should check whether lo_map_reserve returns NULL before using it.

(I think the fuse_log will always fall through the default_log_func becuase it's very early)

Quoted 41 lines
---
 tools/virtiofsd/passthrough_ll.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/virtiofsd/passthrough_ll.c +11 -1
index ec1008bceb..3e9bbc7a04 100644
@@ -3433,6 +3433,7 @@ int main(int argc, char *argv[])
         .proc_self_fd = -1,
     };
     struct lo_map_elem *root_elem;
+    struct lo_map_elem *reserve_elem;
     int ret = -1;
 
     /* Don't mask creation mode, kernel already did that */
@@ -3452,8 +3453,17 @@ int main(int argc, char *argv[])
      * [1] Root inode
      */
     lo_map_init(&lo.ino_map);
-    lo_map_reserve(&lo.ino_map, 0)->in_use = false;
+    reserve_elem = lo_map_reserve(&lo.ino_map, 0);
+    if (!reserve_elem) {
+        fuse_log(FUSE_LOG_ERR, "failed to alloc reserve_elem.\n");
+        goto err_out1;
+    }
+    reserve_elem->in_use = false;
     root_elem = lo_map_reserve(&lo.ino_map, lo.root.fuse_ino);
+    if (!root_elem) {
+        fuse_log(FUSE_LOG_ERR, "failed to alloc root_elem.\n");
+        goto err_out1;
+    }
     root_elem->inode = &lo.root;
 
     lo_map_init(&lo.dirp_map);
Signature · 4 lines
_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#5 lore raw

PATCH v3 3/3 virtiofsd: check whether strdup lo.source return NULL in main func

In reply to Haotian Li

In main func, strdup lo.source may fail. So check whether strdup lo.source return NULL before using it.

---
 tools/virtiofsd/passthrough_ll.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/virtiofsd/passthrough_ll.c +4 -0
index 3e9bbc7a04..0c11134fb5 100644
@@ -3525,6 +3525,10 @@ int main(int argc, char *argv[])
         }
     } else {
         lo.source = strdup("/");
+        if (!lo.source) {
+            fuse_log(FUSE_LOG_ERR, "failed to strdup source\n");
+            goto err_out1;
+        }
     }
 
     if (lo.xattrmap) {
Reviewed-by on v3 3/3
#6 lore raw

Re: [Virtio-fs] [PATCH 3/3] virtiofsd: check whether strdup lo.source return NULL in main func

Quoted 14 lines · * Haotian Li ([email protected]) wrote:

In main func, strdup lo.source may fail. So check whether strdup lo.source return NULL before using it.

---
 tools/virtiofsd/passthrough_ll.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/virtiofsd/passthrough_ll.c +0 -0
index 3e9bbc7a04..0c11134fb5 100644

tools/virtiofsd/passthrough_ll.c int main(int argc, char *argv[]) · line 3533

Quoted 9 lines
tools/virtiofsd/passthrough_ll.c +4 -0
@@ -3525,6 +3525,10 @@ int main(int argc, char *argv[])
         }
     } else {
         lo.source = strdup("/");
+        if (!lo.source) {
+            fuse_log(FUSE_LOG_ERR, "failed to strdup source\n");
+            goto err_out1;
+        }
     }
 

(It's interesting we use exit's in some places, goto's in others)

Quoted 7 lines
tools/virtiofsd/passthrough_ll.c +0 -0
     if (lo.xattrmap) {
Signature · 4 lines
_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#7 lore raw
In reply to Haotian Li
Quoted 3 lines · * Haotian Li ([email protected]) wrote:

Hi,
We find some potential NULL pointer bugs on tools/virtiofsd.
Three patches are made to fix them

Queued

Quoted 12 lines

Haotian Li (3):
tools/virtiofsd/buffer.c: check whether buf is NULL in
fuse_bufvec_advance func
virtiofsd: check whether lo_map_reserve returns NULL in main func
virtiofsd: check whether strdup lo.source return NULL in main func.

 tools/virtiofsd/buffer.c         |  4 ++++
 tools/virtiofsd/passthrough_ll.c | 16 +++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK