fortify.c 5.7 KB

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