qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] check return value of fcntl() to detect invalid fd
@ 2014-12-19 13:25 Amos Kong
  2014-12-19 15:58 ` Eric Blake
  2014-12-22  3:48 ` Jason Wang
  0 siblings, 2 replies; 5+ messages in thread
From: Amos Kong @ 2014-12-19 13:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: jasowang, stefanha, mst

Passing some invalid fds in QEMU commandline, the fds don't exist.
QEMU will get error "TUNGETIFF ioctl() failed: Bad file descriptor",
and coredump in setting queues.

This patch checked return value of first operate to fd, QEMU will
report error and exit without coredump. It's effected for both netdev
fds and vhost_net fds.

Signed-off-by: Amos Kong <akong@redhat.com>
---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/net/tap.c b/net/tap.c
index bde6b58..039280a 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -688,7 +688,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
                  NetClientState *peer)
 {
     const NetdevTapOptions *tap;
-    int fd, vnet_hdr = 0, i = 0, queues;
+    int fd, vnet_hdr = 0, i = 0, queues, ret;
     /* for the no-fd, no-helper case */
     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
     const char *downscript = NULL;
@@ -722,7 +722,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
             return -1;
         }
 
-        fcntl(fd, F_SETFL, O_NONBLOCK);
+        ret = fcntl(fd, F_SETFL, O_NONBLOCK);
+        if (ret < 0) {
+            error_report("Fail to set file status to nonblock, "
+                         "%s", strerror(-ret));
+            return -1;
+        }
 
         vnet_hdr = tap_probe_vnet_hdr(fd);
 
@@ -761,7 +766,12 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
                 return -1;
             }
 
-            fcntl(fd, F_SETFL, O_NONBLOCK);
+            ret = fcntl(fd, F_SETFL, O_NONBLOCK);
+            if (ret < 0) {
+                error_report("Fail to set file status to nonblock, "
+                             "%s", strerror(-ret));
+                return -1;
+            }
 
             if (i == 0) {
                 vnet_hdr = tap_probe_vnet_hdr(fd);
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-12-22  5:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-19 13:25 [Qemu-devel] [PATCH] check return value of fcntl() to detect invalid fd Amos Kong
2014-12-19 15:58 ` Eric Blake
2014-12-22  3:48 ` Jason Wang
2014-12-22  5:28   ` Amos Kong
2014-12-22  5:54     ` Jason Wang

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).