blob: 7add272edddf3ec446ce50b3276deb065aeee9fd [file] [log] [blame]
Anthony Liguori5c745212012-06-25 10:34:09 -05001/*
2 * QEMU Random Number Generator Backend
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
Peter Maydell9c058332016-01-29 17:49:54 +000013#include "qemu/osdep.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020014#include "sysemu/rng-random.h"
15#include "sysemu/rng.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020019#include "qemu/module.h"
Anthony Liguori5c745212012-06-25 10:34:09 -050020
Wei Jiangangcde63612016-04-15 09:36:08 +080021struct RngRandom
Anthony Liguori5c745212012-06-25 10:34:09 -050022{
23 RngBackend parent;
24
25 int fd;
26 char *filename;
Anthony Liguori5c745212012-06-25 10:34:09 -050027};
28
29/**
30 * A simple and incomplete backend to request entropy from /dev/random.
31 *
32 * This backend exposes an additional "filename" property that can be used to
33 * set the filename to use to open the backend.
34 */
35
36static void entropy_available(void *opaque)
37{
Wei Jiangangcde63612016-04-15 09:36:08 +080038 RngRandom *s = RNG_RANDOM(opaque);
Anthony Liguori5c745212012-06-25 10:34:09 -050039
Ladi Prosek443590c2016-03-03 14:16:11 +010040 while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
41 RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
Ladi Prosek60253ed2016-03-03 09:37:18 +010042 ssize_t len;
43
44 len = read(s->fd, req->data, req->size);
45 if (len < 0 && errno == EAGAIN) {
46 return;
47 }
48 g_assert(len != -1);
49
50 req->receive_entropy(req->opaque, req->data, len);
51
52 rng_backend_finalize_request(&s->parent, req);
Amit Shahacbbc032013-04-16 15:58:16 +053053 }
Anthony Liguori5c745212012-06-25 10:34:09 -050054
Ladi Prosek60253ed2016-03-03 09:37:18 +010055 /* We've drained all requests, the fd handler can be reset. */
Anthony Liguori5c745212012-06-25 10:34:09 -050056 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
57}
58
Ladi Prosek60253ed2016-03-03 09:37:18 +010059static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
Anthony Liguori5c745212012-06-25 10:34:09 -050060{
Wei Jiangangcde63612016-04-15 09:36:08 +080061 RngRandom *s = RNG_RANDOM(b);
Anthony Liguori5c745212012-06-25 10:34:09 -050062
Ladi Prosek443590c2016-03-03 14:16:11 +010063 if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
Ladi Prosek60253ed2016-03-03 09:37:18 +010064 /* If there are no pending requests yet, we need to
65 * install our fd handler. */
66 qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
Anthony Liguori5c745212012-06-25 10:34:09 -050067 }
Anthony Liguori5c745212012-06-25 10:34:09 -050068}
69
70static void rng_random_opened(RngBackend *b, Error **errp)
71{
Wei Jiangangcde63612016-04-15 09:36:08 +080072 RngRandom *s = RNG_RANDOM(b);
Anthony Liguori5c745212012-06-25 10:34:09 -050073
74 if (s->filename == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +010075 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
76 "filename", "a valid filename");
Anthony Liguori5c745212012-06-25 10:34:09 -050077 } else {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +010078 s->fd = qemu_open_old(s->filename, O_RDONLY | O_NONBLOCK);
Anthony Liguori5c745212012-06-25 10:34:09 -050079 if (s->fd == -1) {
Luiz Capitulinobc5741a2013-06-07 14:28:02 -040080 error_setg_file_open(errp, errno, s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050081 }
82 }
83}
84
85static char *rng_random_get_filename(Object *obj, Error **errp)
86{
Wei Jiangangcde63612016-04-15 09:36:08 +080087 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050088
Markus Armbruster24588102014-12-04 10:26:55 +010089 return g_strdup(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050090}
91
92static void rng_random_set_filename(Object *obj, const char *filename,
93 Error **errp)
94{
95 RngBackend *b = RNG_BACKEND(obj);
Wei Jiangangcde63612016-04-15 09:36:08 +080096 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050097
98 if (b->opened) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +010099 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori5c745212012-06-25 10:34:09 -0500100 return;
101 }
102
Eduardo Habkostfeced892014-05-30 17:02:18 -0300103 g_free(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -0500104 s->filename = g_strdup(filename);
105}
106
107static void rng_random_init(Object *obj)
108{
Wei Jiangangcde63612016-04-15 09:36:08 +0800109 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500110
Kashyap Chamarthya2230bd2019-05-29 16:31:03 +0200111 s->filename = g_strdup("/dev/urandom");
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100112 s->fd = -1;
Anthony Liguori5c745212012-06-25 10:34:09 -0500113}
114
115static void rng_random_finalize(Object *obj)
116{
Wei Jiangangcde63612016-04-15 09:36:08 +0800117 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500118
Anthony Liguori5c745212012-06-25 10:34:09 -0500119 if (s->fd != -1) {
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100120 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
Stefan Berger7f9c9d12013-03-01 07:53:55 -0500121 qemu_close(s->fd);
Anthony Liguori5c745212012-06-25 10:34:09 -0500122 }
123
124 g_free(s->filename);
125}
126
127static void rng_random_class_init(ObjectClass *klass, void *data)
128{
129 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
130
131 rbc->request_entropy = rng_random_request_entropy;
132 rbc->opened = rng_random_opened;
Eduardo Habkosta3d3ee62020-09-21 18:10:25 -0400133 object_class_property_add_str(klass, "filename",
134 rng_random_get_filename,
135 rng_random_set_filename);
136
Anthony Liguori5c745212012-06-25 10:34:09 -0500137}
138
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100139static const TypeInfo rng_random_info = {
Anthony Liguori5c745212012-06-25 10:34:09 -0500140 .name = TYPE_RNG_RANDOM,
141 .parent = TYPE_RNG_BACKEND,
Wei Jiangangcde63612016-04-15 09:36:08 +0800142 .instance_size = sizeof(RngRandom),
Anthony Liguori5c745212012-06-25 10:34:09 -0500143 .class_init = rng_random_class_init,
144 .instance_init = rng_random_init,
145 .instance_finalize = rng_random_finalize,
146};
147
148static void register_types(void)
149{
150 type_register_static(&rng_random_info);
151}
152
153type_init(register_types);