fortify.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020 Francis Laniel <laniel_francis@privacyrequired.com>
  4. *
  5. * Add tests related to fortified functions in this file.
  6. */
  7. #include "lkdtm.h"
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. static volatile int fortify_scratch_space;
  11. static void lkdtm_FORTIFY_STR_OBJECT(void)
  12. {
  13. struct target {
  14. char a[10];
  15. int foo;
  16. } target[3] = {};
  17. /*
  18. * Using volatile prevents the compiler from determining the value of
  19. * 'size' at compile time. Without that, we would get a compile error
  20. * rather than a runtime error.
  21. */
  22. volatile int size = 20;
  23. pr_info("trying to strcmp() past the end of a struct\n");
  24. strncpy(target[0].a, target[1].a, size);
  25. /* Store result to global to prevent the code from being eliminated */
  26. fortify_scratch_space = target[0].a[3];
  27. pr_err("FAIL: fortify did not block a strncpy() object write overflow!\n");
  28. pr_expected_config(CONFIG_FORTIFY_SOURCE);
  29. }
  30. static void lkdtm_FORTIFY_STR_MEMBER(void)
  31. {
  32. struct target {
  33. char a[10];
  34. char b[10];
  35. } target;
  36. volatile int size = 20;
  37. char *src;
  38. src = kmalloc(size, GFP_KERNEL);
  39. if (!src)
  40. return;
  41. strscpy(src, "over ten bytes", size);
  42. size = strlen(src) + 1;
  43. pr_info("trying to strncpy() past the end of a struct member...\n");
  44. /*
  45. * strncpy(target.a, src, 20); will hit a compile error because the
  46. * compiler knows at build time that target.a < 20 bytes. Use a
  47. * volatile to force a runtime error.
  48. */
  49. strncpy(target.a, src, size);
  50. /* Store result to global to prevent the code from being eliminated */
  51. fortify_scratch_space = target.a[3];
  52. pr_err("FAIL: fortify did not block a strncpy() struct member write overflow!\n");
  53. pr_expected_config(CONFIG_FORTIFY_SOURCE);
  54. kfree(src);
  55. }
  56. static void lkdtm_FORTIFY_MEM_OBJECT(void)
  57. {
  58. int before[10];
  59. struct target {
  60. char a[10];
  61. int foo;
  62. } target = {};
  63. int after[10];
  64. /*
  65. * Using volatile prevents the compiler from determining the value of
  66. * 'size' at compile time. Without that, we would get a compile error
  67. * rather than a runtime error.
  68. */
  69. volatile int size = 20;
  70. memset(before, 0, sizeof(before));
  71. memset(after, 0, sizeof(after));
  72. fortify_scratch_space = before[5];
  73. fortify_scratch_space = after[5];
  74. pr_info("trying to memcpy() past the end of a struct\n");
  75. pr_info("0: %zu\n", __builtin_object_size(&target, 0));
  76. pr_info("1: %zu\n", __builtin_object_size(&target, 1));
  77. pr_info("s: %d\n", size);
  78. memcpy(&target, &before, size);
  79. /* Store result to global to prevent the code from being eliminated */
  80. fortify_scratch_space = target.a[3];
  81. pr_err("FAIL: fortify did not block a memcpy() object write overflow!\n");
  82. pr_expected_config(CONFIG_FORTIFY_SOURCE);
  83. }
  84. static void lkdtm_FORTIFY_MEM_MEMBER(void)
  85. {
  86. struct target {
  87. char a[10];
  88. char b[10];
  89. } target;
  90. volatile int size = 20;
  91. char *src;
  92. src = kmalloc(size, GFP_KERNEL);
  93. if (!src)
  94. return;
  95. strscpy(src, "over ten bytes", size);
  96. size = strlen(src) + 1;
  97. pr_info("trying to memcpy() past the end of a struct member...\n");
  98. /*
  99. * strncpy(target.a, src, 20); will hit a compile error because the
  100. * compiler knows at build time that target.a < 20 bytes. Use a
  101. * volatile to force a runtime error.
  102. */
  103. memcpy(target.a, src, size);
  104. /* Store result to global to prevent the code from being eliminated */
  105. fortify_scratch_space = target.a[3];
  106. pr_err("FAIL: fortify did not block a memcpy() struct member write overflow!\n");
  107. pr_expected_config(CONFIG_FORTIFY_SOURCE);
  108. kfree(src);
  109. }
  110. /*
  111. * Calls fortified strscpy to test that it returns the same result as vanilla
  112. * strscpy and generate a panic because there is a write overflow (i.e. src
  113. * length is greater than dst length).
  114. */
  115. static void lkdtm_FORTIFY_STRSCPY(void)
  116. {
  117. char *src;
  118. char dst[5];
  119. struct {
  120. union {
  121. char big[10];
  122. char src[5];
  123. };
  124. } weird = { .big = "hello!" };
  125. char weird_dst[sizeof(weird.src) + 1];
  126. src = kstrdup("foobar", GFP_KERNEL);
  127. if (src == NULL)
  128. return;
  129. /* Vanilla strscpy returns -E2BIG if size is 0. */
  130. if (strscpy(dst, src, 0) != -E2BIG)
  131. pr_warn("FAIL: strscpy() of 0 length did not return -E2BIG\n");
  132. /* Vanilla strscpy returns -E2BIG if src is truncated. */
  133. if (strscpy(dst, src, sizeof(dst)) != -E2BIG)
  134. pr_warn("FAIL: strscpy() did not return -E2BIG while src is truncated\n");
  135. /* After above call, dst must contain "foob" because src was truncated. */
  136. if (strncmp(dst, "foob", sizeof(dst)) != 0)
  137. pr_warn("FAIL: after strscpy() dst does not contain \"foob\" but \"%s\"\n",
  138. dst);
  139. /* Shrink src so the strscpy() below succeeds. */
  140. src[3] = '\0';
  141. /*
  142. * Vanilla strscpy returns number of character copied if everything goes
  143. * well.
  144. */
  145. if (strscpy(dst, src, sizeof(dst)) != 3)
  146. pr_warn("FAIL: strscpy() did not return 3 while src was copied entirely truncated\n");
  147. /* After above call, dst must contain "foo" because src was copied. */
  148. if (strncmp(dst, "foo", sizeof(dst)) != 0)
  149. pr_warn("FAIL: after strscpy() dst does not contain \"foo\" but \"%s\"\n",
  150. dst);
  151. /* Test when src is embedded inside a union. */
  152. strscpy(weird_dst, weird.src, sizeof(weird_dst));
  153. if (strcmp(weird_dst, "hello") != 0)
  154. pr_warn("FAIL: after strscpy() weird_dst does not contain \"hello\" but \"%s\"\n",
  155. weird_dst);
  156. /* Restore src to its initial value. */
  157. src[3] = 'b';
  158. /*
  159. * Use strlen here so size cannot be known at compile time and there is
  160. * a runtime write overflow.
  161. */
  162. strscpy(dst, src, strlen(src));
  163. pr_err("FAIL: strscpy() overflow not detected!\n");
  164. pr_expected_config(CONFIG_FORTIFY_SOURCE);
  165. kfree(src);
  166. }
  167. static struct crashtype crashtypes[] = {
  168. CRASHTYPE(FORTIFY_STR_OBJECT),
  169. CRASHTYPE(FORTIFY_STR_MEMBER),
  170. CRASHTYPE(FORTIFY_MEM_OBJECT),
  171. CRASHTYPE(FORTIFY_MEM_MEMBER),
  172. CRASHTYPE(FORTIFY_STRSCPY),
  173. };
  174. struct crashtype_category fortify_crashtypes = {
  175. .crashtypes = crashtypes,
  176. .len = ARRAY_SIZE(crashtypes),
  177. };