Skip to contentSkip to conversation

qemu-ga: don't leak fds to exec()ed processes

PATCH Applied Luiz Capitulino 4 participants 7 messages Jan 8, 2013 – Jan 10, 2013

lore
Share & tools
lore
#0 lore raw

cover qemu-ga: don't leak fds to exec()ed processes

Please, check individual patches for details.

Luiz Capitulino (2):
qemu-ga: ga_open_pidfile(): use qemu_open()
qemu-ga: add ga_open_logfile()

 qga/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.0
#1 lore raw

PATCH 1/2 qemu-ga: ga_open_pidfile(): use qemu_open()

This ensures that O_CLOEXEC is passed to open(), this way the pid file fd is not leaked to executed processes.

---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/main.c +1 -1
index ba5fa1c..e4245cc 100644
@@ -242,7 +242,7 @@ static bool ga_open_pidfile(const char *pidfile)
     int pidfd;
     char pidstr[32];
 
-    pidfd = open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
+    pidfd = qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
     if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) {
         g_critical("Cannot lock pid file, %s", strerror(errno));
         if (pidfd != -1) {
Signature · 1 line
1.8.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] qemu-ga: ga_open_pidfile(): use qemu_open()

Quoted 7 lines · Luiz Capitulino

This ensures that O_CLOEXEC is passed to open(), this way the pid file fd is not leaked to executed processes.

---
 qga/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
qga/main.c +1 -1
index ba5fa1c..e4245cc 100644
@@ -242,7 +242,7 @@ static bool ga_open_pidfile(const char *pidfile)
     int pidfd;
     char pidstr[32];
 
-    pidfd = open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
+    pidfd = qemu_open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
     if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) {
         g_critical("Cannot lock pid file, %s", strerror(errno));
         if (pidfd != -1) {
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH 2/2 qemu-ga: add ga_open_logfile()

In reply to Luiz Capitulino

This function sets O_CLOEXEC on the log file fd so that it isn't leaked to executed processes.

---
 qga/main.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
qga/main.c +15 -2
index e4245cc..15be74f 100644
@@ -236,6 +236,19 @@ void ga_set_response_delimited(GAState *s)
     s->delimit_response = true;
 }
 
+static FILE *ga_open_logfile(const char *logfile)
+{
+    FILE *f;
+
+    f = fopen(logfile, "a");
+    if (!f) {
+        return NULL;
+    }
+
+    qemu_set_cloexec(fileno(f));
+    return f;
+}
+
 #ifndef _WIN32
 static bool ga_open_pidfile(const char *pidfile)
 {
@@ -377,7 +390,7 @@ void ga_unset_frozen(GAState *s)
      * in a frozen state at start up, do it now
      */
     if (s->deferred_options.log_filepath) {
-        s->log_file = fopen(s->deferred_options.log_filepath, "a");
+        s->log_file = ga_open_logfile(s->deferred_options.log_filepath);
         if (!s->log_file) {
             s->log_file = stderr;
         }
@@ -838,7 +851,7 @@ int main(int argc, char **argv)
             become_daemon(pid_filepath);
         }
         if (log_filepath) {
-            FILE *log_file = fopen(log_filepath, "a");
+            FILE *log_file = ga_open_logfile(log_filepath);
             if (!log_file) {
                 g_critical("unable to open specified log file: %s",
                            strerror(errno));
Signature · 1 line
1.8.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu-ga: add ga_open_logfile()

Quoted 7 lines · Luiz Capitulino

This function sets O_CLOEXEC on the log file fd so that it isn't leaked to executed processes.

---
 qga/main.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Applied
#6 lore raw
In reply to Luiz Capitulino
Quoted 5 lines · Luiz Capitulino

Please, check individual patches for details.

Luiz Capitulino (2):
qemu-ga: ga_open_pidfile(): use qemu_open()
qemu-ga: add ga_open_logfile()

Thanks, applied to qga branch.

Quoted 7 lines
 qga/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.0