qemu-e2k/migration/tls.c
Zhenyu Ye a9e80a5f0c migration: tls: fix memory leak in migration_tls_get_creds
Currently migration_tls_get_creds() adds the reference of creds
but there was no place to unref it.  So the OBJECT(creds) will
never be freed and result in memory leak.

The leak stack:
Direct leak of 104 byte(s) in 1 object(s) allocated from:
    #0 0xffffa88bd20b in __interceptor_malloc (/usr/lib64/libasan.so.4+0xd320b)
    #1 0xffffa7f0cb1b in g_malloc (/usr/lib64/libglib-2.0.so.0+0x58b1b)
    #2 0x14b58cb in object_new_with_type qom/object.c:634
    #3 0x14b597b in object_new qom/object.c:645
    #4 0x14c0e4f in user_creatable_add_type qom/object_interfaces.c:59
    #5 0x141c78b in qmp_object_add qom/qom-qmp-cmds.c:312
    #6 0x140e513 in qmp_marshal_object_add qapi/qapi-commands-qom.c:279
    #7 0x176ba97 in do_qmp_dispatch qapi/qmp-dispatch.c:165
    #8 0x176bee7 in qmp_dispatch qapi/qmp-dispatch.c:208
    #9 0x136e337 in monitor_qmp_dispatch monitor/qmp.c:150
    #10 0x136eae3 in monitor_qmp_bh_dispatcher monitor/qmp.c:239
    #11 0x1852e93 in aio_bh_call util/async.c:89
    #12 0x18531b7 in aio_bh_poll util/async.c:117
    #13 0x18616bf in aio_dispatch util/aio-posix.c:459
    #14 0x1853f37 in aio_ctx_dispatch util/async.c:268
    #15 0xffffa7f06a7b in g_main_context_dispatch (/usr/lib64/libglib-2.0.so.0+0x52a7b)

Since we're fine to use the borrowed reference when using the creds,
so just remove the object_ref() in migration_tls_get_creds().

Signed-off-by: Zhenyu Ye <yezhenyu2@huawei.com>
Message-Id: <20200722033228.71-1-yezhenyu2@huawei.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
2020-08-28 13:34:52 +01:00

165 lines
4.8 KiB
C

/*
* QEMU migration TLS support
*
* Copyright (c) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "qemu/osdep.h"
#include "channel.h"
#include "migration.h"
#include "tls.h"
#include "io/channel-tls.h"
#include "crypto/tlscreds.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "trace.h"
static QCryptoTLSCreds *
migration_tls_get_creds(MigrationState *s,
QCryptoTLSCredsEndpoint endpoint,
Error **errp)
{
Object *creds;
QCryptoTLSCreds *ret;
creds = object_resolve_path_component(
object_get_objects_root(), s->parameters.tls_creds);
if (!creds) {
error_setg(errp, "No TLS credentials with id '%s'",
s->parameters.tls_creds);
return NULL;
}
ret = (QCryptoTLSCreds *)object_dynamic_cast(
creds, TYPE_QCRYPTO_TLS_CREDS);
if (!ret) {
error_setg(errp, "Object with id '%s' is not TLS credentials",
s->parameters.tls_creds);
return NULL;
}
if (ret->endpoint != endpoint) {
error_setg(errp,
"Expected TLS credentials for a %s endpoint",
endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ?
"client" : "server");
return NULL;
}
return ret;
}
static void migration_tls_incoming_handshake(QIOTask *task,
gpointer opaque)
{
QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
Error *err = NULL;
if (qio_task_propagate_error(task, &err)) {
trace_migration_tls_incoming_handshake_error(error_get_pretty(err));
error_report_err(err);
} else {
trace_migration_tls_incoming_handshake_complete();
migration_channel_process_incoming(ioc);
}
object_unref(OBJECT(ioc));
}
void migration_tls_channel_process_incoming(MigrationState *s,
QIOChannel *ioc,
Error **errp)
{
QCryptoTLSCreds *creds;
QIOChannelTLS *tioc;
creds = migration_tls_get_creds(
s, QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, errp);
if (!creds) {
return;
}
tioc = qio_channel_tls_new_server(
ioc, creds,
s->parameters.tls_authz,
errp);
if (!tioc) {
return;
}
trace_migration_tls_incoming_handshake_start();
qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-incoming");
qio_channel_tls_handshake(tioc,
migration_tls_incoming_handshake,
NULL,
NULL,
NULL);
}
static void migration_tls_outgoing_handshake(QIOTask *task,
gpointer opaque)
{
MigrationState *s = opaque;
QIOChannel *ioc = QIO_CHANNEL(qio_task_get_source(task));
Error *err = NULL;
if (qio_task_propagate_error(task, &err)) {
trace_migration_tls_outgoing_handshake_error(error_get_pretty(err));
} else {
trace_migration_tls_outgoing_handshake_complete();
}
migration_channel_connect(s, ioc, NULL, err);
object_unref(OBJECT(ioc));
}
void migration_tls_channel_connect(MigrationState *s,
QIOChannel *ioc,
const char *hostname,
Error **errp)
{
QCryptoTLSCreds *creds;
QIOChannelTLS *tioc;
creds = migration_tls_get_creds(
s, QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, errp);
if (!creds) {
return;
}
if (s->parameters.tls_hostname && *s->parameters.tls_hostname) {
hostname = s->parameters.tls_hostname;
}
if (!hostname) {
error_setg(errp, "No hostname available for TLS");
return;
}
tioc = qio_channel_tls_new_client(
ioc, creds, hostname, errp);
if (!tioc) {
return;
}
trace_migration_tls_outgoing_handshake_start(hostname);
qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-outgoing");
qio_channel_tls_handshake(tioc,
migration_tls_outgoing_handshake,
s,
NULL,
NULL);
}