hw/misc/ivshmem-pci: Improve error handling
PATCH 1/1 hw/misc/ivshmem-pci: Improve error handling
Coverity points out that the ivshmem-pci code has some error handling cases where it incorrectly tries to use an invalid filedescriptor. These generally happen because ivshmem_recv_msg() calls qemu_chr_fe_get_msgfd(), which might return -1, but the code in process_msg() generally assumes that the file descriptor was provided when it was supposed to be. In particular:
* the error case in process_msg() only needs to close the fd if one was provided
* process_msg_shmem() should fail if no fd was provided
Coverity: CID 1508726
---
Disclaimer: tested only with "make check"
--- hw/misc/ivshmem-pci.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
hw/misc/ivshmem-pci.c +8 -1
@@ -479,6 +479,11 @@ static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)struct stat buf;size_t size; +if (fd <0 ) { +error_setg (errp,"server didn't provide fd with shared memory message" ); +return ; + } +if (s->ivshmem_bar2) {error_setg (errp,"server sent unexpected shared memory message" );close (fd);
@@ -553,7 +558,9 @@ static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp)if (msg <-1 || msg >IVSHMEM_MAX_PEERS ) {error_setg (errp,"server sent invalid message %" PRId64, msg); -close (fd); +if (fd >=0 ) { +close (fd); + }return ; }
Signature · 1 line
2.43.0
Quoted 9 lines · Peter Maydell
Coverity points out that the ivshmem-pci code has some error handling cases where it incorrectly tries to use an invalid filedescriptor. These generally happen because
ivshmem_recv_msg()callsqemu_chr_fe_get_msgfd(), which might return -1, but the code inprocess_msg()generally assumes that the file descriptor was provided when it was supposed to be. In particular:
* the error case inprocess_msg()only needs to close the fd if one was provided
*process_msg_shmem()should fail if no fd was provided
It does even before the patch, because ...
Quoted 15 lines
Coverity: CID 1508726
Signed-off-by Peter Maydell <[email protected]>---
Disclaimer: tested only with "make check"--- hw/misc/ivshmem-pci.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)hw/misc/ivshmem-pci.c +0
-0@@ -479,6 +479,11 @@ static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)struct stat buf;
hw/misc/ivshmem-pci.c static void process_msg_shmem(IVShmemState *s, int fd, Error **errp) · line 489
Quoted 10 lines
hw/misc/ivshmem-pci.c +5
-0size_t size; +if (fd <0 ) { +error_setg (errp,"server didn't provide fd with shared memory message" ); +return ; + } +if (s->ivshmem_bar2) {error_setg (errp,"server sent unexpected shared memory message" );close (fd);
return;
}
if (fstat(fd, &buf) < 0) {... fstat(-1, ...) fails with EBADF.
The additional check gets us a more helpful error message when the server misbehaves this way. Fine.
error_setg_errno(errp, errno,
"can't determine size of shared memory sent by server");
close(fd);
return;
}hw/misc/ivshmem-pci.c static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp) · line 563
Quoted 8 lines
hw/misc/ivshmem-pci.c +3
-1@@ -553,7 +558,9 @@ static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp)if (msg <-1 || msg >IVSHMEM_MAX_PEERS ) {error_setg (errp,"server sent invalid message %" PRId64, msg); -close (fd); +if (fd >=0 ) { +close (fd); + }
Coverity is overly picky. close(-1) is *fine*. Just like free(NULL).
hw/misc/ivshmem-pci.c static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp) · line 565
Quoted 2 lines
hw/misc/ivshmem-pci.c +0
-0return ; }
Regardless
Quoted 13 lines · Markus Armbruster
Quoted 9 lines · Peter Maydell
Coverity points out that the ivshmem-pci code has some error handling cases where it incorrectly tries to use an invalid filedescriptor. These generally happen because
ivshmem_recv_msg()callsqemu_chr_fe_get_msgfd(), which might return -1, but the code inprocess_msg()generally assumes that the file descriptor was provided when it was supposed to be. In particular:
* the error case inprocess_msg()only needs to close the fd if one was provided
*process_msg_shmem()should fail if no fd was provided
Quoted 8 lines
hw/misc/ivshmem-pci.c static void process_msg(IVShmemState *s, int64_t msg, int fd, Error **errp) · line 563
Quoted 6 lines
if (msg < -1 || msg > IVSHMEM_MAX_PEERS) { error_setg(errp, "server sent invalid message %" PRId64, msg);hw/misc/ivshmem-pci.c +3
-1-close (fd); +if (fd >=0 ) { +close (fd); + }Coverity is overly picky. close(-1) is *fine*. Just like free(NULL).
I think there is a distinction here. free(NULL) is documented to be a correct program action which must have no effect. close() on a bad file descriptor is documented to be an error, which the implementation is supposed to diagnose.
Also, Coverity flags this kind of thing up because it tends to indicate that whoever wrote the code was not actually thinking about the error condition; so when it gets caught later this is more by luck than judgement.
thanks
-- PMM