Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 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 | |
Peter Maydell | 9c05833 | 2016-01-29 17:49:54 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 14 | #include "sysemu/rng-random.h" |
| 15 | #include "sysemu/rng.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 16 | #include "qapi/error.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 17 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 19 | #include "qemu/module.h" |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 20 | |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 21 | struct RngRandom |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 22 | { |
| 23 | RngBackend parent; |
| 24 | |
| 25 | int fd; |
| 26 | char *filename; |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 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 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 38 | RngRandom *s = RNG_RANDOM(opaque); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 39 | |
Ladi Prosek | 443590c | 2016-03-03 14:16:11 +0100 | [diff] [blame] | 40 | while (!QSIMPLEQ_EMPTY(&s->parent.requests)) { |
| 41 | RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests); |
Ladi Prosek | 60253ed | 2016-03-03 09:37:18 +0100 | [diff] [blame] | 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); |
Amit Shah | acbbc03 | 2013-04-16 15:58:16 +0530 | [diff] [blame] | 53 | } |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 54 | |
Ladi Prosek | 60253ed | 2016-03-03 09:37:18 +0100 | [diff] [blame] | 55 | /* We've drained all requests, the fd handler can be reset. */ |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 56 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 57 | } |
| 58 | |
Ladi Prosek | 60253ed | 2016-03-03 09:37:18 +0100 | [diff] [blame] | 59 | static void rng_random_request_entropy(RngBackend *b, RngRequest *req) |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 60 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 61 | RngRandom *s = RNG_RANDOM(b); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 62 | |
Ladi Prosek | 443590c | 2016-03-03 14:16:11 +0100 | [diff] [blame] | 63 | if (QSIMPLEQ_EMPTY(&s->parent.requests)) { |
Ladi Prosek | 60253ed | 2016-03-03 09:37:18 +0100 | [diff] [blame] | 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); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 67 | } |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void rng_random_opened(RngBackend *b, Error **errp) |
| 71 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 72 | RngRandom *s = RNG_RANDOM(b); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 73 | |
| 74 | if (s->filename == NULL) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 75 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 76 | "filename", "a valid filename"); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 77 | } else { |
Daniel P. Berrangé | 448058a | 2020-07-21 13:25:21 +0100 | [diff] [blame] | 78 | s->fd = qemu_open_old(s->filename, O_RDONLY | O_NONBLOCK); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 79 | if (s->fd == -1) { |
Luiz Capitulino | bc5741a | 2013-06-07 14:28:02 -0400 | [diff] [blame] | 80 | error_setg_file_open(errp, errno, s->filename); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static char *rng_random_get_filename(Object *obj, Error **errp) |
| 86 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 87 | RngRandom *s = RNG_RANDOM(obj); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 88 | |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 89 | return g_strdup(s->filename); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void rng_random_set_filename(Object *obj, const char *filename, |
| 93 | Error **errp) |
| 94 | { |
| 95 | RngBackend *b = RNG_BACKEND(obj); |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 96 | RngRandom *s = RNG_RANDOM(obj); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 97 | |
| 98 | if (b->opened) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 99 | error_setg(errp, QERR_PERMISSION_DENIED); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
Eduardo Habkost | feced89 | 2014-05-30 17:02:18 -0300 | [diff] [blame] | 103 | g_free(s->filename); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 104 | s->filename = g_strdup(filename); |
| 105 | } |
| 106 | |
| 107 | static void rng_random_init(Object *obj) |
| 108 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 109 | RngRandom *s = RNG_RANDOM(obj); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 110 | |
Kashyap Chamarthy | a2230bd | 2019-05-29 16:31:03 +0200 | [diff] [blame] | 111 | s->filename = g_strdup("/dev/urandom"); |
Paolo Bonzini | 513b8c7 | 2013-12-20 23:21:06 +0100 | [diff] [blame] | 112 | s->fd = -1; |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void rng_random_finalize(Object *obj) |
| 116 | { |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 117 | RngRandom *s = RNG_RANDOM(obj); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 118 | |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 119 | if (s->fd != -1) { |
Paolo Bonzini | 513b8c7 | 2013-12-20 23:21:06 +0100 | [diff] [blame] | 120 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
Stefan Berger | 7f9c9d1 | 2013-03-01 07:53:55 -0500 | [diff] [blame] | 121 | qemu_close(s->fd); |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | g_free(s->filename); |
| 125 | } |
| 126 | |
| 127 | static 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 Habkost | a3d3ee6 | 2020-09-21 18:10:25 -0400 | [diff] [blame] | 133 | object_class_property_add_str(klass, "filename", |
| 134 | rng_random_get_filename, |
| 135 | rng_random_set_filename); |
| 136 | |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 139 | static const TypeInfo rng_random_info = { |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 140 | .name = TYPE_RNG_RANDOM, |
| 141 | .parent = TYPE_RNG_BACKEND, |
Wei Jiangang | cde6361 | 2016-04-15 09:36:08 +0800 | [diff] [blame] | 142 | .instance_size = sizeof(RngRandom), |
Anthony Liguori | 5c74521 | 2012-06-25 10:34:09 -0500 | [diff] [blame] | 143 | .class_init = rng_random_class_init, |
| 144 | .instance_init = rng_random_init, |
| 145 | .instance_finalize = rng_random_finalize, |
| 146 | }; |
| 147 | |
| 148 | static void register_types(void) |
| 149 | { |
| 150 | type_register_static(&rng_random_info); |
| 151 | } |
| 152 | |
| 153 | type_init(register_types); |