migration: Fix error handling after dup in file migration

The file migration code was allowing a possible -1 from a failed call
to dup() to propagate into the new QIOFileChannel::fd before checking
for validity. Coverity doesn't like that, possibly due to the the
lseek(-1, ...) call that would ensue before returning from the channel
creation routine.

Use the newly introduced qio_channel_file_dupfd() to properly check
the return of dup() before proceeding.

Fixes: CID 1539961
Fixes: CID 1539965
Fixes: CID 1539960
Fixes: 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes: decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
Reviewed-by: "Daniel P. Berrangé" <berrange@redhat.com>
Link: https://lore.kernel.org/r/20240311233335.17299-3-farosas@suse.de
Signed-off-by: Peter Xu <peterx@redhat.com>
This commit is contained in:
Fabiano Rosas 2024-03-11 20:33:35 -03:00 committed by Peter Xu
parent 4760cedc61
commit c827fafcaa
2 changed files with 11 additions and 12 deletions

View File

@ -80,6 +80,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
void fd_start_incoming_migration(const char *fdname, Error **errp) void fd_start_incoming_migration(const char *fdname, Error **errp)
{ {
QIOChannel *ioc; QIOChannel *ioc;
QIOChannelFile *fioc;
int fd = monitor_fd_param(monitor_cur(), fdname, errp); int fd = monitor_fd_param(monitor_cur(), fdname, errp);
if (fd == -1) { if (fd == -1) {
return; return;
@ -103,15 +104,13 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)
int channels = migrate_multifd_channels(); int channels = migrate_multifd_channels();
while (channels--) { while (channels--) {
ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd))); fioc = qio_channel_file_new_dupfd(fd, errp);
if (!fioc) {
if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
error_setg(errp, "Failed to duplicate fd %d", fd);
return; return;
} }
qio_channel_set_name(ioc, "migration-fd-incoming"); qio_channel_set_name(ioc, "migration-fd-incoming");
qio_channel_add_watch_full(ioc, G_IO_IN, qio_channel_add_watch_full(QIO_CHANNEL(fioc), G_IO_IN,
fd_accept_incoming_migration, fd_accept_incoming_migration,
NULL, NULL, NULL, NULL,
g_main_context_get_thread_default()); g_main_context_get_thread_default());

View File

@ -58,12 +58,13 @@ bool file_send_channel_create(gpointer opaque, Error **errp)
int fd = fd_args_get_fd(); int fd = fd_args_get_fd();
if (fd && fd != -1) { if (fd && fd != -1) {
ioc = qio_channel_file_new_fd(dup(fd)); ioc = qio_channel_file_new_dupfd(fd, errp);
} else { } else {
ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp); ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
if (!ioc) { }
goto out;
} if (!ioc) {
goto out;
} }
multifd_channel_connect(opaque, QIO_CHANNEL(ioc)); multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
@ -147,10 +148,9 @@ void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
NULL, NULL, NULL, NULL,
g_main_context_get_thread_default()); g_main_context_get_thread_default());
fioc = qio_channel_file_new_fd(dup(fioc->fd)); fioc = qio_channel_file_new_dupfd(fioc->fd, errp);
if (!fioc || fioc->fd == -1) { if (!fioc) {
error_setg(errp, "Error creating migration incoming channel");
break; break;
} }
} while (++i < channels); } while (++i < channels);