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