@samitouri / QOSamiQemu / commits / f77a7cec9f

migration: Use QAPI_CLONE_MEMBERS in migrate_params_test_apply

Use QAPI_CLONE_MEMBERS instead of making an assignment. The QAPI method makes the handling of the TLS strings more intuitive because it clones them as well. This also fixes a segfault when a NULL TLS option is accessed as part of a validation check for another option (e.g. in the zero-copy + multifd compression case). Details follow: Currently, after copying s->parameters to the temporary MigrationParameters object before migrate_params_check(), the references in temporary object to the TLS options are dropped, either because: a) the user set a new option, in which case that's fine as s->parameters still holds the reference to the old memory or, b) the user did not set a new option, in which case keeping the references in the temporary object would later cause them to be freed along with it, leading to double-free when s->parameters is also freed later on. In this second case, it was overlooked that the TLS options can be accessed already during migrate_params_check() as part of validation of another option. Those pointers should not have been cleared. Using QAPI_CLONE_MEMBERS fixes the issue because the temporary object is not stealing a reference from s->parameters anymore. Cc: qemu-stable <qemu-stable@nongnu.org> Fixes: aed97f0563 ("migration: Normalize tls arguments") Reported-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name> Link: https://lore.kernel.org/r/a65a1049-9f19-460a-8e27-a62bb30d2727@maciej.szmigiero.name Reviewed-by: Peter Xu <peterx@redhat.com> Signed-off-by: Fabiano Rosas <farosas@suse.de> Tested-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> Link: https://lore.kernel.org/r/20260414223718.23965-1-farosas@suse.de Signed-off-by: Peter Xu <peterx@redhat.com>

Fabiano Rosas committed Apr 14, 2026 at 19:37 UTC f77a7cec9f64ed94062f7b9eb661bd07d60e859a
1 file changed +12 -14
migration/options.c
+12 -14
@@ -1279,9 +1279,9 @@ bool migrate_params_check(MigrationParameters *params, Error **errp)
1279 static void migrate_params_test_apply(MigrationParameters *params,
1280 MigrationParameters *dest)
1281 {
1282 - *dest = migrate_get_current()->parameters;
1282 + MigrationState *s = migrate_get_current();
1283
1284 - /* TODO use QAPI_CLONE() instead of duplicating it inline */
1284 + QAPI_CLONE_MEMBERS(MigrationParameters, dest, &s->parameters);
1285
1286 if (params->has_throttle_trigger_threshold) {
1287 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
@@ -1300,24 +1300,18 @@ static void migrate_params_test_apply(MigrationParameters *params,
1300 }
1301
1302 if (params->tls_creds) {
1303 + qapi_free_StrOrNull(dest->tls_creds);
1304 dest->tls_creds = QAPI_CLONE(StrOrNull, params->tls_creds);
1304 - } else {
1305 - /* clear the reference, it's owned by s->parameters */
1306 - dest->tls_creds = NULL;
1305 }
1306
1307 if (params->tls_hostname) {
1308 + qapi_free_StrOrNull(dest->tls_hostname);
1309 dest->tls_hostname = QAPI_CLONE(StrOrNull, params->tls_hostname);
1311 - } else {
1312 - /* clear the reference, it's owned by s->parameters */
1313 - dest->tls_hostname = NULL;
1310 }
1311
1312 if (params->tls_authz) {
1313 + qapi_free_StrOrNull(dest->tls_authz);
1314 dest->tls_authz = QAPI_CLONE(StrOrNull, params->tls_authz);
1318 - } else {
1319 - /* clear the reference, it's owned by s->parameters */
1320 - dest->tls_authz = NULL;
1315 }
1316
1317 if (params->has_max_bandwidth) {
@@ -1374,8 +1368,9 @@ static void migrate_params_test_apply(MigrationParameters *params,
1368 }
1369
1370 if (params->has_block_bitmap_mapping) {
1377 - dest->has_block_bitmap_mapping = true;
1378 - dest->block_bitmap_mapping = params->block_bitmap_mapping;
1371 + qapi_free_BitmapMigrationNodeAliasList(dest->block_bitmap_mapping);
1372 + dest->block_bitmap_mapping = QAPI_CLONE(BitmapMigrationNodeAliasList,
1373 + params->block_bitmap_mapping);
1374 }
1375
1376 if (params->has_x_vcpu_dirty_limit_period) {
@@ -1399,7 +1394,8 @@ static void migrate_params_test_apply(MigrationParameters *params,
1394 }
1395
1396 if (params->has_cpr_exec_command) {
1402 - dest->cpr_exec_command = params->cpr_exec_command;
1397 + qapi_free_strList(dest->cpr_exec_command);
1398 + dest->cpr_exec_command = QAPI_CLONE(strList, params->cpr_exec_command);
1399 }
1400 }
1401
@@ -1555,4 +1551,6 @@ void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
1551 }
1552
1553 migrate_tls_opts_free(&tmp);
1554 + qapi_free_BitmapMigrationNodeAliasList(tmp.block_bitmap_mapping);
1555 + qapi_free_strList(tmp.cpr_exec_command);
1556 }