Skip to contentSkip to conversation

monitor: fix use after free

PATCH In review Michael S. Tsirkin 2 participants 4 messages Aug 17, 2014 – Aug 19, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 monitor: fix use after free

The function monitor_fdset_dup_fd_find_remove() references member of 'mon_fdset' which - when remove flag is set - may be freed in function monitor_fdset_cleanup().
remove is set by monitor_fdset_dup_fd_remove which in practice does not need the returned value, so make it void,
and return -1 from monitor_fdset_dup_fd_find_remove.

---
 include/monitor/monitor.h | 2 +-
 monitor.c                 | 8 +++++---
 stubs/fdset-remove-fd.c   | 3 +--
 3 files changed, 7 insertions(+), 6 deletions(-)
include/monitor/monitor.h +1 -1
index 3d6929d..78a5fc8 100644
@@ -64,7 +64,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
                                 Error **errp);
 int monitor_fdset_get_fd(int64_t fdset_id, int flags);
 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
-int monitor_fdset_dup_fd_remove(int dup_fd);
+void monitor_fdset_dup_fd_remove(int dup_fd);
 int monitor_fdset_dup_fd_find(int dup_fd);
 
 #endif /* !MONITOR_H */
monitor.c +5 -3
index cdbaa60..ba1908f 100644
@@ -2542,8 +2542,10 @@ static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
                     if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
                         monitor_fdset_cleanup(mon_fdset);
                     }
+                    return -1;
+                } else {
+                    return mon_fdset->id;
                 }
-                return mon_fdset->id;
             }
         }
     }
@@ -2555,9 +2557,9 @@ int monitor_fdset_dup_fd_find(int dup_fd)
     return monitor_fdset_dup_fd_find_remove(dup_fd, false);
 }
 
-int monitor_fdset_dup_fd_remove(int dup_fd)
+void monitor_fdset_dup_fd_remove(int dup_fd)
 {
-    return monitor_fdset_dup_fd_find_remove(dup_fd, true);
+    monitor_fdset_dup_fd_find_remove(dup_fd, true);
 }
 
 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
stubs/fdset-remove-fd.c +1 -2
index b3886d9..7f6d61e 100644
@@ -1,7 +1,6 @@
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 
-int monitor_fdset_dup_fd_remove(int dupfd)
+void monitor_fdset_dup_fd_remove(int dupfd)
 {
-    return -1;
 }
Signature · 1 line
MST
#1 lore raw
Quoted 9 lines · On Sun, 17 Aug 2014 11:45:17 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

The function monitor_fdset_dup_fd_find_remove() references member of 'mon_fdset' which - when remove flag is set - may be freed in function monitor_fdset_cleanup().
remove is set by monitor_fdset_dup_fd_remove which in practice does not need the returned value, so make it void,
and return -1 from monitor_fdset_dup_fd_find_remove.

I've applied this one to the qmp tree, but I have one comment below.

Quoted 24 lines
---
 include/monitor/monitor.h | 2 +-
 monitor.c                 | 8 +++++---
 stubs/fdset-remove-fd.c   | 3 +--
 3 files changed, 7 insertions(+), 6 deletions(-)
include/monitor/monitor.h +1 -1
index 3d6929d..78a5fc8 100644
@@ -64,7 +64,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
                                 Error **errp);
 int monitor_fdset_get_fd(int64_t fdset_id, int flags);
 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
-int monitor_fdset_dup_fd_remove(int dup_fd);
+void monitor_fdset_dup_fd_remove(int dup_fd);
 int monitor_fdset_dup_fd_find(int dup_fd);
 
 #endif /* !MONITOR_H */
monitor.c +0 -0
index cdbaa60..ba1908f 100644

monitor.c static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) · line 2545

Quoted 4 lines
monitor.c +1 -0
@@ -2542,8 +2542,10 @@ static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
                     if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
                         monitor_fdset_cleanup(mon_fdset);
                     }
+                    return -1;

Returning -1 here looks wrong. A better fix is probably to split this function into two functions as it's doing two unrelated things currently.

I've applied the fix anyway because the only user of remove=true doesn't care about the return value and also because the bad semantic is less worse than the use after free...

Quoted 32 lines
monitor.c +4 -3
+                } else {
+                    return mon_fdset->id;
                 }
-                return mon_fdset->id;
             }
         }
     }
@@ -2555,9 +2557,9 @@ int monitor_fdset_dup_fd_find(int dup_fd)
     return monitor_fdset_dup_fd_find_remove(dup_fd, false);
 }
 
-int monitor_fdset_dup_fd_remove(int dup_fd)
+void monitor_fdset_dup_fd_remove(int dup_fd)
 {
-    return monitor_fdset_dup_fd_find_remove(dup_fd, true);
+    monitor_fdset_dup_fd_find_remove(dup_fd, true);
 }
 
 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
stubs/fdset-remove-fd.c +1 -2
index b3886d9..7f6d61e 100644
@@ -1,7 +1,6 @@
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 
-int monitor_fdset_dup_fd_remove(int dupfd)
+void monitor_fdset_dup_fd_remove(int dupfd)
 {
-    return -1;
 }
#2 lore raw
Quoted 47 lines · Luiz Capitulino
Quoted 9 lines · On Sun, 17 Aug 2014 11:45:17 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

The function monitor_fdset_dup_fd_find_remove() references member of 'mon_fdset' which - when remove flag is set - may be freed in function monitor_fdset_cleanup().
remove is set by monitor_fdset_dup_fd_remove which in practice does not need the returned value, so make it void,
and return -1 from monitor_fdset_dup_fd_find_remove.

I've applied this one to the qmp tree, but I have one comment below.

Quoted 24 lines
---
 include/monitor/monitor.h | 2 +-
 monitor.c                 | 8 +++++---
 stubs/fdset-remove-fd.c   | 3 +--
 3 files changed, 7 insertions(+), 6 deletions(-)
include/monitor/monitor.h +1 -1
index 3d6929d..78a5fc8 100644
@@ -64,7 +64,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
                                 Error **errp);
 int monitor_fdset_get_fd(int64_t fdset_id, int flags);
 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
-int monitor_fdset_dup_fd_remove(int dup_fd);
+void monitor_fdset_dup_fd_remove(int dup_fd);
 int monitor_fdset_dup_fd_find(int dup_fd);
 
 #endif /* !MONITOR_H */
monitor.c +0 -0
index cdbaa60..ba1908f 100644

monitor.c static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) · line 2545

Quoted 4 lines
monitor.c +1 -0
@@ -2542,8 +2542,10 @@ static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
                     if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
                         monitor_fdset_cleanup(mon_fdset);
                     }
+                    return -1;

Returning -1 here looks wrong. A better fix is probably to split this function into two functions as it's doing two unrelated things currently.

Quoted 3 lines

I've applied the fix anyway because the only user of remove=true doesn't care about the return value and also because the bad semantic is less worse than the use after free...

I agree generally, it's just that I don't understand this function. For example, doesn't it leak memory? When monitor_fdset_dup_fd_find_remove drops a MonFdsetFd entry from list, it does not seem to free it.

Quoted 32 lines
Quoted 32 lines
monitor.c +4 -3
+                } else {
+                    return mon_fdset->id;
                 }
-                return mon_fdset->id;
             }
         }
     }
@@ -2555,9 +2557,9 @@ int monitor_fdset_dup_fd_find(int dup_fd)
     return monitor_fdset_dup_fd_find_remove(dup_fd, false);
 }
 
-int monitor_fdset_dup_fd_remove(int dup_fd)
+void monitor_fdset_dup_fd_remove(int dup_fd)
 {
-    return monitor_fdset_dup_fd_find_remove(dup_fd, true);
+    monitor_fdset_dup_fd_find_remove(dup_fd, true);
 }
 
 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
stubs/fdset-remove-fd.c +1 -2
index b3886d9..7f6d61e 100644
@@ -1,7 +1,6 @@
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 
-int monitor_fdset_dup_fd_remove(int dupfd)
+void monitor_fdset_dup_fd_remove(int dupfd)
 {
-    return -1;
 }
#3 lore raw
Quoted 53 lines · On Mon, 18 Aug 2014 22:03:31 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 47 lines · Luiz Capitulino
Quoted 9 lines · On Sun, 17 Aug 2014 11:45:17 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

The function monitor_fdset_dup_fd_find_remove() references member of 'mon_fdset' which - when remove flag is set - may be freed in function monitor_fdset_cleanup().
remove is set by monitor_fdset_dup_fd_remove which in practice does not need the returned value, so make it void,
and return -1 from monitor_fdset_dup_fd_find_remove.

I've applied this one to the qmp tree, but I have one comment below.

Quoted 24 lines
---
 include/monitor/monitor.h | 2 +-
 monitor.c                 | 8 +++++---
 stubs/fdset-remove-fd.c   | 3 +--
 3 files changed, 7 insertions(+), 6 deletions(-)
include/monitor/monitor.h +1 -1
index 3d6929d..78a5fc8 100644
@@ -64,7 +64,7 @@ AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
                                 Error **errp);
 int monitor_fdset_get_fd(int64_t fdset_id, int flags);
 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
-int monitor_fdset_dup_fd_remove(int dup_fd);
+void monitor_fdset_dup_fd_remove(int dup_fd);
 int monitor_fdset_dup_fd_find(int dup_fd);
 
 #endif /* !MONITOR_H */
monitor.c +0 -0
index cdbaa60..ba1908f 100644

monitor.c static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) · line 2545

Quoted 4 lines
monitor.c +1 -0
@@ -2542,8 +2542,10 @@ static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
                     if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
                         monitor_fdset_cleanup(mon_fdset);
                     }
+                    return -1;

Returning -1 here looks wrong. A better fix is probably to split this function into two functions as it's doing two unrelated things currently.

Quoted 3 lines

I've applied the fix anyway because the only user of remove=true doesn't care about the return value and also because the bad semantic is less worse than the use after free...

Quoted 4 lines

I agree generally, it's just that I don't understand this function. For example, doesn't it leak memory? When monitor_fdset_dup_fd_find_remove drops a MonFdsetFd entry from list, it does not seem to free it.

It seems so. Corey, are you still around? Can you take a look at this?

Quoted 36 lines
Quoted 35 lines
Quoted 35 lines
monitor.c +4 -3
+                } else {
+                    return mon_fdset->id;
                 }
-                return mon_fdset->id;
             }
         }
     }
@@ -2555,9 +2557,9 @@ int monitor_fdset_dup_fd_find(int dup_fd)
     return monitor_fdset_dup_fd_find_remove(dup_fd, false);
 }
 
-int monitor_fdset_dup_fd_remove(int dup_fd)
+void monitor_fdset_dup_fd_remove(int dup_fd)
 {
-    return monitor_fdset_dup_fd_find_remove(dup_fd, true);
+    monitor_fdset_dup_fd_find_remove(dup_fd, true);
 }
 
 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
stubs/fdset-remove-fd.c +1 -2
index b3886d9..7f6d61e 100644
@@ -1,7 +1,6 @@
 #include "qemu-common.h"
 #include "monitor/monitor.h"
 
-int monitor_fdset_dup_fd_remove(int dupfd)
+void monitor_fdset_dup_fd_remove(int dupfd)
 {
-    return -1;
 }