* [PATCH v2 1/7] hw/9pfs: make FID type assignment atomic across yields
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 2/7] libqos/virtio: split qvirtqueue_kick into enqueue and notify Christian Schoenebeck
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel, qemu-stable
Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Request types that assign a semantic type to an untyped FID, currently:
(Request Type) -> (Handler Function)
==================================
- Topen/Tlopen -> v9fs_open()
- Tlcreate -> v9fs_lcreate()
- Tcreate -> v9fs_create()
- Txattrwalk -> v9fs_xattrwalk()
already verify that the FID is untyped (fid_type == P9_FID_NONE) at the
beginning of their handler, and eventually assign fidp->fid_type once the
asynchronous fs driver operation(s) succeeded.
However, as this overall operation spans several thread hops between check
and type assignment (without re-checking the type), a concurrent request
that intends to change the type (of the same FID) may cause a race
condition (TOCTOU), eventually allowing a guest-controlled (union
V9fsFidOpenState) type confusion.
Fix this by introducing an intermediate FID type state P9_FID_PENDING,
which is assigned early on main thread to temporarily mark the FID as
"in transition" to its final, real FID type. This prevents other requests
from concurrently changing the FID type while OTOH distinguishing it
from a real FID type, which in turn prevents access to undefined union
payload.
Once the overall FID type operation completed successfully on the request
handler, which may involve several fs driver operations on background
worker threads, the FID is then assigned its final, real FID type, which
eventually unlocks access to the FID type dependent union payload by other
requests.
This changes the FID state machine from:
P9_FID_NONE (start) ---> ( P9_FID_FILE |
P9_FID_DIR |
P9_FID_XATTR )
to now:
P9_FID_NONE (start) ---> P9_FID_PENDING ---> ( P9_FID_FILE |
^ | P9_FID_DIR |
| | P9_FID_XATTR )
| |
+------------------------+
The fallback from "pending" to "none" happens if an error occurs during
the overall FID type assignment operation.
Like before though, there is still no return to "none" once a real FID
type had been assigned, because the 9p protocol only allows to Tclunk a
FID which destroys the FID (end of life).
Fixes: d62dbb51f76d ("virtio-9p: Add fidtype so that we can do type specific operation")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4664
Reported-by: Ken Hsu <https://gitlab.com/96baws69>
Reported-by: Rick de Jager <https://gitlab.com/rick219>
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
hw/9pfs/9p.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++---
hw/9pfs/9p.h | 21 ++++++++++++++++++++
2 files changed, 73 insertions(+), 3 deletions(-)
diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
index 730d1279bb..7117a73747 100644
--- a/hw/9pfs/9p.c
+++ b/hw/9pfs/9p.c
@@ -672,6 +672,44 @@ static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
s->ctx.xattr_fid_count = 0;
}
+/*
+ * Claim a FID type change for a yet untyped FID.
+ *
+ * This function must be called on main thread only. It sets the FID type as
+ * "pending", to prevent further concurrent mutations of the FID type dependent
+ * union payload, until that union payload was fully defined by the request
+ * handler, which may span several thread hops.
+ *
+ * Returns true if the FID type change was claimed successfully, false if a
+ * FID type change has already been claimed or a real FID type assigned.
+ *
+ * Since "pending" is just a transitional state: If the claim succeeded, the
+ * request must then eventually either change to the real FID type once the
+ * overall operation completed successfully, or call v9fs_fid_type_unclaim()
+ * if the overall FID type assignment operation failed for some reason.
+ */
+static bool v9fs_fid_type_claim(V9fsFidState *fidp)
+{
+ if (fidp->fid_type != P9_FID_NONE) {
+ return false;
+ }
+ fidp->fid_type = P9_FID_PENDING;
+ return true;
+}
+
+/*
+ * Drop the previously claimed FID type change. This must be called by the
+ * request handler on error case, i.e. if it failed to assign the final real
+ * FID type.
+ */
+static void v9fs_fid_type_unclaim(V9fsFidState *fidp)
+{
+ /* intentionally inert on real FID types: simplifies error-case handling */
+ if (fidp->fid_type == P9_FID_PENDING) {
+ fidp->fid_type = P9_FID_NONE;
+ }
+}
+
#define P9_QID_TYPE_DIR 0x80
#define P9_QID_TYPE_SYMLINK 0x02
@@ -2153,7 +2191,7 @@ static void coroutine_fn v9fs_open(void *opaque)
err = -ENOENT;
goto out_nofid;
}
- if (fidp->fid_type != P9_FID_NONE) {
+ if (!v9fs_fid_type_claim(fidp)) {
err = -EINVAL;
goto out;
}
@@ -2213,6 +2251,7 @@ static void coroutine_fn v9fs_open(void *opaque)
trace_v9fs_open_return(pdu->tag, pdu->id,
qid.type, qid.version, qid.path, iounit);
out:
+ v9fs_fid_type_unclaim(fidp);
put_fid(pdu, fidp);
out_nofid:
pdu_complete(pdu, err);
@@ -2249,7 +2288,7 @@ static void coroutine_fn v9fs_lcreate(void *opaque)
err = -ENOENT;
goto out_nofid;
}
- if (fidp->fid_type != P9_FID_NONE) {
+ if (!v9fs_fid_type_claim(fidp)) {
err = -EINVAL;
goto out;
}
@@ -2282,6 +2321,7 @@ static void coroutine_fn v9fs_lcreate(void *opaque)
trace_v9fs_lcreate_return(pdu->tag, pdu->id,
qid.type, qid.version, qid.path, iounit);
out:
+ v9fs_fid_type_unclaim(fidp);
put_fid(pdu, fidp);
out_nofid:
pdu_complete(pdu, err);
@@ -2965,7 +3005,7 @@ static void coroutine_fn v9fs_create(void *opaque)
err = -EINVAL;
goto out_nofid;
}
- if (fidp->fid_type != P9_FID_NONE) {
+ if (!v9fs_fid_type_claim(fidp)) {
err = -EINVAL;
goto out;
}
@@ -3115,6 +3155,7 @@ static void coroutine_fn v9fs_create(void *opaque)
trace_v9fs_create_return(pdu->tag, pdu->id,
qid.type, qid.version, qid.path, iounit);
out:
+ v9fs_fid_type_unclaim(fidp);
put_fid(pdu, fidp);
out_nofid:
pdu_complete(pdu, err);
@@ -4075,6 +4116,13 @@ static void coroutine_fn v9fs_xattrwalk(void *opaque)
err = -EINVAL;
goto out;
}
+ /*
+ * This freshly allocated FID is visible to other requests from now on, but
+ * gets its final real type assigned below only after fs background worker
+ * yields. So claim a FID type change right away (nobody else could have
+ * claimed a FID type change at this point yet, so result can be ignored).
+ */
+ v9fs_fid_type_claim(xattr_fidp);
v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
if (!v9fs_string_size(&name)) {
/*
@@ -4163,6 +4211,7 @@ static void coroutine_fn v9fs_xattrwalk(void *opaque)
out:
put_fid(pdu, file_fidp);
if (xattr_fidp) {
+ v9fs_fid_type_unclaim(xattr_fidp);
put_fid(pdu, xattr_fidp);
}
out_nofid:
diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h
index 0e52ffbdf3..34921a097c 100644
--- a/hw/9pfs/9p.h
+++ b/hw/9pfs/9p.h
@@ -171,6 +171,27 @@ enum {
P9_FID_FILE,
P9_FID_DIR,
P9_FID_XATTR,
+ /*
+ * In transition from "none" to one of the actual FID types above:
+ *
+ * The data semantic (union V9fsFidOpenState access) of a FID object
+ * depends on the FID type. A FID type assignment operation spans from main
+ * thread to fs worker thread(s). This intermediate "pending" state
+ * makes this thread-spanning operation atomic, solving the following
+ * concurrency dilemma:
+ *
+ * - Setting the real FID type early on main thread would allow concurrent
+ * requests to access yet undefined union payload.
+ *
+ * - Setting the real type late OTOH (directly from "none") after fs worker
+ * thread(s) completed, would allow concurrent requests to override an
+ * already (partially or fully) defined union payload.
+ *
+ * Instead, a FID type is therefore first set from "none" -> "pending" on
+ * main thread, and then set from "pending" -> real FID type after required
+ * fs worker thread(s) completed.
+ */
+ P9_FID_PENDING,
};
typedef struct V9fsConf
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 2/7] libqos/virtio: split qvirtqueue_kick into enqueue and notify
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 1/7] hw/9pfs: make FID type assignment atomic across yields Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 3/7] libqos/virtio: add qvirtqueue_wait_next_used() Christian Schoenebeck
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Split qvirtqueue_kick() into two functions:
1. qvirtqueue_enqueue(): add the descriptor chain to the
virtqueue's available ring, making it visible to the device,
but without notifying it yet.
2. qvirtqueue_notify(): notify the device that there are pending
descriptors for it to receive.
qvirtqueue_kick() itself simply becomes a combination of calling
both of these two new functions, its actual behavior remains
unchanged.
This is a preparatory patch, the split functions will be used by the
subsequently added 9p test case, which needs to submit multiple
requests simultaneously to the 9p server within one single
virtqueue notification.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/libqos/virtio.c | 42 ++++++++++++++++++++++++++++++-------
tests/qtest/libqos/virtio.h | 3 +++
2 files changed, 38 insertions(+), 7 deletions(-)
diff --git a/tests/qtest/libqos/virtio.c b/tests/qtest/libqos/virtio.c
index ccbb325222..a2b69c9e92 100644
--- a/tests/qtest/libqos/virtio.c
+++ b/tests/qtest/libqos/virtio.c
@@ -396,22 +396,43 @@ void qvirtqueue_set_avail_idx(QTestState *qts, QVirtioDevice *d,
qvirtio_writew(d, qts, vq->avail + 2, idx);
}
-void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
- uint32_t free_head)
+/*
+ * qvirtqueue_enqueue:
+ * @free_head: head index of descriptor chain to enqueue
+ *
+ * Add the descriptor chain at @free_head to the virtqueue's available
+ * ring, making it visible to the device, but do not notify the device
+ * about it yet (see qvirtqueue_notify()). This allows simultaneously
+ * scheduling multiple requests to the device within one single notification.
+ */
+void qvirtqueue_enqueue(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
+ uint32_t free_head)
{
/* vq->avail->idx */
uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
- /* vq->used->flags */
- uint16_t flags;
- /* vq->used->avail_event */
- uint16_t avail_event;
/* vq->avail->ring[idx % vq->size] */
qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
qvirtqueue_set_avail_idx(qts, d, vq, idx + 1);
+}
- /* Must read after idx is updated */
+/*
+ * qvirtqueue_notify:
+ *
+ * Tell the device about pending requests that were already enqueued on this
+ * virtqueue (see qvirtqueue_enqueue()).
+ */
+void qvirtqueue_notify(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq)
+{
+ /* vq->avail->idx of the last enqueued element */
+ uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2) - 1;
+ /* vq->used->flags */
+ uint16_t flags;
+ /* vq->used->avail_event */
+ uint16_t avail_event;
+
+ /* Must read after avail idx was updated (e.g. by qvirtqueue_enqueue()) */
flags = qvirtio_readw(d, qts, vq->used);
avail_event = qvirtio_readw(d, qts, vq->used + 4 +
sizeof(struct vring_used_elem) * vq->size);
@@ -423,6 +444,13 @@ void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
}
}
+void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
+ uint32_t free_head)
+{
+ qvirtqueue_enqueue(qts, d, vq, free_head);
+ qvirtqueue_notify(qts, d, vq);
+}
+
/*
* qvirtqueue_get_buf:
* @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
diff --git a/tests/qtest/libqos/virtio.h b/tests/qtest/libqos/virtio.h
index f17be0b9b6..2299a86802 100644
--- a/tests/qtest/libqos/virtio.h
+++ b/tests/qtest/libqos/virtio.h
@@ -145,6 +145,9 @@ uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
QVRingIndirectDesc *indirect);
void qvirtqueue_set_avail_idx(QTestState *qts, QVirtioDevice *d,
QVirtQueue *vq, uint16_t idx);
+void qvirtqueue_enqueue(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
+ uint32_t free_head);
+void qvirtqueue_notify(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq);
void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
uint32_t free_head);
bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 3/7] libqos/virtio: add qvirtqueue_wait_next_used()
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 1/7] hw/9pfs: make FID type assignment atomic across yields Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 2/7] libqos/virtio: split qvirtqueue_kick into enqueue and notify Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 4/7] tests/9p-client: add ability to schedule requests without notifying Christian Schoenebeck
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
qvirtio_wait_used_elem() only waits for a specific used ring element
and aborts if a different one completes first, while
qvirtqueue_get_buf() only checks for a ready element without waiting.
Therefore add qvirtqueue_wait_next_used(), which waits until the
device makes at least one element available in the used ring and
consumes it, regardless of which request it belongs to. It fails if
the waiting time exceeded the passed timeout.
This is a preparatory patch. This new function will be used by the
subsequently added 9p test case, which collects multiple pending
responses without any assumption about their arrival order.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/libqos/virtio.c | 31 +++++++++++++++++++++++++++++++
tests/qtest/libqos/virtio.h | 3 +++
2 files changed, 34 insertions(+)
diff --git a/tests/qtest/libqos/virtio.c b/tests/qtest/libqos/virtio.c
index a2b69c9e92..b9e8f6e89c 100644
--- a/tests/qtest/libqos/virtio.c
+++ b/tests/qtest/libqos/virtio.c
@@ -219,6 +219,9 @@ uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
* @timeout_us: How many microseconds to wait before failing
*
* This function waits for the next completed request on the used ring.
+ *
+ * Unlike qvirtqueue_wait_next_used(), this function is not order-agnostic,
+ * instead it assumes the next element to match the expected passed @desc_idx.
*/
void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
QVirtQueue *vq,
@@ -241,6 +244,34 @@ void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
}
}
+/*
+ * qvirtqueue_wait_next_used:
+ * @desc_idx: A pointer that is filled with the vq->desc[] index of the
+ * consumed element, may be NULL
+ * @len: A pointer that is filled with the length written into the
+ * buffer (may be NULL)
+ * @timeout_us: How many microseconds to wait before failing
+ *
+ * Wait until the device makes at least one element available in the
+ * virtqueue's used ring, and consume it, no matter to which request it
+ * belongs to.
+ *
+ * This is the order-agnostic counterpart of qvirtio_wait_used_elem().
+ */
+void qvirtqueue_wait_next_used(QTestState *qts, QVirtQueue *vq,
+ uint32_t *desc_idx, uint32_t *len,
+ gint64 timeout_us)
+{
+ gint64 start_time = g_get_monotonic_time();
+
+ for (;;) {
+ if (qvirtqueue_get_buf(qts, vq, desc_idx, len)) {
+ return;
+ }
+ g_assert(g_get_monotonic_time() - start_time <= timeout_us);
+ }
+}
+
void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
{
d->bus->wait_config_isr_status(d, timeout_us);
diff --git a/tests/qtest/libqos/virtio.h b/tests/qtest/libqos/virtio.h
index 2299a86802..89bc2fbed5 100644
--- a/tests/qtest/libqos/virtio.h
+++ b/tests/qtest/libqos/virtio.h
@@ -125,6 +125,9 @@ void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
uint32_t desc_idx,
uint32_t *len,
gint64 timeout_us);
+void qvirtqueue_wait_next_used(QTestState *qts, QVirtQueue *vq,
+ uint32_t *desc_idx, uint32_t *len,
+ gint64 timeout_us);
void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us);
QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
QGuestAllocator *alloc, uint16_t index);
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 4/7] tests/9p-client: add ability to schedule requests without notifying
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
` (2 preceding siblings ...)
2026-12-01 19:36 ` [PATCH v2 3/7] libqos/virtio: add qvirtqueue_wait_next_used() Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 5/7] tests/9p-client: move struct P9Hdr into the client header file Christian Schoenebeck
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Add a 'noSend' field to TLOpenOpt and TXattrCreateOpt: if set, such
9p requests are written into the virtqueue's available ring as usual,
which makes them available to the device, however the device (i.e.
9p server) is not notified about the new virtio descriptors (new 9p
requests) yet.
This change allows to send multiple 9p requests simultaneously,
needed by the subsequently added 9p test case.
Since .noSend implies that server did not process the 9p request yet,
it therefore behaves similar to .requestOnly by returning immediately
without waiting for a reply.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/libqos/virtio-9p-client.c | 37 ++++++++++++++++++++++-----
tests/qtest/libqos/virtio-9p-client.h | 10 ++++++++
2 files changed, 41 insertions(+), 6 deletions(-)
diff --git a/tests/qtest/libqos/virtio-9p-client.c b/tests/qtest/libqos/virtio-9p-client.c
index 20c7af91bd..6a8c442da6 100644
--- a/tests/qtest/libqos/virtio-9p-client.c
+++ b/tests/qtest/libqos/virtio-9p-client.c
@@ -212,7 +212,14 @@ P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
return req;
}
-void v9fs_req_send(P9Req *req)
+/*
+ * Add the request's virtio descriptors to the virtqueue and make the request
+ * available to the device by writing it into the virtqueue's available
+ * ring, but do not notify the device yet.
+ *
+ * This e.g. allows to schedule multiple 9p requests simultaneously.
+ */
+static void v9fs_req_enqueue(P9Req *req)
{
QVirtio9P *v9p = req->v9p;
@@ -220,7 +227,15 @@ void v9fs_req_send(P9Req *req)
req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size,
false, true);
qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
- qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head);
+ qvirtqueue_enqueue(req->qts, v9p->vdev, v9p->vq, req->free_head);
+}
+
+void v9fs_req_send(P9Req *req)
+{
+ QVirtio9P *v9p = req->v9p;
+
+ v9fs_req_enqueue(req);
+ qvirtqueue_notify(req->qts, v9p->vdev, v9p->vq);
req->t_off = 0;
}
@@ -757,6 +772,7 @@ TLOpenRes v9fs_tlopen(TLOpenOpt opt)
{
P9Req *req;
uint32_t err;
+ bool wait_for_reply = !opt.requestOnly && !opt.noSend;
g_assert(opt.client);
/* expecting either Rlopen or Rlerror, but obviously not both */
@@ -765,9 +781,13 @@ TLOpenRes v9fs_tlopen(TLOpenOpt opt)
req = v9fs_req_init(opt.client, 4 + 4, P9_TLOPEN, opt.tag);
v9fs_uint32_write(req, opt.fid);
v9fs_uint32_write(req, opt.flags);
- v9fs_req_send(req);
+ if (opt.noSend) {
+ v9fs_req_enqueue(req);
+ } else {
+ v9fs_req_send(req);
+ }
- if (!opt.requestOnly) {
+ if (wait_for_reply) {
v9fs_req_wait_for_reply(req, NULL);
if (opt.expectErr) {
v9fs_rlerror(req, &err);
@@ -1240,6 +1260,7 @@ TXattrCreateRes v9fs_txattrcreate(TXattrCreateOpt opt)
{
P9Req *req;
uint32_t err;
+ bool wait_for_reply = !opt.requestOnly && !opt.noSend;
g_assert(opt.client);
g_assert(opt.name);
@@ -1255,10 +1276,14 @@ TXattrCreateRes v9fs_txattrcreate(TXattrCreateOpt opt)
v9fs_string_write(req, opt.name);
v9fs_uint64_write(req, opt.size);
v9fs_uint32_write(req, opt.flags);
- v9fs_req_send(req);
+ if (opt.noSend) {
+ v9fs_req_enqueue(req);
+ } else {
+ v9fs_req_send(req);
+ }
err = 0;
- if (!opt.requestOnly) {
+ if (wait_for_reply) {
v9fs_req_wait_for_reply(req, NULL);
if (opt.expectErr) {
v9fs_rlerror(req, &err);
diff --git a/tests/qtest/libqos/virtio-9p-client.h b/tests/qtest/libqos/virtio-9p-client.h
index 88e0f41ff0..d0725c1915 100644
--- a/tests/qtest/libqos/virtio-9p-client.h
+++ b/tests/qtest/libqos/virtio-9p-client.h
@@ -279,6 +279,11 @@ typedef struct TLOpenOpt {
v9fs_qid *qid;
uint32_t *iounit;
} rlopen;
+ /*
+ * only enqueue Tlopen request, but do not notify 9p server yet
+ * (optional)
+ */
+ bool noSend;
/* only send Tlopen request but not wait for a reply? (optional) */
bool requestOnly;
/* do we expect an Rlerror response, if yes which error code? (optional) */
@@ -606,6 +611,11 @@ typedef struct TXattrCreateOpt {
uint64_t size;
/* flags: P9_XATTR_CREATE or P9_XATTR_REPLACE (optional) */
uint32_t flags;
+ /*
+ * only enqueue Txattrcreate request, but do not notify 9p server yet
+ * (optional)
+ */
+ bool noSend;
/* only send Txattrcreate request but not wait for a reply? (optional) */
bool requestOnly;
/* do we expect an Rlerror response, if yes which error code? (optional) */
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 5/7] tests/9p-client: move struct P9Hdr into the client header file
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
` (3 preceding siblings ...)
2026-12-01 19:36 ` [PATCH v2 4/7] tests/9p-client: add ability to schedule requests without notifying Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 6/7] tests/9p-client: add v9fs_resp_pop_next_hdr() Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 7/7] tests/9p: add FID type atomicity test cases Christian Schoenebeck
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Move the struct P9Hdr declaration from virtio-9p-client.c into
virtio-9p-client.h, so that test cases can make use of it as well.
This is a preparatory patch, the P9Hdr struct will be used by the
subsequently added function which provides a popped response's 9p
header to the caller of the function.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/libqos/virtio-9p-client.c | 6 ------
tests/qtest/libqos/virtio-9p-client.h | 7 +++++++
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/tests/qtest/libqos/virtio-9p-client.c b/tests/qtest/libqos/virtio-9p-client.c
index 6a8c442da6..6fad275c96 100644
--- a/tests/qtest/libqos/virtio-9p-client.c
+++ b/tests/qtest/libqos/virtio-9p-client.c
@@ -181,12 +181,6 @@ void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
}
}
-typedef struct {
- uint32_t size;
- uint8_t id;
- uint16_t tag;
-} QEMU_PACKED P9Hdr;
-
P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
uint16_t tag)
{
diff --git a/tests/qtest/libqos/virtio-9p-client.h b/tests/qtest/libqos/virtio-9p-client.h
index d0725c1915..28dec08d85 100644
--- a/tests/qtest/libqos/virtio-9p-client.h
+++ b/tests/qtest/libqos/virtio-9p-client.h
@@ -24,6 +24,13 @@
/* Max size of a T-message or R-message */
#define P9_MAX_SIZE (32 * 1024)
+/* size[4] id[1] tag[2] */
+typedef struct {
+ uint32_t size;
+ uint8_t id;
+ uint16_t tag;
+} QEMU_PACKED P9Hdr;
+
typedef struct {
QTestState *qts;
QVirtio9P *v9p;
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 6/7] tests/9p-client: add v9fs_resp_pop_next_hdr()
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
` (4 preceding siblings ...)
2026-12-01 19:36 ` [PATCH v2 5/7] tests/9p-client: move struct P9Hdr into the client header file Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 7/7] tests/9p: add FID type atomicity test cases Christian Schoenebeck
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Add function v9fs_resp_pop_next_hdr() to pop the next response header.
It waits for and consumes the next response that 9p server makes
available on the client's virtqueue, whatever request it belongs to,
and returns a copy of that response's 9p header (size, id, tag).
This is a preparatory patch. This new function will be used by the
subsequently added 9p test case, which handles two pending responses
and must not make any assumption about their order of arrival, as
the exact order is 9p server internal implementation detail that the
test case should be agnostic about.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/libqos/virtio-9p-client.c | 46 +++++++++++++++++++++++++++
tests/qtest/libqos/virtio-9p-client.h | 1 +
2 files changed, 47 insertions(+)
diff --git a/tests/qtest/libqos/virtio-9p-client.c b/tests/qtest/libqos/virtio-9p-client.c
index 6fad275c96..2089ec9206 100644
--- a/tests/qtest/libqos/virtio-9p-client.c
+++ b/tests/qtest/libqos/virtio-9p-client.c
@@ -264,6 +264,52 @@ void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
QVIRTIO_9P_TIMEOUT_US);
}
+/*
+ * Consume the next response that the 9p server makes available on
+ * this client's virtqueue, regardless of the order in which the
+ * related requests were sent or completed.
+ *
+ * Returns a copy of the response's 9p header (size, id, tag).
+ *
+ * Only the response's header is read here, not the entire response body.
+ */
+P9Hdr v9fs_resp_pop_next_hdr(QVirtio9P *v9p)
+{
+ QTestState *qts = global_qtest;
+ uint64_t desc, resp;
+ uint32_t head;
+ uint16_t next, flags;
+ P9Hdr hdr;
+
+ qvirtqueue_wait_next_used(qts, v9p->vq, &head, NULL,
+ QVIRTIO_9P_TIMEOUT_US);
+
+ /*
+ * Resolve the chain's response buffer: this client always passes
+ * requests as two chained, non-indirect descriptors, of which the
+ * second is the device-writable one (see v9fs_req_enqueue())
+ */
+ desc = v9p->vq->desc + (sizeof(struct vring_desc) * head);
+ flags = qtest_readw(qts, desc + offsetof(struct vring_desc, flags));
+ g_assert(flags & VRING_DESC_F_NEXT);
+ g_assert(!(flags & (VRING_DESC_F_WRITE | VRING_DESC_F_INDIRECT)));
+ next = qtest_readw(qts, desc + offsetof(struct vring_desc, next));
+
+ desc = v9p->vq->desc + (sizeof(struct vring_desc) * next);
+ flags = qtest_readw(qts, desc + offsetof(struct vring_desc, flags));
+ g_assert(flags & VRING_DESC_F_WRITE);
+ g_assert(!(flags & VRING_DESC_F_INDIRECT));
+ resp = qtest_readq(qts, desc);
+
+ qtest_memread(qts, resp, &hdr, sizeof(hdr));
+ hdr.size = ldl_le_p(&hdr.size);
+ hdr.tag = lduw_le_p(&hdr.tag);
+ g_assert_cmpuint(hdr.size, >=, sizeof(hdr));
+ g_assert_cmpuint(hdr.size, <=, P9_MAX_SIZE);
+
+ return hdr;
+}
+
void v9fs_req_recv(P9Req *req, uint8_t id)
{
P9Hdr hdr;
diff --git a/tests/qtest/libqos/virtio-9p-client.h b/tests/qtest/libqos/virtio-9p-client.h
index 28dec08d85..8236c11ec4 100644
--- a/tests/qtest/libqos/virtio-9p-client.h
+++ b/tests/qtest/libqos/virtio-9p-client.h
@@ -705,6 +705,7 @@ P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
uint16_t tag);
void v9fs_req_send(P9Req *req);
void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len);
+P9Hdr v9fs_resp_pop_next_hdr(QVirtio9P *v9p);
void v9fs_req_recv(P9Req *req, uint8_t id);
void v9fs_req_free(P9Req *req);
void v9fs_rerror(P9Req *req, uint32_t *err);
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 7/7] tests/9p: add FID type atomicity test cases
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
` (5 preceding siblings ...)
2026-12-01 19:36 ` [PATCH v2 6/7] tests/9p-client: add v9fs_resp_pop_next_hdr() Christian Schoenebeck
@ 2026-12-01 19:36 ` Christian Schoenebeck
6 siblings, 0 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:36 UTC (permalink / raw)
To: qemu-devel; +Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
Add two new test cases:
- 'synth/fid_type/atomic_claim'
- 'local/fid_type/atomic_claim'
Both actually test the same thing (just on the 'synth' and 'local'
fs driver backend respectively):
1. Clone an unopened directory FID.
2. Send a Tlopen and a Txattrcreate request simultaneously.
3. Collect both responses (no matter in which order they arrive).
The core verification is: Tlopen must have won the FID type
transition race, i.e. it must have been the request that assigned
the directory type to the FID, while the Txattrcreate request must
have been rejected (returning a Rlerror response with EINVAL).
Tlopen winning the race is not just the current, coincidental
behavior of the 9p server implementation, but rather the only valid
outcome:
- Tlopen was the first of the two requests sent to 9p server.
- 9p server (starts to) handle the requests in the order it
receives them.
- The atomic FID type transition (fixed by recent previous commit)
claims the FID-type change right at the beginning when handling
the request.
A different outcome would therefore indicate a broken 9p server, so
the test intentionally fails in this case.
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
tests/qtest/virtio-9p-test.c | 71 ++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)
diff --git a/tests/qtest/virtio-9p-test.c b/tests/qtest/virtio-9p-test.c
index 22302bd2f8..97fac58cd7 100644
--- a/tests/qtest/virtio-9p-test.c
+++ b/tests/qtest/virtio-9p-test.c
@@ -299,6 +299,73 @@ static void do_readdir_split(QVirtio9P *v9p, uint32_t count)
g_free(wnames[0]);
}
+/* verify atomicity of 9p server's FID type assignments */
+static void fs_fid_type_atomic_claim(void *obj, void *data,
+ QGuestAllocator *t_alloc)
+{
+ QVirtio9P *v9p = obj;
+ v9fs_set_allocator(t_alloc);
+ P9Req *req_open, *req_xattr;
+ P9Hdr hdr[2];
+ struct V9fsDirent *entries = NULL;
+ uint32_t count, nentries, err;
+
+ tattach({ .client = v9p });
+
+ /* clone the root fid */
+ twalk({ .client = v9p, .fid = 0, .newfid = 1, .nwname = 0 });
+
+ /*
+ * By using .noSend = true the following two requests (Tlopen and
+ * Txattrcreate) are scheduled and eventually transmitted simultaneously to
+ * 9p server. This is mandatory for this test, as it is about concurrency
+ * of FID-type assigning requests.
+ */
+ req_open = tlopen({
+ .client = v9p, .fid = 1, .flags = O_RDONLY, .tag = 0, .noSend = true
+ }).req;
+
+ req_xattr = txattrcreate({
+ .client = v9p, .fid = 1, .name = "user.foo", .size = 1, .flags = 0,
+ .tag = 1, .requestOnly = true
+ }).req;
+
+ /*
+ * Collect both responses, whatever order they arrive in, and store them
+ * in the slot named by their request tag.
+ */
+ for (int i = 0; i < 2; i++) {
+ P9Hdr h = v9fs_resp_pop_next_hdr(v9p);
+ g_assert(h.tag == 0 || h.tag == 1); /* one of the two request tags */
+ hdr[h.tag] = h;
+ }
+
+ /*
+ * Tlopen was sent first and the FID type assignment is atomic, so Tlopen
+ * must have won the race and Txattrcreate must have been rejected. Any
+ * other outcome would indicate a broken FID-type transition by 9p server.
+ */
+ g_assert_cmpint(hdr[0].id, ==, P9_RLOPEN);
+ v9fs_rlopen(req_open, NULL, NULL);
+ g_assert_cmpint(hdr[1].id, ==, P9_RLERROR);
+ v9fs_rlerror(req_xattr, &err);
+ g_assert_cmpint(err, ==, EINVAL);
+
+ /*
+ * As we have a valid open directory FID, let's retrieve some directory
+ * entries (not mandatory for this test case, rather a bonus check).
+ */
+ treaddir({
+ .client = v9p, .fid = 1, .offset = 0, .count = P9_MAX_SIZE - 11,
+ .rreaddir = { .count = &count, .nentries = &nentries,
+ .entries = &entries }
+ });
+ v9fs_free_dirents(entries);
+
+ /* the FID must be clunkable */
+ tclunk({ .client = v9p, .fid = 1 });
+}
+
/*
* Test 9p server's xattr FID count limit enforcement.
*
@@ -1114,6 +1181,8 @@ static void register_virtio_9p_test(void)
fs_readdir_split_256, &opts);
qos_add_test("synth/readdir/split_128", "virtio-9p",
fs_readdir_split_128, &opts);
+ qos_add_test("synth/fid_type/atomic_claim", "virtio-9p",
+ fs_fid_type_atomic_claim, &opts);
qos_add_test("synth/xattr_limit/default", "virtio-9p",
fs_synth_xattr_limit_default, &opts);
opts.before = synth_max_xattr_custom_opt;
@@ -1138,6 +1207,8 @@ static void register_virtio_9p_test(void)
&opts);
qos_add_test("local/use_after_unlink", "virtio-9p", fs_use_after_unlink,
&opts);
+ qos_add_test("local/fid_type/atomic_claim", "virtio-9p",
+ fs_fid_type_atomic_claim, &opts);
qos_add_test("local/deep_absolute_path", "virtio-9p",
fs_deep_absolute_path, &opts);
qos_add_test("local/xattr_limit/default", "virtio-9p",
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments
@ 2026-12-01 19:38 Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 1/7] hw/9pfs: make FID type assignment atomic across yields Christian Schoenebeck
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Christian Schoenebeck @ 2026-12-01 19:38 UTC (permalink / raw)
To: qemu-devel, qemu-stable
Cc: Greg Kurz, Fabiano Rosas, Laurent Vivier, Paolo Bonzini
This series fixes the following issue:
https://gitlab.com/qemu-project/qemu/-/issues/4664
(see patch 1 for a more compact issue description)
The actual fix is only one patch:
- Patch 1: fix TOCTUA issue by introducing an intermediate, early
P9_FID_PENDING state
The rest of the patches are test case changes required for covering the
fixed issue:
- Patch 2: splits the test virtio device's qvirtqueue_kick() into
two functions (qvirtqueue_enqueue() and qvirtqueue_notify()).
- Patch 3: adds new test virtio device function qvirtqueue_wait_next_used()
which allows to wait for the next element made available the device,
independent of order of appearance.
- Patch 4: allows 9p test client to schedule several 9p requests
simultaniously.
- Patch 5: moves strct P9Hdr from .c to .h file.
- Patch 6: allows 9p test client to retrieve the next 9p server response,
independent of order of arrival.
- Patch 7: finally provides the 9p test case guarding the issue fixed by
patch 1.
Note: originally I started implementing the test as (non-deterministic)
race-test, but then realized, due to the way the fix applies behaviour, the
test actually degrades to a perfect deterministic 9p protocol check.
v2: no changes; just resent this series with proper email threading.
Christian Schoenebeck (7):
hw/9pfs: make FID type assignment atomic across yields
libqos/virtio: split qvirtqueue_kick into enqueue and notify
libqos/virtio: add qvirtqueue_wait_next_used()
tests/9p-client: add ability to schedule requests without notifying
tests/9p-client: move struct P9Hdr into the client header file
tests/9p-client: add v9fs_resp_pop_next_hdr()
tests/9p: add FID type atomicity test cases
hw/9pfs/9p.c | 55 ++++++++++++++++-
hw/9pfs/9p.h | 21 +++++++
tests/qtest/libqos/virtio-9p-client.c | 89 +++++++++++++++++++++++----
tests/qtest/libqos/virtio-9p-client.h | 18 ++++++
tests/qtest/libqos/virtio.c | 73 +++++++++++++++++++---
tests/qtest/libqos/virtio.h | 6 ++
tests/qtest/virtio-9p-test.c | 71 +++++++++++++++++++++
7 files changed, 311 insertions(+), 22 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-01 19:47 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 19:38 [PATCH v2 0/7] 9pfs: fix TOCTUA on concurrent FID-type assignments Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 1/7] hw/9pfs: make FID type assignment atomic across yields Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 2/7] libqos/virtio: split qvirtqueue_kick into enqueue and notify Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 3/7] libqos/virtio: add qvirtqueue_wait_next_used() Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 4/7] tests/9p-client: add ability to schedule requests without notifying Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 5/7] tests/9p-client: move struct P9Hdr into the client header file Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 6/7] tests/9p-client: add v9fs_resp_pop_next_hdr() Christian Schoenebeck
2026-12-01 19:36 ` [PATCH v2 7/7] tests/9p: add FID type atomicity test cases 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.