-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlrng_interface_dev_common.c
315 lines (268 loc) · 7.51 KB
/
lrng_interface_dev_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
/*
* LRNG User and kernel space interfaces
*
* Copyright (C) 2022, Stephan Mueller <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/random.h>
#include <linux/slab.h>
#include "lrng_drng_mgr.h"
#include "lrng_es_aux.h"
#include "lrng_es_mgr.h"
#include "lrng_interface_dev_common.h"
DECLARE_WAIT_QUEUE_HEAD(lrng_write_wait);
static struct fasync_struct *fasync;
static bool lrng_seed_hw = true; /* Allow HW to provide seed */
static bool lrng_seed_user = true; /* Allow user space to provide seed */
/********************************** Helper ***********************************/
static u32 lrng_get_aux_ent(void)
{
return lrng_es[lrng_ext_es_aux]->curr_entropy(0);
}
/* Is the DRNG seed level too low? */
bool lrng_need_entropy(void)
{
return (lrng_get_aux_ent() < lrng_write_wakeup_bits);
}
void lrng_writer_wakeup(void)
{
if (lrng_need_entropy() && wq_has_sleeper(&lrng_write_wait)) {
wake_up_interruptible(&lrng_write_wait);
kill_fasync(&fasync, SIGIO, POLL_OUT);
}
}
void lrng_init_wakeup_dev(void)
{
kill_fasync(&fasync, SIGIO, POLL_IN);
}
/* External entropy provider is allowed to provide seed data */
bool lrng_state_exseed_allow(enum lrng_external_noise_source source)
{
if (source == lrng_noise_source_hw)
return lrng_seed_hw;
return lrng_seed_user;
}
/* Enable / disable external entropy provider to furnish seed */
void lrng_state_exseed_set(enum lrng_external_noise_source source, bool type)
{
/*
* If the LRNG is not yet operational, allow all entropy sources
* to deliver data unconditionally to get fully seeded asap.
*/
if (!lrng_state_operational())
return;
if (source == lrng_noise_source_hw)
lrng_seed_hw = type;
else
lrng_seed_user = type;
}
void lrng_state_exseed_allow_all(void)
{
lrng_state_exseed_set(lrng_noise_source_hw, true);
lrng_state_exseed_set(lrng_noise_source_user, true);
}
/************************ LRNG user output interfaces *************************/
ssize_t lrng_read_seed(char __user *buf, size_t nbytes, unsigned int flags)
{
ssize_t ret = 0;
u64 t[(sizeof(struct entropy_buf) + 3 * sizeof(u64) - 1) / sizeof(u64)];
memset(t, 0, sizeof(t));
ret = lrng_get_seed(t, min_t(size_t, nbytes, sizeof(t)), flags);
if (ret == -EMSGSIZE && copy_to_user(buf, t, sizeof(u64)))
ret = -EFAULT;
else if (ret > 0 && copy_to_user(buf, t, ret))
ret = -EFAULT;
memzero_explicit(t, sizeof(t));
return ret;
}
ssize_t lrng_read_common(char __user *buf, size_t nbytes, bool pr)
{
ssize_t ret = 0;
u8 tmpbuf[LRNG_DRNG_BLOCKSIZE] __aligned(LRNG_KCAPI_ALIGN);
u8 *tmp_large = NULL, *tmp = tmpbuf;
u32 tmplen = sizeof(tmpbuf);
if (nbytes == 0)
return 0;
/*
* Satisfy large read requests -- as the common case are smaller
* request sizes, such as 16 or 32 bytes, avoid a kmalloc overhead for
* those by using the stack variable of tmpbuf.
*/
if (!IS_ENABLED(CONFIG_BASE_SMALL) && (nbytes > sizeof(tmpbuf))) {
tmplen = min_t(u32, nbytes, LRNG_DRNG_MAX_REQSIZE);
tmp_large = kmalloc(tmplen + LRNG_KCAPI_ALIGN, GFP_KERNEL);
if (!tmp_large)
tmplen = sizeof(tmpbuf);
else
tmp = PTR_ALIGN(tmp_large, LRNG_KCAPI_ALIGN);
}
while (nbytes) {
u32 todo = min_t(u32, nbytes, tmplen);
int rc = 0;
/* Reschedule if we received a large request. */
if ((tmp_large) && need_resched()) {
if (signal_pending(current)) {
if (ret == 0)
ret = -ERESTARTSYS;
break;
}
schedule();
}
rc = lrng_drng_get_sleep(tmp, todo, pr);
if (rc <= 0) {
if (rc < 0)
ret = rc;
break;
}
if (copy_to_user(buf, tmp, rc)) {
ret = -EFAULT;
break;
}
nbytes -= rc;
buf += rc;
ret += rc;
}
/* Wipe data just returned from memory */
if (tmp_large)
kfree_sensitive(tmp_large);
else
memzero_explicit(tmpbuf, sizeof(tmpbuf));
return ret;
}
ssize_t lrng_read_common_block(int nonblock, int pr,
char __user *buf, size_t nbytes)
{
int ret;
if (nbytes == 0)
return 0;
ret = lrng_drng_sleep_while_nonoperational(nonblock);
if (ret)
return ret;
return lrng_read_common(buf, nbytes, !!pr);
}
ssize_t lrng_drng_read_block(struct file *file, char __user *buf, size_t nbytes,
loff_t *ppos)
{
return lrng_read_common_block(file->f_flags & O_NONBLOCK,
file->f_flags & O_SYNC, buf, nbytes);
}
__poll_t lrng_random_poll(struct file *file, poll_table *wait)
{
__poll_t mask;
poll_wait(file, &lrng_init_wait, wait);
poll_wait(file, &lrng_write_wait, wait);
mask = 0;
if (lrng_state_operational())
mask |= EPOLLIN | EPOLLRDNORM;
if (lrng_need_entropy() ||
lrng_state_exseed_allow(lrng_noise_source_user)) {
lrng_state_exseed_set(lrng_noise_source_user, false);
mask |= EPOLLOUT | EPOLLWRNORM;
}
return mask;
}
ssize_t lrng_drng_write_common(const char __user *buffer, size_t count,
u32 entropy_bits)
{
ssize_t ret = 0;
u8 buf[64] __aligned(LRNG_KCAPI_ALIGN);
const char __user *p = buffer;
u32 orig_entropy_bits = entropy_bits;
if (!lrng_get_available()) {
ret = lrng_drng_initalize();
if (!ret)
return ret;
}
count = min_t(size_t, count, INT_MAX);
while (count > 0) {
size_t bytes = min_t(size_t, count, sizeof(buf));
u32 ent = min_t(u32, bytes<<3, entropy_bits);
if (copy_from_user(&buf, p, bytes))
return -EFAULT;
/* Inject data into entropy pool */
lrng_pool_insert_aux(buf, bytes, ent);
count -= bytes;
p += bytes;
ret += bytes;
entropy_bits -= ent;
cond_resched();
}
/* Force reseed of DRNG during next data request. */
if (!orig_entropy_bits)
lrng_drng_force_reseed();
return ret;
}
ssize_t lrng_drng_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
return lrng_drng_write_common(buffer, count, 0);
}
long lrng_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
u32 digestsize_bits;
int size, ent_count_bits, ret;
int __user *p = (int __user *)arg;
switch (cmd) {
case RNDGETENTCNT:
ent_count_bits = lrng_avail_entropy_aux();
if (put_user(ent_count_bits, p))
return -EFAULT;
return 0;
case RNDADDTOENTCNT:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(ent_count_bits, p))
return -EFAULT;
ent_count_bits = (int)lrng_get_aux_ent() + ent_count_bits;
if (ent_count_bits < 0)
ent_count_bits = 0;
digestsize_bits = lrng_get_digestsize();
if (ent_count_bits > digestsize_bits)
ent_count_bits = digestsize_bits;
lrng_pool_set_entropy(ent_count_bits);
return 0;
case RNDADDENTROPY:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(ent_count_bits, p++))
return -EFAULT;
if (ent_count_bits < 0)
return -EINVAL;
if (get_user(size, p++))
return -EFAULT;
if (size < 0)
return -EINVAL;
/* there cannot be more entropy than data */
ent_count_bits = min(ent_count_bits, size<<3);
ret = lrng_drng_write_common((const char __user *)p, size,
ent_count_bits);
return (ret < 0) ? ret : 0;
case RNDZAPENTCNT:
case RNDCLEARPOOL:
/* Clear the entropy pool counter. */
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
lrng_pool_set_entropy(0);
return 0;
case RNDRESEEDCRNG:
/*
* We leave the capability check here since it is present
* in the upstream's RNG implementation. Yet, user space
* can trigger a reseed as easy as writing into /dev/random
* or /dev/urandom where no privilege is needed.
*/
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
/* Force a reseed of all DRNGs */
lrng_drng_force_reseed();
return 0;
default:
return -EINVAL;
}
}
EXPORT_SYMBOL(lrng_ioctl);
int lrng_fasync(int fd, struct file *filp, int on)
{
return fasync_helper(fd, filp, on, &fasync);
}