Skip to contentSkip to conversation

check return value of fcntl() to detect invalid fd

PATCH In review Amos Kong 3 participants 5 messages Dec 19, 2014 – Dec 22, 2014 Networking

lore
Share & tools
lore

Amos Kong

author
#0 lore raw

PATCH 1/1 check return value of fcntl() to detect invalid fd

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.

---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
net/tap.c +13 -3
index bde6b58..039280a 100644
@@ -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);
Signature · 1 line
2.1.0
#1 lore raw
Quoted 6 lines · Amos Kong

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

s/effected/needed/

Quoted 9 lines

fds and vhost_net fds.

---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name, · line 728

Quoted 5 lines
net/tap.c +4 -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));

Awkward grammar; I would suggest s/Fail/Failed/; s/nonblock/nonblocking/, if you still end up reporting the error here.

Wrong, in multiple ways. fcntl does not return negative errno values, just -1, and strerror(1) is not what you want. Furthermore, this code is blindly clearing all other flags as part of setting O_NONBLOCK. The correct way to set O_NONBLOCK is to call F_GETFL first. Which is what qemu_set_nonblock() from oslib-posix.c already does (so reuse that instead of reinventing it here).

Except _that_ function fails to report failure, so we need a bigger audit to fix it and all callers.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Amos Kong
Quoted 27 lines · Amos Kong

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.

---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
net/tap.c +1 -1
index bde6b58..039280a 100644
@@ -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;

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name, · line 730

Quoted 10 lines
net/tap.c +6 -1
@@ -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;
+        }

This may not work. There may be still some kinds of fd can pass this but still fail at TUNGETIFF or other tun ioctls.

Probably you need to fail during TUNGETIFF, which can make sure it was not a tap fd.

Quoted 17 lines
net/tap.c +6 -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);
author
#3 lore raw
Quoted 32 lines · Jason Wang
Quoted 30 lines · Amos Kong

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.

---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
net/tap.c +1 -1
index bde6b58..039280a 100644
@@ -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;
         }
 
Quoted 10 lines

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name, · line 730

Quoted 7 lines
net/tap.c +6 -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;
+        }

This may not work. There may be still some kinds of fd can pass this but still fail at TUNGETIFF or other tun ioctls.

Early catching the error is better. This only help to check if the fd exists.

Quoted 2 lines

Probably you need to fail during TUNGETIFF, which can make sure it was not a tap fd.

Currently if ioctl fails, we treat the IFF_VNET_HDR flag isn't set. We can return -1 in this case, and checking return value of tap_probe_vnet_hdr(), and fail qemu.

qemu/net/tap-linux.c:
int tap_probe_vnet_hdr(int fd)

{
    struct ifreq ifr;

    if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
        error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
        return 0;        <============
    }

    return ifr.ifr_flags & IFF_VNET_HDR;
}

I think we can fix tap_probe_vnet_hdr() and add checking return value of fcntl().

Signature · 1 line
			Amos.
#4 lore raw
Quoted 41 lines · Amos Kong
Quoted 31 lines · Jason Wang
Quoted 30 lines · Amos Kong

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.

---
 net/tap.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
net/tap.c +1 -1
index bde6b58..039280a 100644
@@ -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;
         }
 
Quoted 9 lines

net/tap.c int net_init_tap(const NetClientOptions *opts, const char *name, · line 730

Quoted 7 lines
net/tap.c +6 -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;
+        }

This may not work. There may be still some kinds of fd can pass this but still fail at TUNGETIFF or other tun ioctls.

Quoted 2 lines

Early catching the error is better. This only help to check if the fd exists.

If you just want to check the existence. Why don't you do it in monitor_handle_fd_param() to let other case to benefit also? And probably F_GETFL is better in this case.

But doing this does not solve the issue you mention in the commit log. Even if fd exists, if it was not a tap fd, qemu may still abort.