Skip to content

Commit 68b69fa

Browse files
Yuto OhnukiMiklos Szeredi
authored andcommitted
virtiofs: add FUSE protocol validation
Add virtio_fs_verify_response() to validate that the server properly follows the FUSE protocol by checking: - Response length is at least sizeof(struct fuse_out_header). - oh.len matches the actual response length. - oh.unique matches the request's unique identifier. On validation failure, set error to -EIO and normalize oh.len to prevent underflow in copy_args_from_argbuf(). Addresses the TODO comment in virtio_fs_request_complete(). Signed-off-by: Yuto Ohnuki <ytohnuki@amazon.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 42fbb31 commit 68b69fa

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

fs/fuse/virtio_fs.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,27 @@ static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
758758
req->argbuf = NULL;
759759
}
760760

761+
/* Verify that the server properly follows the FUSE protocol */
762+
static bool virtio_fs_verify_response(struct fuse_req *req, unsigned int len)
763+
{
764+
struct fuse_out_header *oh = &req->out.h;
765+
766+
if (len < sizeof(*oh)) {
767+
pr_warn("virtio-fs: response too short (%u)\n", len);
768+
return false;
769+
}
770+
if (oh->len != len) {
771+
pr_warn("virtio-fs: oh.len mismatch (%u != %u)\n", oh->len, len);
772+
return false;
773+
}
774+
if (oh->unique != req->in.h.unique) {
775+
pr_warn("virtio-fs: oh.unique mismatch (%llu != %llu)\n",
776+
oh->unique, req->in.h.unique);
777+
return false;
778+
}
779+
return true;
780+
}
781+
761782
/* Work function for request completion */
762783
static void virtio_fs_request_complete(struct fuse_req *req,
763784
struct virtio_fs_vq *fsvq)
@@ -767,10 +788,6 @@ static void virtio_fs_request_complete(struct fuse_req *req,
767788
unsigned int len, i, thislen;
768789
struct folio *folio;
769790

770-
/*
771-
* TODO verify that server properly follows FUSE protocol
772-
* (oh.uniq, oh.len)
773-
*/
774791
args = req->args;
775792
copy_args_from_argbuf(args, req);
776793

@@ -824,6 +841,10 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
824841
virtqueue_disable_cb(vq);
825842

826843
while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
844+
if (!virtio_fs_verify_response(req, len)) {
845+
req->out.h.error = -EIO;
846+
req->out.h.len = sizeof(struct fuse_out_header);
847+
}
827848
spin_lock(&fpq->lock);
828849
list_move_tail(&req->list, &reqs);
829850
spin_unlock(&fpq->lock);

0 commit comments

Comments
 (0)