qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org, armbru@redhat.com
Subject: [Qemu-devel] [PATCH 4/7] qemu-sockets: improve error reporting in unix_listen_opts
Date: Mon, 26 Jan 2015 12:12:24 +0100	[thread overview]
Message-ID: <1422270747-23994-5-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1422270747-23994-1-git-send-email-pbonzini@redhat.com>

Coverity complains about not checking the returned value of mkstemp.  While
at it, also improve error checking for snprintf, and refine error messages
in general.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 util/qemu-sockets.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index a76bb3c..cf4b91f 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -694,7 +694,7 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
 
     sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
     if (sock < 0) {
-        error_setg_errno(errp, errno, "Failed to create socket");
+        error_setg_errno(errp, errno, "Failed to create Unix socket");
         return -1;
     }
 
@@ -703,9 +703,15 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
     if (path && strlen(path)) {
         snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
     } else {
-        char *tmpdir = getenv("TMPDIR");
-        snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
-                 tmpdir ? tmpdir : "/tmp");
+        const char *tmpdir = getenv("TMPDIR");
+        tmpdir = tmpdir ? tmpdir : "/tmp";
+        if (snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
+                     tmpdir) >= sizeof(un.sun_path)) {
+            error_setg_errno(errp, errno,
+                             "TMPDIR environment variable (%s) too large", tmpdir);
+            goto err;
+        }
+
         /*
          * This dummy fd usage silences the mktemp() unsecure warning.
          * Using mkstemp() doesn't make things more secure here
@@ -713,13 +719,19 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
          * to unlink first and thus re-open the race window.  The
          * worst case possible is bind() failing, i.e. a DoS attack.
          */
-        fd = mkstemp(un.sun_path); close(fd);
+        fd = mkstemp(un.sun_path);
+        if (fd < 0) {
+            error_setg_errno(errp, errno,
+                             "Failed to make a temporary socket name in %s", tmpdir);
+            goto err;
+        }
+        close(fd);
         qemu_opt_set(opts, "path", un.sun_path);
     }
 
     unlink(un.sun_path);
     if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
-        error_setg_errno(errp, errno, "Failed to bind socket");
+        error_setg_errno(errp, errno, "Failed to bind socket to %s", un.sun_path);
         goto err;
     }
     if (listen(sock, 1) < 0) {
-- 
1.8.3.1

  parent reply	other threads:[~2015-01-26 11:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-26 11:12 [Qemu-devel] [PATCH 0/7] Six coverity fixes and a cleanup Paolo Bonzini
2015-01-26 11:12 ` [Qemu-devel] [PATCH 1/7] cpu-exec: drop dead assignment Paolo Bonzini
2015-01-26 11:12 ` [Qemu-devel] [PATCH 2/7] cpu-exec: simplify icount code Paolo Bonzini
2015-01-26 11:12 ` [Qemu-devel] [PATCH 3/7] uri: avoid NULL arguments to strcmp Paolo Bonzini
2015-01-26 11:12 ` Paolo Bonzini [this message]
2015-01-26 11:12 ` [Qemu-devel] [PATCH 5/7] cutils: refine strtol error handling in parse_debug_env Paolo Bonzini
2015-02-07  8:52   ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev
2015-01-26 11:12 ` [Qemu-devel] [PATCH 6/7] aes: remove a dead return statement Paolo Bonzini
2015-01-26 11:12 ` [Qemu-devel] [PATCH 7/7] migration: do floating-point division Paolo Bonzini
2015-01-26 18:06   ` Dr. David Alan Gilbert
2015-01-26 18:15     ` Juan Quintela
2015-02-07  9:00 ` [Qemu-devel] [Qemu-trivial] [PATCH 0/7] Six coverity fixes and a cleanup Michael Tokarev
2015-02-07 20:01   ` Paolo Bonzini
2015-02-07 20:39     ` Michael Tokarev
2015-02-09 12:17     ` Juan Quintela

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1422270747-23994-5-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).