From patchwork Mon Jun 25 22:46:00 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 167249 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1069CB6FB4 for ; Tue, 26 Jun 2012 08:46:32 +1000 (EST) Received: from localhost ([::1]:59370 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjI3C-00037D-0k for incoming@patchwork.ozlabs.org; Mon, 25 Jun 2012 18:46:30 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45991) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjI33-000375-9C for qemu-devel@nongnu.org; Mon, 25 Jun 2012 18:46:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SjI30-0001m2-6P for qemu-devel@nongnu.org; Mon, 25 Jun 2012 18:46:20 -0400 Received: from e4.ny.us.ibm.com ([32.97.182.144]:57634) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SjI30-0001ld-3G for qemu-devel@nongnu.org; Mon, 25 Jun 2012 18:46:18 -0400 Received: from /spool/local by e4.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 25 Jun 2012 18:46:14 -0400 Received: from d01dlp02.pok.ibm.com (9.56.224.85) by e4.ny.us.ibm.com (192.168.1.104) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 25 Jun 2012 18:46:12 -0400 Received: from d01relay01.pok.ibm.com (d01relay01.pok.ibm.com [9.56.227.233]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 704F16E804D for ; Mon, 25 Jun 2012 18:46:11 -0400 (EDT) Received: from d01av02.pok.ibm.com (d01av02.pok.ibm.com [9.56.224.216]) by d01relay01.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5PMkBHN163480 for ; Mon, 25 Jun 2012 18:46:11 -0400 Received: from d01av02.pok.ibm.com (loopback [127.0.0.1]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5PMkA4W015789 for ; Mon, 25 Jun 2012 19:46:11 -0300 Received: from titi.austin.rr.com (sig-9-65-39-74.mts.ibm.com [9.65.39.74]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q5PMk5JP015487; Mon, 25 Jun 2012 19:46:09 -0300 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 25 Jun 2012 17:46:00 -0500 Message-Id: <1340664362-25603-3-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1340664362-25603-1-git-send-email-aliguori@us.ibm.com> References: <1340664362-25603-1-git-send-email-aliguori@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12062522-3534-0000-0000-000009CB7910 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.144 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , Markus Armbruster , Amit Shah , Paolo Bonzini , Andreas Faerber Subject: [Qemu-devel] [PATCH 2/4] rng-urandom: add an RNG backend that uses /dev/urandom 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The filename can be overridden but it expects a non-blocking source of entropy. A typical invocation would be: qemu -object rng-urandom,id=rng0 -device virtio-rng-pci,rng=rng0 Signed-off-by: Anthony Liguori --- backends/Makefile.objs | 2 +- backends/rng-urandom.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletions(-) create mode 100644 backends/rng-urandom.c diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 06e08c7..423c9df 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -1 +1 @@ -common-obj-y += rng.o +common-obj-y += rng.o rng-urandom.o diff --git a/backends/rng-urandom.c b/backends/rng-urandom.c new file mode 100644 index 0000000..7fbe886 --- /dev/null +++ b/backends/rng-urandom.c @@ -0,0 +1,144 @@ +/* + * QEMU Random Number Generator Backend + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/rng.h" +#include "qerror.h" + +#define TYPE_RNG_URANDOM "rng-urandom" +#define RNG_URANDOM(obj) OBJECT_CHECK(RndURandom, (obj), TYPE_RNG_URANDOM) + +typedef struct RndURandom +{ + RngBackend parent; + + int fd; + char *filename; + GSList *pending_data; +} RndURandom; + +/** + * A simple and incomplete backend to request entropy from /dev/urandom. + * + * This backend exposes an additional "filename" property that can be used to + * set the filename to use to open the backend. + */ +static void rng_urandom_request_entropy(RngBackend *b, size_t size, + EntropyReceiveFunc *receive_entropy, + void *opaque) +{ + RndURandom *s = RNG_URANDOM(b); + uint8_t data[size]; + ssize_t ret; + + g_assert(s->fd != -1); + + do { + ret = read(s->fd, data, size); + } while (ret == -1 && errno == EINTR); + + g_assert(ret != -1); + + /* FIXME: needs to be done via a BH */ + receive_entropy(opaque, data, ret); +} + +static void rng_urandom_opened(RngBackend *b, Error **errp) +{ + RndURandom *s = RNG_URANDOM(b); + + if (s->filename == NULL) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, + "filename", "a valid filename"); + } else { + s->fd = open(s->filename, O_RDONLY); + + if (s->fd == -1) { + error_set(errp, QERR_OPEN_FILE_FAILED, s->filename); + } + } +} + +static char *rng_urandom_get_filename(Object *obj, Error **errp) +{ + RndURandom *s = RNG_URANDOM(obj); + + if (s->filename) { + return g_strdup(s->filename); + } + + return NULL; +} + +static void rng_urandom_set_filename(Object *obj, const char *filename, + Error **errp) +{ + RngBackend *b = RNG_BACKEND(obj); + RndURandom *s = RNG_URANDOM(obj); + + if (b->opened) { + error_set(errp, QERR_PERMISSION_DENIED); + return; + } + + if (s->filename) { + g_free(s->filename); + } + + s->filename = g_strdup(filename); +} + +static void rng_urandom_init(Object *obj) +{ + RndURandom *s = RNG_URANDOM(obj); + + object_property_add_str(obj, "filename", + rng_urandom_get_filename, + rng_urandom_set_filename, + NULL); + + s->filename = g_strdup("/dev/urandom"); +} + +static void rng_urandom_finalize(Object *obj) +{ + RndURandom *s = RNG_URANDOM(obj); + + if (s->fd != -1) { + close(s->fd); + } + + g_free(s->filename); +} + +static void rng_urandom_class_init(ObjectClass *klass, void *data) +{ + RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); + + rbc->request_entropy = rng_urandom_request_entropy; + rbc->opened = rng_urandom_opened; +} + +static TypeInfo rng_urandom_info = { + .name = TYPE_RNG_URANDOM, + .parent = TYPE_RNG_BACKEND, + .instance_size = sizeof(RndURandom), + .class_init = rng_urandom_class_init, + .instance_init = rng_urandom_init, + .instance_finalize = rng_urandom_finalize, +}; + +static void register_types(void) +{ + type_register_static(&rng_urandom_info); +} + +type_init(register_types);