Per reported and analyzed by Peter:
https://lore.kernel.org/r/CAFEAcA_mUQ2NeoguR5efrhw7XYGofnriWEA=+Dg+Ocvyam1wAw@mail.gmail.com
mfd leak is a false positive, try to use a coverity annotation (which I
didn't find manual myself, but still give it a shot).
Fix the other one by capture error if setenv() failed. When at it, pass
the error to the top (cpr_state_save()). Along the way, changing all
retval to bool when errp is around.
Resolves: Coverity CID
1641391
Resolves: Coverity CID
1641392
Fixes: efc6587313 ("migration: cpr-exec save and load")
Reviewed-by: Fabiano Rosas <farosas@suse.de>
Link: https://lore.kernel.org/r/20251021220407.2662288-3-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
void cpr_set_incoming_mode(MigMode mode);
bool cpr_is_incoming(void);
-int cpr_state_save(MigrationChannel *channel, Error **errp);
+bool cpr_state_save(MigrationChannel *channel, Error **errp);
int cpr_state_load(MigrationChannel *channel, Error **errp);
void cpr_state_close(void);
struct QIOChannel *cpr_state_ioc(void);
void cpr_exec_init(void);
QEMUFile *cpr_exec_output(Error **errp);
QEMUFile *cpr_exec_input(Error **errp);
-void cpr_exec_persist_state(QEMUFile *f);
+bool cpr_exec_persist_state(QEMUFile *f, Error **errp);
bool cpr_exec_has_state(void);
void cpr_exec_unpersist_state(void);
void cpr_exec_unpreserve_fds(void);
return qemu_file_new_output(ioc);
}
-void cpr_exec_persist_state(QEMUFile *f)
+bool cpr_exec_persist_state(QEMUFile *f, Error **errp)
{
QIOChannelFile *fioc = QIO_CHANNEL_FILE(qemu_file_get_ioc(f));
+ /* coverity[leaked_storage] - mfd intentionally kept open across exec() */
int mfd = dup(fioc->fd);
char val[16];
/* Remember mfd in environment for post-exec load */
qemu_clear_cloexec(mfd);
snprintf(val, sizeof(val), "%d", mfd);
- g_setenv(CPR_EXEC_STATE_NAME, val, 1);
+ if (!g_setenv(CPR_EXEC_STATE_NAME, val, 1)) {
+ error_setg(errp, "Setting env %s = %s failed", CPR_EXEC_STATE_NAME, val);
+ return false;
+ }
+
+ return true;
}
static int cpr_exec_find_state(void)
return incoming_mode != MIG_MODE_NONE;
}
-int cpr_state_save(MigrationChannel *channel, Error **errp)
+bool cpr_state_save(MigrationChannel *channel, Error **errp)
{
int ret;
QEMUFile *f;
} else if (mode == MIG_MODE_CPR_EXEC) {
f = cpr_exec_output(errp);
} else {
- return 0;
+ return true;
}
if (!f) {
- return -1;
+ return false;
}
qemu_put_be32(f, QEMU_CPR_FILE_MAGIC);
ret = vmstate_save_state(f, &vmstate_cpr_state, &cpr_state, 0, errp);
if (ret) {
qemu_fclose(f);
- return ret;
+ return false;
}
if (migrate_mode() == MIG_MODE_CPR_EXEC) {
- cpr_exec_persist_state(f);
+ if (!cpr_exec_persist_state(f, errp)) {
+ qemu_fclose(f);
+ return false;
+ }
}
/*
qio_channel_shutdown(qemu_file_get_ioc(f), QIO_CHANNEL_SHUTDOWN_WRITE,
NULL);
cpr_state_file = f;
- return 0;
+ return true;
}
int cpr_state_load(MigrationChannel *channel, Error **errp)
return;
}
- if (cpr_state_save(cpr_channel, &local_err)) {
+ if (!cpr_state_save(cpr_channel, &local_err)) {
goto out;
}