virtiofsd: fix some accessing NULL pointer problem
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(-)
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
@@ -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) {
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_advancefunc, callingfuse_bufvec_currentfunc may return NULL, so we should check whether buf is NULL before using it.Signed-off-by Haotian Li <[email protected]>Signed-off-by Zhiqiang Liu <[email protected]>--- tools/virtiofsd/buffer.c | 4 ++++ 1 file changed, 4 insertions(+)tools/virtiofsd/buffer.c +0
-0
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
-0bufv->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
PATCH v3 2/3 virtiofsd: check whether lo_map_reserve returns NULL in, main func
In reply to Haotian LiIn 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
@@ -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);
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_reserveis called without NULL check. If reallocingnew_elemsfails in funclo_map_grow, the funclo_map_reservemay return NULL. We should check whetherlo_map_reservereturns NULL before using it.Signed-off-by Haotian Li <[email protected]>Signed-off-by Zhiqiang Liu <[email protected]>
(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@@ -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
PATCH v3 3/3 virtiofsd: check whether strdup lo.source return NULL in main func
In reply to Haotian LiIn 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
@@ -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) {
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.
Signed-off-by Haotian Li <[email protected]>Signed-off-by Zhiqiang Liu <[email protected]>--- tools/virtiofsd/passthrough_ll.c | 4 ++++ 1 file changed, 4 insertions(+)tools/virtiofsd/passthrough_ll.c +0
-0
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
-0if (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
Quoted 3 lines · * Haotian Li ([email protected]) wrote:
Hi,
We find some potential NULL pointer bugs ontools/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_advancefunc
virtiofsd: check whetherlo_map_reservereturns 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