From patchwork Tue Jan 19 13:09:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 8061891 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CCA4D9F818 for ; Tue, 19 Jan 2016 13:20:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 12636201D3 for ; Tue, 19 Jan 2016 13:20:06 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 14773200F2 for ; Tue, 19 Jan 2016 13:20:04 +0000 (UTC) Received: from localhost ([::1]:36973 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLWCZ-0005b6-D7 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jan 2016 08:20:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39861) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLW2h-0007ZK-7T for qemu-devel@nongnu.org; Tue, 19 Jan 2016 08:09:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLW2f-0005ys-2E for qemu-devel@nongnu.org; Tue, 19 Jan 2016 08:09:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50563) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLW2Z-0005wp-2p; Tue, 19 Jan 2016 08:09:43 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id CB6028D3B5; Tue, 19 Jan 2016 13:09:42 +0000 (UTC) Received: from t530wlan.home.berrange.com.com (vpn1-6-80.ams2.redhat.com [10.36.6.80]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0JD9Qb5012785; Tue, 19 Jan 2016 08:09:41 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 19 Jan 2016 13:09:21 +0000 Message-Id: <1453208963-16834-12-git-send-email-berrange@redhat.com> In-Reply-To: <1453208963-16834-1-git-send-email-berrange@redhat.com> References: <1453208963-16834-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Paolo Bonzini , qemu-block@nongnu.org Subject: [Qemu-devel] [PATCH v3 11/13] nbd: enable use of TLS with NBD block driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This modifies the NBD driver so that it is possible to request use of TLS. This is done by providing the 'tls-creds' parameter with the ID of a previously created QCryptoTLSCreds object. For example $QEMU -object tls-creds-x509,id=tls0,endpoint=client,\ dir=/home/berrange/security/qemutls \ -drive driver=nbd,host=localhost,port=9000,tls-creds=tls0 The client will drop the connection if the NBD server does not provide TLS. Signed-off-by: Daniel P. Berrange --- block/nbd-client.c | 10 ++++--- block/nbd-client.h | 2 ++ block/nbd.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index f7bacfd..024d5f3 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -378,8 +378,12 @@ void nbd_client_close(BlockDriverState *bs) nbd_teardown_connection(bs); } -int nbd_client_init(BlockDriverState *bs, QIOChannelSocket *sioc, - const char *export, Error **errp) +int nbd_client_init(BlockDriverState *bs, + QIOChannelSocket *sioc, + const char *export, + QCryptoTLSCreds *tlscreds, + const char *hostname, + Error **errp) { NbdClientSession *client = nbd_get_client_session(bs); int ret; @@ -390,7 +394,7 @@ int nbd_client_init(BlockDriverState *bs, QIOChannelSocket *sioc, ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), export, &client->nbdflags, - NULL, NULL, + tlscreds, hostname, &client->ioc, &client->size, errp); if (ret < 0) { diff --git a/block/nbd-client.h b/block/nbd-client.h index e8b3283..53f116d 100644 --- a/block/nbd-client.h +++ b/block/nbd-client.h @@ -39,6 +39,8 @@ NbdClientSession *nbd_get_client_session(BlockDriverState *bs); int nbd_client_init(BlockDriverState *bs, QIOChannelSocket *sock, const char *export_name, + QCryptoTLSCreds *tlscreds, + const char *hostname, Error **errp); void nbd_client_close(BlockDriverState *bs); diff --git a/block/nbd.c b/block/nbd.c index 072fc5f..c17415f 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -259,36 +259,92 @@ static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, return sioc; } + +static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) +{ + Object *obj; + QCryptoTLSCreds *creds; + + obj = object_resolve_path_component( + object_get_objects_root(), id); + if (!obj) { + error_setg(errp, "No TLS credentials with id '%s'", + id); + return NULL; + } + creds = (QCryptoTLSCreds *) + object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); + if (!creds) { + error_setg(errp, "Object with id '%s' is not TLS credentials", + id); + return NULL; + } + + if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { + error_setg(errp, + "Expecting TLS credentials with a client endpoint"); + return NULL; + } + object_ref(obj); + return creds; +} + + static int nbd_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { BDRVNBDState *s = bs->opaque; char *export = NULL; - int result; - QIOChannelSocket *sioc; + QIOChannelSocket *sioc = NULL; SocketAddress *saddr; + const char *tlscredsid; + QCryptoTLSCreds *tlscreds = NULL; + const char *hostname = NULL; + int ret = -EINVAL; /* Pop the config into our state object. Exit if invalid. */ saddr = nbd_config(s, options, &export, errp); if (!saddr) { - return -EINVAL; + goto error; + } + + tlscredsid = g_strdup(qdict_get_try_str(options, "tls-creds")); + if (tlscredsid) { + qdict_del(options, "tls-creds"); + tlscreds = nbd_get_tls_creds(tlscredsid, errp); + if (!tlscreds) { + goto error; + } + + if (saddr->type != SOCKET_ADDRESS_KIND_INET) { + error_setg(errp, "TLS only supported over IP sockets"); + goto error; + } + hostname = saddr->u.inet->host; } /* establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. */ sioc = nbd_establish_connection(saddr, errp); - qapi_free_SocketAddress(saddr); if (!sioc) { - g_free(export); - return -ECONNREFUSED; + ret = -ECONNREFUSED; + goto error; } /* NBD handshake */ - result = nbd_client_init(bs, sioc, export, errp); - object_unref(OBJECT(sioc)); + ret = nbd_client_init(bs, sioc, export, + tlscreds, hostname, errp); + error: + if (sioc) { + object_unref(OBJECT(sioc)); + } + if (tlscreds) { + object_unref(OBJECT(tlscreds)); + } + qapi_free_SocketAddress(saddr); g_free(export); - return result; + return ret; } static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, @@ -350,6 +406,7 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) const char *host = qdict_get_try_str(options, "host"); const char *port = qdict_get_try_str(options, "port"); const char *export = qdict_get_try_str(options, "export"); + const char *tlscreds = qdict_get_try_str(options, "tls-creds"); qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd"))); @@ -384,6 +441,9 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) if (export) { qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export))); } + if (tlscreds) { + qdict_put_obj(opts, "tls-creds", QOBJECT(qstring_from_str(tlscreds))); + } bs->full_open_options = opts; }