* [PATCH] hw/9pfs: Check for error return from local_fid_fd()
@ 2026-07-03 13:25 Peter Maydell
2026-07-03 13:52 ` Michael Tokarev
2026-07-03 16:22 ` Christian Schoenebeck
0 siblings, 2 replies; 4+ messages in thread
From: Peter Maydell @ 2026-07-03 13:25 UTC (permalink / raw)
To: qemu-devel; +Cc: Christian Schoenebeck, Greg Kurz
The local_fid_fd() function can fail (returning -1 if dirfd() fails);
check for this explicitly rather than relying on something later on
failing because it was passed a -1 file descriptor.
This was flagged up by Coverity: CID 1660923, 1660924, 1660925
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/9pfs/9p-local.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index ee592b62f8..8f27c562b5 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -788,6 +788,9 @@ static int local_fstat(FsContext *fs_ctx, int fid_type,
{
int err, fd = local_fid_fd(fid_type, fs);
+ if (fd < 0) {
+ return fd;
+ }
err = fstat(fd, stbuf);
if (err) {
return err;
@@ -1055,6 +1058,9 @@ static int local_ftruncate(FsContext *ctx, int fid_type, V9fsFidOpenState *fs,
{
int fd = local_fid_fd(fid_type, fs);
+ if (fd < 0) {
+ return fd;
+ }
return ftruncate(fd, size);
}
@@ -1113,6 +1119,9 @@ static int local_futimens(FsContext *s, int fid_type, V9fsFidOpenState *fs,
{
int fd = local_fid_fd(fid_type, fs);
+ if (fd < 0) {
+ return fd;
+ }
return qemu_futimens(fd, times);
}
@@ -1196,6 +1205,9 @@ static int local_fsync(FsContext *ctx, int fid_type,
{
int fd = local_fid_fd(fid_type, fs);
+ if (fd < 0) {
+ return fd;
+ }
if (datasync) {
return qemu_fdatasync(fd);
} else {
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] hw/9pfs: Check for error return from local_fid_fd()
2026-07-03 13:25 [PATCH] hw/9pfs: Check for error return from local_fid_fd() Peter Maydell
@ 2026-07-03 13:52 ` Michael Tokarev
2026-07-03 14:03 ` Peter Maydell
2026-07-03 16:22 ` Christian Schoenebeck
1 sibling, 1 reply; 4+ messages in thread
From: Michael Tokarev @ 2026-07-03 13:52 UTC (permalink / raw)
To: Peter Maydell, qemu-devel; +Cc: Christian Schoenebeck, Greg Kurz
On 03.07.2026 16:25, Peter Maydell wrote:
> The local_fid_fd() function can fail (returning -1 if dirfd() fails);
> check for this explicitly rather than relying on something later on
> failing because it was passed a -1 file descriptor.
>
> This was flagged up by Coverity: CID 1660923, 1660924, 1660925
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> hw/9pfs/9p-local.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> index ee592b62f8..8f27c562b5 100644
> --- a/hw/9pfs/9p-local.c
> +++ b/hw/9pfs/9p-local.c
> @@ -788,6 +788,9 @@ static int local_fstat(FsContext *fs_ctx, int fid_type,
> {
> int err, fd = local_fid_fd(fid_type, fs);
>
> + if (fd < 0) {
> + return fd;
> + }
> err = fstat(fd, stbuf);
I think this is a wrong approach - if we're going to report error due to
a failed (local_)fstat call, we'll use errno which is not set in this
codepath.
Instead we should check fd earlier.
In this particular context, we don't check possible error return from
dirfd() - which should not return error in our case, as we know we have
valid opendir() handle (unless we really messed up our memory layout,
but in this case we've much bigger problem).
Mayve
assert(dirft() >= 0)
in local_fid_fd() will be better.
In general, seeing code like the suggested in this patch, prompts me
thinking that fd can normally be negative in this context, - and handle
this situation in other cases too. But it really can't, and if it is,
it's an abnormal situation.
Thanks,
/mjt
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] hw/9pfs: Check for error return from local_fid_fd()
2026-07-03 13:52 ` Michael Tokarev
@ 2026-07-03 14:03 ` Peter Maydell
0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2026-07-03 14:03 UTC (permalink / raw)
To: Michael Tokarev; +Cc: qemu-devel, Christian Schoenebeck, Greg Kurz
On Fri, 3 Jul 2026 at 14:53, Michael Tokarev <mjt@tls.msk.ru> wrote:
>
> On 03.07.2026 16:25, Peter Maydell wrote:
> > The local_fid_fd() function can fail (returning -1 if dirfd() fails);
> > check for this explicitly rather than relying on something later on
> > failing because it was passed a -1 file descriptor.
> >
> > This was flagged up by Coverity: CID 1660923, 1660924, 1660925
> >
> > Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> > ---
> > hw/9pfs/9p-local.c | 12 ++++++++++++
> > 1 file changed, 12 insertions(+)
> >
> > diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> > index ee592b62f8..8f27c562b5 100644
> > --- a/hw/9pfs/9p-local.c
> > +++ b/hw/9pfs/9p-local.c
> > @@ -788,6 +788,9 @@ static int local_fstat(FsContext *fs_ctx, int fid_type,
> > {
> > int err, fd = local_fid_fd(fid_type, fs);
> >
> > + if (fd < 0) {
> > + return fd;
> > + }
> > err = fstat(fd, stbuf);
>
> I think this is a wrong approach - if we're going to report error due to
> a failed (local_)fstat call, we'll use errno which is not set in this
> codepath.
>
> Instead we should check fd earlier.
There is no earlier point. local_fid_fd() calls dirfd().
dirfd() can fail, returning -1, so local_fid_fd() can
fail, returning -1, so we should check it.
> In this particular context, we don't check possible error return from
> dirfd() - which should not return error in our case, as we know we have
> valid opendir() handle (unless we really messed up our memory layout,
> but in this case we've much bigger problem).
>
> Mayve
>
> assert(dirft() >= 0)
>
> in local_fid_fd() will be better.
I'm not generally a fan of "assert that this system or library
call can't fail": it's making a claim about code we don't control
and that doesn't document itself as providing that guarantee.
> In general, seeing code like the suggested in this patch, prompts me
> thinking that fd can normally be negative in this context, - and handle
> this situation in other cases too. But it really can't, and if it is,
> it's an abnormal situation.
This is just local_fid_fd() having the usual Unix style
"returns an fd or -1 on error" semantics, same as open(2) does.
thanks
-- PMM
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] hw/9pfs: Check for error return from local_fid_fd()
2026-07-03 13:25 [PATCH] hw/9pfs: Check for error return from local_fid_fd() Peter Maydell
2026-07-03 13:52 ` Michael Tokarev
@ 2026-07-03 16:22 ` Christian Schoenebeck
1 sibling, 0 replies; 4+ messages in thread
From: Christian Schoenebeck @ 2026-07-03 16:22 UTC (permalink / raw)
To: qemu-devel, Peter Maydell; +Cc: Greg Kurz, Michael Tokarev
On Friday, 3 July 2026 15:25:28 CEST Peter Maydell wrote:
> The local_fid_fd() function can fail (returning -1 if dirfd() fails);
> check for this explicitly rather than relying on something later on
> failing because it was passed a -1 file descriptor.
>
> This was flagged up by Coverity: CID 1660923, 1660924, 1660925
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> hw/9pfs/9p-local.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
> index ee592b62f8..8f27c562b5 100644
> --- a/hw/9pfs/9p-local.c
> +++ b/hw/9pfs/9p-local.c
> @@ -788,6 +788,9 @@ static int local_fstat(FsContext *fs_ctx, int fid_type,
> {
> int err, fd = local_fid_fd(fid_type, fs);
>
> + if (fd < 0) {
> + return fd;
> + }
Officially only -1 is an invalid file descriptor, and if you look at this file
we therefore check file descriptors exactly against -1 for that reason.
For the Linux kernel the entire negative range is invalid for file
descriptors, but keep in mind that 9pfs in QEMU also supports FreeBSD and
macOS hosts and we have Windows host patches lurking around where things are
not for certain, at least not for me. So I would not break that assumption.
You might also consider adding error_report_once() in local_fid_fd() as this
would be an unexpected case, even though gracefully handled already.
/Christian
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-03 16:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-03 13:25 [PATCH] hw/9pfs: Check for error return from local_fid_fd() Peter Maydell
2026-07-03 13:52 ` Michael Tokarev
2026-07-03 14:03 ` Peter Maydell
2026-07-03 16:22 ` Christian Schoenebeck
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.