Skip to contentSkip to conversation

tools/virtiofsd/passthrough_ll: Fix double close()

PATCH-for-5.0 Philippe Mathieu-Daudé 3 participants 4 messages Mar 21, 2020 – Mar 24, 2020

lore
Share & tools
lore
#0 lore raw

On success, the fdopendir() call closes fd. Later on the error path we try to close an already-closed fd. This can lead to use-after-free. Fix by only closing the fd if the fdopendir() call failed.

Fixes 7c6b66027 (Import passthrough_ll from libfuse fuse-3.8.0)
Reported-by Coverity (CID 1421933 USE_AFTER_FREE)
---
 tools/virtiofsd/passthrough_ll.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/virtiofsd/passthrough_ll.c +1 -2
index 4f259aac70..4c35c95b25 100644
@@ -1520,8 +1520,7 @@ out_err:
     if (d) {
         if (d->dp) {
             closedir(d->dp);
-        }
-        if (fd != -1) {
+        } else if (fd != -1) {
             close(fd);
         }
         free(d);
Signature · 1 line
2.21.1
#1 lore raw
Quoted 7 lines · Philippe Mathieu-Daudé

On success, the fdopendir() call closes fd. Later on the error path we try to close an already-closed fd. This can lead to use-after-free. Fix by only closing the fd if the fdopendir() call failed.

Fixes 7c6b66027 (Import passthrough_ll from libfuse fuse-3.8.0)

libfuse is correct, the bug was introduced in commit b39bce121b, so:

Fixes b39bce121b (add dirp_map to hide lo_dirp pointers)
Quoted 22 lines
Reported-by Coverity (CID 1421933 USE_AFTER_FREE)
---
  tools/virtiofsd/passthrough_ll.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
tools/virtiofsd/passthrough_ll.c +1 -2
index 4f259aac70..4c35c95b25 100644
@@ -1520,8 +1520,7 @@ out_err:
      if (d) {
          if (d->dp) {
              closedir(d->dp);
-        }
-        if (fd != -1) {
+        } else if (fd != -1) {
              close(fd);
          }
          free(d);
#2 lore raw
Quoted 6 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Quoted 5 lines · Philippe Mathieu-Daudé

On success, the fdopendir() call closes fd. Later on the error path we try to close an already-closed fd. This can lead to use-after-free. Fix by only closing the fd if the fdopendir() call failed.

Quoted 6 lines
Quoted 2 lines
Fixes 7c6b66027 (Import passthrough_ll from libfuse fuse-3.8.0)

libfuse is correct, the bug was introduced in commit b39bce121b, so:

Fixes b39bce121b (add dirp_map to hide lo_dirp pointers)

Queued with that tweak

Quoted 7 lines
Quoted 7 lines
Reported-by Coverity (CID 1421933 USE_AFTER_FREE)
---
  tools/virtiofsd/passthrough_ll.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 17 lines
Quoted 6 lines
tools/virtiofsd/passthrough_ll.c +0 -0
index 4f259aac70..4c35c95b25 100644

tools/virtiofsd/passthrough_ll.c out_err: · line 1526

Quoted 9 lines
tools/virtiofsd/passthrough_ll.c +1 -2
@@ -1520,8 +1520,7 @@ out_err:
      if (d) {
          if (d->dp) {
              closedir(d->dp);
-        }
-        if (fd != -1) {
+        } else if (fd != -1) {
              close(fd);
          }
          free(d);

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

On success, the fdopendir() call closes fd. Later on the error path we try to close an already-closed fd. This can lead to use-after-free. Fix by only closing the fd if the fdopendir() call failed.

Quoted 8 lines
Fixes 7c6b66027 (Import passthrough_ll from libfuse fuse-3.8.0)
Reported-by Coverity (CID 1421933 USE_AFTER_FREE)
---
 tools/virtiofsd/passthrough_ll.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Thanks!