blob: 3ce6cc9b4af677267482ebb00503a031aa250bb4 [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"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010014#include "system/rng-random.h"
15#include "system/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 {
Zhao Liu18f3a072024-07-15 16:21:55 +080078 s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK, errp);
Anthony Liguori5c745212012-06-25 10:34:09 -050079 }
80}
81
82static char *rng_random_get_filename(Object *obj, Error **errp)
83{
Wei Jiangangcde63612016-04-15 09:36:08 +080084 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050085
Markus Armbruster24588102014-12-04 10:26:55 +010086 return g_strdup(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050087}
88
89static void rng_random_set_filename(Object *obj, const char *filename,
90 Error **errp)
91{
92 RngBackend *b = RNG_BACKEND(obj);
Wei Jiangangcde63612016-04-15 09:36:08 +080093 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050094
95 if (b->opened) {
Markus Armbrusterff924442022-10-12 17:37:59 +020096 error_setg(errp, "Property 'filename' can no longer be set");
Anthony Liguori5c745212012-06-25 10:34:09 -050097 return;
98 }
99
Eduardo Habkostfeced892014-05-30 17:02:18 -0300100 g_free(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -0500101 s->filename = g_strdup(filename);
102}
103
104static void rng_random_init(Object *obj)
105{
Wei Jiangangcde63612016-04-15 09:36:08 +0800106 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500107
Kashyap Chamarthya2230bd2019-05-29 16:31:03 +0200108 s->filename = g_strdup("/dev/urandom");
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100109 s->fd = -1;
Anthony Liguori5c745212012-06-25 10:34:09 -0500110}
111
112static void rng_random_finalize(Object *obj)
113{
Wei Jiangangcde63612016-04-15 09:36:08 +0800114 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500115
Anthony Liguori5c745212012-06-25 10:34:09 -0500116 if (s->fd != -1) {
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100117 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
Stefan Berger7f9c9d12013-03-01 07:53:55 -0500118 qemu_close(s->fd);
Anthony Liguori5c745212012-06-25 10:34:09 -0500119 }
120
121 g_free(s->filename);
122}
123
124static void rng_random_class_init(ObjectClass *klass, void *data)
125{
126 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
127
128 rbc->request_entropy = rng_random_request_entropy;
129 rbc->opened = rng_random_opened;
Eduardo Habkosta3d3ee62020-09-21 18:10:25 -0400130 object_class_property_add_str(klass, "filename",
131 rng_random_get_filename,
132 rng_random_set_filename);
133
Anthony Liguori5c745212012-06-25 10:34:09 -0500134}
135
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100136static const TypeInfo rng_random_info = {
Anthony Liguori5c745212012-06-25 10:34:09 -0500137 .name = TYPE_RNG_RANDOM,
138 .parent = TYPE_RNG_BACKEND,
Wei Jiangangcde63612016-04-15 09:36:08 +0800139 .instance_size = sizeof(RngRandom),
Anthony Liguori5c745212012-06-25 10:34:09 -0500140 .class_init = rng_random_class_init,
141 .instance_init = rng_random_init,
142 .instance_finalize = rng_random_finalize,
143};
144
145static void register_types(void)
146{
147 type_register_static(&rng_random_info);
148}
149
150type_init(register_types);