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