Skip to content

vhost-vdpa: fix a use-after-free on the config eventfd

Yu Zhang kvm lkml netdev virtualization 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2vhost-vdpa: don't install the eventfd_ctx_fdget() error in config_ctxdrivers/vhost/vdpa.c+4 -80 replies
2/2vhost-vdpa: protect config_ctx from being freed under the config callbackdrivers/vhost/vdpa.c+25 -70 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] vhost-vdpa: fix a use-after-free on the config eventfd

vhost_vdpa_config_cb() reads v->config_ctx with no reference and no lock while VHOST_VDPA_SET_CONFIG_CALL frees the old context, so a config interrupt delivered at the wrong moment signals freed memory.

Patch 1 stops the fdget error value from ever being installed in config_ctx. It is a fix in its own right -- the callback tests only for NULL and would signal the ERR_PTR -- and it is also what lets patch 2's lock establish "non-NULL implies valid".

Patch 2 adds the lock that closes the use-after-free.

This is not the reopen refcount underflow fixed by f6bbf0010ba0 ("vhost-vdpa: fix use-after-free of v->config_ctx"), and it is a different bug from the vq call fd one I sent on Aug 6
(https://lore.kernel.org/all/[email protected]/),
which is about the parent caching an eventfd_ctx it does not own. The three touch different functions.

Tested on v7.2-rc6 with KASAN under qemu, VDUSE as the parent: one thread swaps the config call fd on the vhost-vdpa fd while another injects VDUSE_DEV_INJECT_CONFIG_IRQ on the VDUSE fd. 9 of 10 30-second runs hit the UAF; 0 of 5 with the series applied, with the workload counters unchanged either way (~30k swaps and ~2.6M injects per run), so the race is still being exercised rather than merely not reached. Also clean over 5 runs with PROVE_LOCKING, and vdpa_sim shows no change on the shared VHOST_VDPA_SET_CONFIG_CALL path (20000 install/unbind cycles plus the bad-fd path, identical before and after).

For patch 1 specifically: with a working config fd installed, 250 injects deliver 250 signals; after a VHOST_VDPA_SET_CONFIG_CALL with a bad fd, delivery stops on an unpatched kernel and continues with the patch.

Yu Zhang (2):
vhost-vdpa: don't install the eventfd_ctx_fdget() error in config_ctx
vhost-vdpa: protect config_ctx from being freed under the config
callback

 drivers/vhost/vdpa.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0