test_user_copy.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Kernel module for testing copy_to/from_user infrastructure.
  3. *
  4. * Copyright 2013 Google Inc. All Rights Reserved
  5. *
  6. * Authors:
  7. * Kees Cook <keescook@chromium.org>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/mman.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/vmalloc.h>
  25. #define test(condition, msg) \
  26. ({ \
  27. int cond = (condition); \
  28. if (cond) \
  29. pr_warn("%s\n", msg); \
  30. cond; \
  31. })
  32. static int __init test_user_copy_init(void)
  33. {
  34. int ret = 0;
  35. char *kmem;
  36. char __user *usermem;
  37. char *bad_usermem;
  38. unsigned long user_addr;
  39. unsigned long value = 0x5A;
  40. kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
  41. if (!kmem)
  42. return -ENOMEM;
  43. user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
  44. PROT_READ | PROT_WRITE | PROT_EXEC,
  45. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  46. if (user_addr >= (unsigned long)(TASK_SIZE)) {
  47. pr_warn("Failed to allocate user memory\n");
  48. kfree(kmem);
  49. return -ENOMEM;
  50. }
  51. usermem = (char __user *)user_addr;
  52. bad_usermem = (char *)user_addr;
  53. /* Legitimate usage: none of these should fail. */
  54. ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
  55. "legitimate copy_from_user failed");
  56. ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
  57. "legitimate copy_to_user failed");
  58. ret |= test(get_user(value, (unsigned long __user *)usermem),
  59. "legitimate get_user failed");
  60. ret |= test(put_user(value, (unsigned long __user *)usermem),
  61. "legitimate put_user failed");
  62. /* Invalid usage: none of these should succeed. */
  63. memset(kmem, 0x5a, PAGE_SIZE);
  64. memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
  65. ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
  66. PAGE_SIZE),
  67. "illegal all-kernel copy_from_user passed");
  68. ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
  69. "zeroing failure for illegal all-kernel copy_from_user");
  70. memset(bad_usermem, 0x5A, PAGE_SIZE);
  71. ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
  72. PAGE_SIZE),
  73. "illegal reversed copy_from_user passed");
  74. ret |= test(memcmp(kmem + PAGE_SIZE, bad_usermem, PAGE_SIZE),
  75. "zeroing failure for illegal reversed copy_from_user");
  76. ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
  77. PAGE_SIZE),
  78. "illegal all-kernel copy_to_user passed");
  79. ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
  80. PAGE_SIZE),
  81. "illegal reversed copy_to_user passed");
  82. memset(kmem, 0x5a, PAGE_SIZE);
  83. ret |= test(!get_user(value, (unsigned long __user *)kmem),
  84. "illegal get_user passed");
  85. ret |= test(memcmp(kmem + PAGE_SIZE, kmem, sizeof(value)),
  86. "zeroing failure for illegal get_user");
  87. ret |= test(!put_user(value, (unsigned long __user *)kmem),
  88. "illegal put_user passed");
  89. vm_munmap(user_addr, PAGE_SIZE * 2);
  90. kfree(kmem);
  91. if (ret == 0) {
  92. pr_info("tests passed.\n");
  93. return 0;
  94. }
  95. return -EINVAL;
  96. }
  97. module_init(test_user_copy_init);
  98. static void __exit test_user_copy_exit(void)
  99. {
  100. pr_info("unloaded.\n");
  101. }
  102. module_exit(test_user_copy_exit);
  103. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  104. MODULE_LICENSE("GPL");