xref: /openbmc/qemu/backends/rng-random.c (revision 682a0528)
1 /*
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 
13 #include "qemu/osdep.h"
14 #include "sysemu/rng-random.h"
15 #include "sysemu/rng.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/module.h"
20 
21 struct RngRandom
22 {
23     RngBackend parent;
24 
25     int fd;
26     char *filename;
27 };
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 
36 static void entropy_available(void *opaque)
37 {
38     RngRandom *s = RNG_RANDOM(opaque);
39 
40     while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
41         RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
42         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);
53     }
54 
55     /* We've drained all requests, the fd handler can be reset. */
56     qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
57 }
58 
59 static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
60 {
61     RngRandom *s = RNG_RANDOM(b);
62 
63     if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
64         /* 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);
67     }
68 }
69 
70 static void rng_random_opened(RngBackend *b, Error **errp)
71 {
72     RngRandom *s = RNG_RANDOM(b);
73 
74     if (s->filename == NULL) {
75         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
76                    "filename", "a valid filename");
77     } else {
78         s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK, errp);
79     }
80 }
81 
82 static char *rng_random_get_filename(Object *obj, Error **errp)
83 {
84     RngRandom *s = RNG_RANDOM(obj);
85 
86     return g_strdup(s->filename);
87 }
88 
89 static void rng_random_set_filename(Object *obj, const char *filename,
90                                  Error **errp)
91 {
92     RngBackend *b = RNG_BACKEND(obj);
93     RngRandom *s = RNG_RANDOM(obj);
94 
95     if (b->opened) {
96         error_setg(errp, "Property 'filename' can no longer be set");
97         return;
98     }
99 
100     g_free(s->filename);
101     s->filename = g_strdup(filename);
102 }
103 
104 static void rng_random_init(Object *obj)
105 {
106     RngRandom *s = RNG_RANDOM(obj);
107 
108     s->filename = g_strdup("/dev/urandom");
109     s->fd = -1;
110 }
111 
112 static void rng_random_finalize(Object *obj)
113 {
114     RngRandom *s = RNG_RANDOM(obj);
115 
116     if (s->fd != -1) {
117         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
118         qemu_close(s->fd);
119     }
120 
121     g_free(s->filename);
122 }
123 
124 static 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;
130     object_class_property_add_str(klass, "filename",
131                                   rng_random_get_filename,
132                                   rng_random_set_filename);
133 
134 }
135 
136 static const TypeInfo rng_random_info = {
137     .name = TYPE_RNG_RANDOM,
138     .parent = TYPE_RNG_BACKEND,
139     .instance_size = sizeof(RngRandom),
140     .class_init = rng_random_class_init,
141     .instance_init = rng_random_init,
142     .instance_finalize = rng_random_finalize,
143 };
144 
145 static void register_types(void)
146 {
147     type_register_static(&rng_random_info);
148 }
149 
150 type_init(register_types);
151