test_kasan_module.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  5. * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
  6. */
  7. #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
  8. #include <linux/mman.h>
  9. #include <linux/module.h>
  10. #include <linux/printk.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include "../mm/kasan/kasan.h"
  14. #define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_GRANULE_SIZE)
  15. static noinline void __init copy_user_test(void)
  16. {
  17. char *kmem;
  18. char __user *usermem;
  19. size_t size = 10;
  20. int unused;
  21. kmem = kmalloc(size, GFP_KERNEL);
  22. if (!kmem)
  23. return;
  24. usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
  25. PROT_READ | PROT_WRITE | PROT_EXEC,
  26. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  27. if (IS_ERR(usermem)) {
  28. pr_err("Failed to allocate user memory\n");
  29. kfree(kmem);
  30. return;
  31. }
  32. pr_info("out-of-bounds in copy_from_user()\n");
  33. unused = copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
  34. pr_info("out-of-bounds in copy_to_user()\n");
  35. unused = copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
  36. pr_info("out-of-bounds in __copy_from_user()\n");
  37. unused = __copy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
  38. pr_info("out-of-bounds in __copy_to_user()\n");
  39. unused = __copy_to_user(usermem, kmem, size + 1 + OOB_TAG_OFF);
  40. pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
  41. unused = __copy_from_user_inatomic(kmem, usermem, size + 1 + OOB_TAG_OFF);
  42. pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
  43. unused = __copy_to_user_inatomic(usermem, kmem, size + 1 + OOB_TAG_OFF);
  44. pr_info("out-of-bounds in strncpy_from_user()\n");
  45. unused = strncpy_from_user(kmem, usermem, size + 1 + OOB_TAG_OFF);
  46. vm_munmap((unsigned long)usermem, PAGE_SIZE);
  47. kfree(kmem);
  48. }
  49. static struct kasan_rcu_info {
  50. int i;
  51. struct rcu_head rcu;
  52. } *global_rcu_ptr;
  53. static noinline void __init kasan_rcu_reclaim(struct rcu_head *rp)
  54. {
  55. struct kasan_rcu_info *fp = container_of(rp,
  56. struct kasan_rcu_info, rcu);
  57. kfree(fp);
  58. fp->i = 1;
  59. }
  60. static noinline void __init kasan_rcu_uaf(void)
  61. {
  62. struct kasan_rcu_info *ptr;
  63. pr_info("use-after-free in kasan_rcu_reclaim\n");
  64. ptr = kmalloc(sizeof(struct kasan_rcu_info), GFP_KERNEL);
  65. if (!ptr) {
  66. pr_err("Allocation failed\n");
  67. return;
  68. }
  69. global_rcu_ptr = rcu_dereference_protected(ptr, NULL);
  70. call_rcu(&global_rcu_ptr->rcu, kasan_rcu_reclaim);
  71. }
  72. static noinline void __init kasan_workqueue_work(struct work_struct *work)
  73. {
  74. kfree(work);
  75. }
  76. static noinline void __init kasan_workqueue_uaf(void)
  77. {
  78. struct workqueue_struct *workqueue;
  79. struct work_struct *work;
  80. workqueue = create_workqueue("kasan_wq_test");
  81. if (!workqueue) {
  82. pr_err("Allocation failed\n");
  83. return;
  84. }
  85. work = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
  86. if (!work) {
  87. pr_err("Allocation failed\n");
  88. return;
  89. }
  90. INIT_WORK(work, kasan_workqueue_work);
  91. queue_work(workqueue, work);
  92. destroy_workqueue(workqueue);
  93. pr_info("use-after-free on workqueue\n");
  94. ((volatile struct work_struct *)work)->data;
  95. }
  96. static int __init test_kasan_module_init(void)
  97. {
  98. /*
  99. * Temporarily enable multi-shot mode. Otherwise, KASAN would only
  100. * report the first detected bug and panic the kernel if panic_on_warn
  101. * is enabled.
  102. */
  103. bool multishot = kasan_save_enable_multi_shot();
  104. copy_user_test();
  105. kasan_rcu_uaf();
  106. kasan_workqueue_uaf();
  107. kasan_restore_multi_shot(multishot);
  108. return -EAGAIN;
  109. }
  110. module_init(test_kasan_module_init);
  111. MODULE_LICENSE("GPL");