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