Skip to contentSkip to conversation

oslib-posix: add qemu_pipe_non_block

PATCH Applied Alon Levy 5 participants 20 messages Jun 4, 2013 – Jun 12, 2013

lore
Share & tools
lore

Alon Levy

author
#0 lore raw

PATCH 1/5 oslib-posix: add qemu_pipe_non_block

Used by the followin patch.

---
 include/qemu-common.h |  1 +
 util/oslib-posix.c    | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
include/qemu-common.h +1 -0
index cb82ef3..c24d75c 100644
@@ -232,6 +232,7 @@ ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
 
 #ifndef _WIN32
 int qemu_pipe(int pipefd[2]);
+int qemu_pipe_non_block(int pipefd[2]);
 #endif
 
 #ifdef _WIN32
util/oslib-posix.c +19 -0
index 3dc8b1b..bc2ce2e 100644
@@ -188,6 +188,25 @@ int qemu_pipe(int pipefd[2])
     return ret;
 }
 
+int qemu_pipe_non_block(int pipefd[2])
+{
+    int ret;
+
+    ret = qemu_pipe(pipefd);
+    if (ret) {
+        return ret;
+    }
+    if (fcntl(card->pipe[0], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
+    if (fcntl(card->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
+    if (fcntl(card->pipe[0], F_SETOWN, getpid()) == -1) {
+        return -errno;
+    }
+}
+
 int qemu_utimens(const char *path, const struct timespec *times)
 {
     struct timeval tv[2], tv_now;
Signature · 1 line
1.8.2.1
author
#1 lore raw

PATCH 2/5 use qemu_pipe_non_block

This fixes six instances of unchecked fcntl return status, spotted by Coverity.

---
 hw/display/qxl.c            | 10 +++-------
 hw/usb/ccid-card-emulated.c |  8 +++-----
 2 files changed, 6 insertions(+), 12 deletions(-)
hw/display/qxl.c +3 -7
index 9e5b7ad..25c8c5a 100644
@@ -1797,15 +1797,11 @@ static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
 
 static void init_pipe_signaling(PCIQXLDevice *d)
 {
-    if (pipe(d->pipe) < 0) {
-        fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
-                __FILE__, __func__);
+    if (qxl_pipe_non_block(d->pipe)) {
+        fprintf(stderr, "%s:%s: qxl pipe creation failed: %s\n",
+                __FILE__, __func__, stderror(errno));
         exit(1);
     }
-    fcntl(d->pipe[0], F_SETFL, O_NONBLOCK);
-    fcntl(d->pipe[1], F_SETFL, O_NONBLOCK);
-    fcntl(d->pipe[0], F_SETOWN, getpid());
-
     qemu_thread_get_self(&d->main);
     qemu_set_fd_handler(d->pipe[0], pipe_read, NULL, d);
 }
hw/usb/ccid-card-emulated.c +3 -5
index deb6d47..2e6942e 100644
@@ -406,13 +406,11 @@ static void pipe_read(void *opaque)
 
 static int init_pipe_signaling(EmulatedState *card)
 {
-    if (pipe(card->pipe) < 0) {
-        DPRINTF(card, 2, "pipe creation failed\n");
+    if (qemu_pipe_non_block(card->pipe) < 0) {
+        DPRINTF(card, 2, "pipe creation failed: %s\n",
+                strerror(errno));
         return -1;
     }
-    fcntl(card->pipe[0], F_SETFL, O_NONBLOCK);
-    fcntl(card->pipe[1], F_SETFL, O_NONBLOCK);
-    fcntl(card->pipe[0], F_SETOWN, getpid());
     qemu_set_fd_handler(card->pipe[0], pipe_read, NULL, card);
     return 0;
 }
Signature · 1 line
1.8.2.1
#2 lore raw

Re: [Qemu-devel] [PATCH 2/5] use qemu_pipe_non_block

Il 04/06/2013 22:23, Alon Levy ha scritto:

Quoted 2 lines

This fixes six instances of unchecked fcntl return status, spotted by Coverity.

I think we're just assuming that they cannot fail... I don't think we need the previous patch and this one, unless they help porting stuff to Windows.

Paolo

author
#3 lore raw

Re: [Qemu-devel] [PATCH 2/5] use qemu_pipe_non_block

Quoted 7 lines

Il 04/06/2013 22:23, Alon Levy ha scritto:

Quoted 2 lines

This fixes six instances of unchecked fcntl return status, spotted by Coverity.

I think we're just assuming that they cannot fail... I don't think we need the previous patch and this one, unless they help porting stuff to Windows.

This was purely to satisfy coverity, but I thought we would want to check fcntl return status? also, shouldn't we be looping if EINTR?

Quoted 4 lines

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH 2/5] use qemu_pipe_non_block

Quoted 1 line · Alon Levy

shouldn't we be looping if EINTR?

Don't open that can of worms if you value your sanity ;-)

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] use qemu_pipe_non_block

In reply to Alon Levy

hw/display/qxl.c static void qxl_send_events(PCIQXLDevice *d, uint32_t events) · line 1800

Quoted 10 lines · Alon Levy
hw/display/qxl.c +1 -3
@@ -1797,15 +1797,11 @@ static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
 
 static void init_pipe_signaling(PCIQXLDevice *d)
 {
-    if (pipe(d->pipe) < 0) {
-        fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
-                __FILE__, __func__);
+    if (qxl_pipe_non_block(d->pipe)) {

Surely this can't compile? -- this function doesn't exist.

thanks
-- PMM

author
#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] use qemu_pipe_non_block

Quoted 6 lines · Alon Levy
Quoted 5 lines
hw/display/qxl.c +0 -0
@@ -1797,15 +1797,11 @@ static void qxl_send_events(PCIQXLDevice *d,
 uint32_t events)
 
Quoted 8 lines

hw/display/qxl.c static void qxl_send_events(PCIQXLDevice *d, uint32_t events) · line 1800

Quoted 6 lines
hw/display/qxl.c +1 -3
 static void init_pipe_signaling(PCIQXLDevice *d)
 {
-    if (pipe(d->pipe) < 0) {
-        fprintf(stderr, "%s:%s: qxl pipe creation failed\n",
-                __FILE__, __func__);
+    if (qxl_pipe_non_block(d->pipe)) {

Surely this can't compile? -- this function doesn't exist.

I am abusing my right to post to this list, sorry. I didn't actually try to compile.

Quoted 5 lines

thanks
-- PMM

author
#7 lore raw

PATCH 3/5 libcacard/vscclient: fix leakage of socket on error paths

In reply to Alon Levy

Spotted by Coverity.

---
 libcacard/vscclient.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libcacard/vscclient.c +7 -2
index ac23647..5180d29 100644
@@ -618,18 +618,22 @@ connect_to_qemu(
     if (ret != 0) {
         /* Error */
         fprintf(stderr, "getaddrinfo failed\n");
-        return -1;
+        goto cleanup_socket;
     }
 
     if (connect(sock, server->ai_addr, server->ai_addrlen) < 0) {
         /* Error */
         fprintf(stderr, "Could not connect\n");
-        return -1;
+        goto cleanup_socket;
     }
     if (verbose) {
         printf("Connected (sizeof Header=%zd)!\n", sizeof(VSCMsgHeader));
     }
     return sock;
+
+cleanup_socket:
+    closesocket(sock);
+    return -1;
 }
 
 int
@@ -759,5 +763,6 @@ main(
     g_io_channel_unref(channel_socket);
     g_byte_array_unref(socket_to_send);
 
+    closesocket(sock);
     return 0;
 }
Signature · 1 line
1.8.2.1
#9 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH 3/5] libcacard/vscclient: fix leakage of socket on error paths

In reply to Alon Levy

libcacard/vscclient.c main( · line 768

Quoted 9 lines · Alon Levy
libcacard/vscclient.c +1 -0
@@ -759,5 +763,6 @@ main(
     g_io_channel_unref(channel_socket);
     g_byte_array_unref(socket_to_send);
 
+    closesocket(sock);
     return 0;
 }

This one isn't really needed, -- there's no need to close filedescriptors at the end of main(). I understand the memory unref/free calls above it, to make valgrind and glib trackers happy. But it ofcourse does not hurt.

Besides, in all these error places it'd be really nice to print the actual cause of the problem too - like strerror(errno) or something like that. Unfortunately we had too many of these already in all parts of the code, and it really is sometimes difficult to understand WHY it fails without seeing the actual cause. I'll send a separate patch for that.

Thanks, applied to the trivial patches queue.

/mjt

author
#10 lore raw

PATCH 4/5 libcacard/vreader.c: fix possible NULL dereference

In reply to Alon Levy

Reported by Coverity:

Error: FORWARD_NULL (CWE-476):
qemu-1.5.0/libcacard/vreader.c:267: cond_false: Condition "card == NULL", taking false branch qemu-1.5.0/libcacard/vreader.c:269: if_end: End of if statement
qemu-1.5.0/libcacard/vreader.c:272: cond_false: Condition "apdu == NULL", taking false branch qemu-1.5.0/libcacard/vreader.c:275: else_branch: Reached else branch qemu-1.5.0/libcacard/vreader.c:280: cond_false: Condition "response", taking false branch qemu-1.5.0/libcacard/vreader.c:284: if_end: End of if statement
qemu-1.5.0/libcacard/vreader.c:280: var_compare_op: Comparing "response" to null implies that "response" might be null. qemu-1.5.0/libcacard/vreader.c:286: cond_true: Condition "card_status == VCARD_DONE", taking true branch qemu-1.5.0/libcacard/vreader.c:287: cond_true: Condition "card_status == VCARD_DONE", taking true branch qemu-1.5.0/libcacard/vreader.c:288: var_deref_op: Dereferencing null pointer "response".

---
 libcacard/vreader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libcacard/vreader.c +1 -1
index 5793d73..60eb43b 100644
@@ -260,7 +260,7 @@ vreader_xfr_bytes(VReader *reader,
 {
     VCardAPDU *apdu;
     VCardResponse *response = NULL;
-    VCardStatus card_status;
+    VCardStatus card_status = VCARD_FAIL;
     unsigned short status;
     VCard *card = vreader_get_card(reader);
 
Signature · 1 line
1.8.2.1
#11 lore raw

Re: [Qemu-devel] [PATCH 4/5] libcacard/vreader.c: fix possible NULL dereference

Quoted 24 lines · Alon Levy

Reported by Coverity:

Error: FORWARD_NULL (CWE-476):
qemu-1.5.0/libcacard/vreader.c:267: cond_false: Condition "card == NULL", taking false branch qemu-1.5.0/libcacard/vreader.c:269: if_end: End of if statement
qemu-1.5.0/libcacard/vreader.c:272: cond_false: Condition "apdu == NULL", taking false branch qemu-1.5.0/libcacard/vreader.c:275: else_branch: Reached else branch qemu-1.5.0/libcacard/vreader.c:280: cond_false: Condition "response", taking false branch qemu-1.5.0/libcacard/vreader.c:284: if_end: End of if statement
qemu-1.5.0/libcacard/vreader.c:280: var_compare_op: Comparing "response" to null implies that "response" might be null. qemu-1.5.0/libcacard/vreader.c:286: cond_true: Condition "card_status == VCARD_DONE", taking true branch qemu-1.5.0/libcacard/vreader.c:287: cond_true: Condition "card_status == VCARD_DONE", taking true branch qemu-1.5.0/libcacard/vreader.c:288: var_deref_op: Dereferencing null pointer "response".

---
 libcacard/vreader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libcacard/vreader.c +0 -0
index 5793d73..60eb43b 100644

libcacard/vreader.c vreader_xfr_bytes(VReader *reader, · line 266

Quoted 8 lines
libcacard/vreader.c +1 -1
@@ -260,7 +260,7 @@ vreader_xfr_bytes(VReader *reader,
 {
     VCardAPDU *apdu;
     VCardResponse *response = NULL;
-    VCardStatus card_status;
+    VCardStatus card_status = VCARD_FAIL;
     unsigned short status;
     VCard *card = vreader_get_card(reader);
 

This doesn't make any sense to me as a fix for the quoted coverity issue. It's complaining because the function both checks that response isn't NULL (line 280) and uses it without checking (line 288). If your change makes it stop complaining it's only because you've managed to confuse it.

You either need to:
* assume that vcard_make_response() and vcard_process_apdu() both guarantee to set response to non-NULL, and drop the "if (response)" check
* don't assume it, and handle NULL response consistently in this function (eg by changing the line 287 check to "if (card_status == VCARD_DONE && response)"
* take some middle line, eg "response is guaranteed not to be NULL if and only if status is VCARD_DONE" and then consistently check card_status in both places.

Also, this sequence:

    assert(card_status == VCARD_DONE);
    if (card_status == VCARD_DONE) {

is nonsensical. Either we should assert that the status is always DONE, or we have code to handle the DONE and not DONE cases; not both.

thanks
-- PMM

author
#12 lore raw

PATCH 5/5 libcacard/vscclient.c: fix use of uninitialized variable

In reply to Alon Levy

Found by Coverity.

---
 libcacard/vscclient.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libcacard/vscclient.c +1 -1
index 5180d29..4275c23 100644
@@ -645,7 +645,7 @@ main(
     GIOChannel *channel_stdin;
     char *qemu_host;
     char *qemu_port;
-    VSCMsgHeader mhHeader;
+    VSCMsgHeader mhHeader = {0,};
 
     VCardEmulOptions *command_line_options = NULL;
 
Signature · 1 line
1.8.2.1
#13 lore raw

Re: [Qemu-devel] [PATCH 5/5] libcacard/vscclient.c: fix use of uninitialized variable

Quoted 12 lines · Alon Levy

Found by Coverity.

---
 libcacard/vscclient.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libcacard/vscclient.c +0 -0
index 5180d29..4275c23 100644

libcacard/vscclient.c main( · line 648

Quoted 5 lines
libcacard/vscclient.c +1 -1
@@ -645,7 +645,7 @@ main(
     GIOChannel *channel_stdin;
     char *qemu_host;
     char *qemu_port;
-    VSCMsgHeader mhHeader;
+    VSCMsgHeader mhHeader = {0,};

As far as I can see we only use this variable once:

    send_msg(VSC_Init, mhHeader.reader_id, &init, sizeof(init));

so wouldn't it be better just to directly pass a constant "0" to the send_msg() call? Hiding a single uint32_t in a struct seems a bit obscure.

thanks
-- PMM

#14 lore raw
In reply to Alon Levy
Quoted 5 lines · Alon Levy
util/oslib-posix.c +4 -0
+int qemu_pipe_non_block(int pipefd[2])
+{
+    int ret;
+

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 204

Quoted 10 lines
util/oslib-posix.c +10 -0
+    ret = qemu_pipe(pipefd);
+    if (ret) {
+        return ret;
+    }
+    if (fcntl(card->pipe[0], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
+    if (fcntl(card->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
    qemu_set_nonblock(card->pipe[0]);
    qemu_set_nonblock(card->pipe[1]);

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 207

Quoted 3 lines
util/oslib-posix.c +3 -0
+    if (fcntl(card->pipe[0], F_SETOWN, getpid()) == -1) {
+        return -errno;
+    }

You should either just trust that the fcntl() succeeds (as we do in qemu_set_block() and friends), or you need to close the pipe fds on failure here.

Quoted 1 line

+}

You've forgotten to return anything at the end of the function. (surprised the compiler didn't pick that up, maybe it's one of the warnings that needs optimimisation turned on).

thanks
-- PMM

#15 lore raw
In reply to Alon Levy
Quoted 1 line · Alon Levy

Used by the followin patch.

s/followin/following/

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 195

Quoted 6 lines
util/oslib-posix.c +5 -0
+int qemu_pipe_non_block(int pipefd[2])
+{
+    int ret;
+
+    ret = qemu_pipe(pipefd);

qemu_pipe() already uses pipe2() when available; it seems like it would be nicer to use pipe2's O_NONBLOCK option directly in one syscall (where supported) instead of having to make additional syscalls after the fact.
Would it just be smarter to change the signature of qemu_pipe() to add
a bool block parameter, and then change the 5 existing callers to pass false with your later patch in the series passing true, and do it without creating a new wrapper?

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 203

Quoted 8 lines
util/oslib-posix.c +8 -0
+    if (ret) {
+        return ret;
+    }
+    if (fcntl(card->pipe[0], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
+    if (fcntl(card->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;

Leaks fds. If you're going to report error, then you must close the fds already created.

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 206

Quoted 3 lines
util/oslib-posix.c +3 -0
+    }
+    if (fcntl(card->pipe[0], F_SETOWN, getpid()) == -1) {
+        return -errno;

Same comment about fd leaks.

This part seems like a useful change, IF you plan on using SIGIO and SIGURG signals; and it is something which pipe2() cannot optimize, so I can see why you are adding a new function instead of changing qemu_pipe() and adjust all its callers to pass an additional parameter.
But are you really planning on using SIGIO/SIGURG?

Furthermore, this is undefined behavior. According to POSIX, use of F_SETOWN is only portable on sockets, not pipes. It may work on Linux, but you'll need to be aware of what it does on other platforms.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#16 lore raw
Quoted 4 lines · Alon Levy
Quoted 1 line

Used by the followin patch.

s/followin/following/

Thanks.

Quoted 15 lines

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 195

Quoted 7 lines
util/oslib-posix.c +5 -0
+int qemu_pipe_non_block(int pipefd[2])
+{
+    int ret;
+
+    ret = qemu_pipe(pipefd);

qemu_pipe() already uses pipe2() when available; it seems like it would be nicer to use pipe2's O_NONBLOCK option directly in one syscall (where supported) instead of having to make additional syscalls after the fact.
Would it just be smarter to change the signature of qemu_pipe() to add
a bool block parameter, and then change the 5 existing callers to pass false with your later patch in the series passing true, and do it without creating a new wrapper?

Answered below.

Quoted 10 lines

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 203

Quoted 9 lines
util/oslib-posix.c +8 -0
+    if (ret) {
+        return ret;
+    }
+    if (fcntl(card->pipe[0], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
+    }
+    if (fcntl(card->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
+        return -errno;
Quoted 2 lines

Leaks fds. If you're going to report error, then you must close the fds already created.

As Peter pointed out, I should not go here, so I'll drop these checks, instead doing naked fcntl calls, so no fd leak possible (no returns).

Quoted 5 lines

util/oslib-posix.c int qemu_pipe(int pipefd[2]) · line 206

Quoted 4 lines
util/oslib-posix.c +3 -0
+    }
+    if (fcntl(card->pipe[0], F_SETOWN, getpid()) == -1) {
+        return -errno;
Quoted 7 lines

Same comment about fd leaks.

This part seems like a useful change, IF you plan on using SIGIO and SIGURG signals; and it is something which pipe2() cannot optimize, so I can see why you are adding a new function instead of changing qemu_pipe() and adjust all its callers to pass an additional parameter.
But are you really planning on using SIGIO/SIGURG?

I don't plan on using those signals, so I'll add a parameter instead.

Quoted 10 lines

Furthermore, this is undefined behavior. According to POSIX, use of F_SETOWN is only portable on sockets, not pipes. It may work on Linux, but you'll need to be aware of what it does on other platforms.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org

#17 lore raw
In reply to Alon Levy
Quoted 3 lines · Alon Levy

Used by the followin patch.

+int qemu_pipe_non_block(int pipefd[2]);

A nitpick. I'd name it qemu_pipe_nonblock(), like O_NONBLOCK is named, but that may be just me.

Thanks,

/mjt

#18 lore raw
In reply to Alon Levy

05.06.2013 00:23, Alon Levy wrote:

[PATCH 1/5] oslib-posix: add qemu_pipe_non_block
[PATCH 2/5] use qemu_pipe_non_block
[PATCH 3/5] libcacard/vscclient: fix leakage of socket on error paths [PATCH 4/5] libcacard/vreader.c: fix possible NULL dereference [PATCH 5/5] libcacard/vscclient.c: fix use of uninitialized variable

So, what happened with this series?

Quoted 1 line

From the above 5 patches, only 3/5 (leakage of socket on error paths)

is ready to be applied. Should I apply it now, or wait for the respin of whole series? (Or both?)

Thanks,

/mjt

author
#19 lore raw
Quoted 9 lines

05.06.2013 00:23, Alon Levy wrote:

[PATCH 1/5] oslib-posix: add qemu_pipe_non_block
[PATCH 2/5] use qemu_pipe_non_block
[PATCH 3/5] libcacard/vscclient: fix leakage of socket on error paths [PATCH 4/5] libcacard/vreader.c: fix possible NULL dereference [PATCH 5/5] libcacard/vscclient.c: fix use of uninitialized variable

So, what happened with this series?

I still plan to do it, but didn't get to it yet.

Quoted 4 lines

From the above 5 patches, only 3/5 (leakage of socket on error paths) is ready to be applied. Should I apply it now, or wait for the respin of whole series? (Or both?)

I'll be happy if you do. (Both don't actually make sense :)

Quoted 6 lines

Thanks,

/mjt