test_user_copy.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /*
  54. * Legitimate usage: none of these copies should fail.
  55. */
  56. ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
  57. "legitimate copy_from_user failed");
  58. ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
  59. "legitimate copy_to_user failed");
  60. ret |= test(get_user(value, (unsigned long __user *)usermem),
  61. "legitimate get_user failed");
  62. ret |= test(put_user(value, (unsigned long __user *)usermem),
  63. "legitimate put_user failed");
  64. /*
  65. * Invalid usage: none of these copies should succeed.
  66. */
  67. /* Reject kernel-to-kernel copies through copy_from_user(). */
  68. ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
  69. PAGE_SIZE),
  70. "illegal all-kernel copy_from_user passed");
  71. #if 0
  72. /*
  73. * When running with SMAP/PAN/etc, this will Oops the kernel
  74. * due to the zeroing of userspace memory on failure. This needs
  75. * to be tested in LKDTM instead, since this test module does not
  76. * expect to explode.
  77. */
  78. ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
  79. PAGE_SIZE),
  80. "illegal reversed copy_from_user passed");
  81. #endif
  82. ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
  83. PAGE_SIZE),
  84. "illegal all-kernel copy_to_user passed");
  85. ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
  86. PAGE_SIZE),
  87. "illegal reversed copy_to_user passed");
  88. ret |= test(!get_user(value, (unsigned long __user *)kmem),
  89. "illegal get_user passed");
  90. ret |= test(!put_user(value, (unsigned long __user *)kmem),
  91. "illegal put_user passed");
  92. vm_munmap(user_addr, PAGE_SIZE * 2);
  93. kfree(kmem);
  94. if (ret == 0) {
  95. pr_info("tests passed.\n");
  96. return 0;
  97. }
  98. return -EINVAL;
  99. }
  100. module_init(test_user_copy_init);
  101. static void __exit test_user_copy_exit(void)
  102. {
  103. pr_info("unloaded.\n");
  104. }
  105. module_exit(test_user_copy_exit);
  106. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  107. MODULE_LICENSE("GPL");