From: Christian Schoenebeck <qemu_oss@crudebyte.com>
To: qemu-devel@nongnu.org, Peter Maydell <peter.maydell@linaro.org>
Cc: Greg Kurz <groug@kaod.org>
Subject: Re: [PULL 3/9] 9pfs: local : Introduce local_fid_fd() helper
Date: Fri, 03 Jul 2026 13:41:17 +0200 [thread overview]
Message-ID: <23160010.EfDdHjke4D@weasel> (raw)
In-Reply-To: <CAFEAcA9h2QXi=9tTUpzSHVx8p+ZxtZYxFNiCQUHOu68W15buCQ@mail.gmail.com>
On Friday, 3 July 2026 12:52:24 CEST Peter Maydell wrote:
> On Mon, 5 May 2025 at 10:54, Christian Schoenebeck
>
> <qemu_oss@crudebyte.com> wrote:
> > From: Greg Kurz <groug@kaod.org>
> >
> > Factor out duplicated code to a single helper. More users to come.
>
> Hi; Coverity complains about the callsites of this new
>
> helper (CID 1660923, 1660924, 1660925):
Hi Peter,
I have seen these new CID reports. CID 1660926 (test code only) is a valid one
where I already have a small patch prepared to be sent in the next few days.
However CID 1660923, 1660924, 1660925 are false positives AFAICS:
> > +static int local_fid_fd(int fid_type, V9fsFidOpenState *fs)
> > +{
> > + if (fid_type == P9_FID_DIR) {
> > + return dirfd(fs->dir.stream);
> > + } else {
> > + return fs->fd;
> > + }
> > +}
>
> dirfd() can fail and return -1, so this function is "returns a
> file descriptor, or -1 on error"...
That's the *old* helper function. Apparently you were replying on the wrong PR
series (from 2025!).
CID 1660923, 1660924, 1660925 were triggered by this change
(9p queue 2026-06-29, [PULL 23/23] hw/9pfs/local: harden local_fid_fd() on FID
types) [1]:
[1] https://lore.kernel.org/qemu-devel/75893c058b21d87d1ec66bbd4e8bf84e1fd616d1.1782739719.git.qemu_oss@crudebyte.com/
diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 4708e170a4..ee592b62f8 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -775,8 +775,11 @@ static int local_fid_fd(int fid_type, V9fsFidOpenState *fs)
{
if (fid_type == P9_FID_DIR) {
return dirfd(fs->dir.stream);
- } else {
+ } else if (fid_type == P9_FID_FILE) {
return fs->fd;
+ } else {
+ errno = EBADF;
+ return -1;
}
}
However, that is just a last resort safety net! In 9p.c we are already
checking via fid_has_valid_file_handle(), and in the local fs backend via
local_has_valid_file_handle(), and in both cases would leave via error path. I
have not identified any call path where we would not guard this, so we should
never end up passing -1 as FD to a syscall.
So this new EBADF / -1 path in local_fid_fd() is just to avoid undefined
behaviour as a last resort measure, in case a guard was forgotten in future
changes.
> > +
> >
> > static int local_fstat(FsContext *fs_ctx, int fid_type,
> >
> > V9fsFidOpenState *fs, struct stat *stbuf)
> >
> > {
> >
> > - int err, fd;
> > -
> > - if (fid_type == P9_FID_DIR) {
> > - fd = dirfd(fs->dir.stream);
> > - } else {
> > - fd = fs->fd;
> > - }
> > + int err, fd = local_fid_fd(fid_type, fs);
> >
> > err = fstat(fd, stbuf);
>
> ...but the callsites don't check for error, and instead can
> feed -1 into fstat() or other functions, which isn't a valid
> filedescriptor.
But even then, a syscall receiving -1 for a FD argument would handle this
gracefully by returning -1 and errno EBADF.
/Christian
next prev parent reply other threads:[~2026-07-03 11:41 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-05 9:50 [PULL 0/9] 9p queue 2025-05-05 Christian Schoenebeck
2025-05-05 9:50 ` [PULL 9/9] 9pfs: fix 'total_open_fd' decrementation Christian Schoenebeck
2025-05-05 9:50 ` [PULL 6/9] 9pfs: Introduce futimens file op Christian Schoenebeck
2025-05-05 9:50 ` [PULL 5/9] 9pfs: Introduce ftruncate " Christian Schoenebeck
2025-05-05 9:50 ` [PULL 2/9] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd() Christian Schoenebeck
2025-05-05 9:50 ` [PULL 4/9] 9pfs: Don't use file descriptors in core code Christian Schoenebeck
2025-05-05 9:50 ` [PULL 3/9] 9pfs: local : Introduce local_fid_fd() helper Christian Schoenebeck
2026-07-03 10:52 ` Peter Maydell
2026-07-03 11:41 ` Christian Schoenebeck [this message]
2026-07-03 12:21 ` Peter Maydell
2026-07-03 12:49 ` Christian Schoenebeck
2026-07-03 13:15 ` Peter Maydell
2026-07-03 14:47 ` Christian Schoenebeck
2025-05-05 9:50 ` [PULL 8/9] tests/9p: Test `Tsetattr` can truncate unlinked file Christian Schoenebeck
2025-05-05 9:50 ` [PULL 1/9] 9pfs: fix concurrent v9fs_reclaim_fd() calls Christian Schoenebeck
2025-05-05 9:50 ` [PULL 7/9] tests/9p: add 'Tsetattr' request to test client Christian Schoenebeck
2025-07-10 13:30 ` Peter Maydell
2025-07-10 14:11 ` Christian Schoenebeck
2025-07-10 14:17 ` Peter Maydell
2025-05-06 13:58 ` [PULL 0/9] 9p queue 2025-05-05 Stefan Hajnoczi
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=23160010.EfDdHjke4D@weasel \
--to=qemu_oss@crudebyte.com \
--cc=groug@kaod.org \
--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.