block: Fix BDS use after free during shutdown
PATCH 1/1 block: Fix BDS use after free during shutdown
During shutdown, blockdev_close_all_bdrv_states() drops any block node references that are still owned by the monitor (i.e. the user). However, in doing so, it forgot to also remove the node from monitor_bdrv_states (which qmp_blockdev_del() correctly does), which means that later calls of bdrv_first()/bdrv_next() will still return the (now stale) pointer to the node.
Usually there is no such call after this point, but in some cases it can happen. In the reported case, there was an ongoing migration, and the migration thread wasn't shut down yet: migration_shutdown() called by qemu_cleanup() doesn't actually wait for the migration to be shut down, but may just move it to MIGRATION_STATUS_CANCELLING. The next time migration_iteration_finish() runs, it sees the status and tries to re-activate all block devices that migration may have previously inactivated. This is where bdrv_first()/bdrv_next() get called and the access to the already freed node happens.
It is debatable if migration_shutdown() should really return before migration has settled, but leaving a dangling pointer in the list of monitor-owned block nodes is clearly a bug either way and fixing it solves the immediate problem, so fix it.
--- blockdev.c | 1 + 1 file changed, 1 insertion(+)
blockdev.c +1 -0
@@ -686,6 +686,7 @@ void blockdev_close_all_bdrv_states(void)GLOBAL_STATE_CODE (); QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { +QTAILQ_REMOVE (&monitor_bdrv_states, bs, monitor_list);bdrv_unref (bs); } }
Signature · 1 line
2.52.0
Quoted 22 lines · Kevin Wolf
During shutdown,
blockdev_close_all_bdrv_states()drops any block node references that are still owned by the monitor (i.e. the user). However, in doing so, it forgot to also remove the node frommonitor_bdrv_states(whichqmp_blockdev_del()correctly does), which means that later calls of bdrv_first()/bdrv_next() will still return the (now stale) pointer to the node.Usually there is no such call after this point, but in some cases it can happen. In the reported case, there was an ongoing migration, and the migration thread wasn't shut down yet:
migration_shutdown()called byqemu_cleanup()doesn't actually wait for the migration to be shut down, but may just move it toMIGRATION_STATUS_CANCELLING. The next timemigration_iteration_finish()runs, it sees the status and tries to re-activate all block devices that migration may have previously inactivated. This is where bdrv_first()/bdrv_next() get called and the access to the already freed node happens.It is debatable if
migration_shutdown()should really return before migration has settled, but leaving a dangling pointer in the list of monitor-owned block nodes is clearly a bug either way and fixing it solves the immediate problem, so fix it.
Quoted 6 lines
Reported-by Thomas Huth <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- blockdev.c | 1 + 1 file changed, 1 insertion(+)
Quoted 34 lines · Kevin Wolf
During shutdown,
blockdev_close_all_bdrv_states()drops any block node references that are still owned by the monitor (i.e. the user). However, in doing so, it forgot to also remove the node frommonitor_bdrv_states(whichqmp_blockdev_del()correctly does), which means that later calls of bdrv_first()/bdrv_next() will still return the (now stale) pointer to the node.Usually there is no such call after this point, but in some cases it can happen. In the reported case, there was an ongoing migration, and the migration thread wasn't shut down yet:
migration_shutdown()called byqemu_cleanup()doesn't actually wait for the migration to be shut down, but may just move it toMIGRATION_STATUS_CANCELLING. The next timemigration_iteration_finish()runs, it sees the status and tries to re-activate all block devices that migration may have previously inactivated. This is where bdrv_first()/bdrv_next() get called and the access to the already freed node happens.It is debatable if
migration_shutdown()should really return before migration has settled, but leaving a dangling pointer in the list of monitor-owned block nodes is clearly a bug either way and fixing it solves the immediate problem, so fix it.Reported-by Thomas Huth <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- blockdev.c | 1 + 1 file changed, 1 insertion(+)blockdev.c +0
-0
blockdev.c void blockdev_close_all_bdrv_states(void) · line 692
Quoted 7 lines
blockdev.c +1
-0@@ -686,6 +686,7 @@ void blockdev_close_all_bdrv_states(void)GLOBAL_STATE_CODE (); QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { +QTAILQ_REMOVE (&monitor_bdrv_states, bs, monitor_list);bdrv_unref (bs); } }
Thanks again!
Quoted 17 lines · Kevin Wolf
During shutdown,
blockdev_close_all_bdrv_states()drops any block node references that are still owned by the monitor (i.e. the user). However, in doing so, it forgot to also remove the node frommonitor_bdrv_states(whichqmp_blockdev_del()correctly does), which means that later calls of bdrv_first()/bdrv_next() will still return the (now stale) pointer to the node.Usually there is no such call after this point, but in some cases it can happen. In the reported case, there was an ongoing migration, and the migration thread wasn't shut down yet:
migration_shutdown()called byqemu_cleanup()doesn't actually wait for the migration to be shut down, but may just move it toMIGRATION_STATUS_CANCELLING. The next timemigration_iteration_finish()runs, it sees the status and tries to re-activate all block devices that migration may have previously inactivated. This is where bdrv_first()/bdrv_next() get called and the access to the already freed node happens.
Quoted 8 lines
It is debatable if
migration_shutdown()should really return before migration has settled, but leaving a dangling pointer in the list of monitor-owned block nodes is clearly a bug either way and fixing it solves the immediate problem, so fix it.Reported-by Thomas Huth <[email protected]>Signed-off-by Kevin Wolf <[email protected]>