All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH] hw/misc/ivshmem-pci: Improve error handling
Date: Fri, 11 Jul 2025 19:04:38 +0200	[thread overview]
Message-ID: <87ecumu09l.fsf@pond.sub.org> (raw)
In-Reply-To: <20250711145012.1521936-1-peter.maydell@linaro.org> (Peter Maydell's message of "Fri, 11 Jul 2025 15:50:12 +0100")

Peter Maydell <peter.maydell@linaro.org> writes:

> 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

It does even before the patch, because ...

>
> Coverity: CID 1508726
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Disclaimer: tested only with "make check"
> ---
>  hw/misc/ivshmem-pci.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/hw/misc/ivshmem-pci.c b/hw/misc/ivshmem-pci.c
> index 5a10bca633d..d47ae739d61 100644
> --- a/hw/misc/ivshmem-pci.c
> +++ b/hw/misc/ivshmem-pci.c
> @@ -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);
           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;
       }

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

>          return;
>      }

Regardless
Reviewed-by: Markus Armbruster <armbru@redhat.com>



  reply	other threads:[~2025-07-11 17:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-11 14:50 [PATCH] hw/misc/ivshmem-pci: Improve error handling Peter Maydell
2025-07-11 17:04 ` Markus Armbruster [this message]
2025-07-14  8:49   ` Peter Maydell

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=87ecumu09l.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.