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