Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * hw_random/core.c: HWRNG core API
3 *
4 * Copyright 2006 Michael Buesch <m@bues.ch>
5 * Copyright 2005 (c) MontaVista Software, Inc.
6 *
7 * Please read Documentation/admin-guide/hw_random.rst for details on use.
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 */
12
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/fs.h>
17#include <linux/hw_random.h>
18#include <linux/random.h>
19#include <linux/kernel.h>
20#include <linux/kthread.h>
21#include <linux/sched/signal.h>
22#include <linux/miscdevice.h>
23#include <linux/module.h>
24#include <linux/random.h>
25#include <linux/sched.h>
26#include <linux/slab.h>
27#include <linux/uaccess.h>
28
29#define RNG_MODULE_NAME "hw_random"
30
31static struct hwrng *current_rng;
32/* the current rng has been explicitly chosen by user via sysfs */
33static int cur_rng_set_by_user;
34static struct task_struct *hwrng_fill;
35/* list of registered rngs */
36static LIST_HEAD(rng_list);
37/* Protects rng_list and current_rng */
38static DEFINE_MUTEX(rng_mutex);
39/* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
40static DEFINE_MUTEX(reading_mutex);
41static int data_avail;
42static u8 *rng_buffer, *rng_fillbuf;
43static unsigned short current_quality;
44static unsigned short default_quality; /* = 0; default to "off" */
45
46module_param(current_quality, ushort, 0644);
47MODULE_PARM_DESC(current_quality,
48 "current hwrng entropy estimation per 1024 bits of input -- obsolete, use rng_quality instead");
49module_param(default_quality, ushort, 0644);
50MODULE_PARM_DESC(default_quality,
51 "default entropy content of hwrng per 1024 bits of input");
52
53static void drop_current_rng(void);
54static int hwrng_init(struct hwrng *rng);
55static int hwrng_fillfn(void *unused);
56
57static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
58 int wait);
59
60static size_t rng_buffer_size(void)
61{
62 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
63}
64
65static void add_early_randomness(struct hwrng *rng)
66{
67 int bytes_read;
68
69 mutex_lock(&reading_mutex);
70 bytes_read = rng_get_data(rng, rng_fillbuf, 32, 0);
71 mutex_unlock(&reading_mutex);
72 if (bytes_read > 0)
73 add_device_randomness(rng_fillbuf, bytes_read);
74}
75
76static inline void cleanup_rng(struct kref *kref)
77{
78 struct hwrng *rng = container_of(kref, struct hwrng, ref);
79
80 if (rng->cleanup)
81 rng->cleanup(rng);
82
83 complete(&rng->cleanup_done);
84}
85
86static int set_current_rng(struct hwrng *rng)
87{
88 int err;
89
90 BUG_ON(!mutex_is_locked(&rng_mutex));
91
92 err = hwrng_init(rng);
93 if (err)
94 return err;
95
96 drop_current_rng();
97 current_rng = rng;
98
99 /* if necessary, start hwrng thread */
100 if (!hwrng_fill) {
101 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
102 if (IS_ERR(hwrng_fill)) {
103 pr_err("hwrng_fill thread creation failed\n");
104 hwrng_fill = NULL;
105 }
106 }
107
108 return 0;
109}
110
111static void drop_current_rng(void)
112{
113 BUG_ON(!mutex_is_locked(&rng_mutex));
114 if (!current_rng)
115 return;
116
117 /* decrease last reference for triggering the cleanup */
118 kref_put(¤t_rng->ref, cleanup_rng);
119 current_rng = NULL;
120}
121
122/* Returns ERR_PTR(), NULL or refcounted hwrng */
123static struct hwrng *get_current_rng_nolock(void)
124{
125 if (current_rng)
126 kref_get(¤t_rng->ref);
127
128 return current_rng;
129}
130
131static struct hwrng *get_current_rng(void)
132{
133 struct hwrng *rng;
134
135 if (mutex_lock_interruptible(&rng_mutex))
136 return ERR_PTR(-ERESTARTSYS);
137
138 rng = get_current_rng_nolock();
139
140 mutex_unlock(&rng_mutex);
141 return rng;
142}
143
144static void put_rng(struct hwrng *rng)
145{
146 /*
147 * Hold rng_mutex here so we serialize in case they set_current_rng
148 * on rng again immediately.
149 */
150 mutex_lock(&rng_mutex);
151 if (rng)
152 kref_put(&rng->ref, cleanup_rng);
153 mutex_unlock(&rng_mutex);
154}
155
156static int hwrng_init(struct hwrng *rng)
157{
158 if (kref_get_unless_zero(&rng->ref))
159 goto skip_init;
160
161 if (rng->init) {
162 int ret;
163
164 ret = rng->init(rng);
165 if (ret)
166 return ret;
167 }
168
169 kref_init(&rng->ref);
170 reinit_completion(&rng->cleanup_done);
171
172skip_init:
173 if (!rng->quality)
174 rng->quality = default_quality;
175 if (rng->quality > 1024)
176 rng->quality = 1024;
177 current_quality = rng->quality; /* obsolete */
178
179 return 0;
180}
181
182static int rng_dev_open(struct inode *inode, struct file *filp)
183{
184 /* enforce read-only access to this chrdev */
185 if ((filp->f_mode & FMODE_READ) == 0)
186 return -EINVAL;
187 if (filp->f_mode & FMODE_WRITE)
188 return -EINVAL;
189 return 0;
190}
191
192static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
193 int wait) {
194 int present;
195
196 BUG_ON(!mutex_is_locked(&reading_mutex));
197 if (rng->read)
198 return rng->read(rng, (void *)buffer, size, wait);
199
200 if (rng->data_present)
201 present = rng->data_present(rng, wait);
202 else
203 present = 1;
204
205 if (present)
206 return rng->data_read(rng, (u32 *)buffer);
207
208 return 0;
209}
210
211static ssize_t rng_dev_read(struct file *filp, char __user *buf,
212 size_t size, loff_t *offp)
213{
214 ssize_t ret = 0;
215 int err = 0;
216 int bytes_read, len;
217 struct hwrng *rng;
218
219 while (size) {
220 rng = get_current_rng();
221 if (IS_ERR(rng)) {
222 err = PTR_ERR(rng);
223 goto out;
224 }
225 if (!rng) {
226 err = -ENODEV;
227 goto out;
228 }
229
230 if (mutex_lock_interruptible(&reading_mutex)) {
231 err = -ERESTARTSYS;
232 goto out_put;
233 }
234 if (!data_avail) {
235 bytes_read = rng_get_data(rng, rng_buffer,
236 rng_buffer_size(),
237 !(filp->f_flags & O_NONBLOCK));
238 if (bytes_read < 0) {
239 err = bytes_read;
240 goto out_unlock_reading;
241 }
242 data_avail = bytes_read;
243 }
244
245 if (!data_avail) {
246 if (filp->f_flags & O_NONBLOCK) {
247 err = -EAGAIN;
248 goto out_unlock_reading;
249 }
250 } else {
251 len = data_avail;
252 if (len > size)
253 len = size;
254
255 data_avail -= len;
256
257 if (copy_to_user(buf + ret, rng_buffer + data_avail,
258 len)) {
259 err = -EFAULT;
260 goto out_unlock_reading;
261 }
262
263 size -= len;
264 ret += len;
265 }
266
267 mutex_unlock(&reading_mutex);
268 put_rng(rng);
269
270 if (need_resched())
271 schedule_timeout_interruptible(1);
272
273 if (signal_pending(current)) {
274 err = -ERESTARTSYS;
275 goto out;
276 }
277 }
278out:
279 return ret ? : err;
280
281out_unlock_reading:
282 mutex_unlock(&reading_mutex);
283out_put:
284 put_rng(rng);
285 goto out;
286}
287
288static const struct file_operations rng_chrdev_ops = {
289 .owner = THIS_MODULE,
290 .open = rng_dev_open,
291 .read = rng_dev_read,
292 .llseek = noop_llseek,
293};
294
295static const struct attribute_group *rng_dev_groups[];
296
297static struct miscdevice rng_miscdev = {
298 .minor = HWRNG_MINOR,
299 .name = RNG_MODULE_NAME,
300 .nodename = "hwrng",
301 .fops = &rng_chrdev_ops,
302 .groups = rng_dev_groups,
303};
304
305static int enable_best_rng(void)
306{
307 struct hwrng *rng, *new_rng = NULL;
308 int ret = -ENODEV;
309
310 BUG_ON(!mutex_is_locked(&rng_mutex));
311
312 /* no rng to use? */
313 if (list_empty(&rng_list)) {
314 drop_current_rng();
315 cur_rng_set_by_user = 0;
316 return 0;
317 }
318
319 /* use the rng which offers the best quality */
320 list_for_each_entry(rng, &rng_list, list) {
321 if (!new_rng || rng->quality > new_rng->quality)
322 new_rng = rng;
323 }
324
325 ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
326 if (!ret)
327 cur_rng_set_by_user = 0;
328
329 return ret;
330}
331
332static ssize_t rng_current_store(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t len)
335{
336 int err;
337 struct hwrng *rng, *old_rng, *new_rng;
338
339 err = mutex_lock_interruptible(&rng_mutex);
340 if (err)
341 return -ERESTARTSYS;
342
343 old_rng = current_rng;
344 if (sysfs_streq(buf, "")) {
345 err = enable_best_rng();
346 } else {
347 list_for_each_entry(rng, &rng_list, list) {
348 if (sysfs_streq(rng->name, buf)) {
349 err = set_current_rng(rng);
350 if (!err)
351 cur_rng_set_by_user = 1;
352 break;
353 }
354 }
355 }
356 new_rng = get_current_rng_nolock();
357 mutex_unlock(&rng_mutex);
358
359 if (new_rng) {
360 if (new_rng != old_rng)
361 add_early_randomness(new_rng);
362 put_rng(new_rng);
363 }
364
365 return err ? : len;
366}
367
368static ssize_t rng_current_show(struct device *dev,
369 struct device_attribute *attr,
370 char *buf)
371{
372 ssize_t ret;
373 struct hwrng *rng;
374
375 rng = get_current_rng();
376 if (IS_ERR(rng))
377 return PTR_ERR(rng);
378
379 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
380 put_rng(rng);
381
382 return ret;
383}
384
385static ssize_t rng_available_show(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
388{
389 int err;
390 struct hwrng *rng;
391
392 err = mutex_lock_interruptible(&rng_mutex);
393 if (err)
394 return -ERESTARTSYS;
395 buf[0] = '\0';
396 list_for_each_entry(rng, &rng_list, list) {
397 strlcat(buf, rng->name, PAGE_SIZE);
398 strlcat(buf, " ", PAGE_SIZE);
399 }
400 strlcat(buf, "\n", PAGE_SIZE);
401 mutex_unlock(&rng_mutex);
402
403 return strlen(buf);
404}
405
406static ssize_t rng_selected_show(struct device *dev,
407 struct device_attribute *attr,
408 char *buf)
409{
410 return sysfs_emit(buf, "%d\n", cur_rng_set_by_user);
411}
412
413static ssize_t rng_quality_show(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
416{
417 ssize_t ret;
418 struct hwrng *rng;
419
420 rng = get_current_rng();
421 if (IS_ERR(rng))
422 return PTR_ERR(rng);
423
424 if (!rng) /* no need to put_rng */
425 return -ENODEV;
426
427 ret = sysfs_emit(buf, "%hu\n", rng->quality);
428 put_rng(rng);
429
430 return ret;
431}
432
433static ssize_t rng_quality_store(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf, size_t len)
436{
437 u16 quality;
438 int ret = -EINVAL;
439
440 if (len < 2)
441 return -EINVAL;
442
443 ret = mutex_lock_interruptible(&rng_mutex);
444 if (ret)
445 return -ERESTARTSYS;
446
447 ret = kstrtou16(buf, 0, &quality);
448 if (ret || quality > 1024) {
449 ret = -EINVAL;
450 goto out;
451 }
452
453 if (!current_rng) {
454 ret = -ENODEV;
455 goto out;
456 }
457
458 current_rng->quality = quality;
459 current_quality = quality; /* obsolete */
460
461 /* the best available RNG may have changed */
462 ret = enable_best_rng();
463
464out:
465 mutex_unlock(&rng_mutex);
466 return ret ? ret : len;
467}
468
469static DEVICE_ATTR_RW(rng_current);
470static DEVICE_ATTR_RO(rng_available);
471static DEVICE_ATTR_RO(rng_selected);
472static DEVICE_ATTR_RW(rng_quality);
473
474static struct attribute *rng_dev_attrs[] = {
475 &dev_attr_rng_current.attr,
476 &dev_attr_rng_available.attr,
477 &dev_attr_rng_selected.attr,
478 &dev_attr_rng_quality.attr,
479 NULL
480};
481
482ATTRIBUTE_GROUPS(rng_dev);
483
484static void __exit unregister_miscdev(void)
485{
486 misc_deregister(&rng_miscdev);
487}
488
489static int __init register_miscdev(void)
490{
491 return misc_register(&rng_miscdev);
492}
493
494static int hwrng_fillfn(void *unused)
495{
496 size_t entropy, entropy_credit = 0; /* in 1/1024 of a bit */
497 long rc;
498
499 while (!kthread_should_stop()) {
500 unsigned short quality;
501 struct hwrng *rng;
502
503 rng = get_current_rng();
504 if (IS_ERR(rng) || !rng)
505 break;
506 mutex_lock(&reading_mutex);
507 rc = rng_get_data(rng, rng_fillbuf,
508 rng_buffer_size(), 1);
509 if (current_quality != rng->quality)
510 rng->quality = current_quality; /* obsolete */
511 quality = rng->quality;
512 mutex_unlock(&reading_mutex);
513
514 if (rc <= 0)
515 hwrng_msleep(rng, 10000);
516
517 put_rng(rng);
518
519 if (rc <= 0)
520 continue;
521
522 /* If we cannot credit at least one bit of entropy,
523 * keep track of the remainder for the next iteration
524 */
525 entropy = rc * quality * 8 + entropy_credit;
526 if ((entropy >> 10) == 0)
527 entropy_credit = entropy;
528
529 /* Outside lock, sure, but y'know: randomness. */
530 add_hwgenerator_randomness((void *)rng_fillbuf, rc,
531 entropy >> 10);
532 }
533 hwrng_fill = NULL;
534 return 0;
535}
536
537int hwrng_register(struct hwrng *rng)
538{
539 int err = -EINVAL;
540 struct hwrng *tmp;
541 bool is_new_current = false;
542
543 if (!rng->name || (!rng->data_read && !rng->read))
544 goto out;
545
546 mutex_lock(&rng_mutex);
547
548 /* Must not register two RNGs with the same name. */
549 err = -EEXIST;
550 list_for_each_entry(tmp, &rng_list, list) {
551 if (strcmp(tmp->name, rng->name) == 0)
552 goto out_unlock;
553 }
554 list_add_tail(&rng->list, &rng_list);
555
556 init_completion(&rng->cleanup_done);
557 complete(&rng->cleanup_done);
558 init_completion(&rng->dying);
559
560 if (!current_rng ||
561 (!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
562 /*
563 * Set new rng as current as the new rng source
564 * provides better entropy quality and was not
565 * chosen by userspace.
566 */
567 err = set_current_rng(rng);
568 if (err)
569 goto out_unlock;
570 /* to use current_rng in add_early_randomness() we need
571 * to take a ref
572 */
573 is_new_current = true;
574 kref_get(&rng->ref);
575 }
576 mutex_unlock(&rng_mutex);
577 if (is_new_current || !rng->init) {
578 /*
579 * Use a new device's input to add some randomness to
580 * the system. If this rng device isn't going to be
581 * used right away, its init function hasn't been
582 * called yet by set_current_rng(); so only use the
583 * randomness from devices that don't need an init callback
584 */
585 add_early_randomness(rng);
586 }
587 if (is_new_current)
588 put_rng(rng);
589 return 0;
590out_unlock:
591 mutex_unlock(&rng_mutex);
592out:
593 return err;
594}
595EXPORT_SYMBOL_GPL(hwrng_register);
596
597void hwrng_unregister(struct hwrng *rng)
598{
599 struct hwrng *old_rng, *new_rng;
600 int err;
601
602 mutex_lock(&rng_mutex);
603
604 old_rng = current_rng;
605 list_del(&rng->list);
606 complete_all(&rng->dying);
607 if (current_rng == rng) {
608 err = enable_best_rng();
609 if (err) {
610 drop_current_rng();
611 cur_rng_set_by_user = 0;
612 }
613 }
614
615 new_rng = get_current_rng_nolock();
616 if (list_empty(&rng_list)) {
617 mutex_unlock(&rng_mutex);
618 if (hwrng_fill)
619 kthread_stop(hwrng_fill);
620 } else
621 mutex_unlock(&rng_mutex);
622
623 if (new_rng) {
624 if (old_rng != new_rng)
625 add_early_randomness(new_rng);
626 put_rng(new_rng);
627 }
628
629 wait_for_completion(&rng->cleanup_done);
630}
631EXPORT_SYMBOL_GPL(hwrng_unregister);
632
633static void devm_hwrng_release(struct device *dev, void *res)
634{
635 hwrng_unregister(*(struct hwrng **)res);
636}
637
638static int devm_hwrng_match(struct device *dev, void *res, void *data)
639{
640 struct hwrng **r = res;
641
642 if (WARN_ON(!r || !*r))
643 return 0;
644
645 return *r == data;
646}
647
648int devm_hwrng_register(struct device *dev, struct hwrng *rng)
649{
650 struct hwrng **ptr;
651 int error;
652
653 ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);
654 if (!ptr)
655 return -ENOMEM;
656
657 error = hwrng_register(rng);
658 if (error) {
659 devres_free(ptr);
660 return error;
661 }
662
663 *ptr = rng;
664 devres_add(dev, ptr);
665 return 0;
666}
667EXPORT_SYMBOL_GPL(devm_hwrng_register);
668
669void devm_hwrng_unregister(struct device *dev, struct hwrng *rng)
670{
671 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng);
672}
673EXPORT_SYMBOL_GPL(devm_hwrng_unregister);
674
675long hwrng_msleep(struct hwrng *rng, unsigned int msecs)
676{
677 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
678
679 return wait_for_completion_interruptible_timeout(&rng->dying, timeout);
680}
681EXPORT_SYMBOL_GPL(hwrng_msleep);
682
683static int __init hwrng_modinit(void)
684{
685 int ret;
686
687 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
688 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
689 if (!rng_buffer)
690 return -ENOMEM;
691
692 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
693 if (!rng_fillbuf) {
694 kfree(rng_buffer);
695 return -ENOMEM;
696 }
697
698 ret = register_miscdev();
699 if (ret) {
700 kfree(rng_fillbuf);
701 kfree(rng_buffer);
702 }
703
704 return ret;
705}
706
707static void __exit hwrng_modexit(void)
708{
709 mutex_lock(&rng_mutex);
710 BUG_ON(current_rng);
711 kfree(rng_buffer);
712 kfree(rng_fillbuf);
713 mutex_unlock(&rng_mutex);
714
715 unregister_miscdev();
716}
717
718fs_initcall(hwrng_modinit); /* depends on misc_register() */
719module_exit(hwrng_modexit);
720
721MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
722MODULE_LICENSE("GPL");