Skip to contentSkip to conversation

qemu-ga: fix segv after failure to open log file

PATCH In review Michael Roth 4 participants 8 messages May 14, 2012 – May 15, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-ga: fix segv after failure to open log file

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +4 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
 #ifdef _WIN32
     const char *service = NULL;
 #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;
         }
     }
 
Signature · 1 line
1.7.4.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 30 lines · Michael Roth

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +1 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
 #ifdef _WIN32
     const char *service = NULL;
 #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {

qemu-ga.c int main(int argc, char **argv) · line 849

Quoted 10 lines
qemu-ga.c +3 -0
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;
         }
     }
 
#2 lore raw
In reply to Michael Roth
Quoted 27 lines · On Mon, 14 May 2012 17:04:17 -0500 Michael Roth <[email protected]>…

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +1 -0
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
 #ifdef _WIN32
     const char *service = NULL;
 #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }

qemu-ga.c int main(int argc, char **argv) · line 846

Quoted 10 lines
qemu-ga.c +3 -2
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;

Is it safe to change the log file this way? Isn't it necessary to go through g_log_set_default_handler() or some other function?

Quoted 3 lines
qemu-ga.c +0 -0
         }
     }
 
#3 lore raw
Quoted 35 lines · Luiz Capitulino
Quoted 32 lines · On Mon, 14 May 2012 17:04:17 -0500 Michael Roth <[email protected]>…

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +3 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
 #ifdef _WIN32
     const char *service = NULL;
 #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
Quoted 8 lines

qemu-ga.c int main(int argc, char **argv) · line 846

Quoted 5 lines
qemu-ga.c +1 -0
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;

Is it safe to change the log file this way? Isn't it necessary to go through g_log_set_default_handler() or some other function?

Are you worried about a race condition? g_log_set_default_handler() does do locking, but our log handler is actually unchanged, so the only case it would be needed is if the opaque argument changes, or there were multiple fields changed in the opaque, since that might result it an undefined mix of old/new values.

The locking for the actual fields in the opaque is our responsibility, and there's no way to get glib to comply (short of duplicating the opaque and then using g_log_set_default_handler(), since g_messages_lock isn't exposed)

But since we're only changing a single field, the FILE* the log handler passes to fprintf(), we get the same level of automicity: a log statement will get stuck either in stderr or the new handle, depending on whether we made this change before or after another caller got to that point in ga_log(). So we should be okay, and aren't violating any glib locking protocols with change.

Quoted 6 lines
Quoted 4 lines
qemu-ga.c +0 -0
         }
     }
 
#4 lore raw
Quoted 45 lines · On Tue, 15 May 2012 09:14:13 -0500 Michael Roth <[email protected]>…
Quoted 35 lines · Luiz Capitulino
Quoted 32 lines · On Mon, 14 May 2012 17:04:17 -0500 Michael Roth <[email protected]>…

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +3 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
 #ifdef _WIN32
     const char *service = NULL;
 #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
Quoted 8 lines

qemu-ga.c int main(int argc, char **argv) · line 846

Quoted 5 lines
qemu-ga.c +1 -0
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;

Is it safe to change the log file this way? Isn't it necessary to go through g_log_set_default_handler() or some other function?

Quoted 1 line

Are you worried about a race condition?

Actually, I was worried that glib could store the old s->log_file pointer somewhere, but I think I misread its documentation. Now I see that it's only used by ga_log().

I think that doing the log setup before logging anything would be the best solution, but that's a future improvement:

#5 lore raw
In reply to Michael Roth
Quoted 29 lines · Michael Roth

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +1 -1
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
  #ifdef _WIN32
     const char *service = NULL;
  #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");

qemu-ga.c int main(int argc, char **argv) · line 848

Quoted 10 lines
qemu-ga.c +3 -1
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
             }
+            s->log_file = log_file;
         }
     }

It would be nicer to put the log_file variable definition inside the if(), rather than putting it 150 lines earlier in the source file...

-- PMM

#6 lore raw
Quoted 36 lines · Peter Maydell
Quoted 35 lines · Michael Roth

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +3 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
  #ifdef _WIN32
     const char *service = NULL;
  #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
Quoted 8 lines

qemu-ga.c int main(int argc, char **argv) · line 848

Quoted 4 lines
qemu-ga.c +1 -0
             }
+            s->log_file = log_file;
         }
     }

It would be nicer to put the log_file variable definition inside the if(), rather than putting it 150 lines earlier in the source file...

Agreed...I've had it in my head for the longest time that declarations at the beginning of functions were QEMU coding style, but looking again I'm not sure where I got that idea.

Fixed in tree:

https://github.com/mdroth/qemu/commit/6c615ec57e83bf8cc7b1721bcd58c7d1ed93ef65

Quoted 3 lines

-- PMM

#7 lore raw
Quoted 46 lines · Michael Roth
Quoted 36 lines · Peter Maydell
Quoted 35 lines · Michael Roth

Currently, if we fail to open the specified log file (generally due to a permissions issue), we'll assign NULL to the logfile handle (stderr, initially) used by the logging routines, which can cause a segfault to occur when we attempt to report the error before exiting.

Instead, only re-assign if the open() was successful.

---
 qemu-ga.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)
qemu-ga.c +3 -2
index 3a88333..e2725c8 100644
@@ -681,6 +681,7 @@ int main(int argc, char **argv)
     const char *log_filepath = NULL;
     const char *pid_filepath = QGA_PIDFILE_DEFAULT;
     const char *state_dir = QGA_STATEDIR_DEFAULT;
+    FILE *log_file;
  #ifdef _WIN32
     const char *service = NULL;
  #endif
@@ -836,12 +837,13 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            s->log_file = fopen(log_filepath, "a");
-            if (!s->log_file) {
+            log_file = fopen(log_filepath, "a");
+            if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
                 goto out_bad;
Quoted 8 lines

qemu-ga.c int main(int argc, char **argv) · line 848

Quoted 4 lines
qemu-ga.c +1 -0
             }
+            s->log_file = log_file;
         }
     }

It would be nicer to put the log_file variable definition inside the if(), rather than putting it 150 lines earlier in the source file...

Quoted 3 lines

Agreed...I've had it in my head for the longest time that declarations at the beginning of functions were QEMU coding style, but looking again I'm not sure where I got that idea.

I think there's a dislike of middle-of-block declarations (C++/C99 style), but beginning of a block is certainly fine.

-- PMM