test_user_copy.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
  64. PAGE_SIZE),
  65. "illegal all-kernel copy_from_user passed");
  66. ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
  67. PAGE_SIZE),
  68. "illegal reversed copy_from_user passed");
  69. ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
  70. PAGE_SIZE),
  71. "illegal all-kernel copy_to_user passed");
  72. ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
  73. PAGE_SIZE),
  74. "illegal reversed copy_to_user passed");
  75. ret |= test(!get_user(value, (unsigned long __user *)kmem),
  76. "illegal get_user passed");
  77. ret |= test(!put_user(value, (unsigned long __user *)kmem),
  78. "illegal put_user passed");
  79. vm_munmap(user_addr, PAGE_SIZE * 2);
  80. kfree(kmem);
  81. if (ret == 0) {
  82. pr_info("tests passed.\n");
  83. return 0;
  84. }
  85. return -EINVAL;
  86. }
  87. module_init(test_user_copy_init);
  88. static void __exit test_user_copy_exit(void)
  89. {
  90. pr_info("unloaded.\n");
  91. }
  92. module_exit(test_user_copy_exit);
  93. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  94. MODULE_LICENSE("GPL");