stackleak.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code tests that the current task stack is properly erased (filled
  4. * with STACKLEAK_POISON).
  5. *
  6. * Authors:
  7. * Alexander Popov <alex.popov@linux.com>
  8. * Tycho Andersen <tycho@tycho.ws>
  9. */
  10. #include "lkdtm.h"
  11. #include <linux/stackleak.h>
  12. void lkdtm_STACKLEAK_ERASING(void)
  13. {
  14. unsigned long *sp, left, found, i;
  15. const unsigned long check_depth =
  16. STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
  17. /*
  18. * For the details about the alignment of the poison values, see
  19. * the comment in stackleak_track_stack().
  20. */
  21. sp = PTR_ALIGN(&i, sizeof(unsigned long));
  22. left = ((unsigned long)sp & (THREAD_SIZE - 1)) / sizeof(unsigned long);
  23. sp--;
  24. /*
  25. * One 'long int' at the bottom of the thread stack is reserved
  26. * and not poisoned.
  27. */
  28. if (left > 1) {
  29. left--;
  30. } else {
  31. pr_err("FAIL: not enough stack space for the test\n");
  32. return;
  33. }
  34. pr_info("checking unused part of the thread stack (%lu bytes)...\n",
  35. left * sizeof(unsigned long));
  36. /*
  37. * Search for 'check_depth' poison values in a row (just like
  38. * stackleak_erase() does).
  39. */
  40. for (i = 0, found = 0; i < left && found <= check_depth; i++) {
  41. if (*(sp - i) == STACKLEAK_POISON)
  42. found++;
  43. else
  44. found = 0;
  45. }
  46. if (found <= check_depth) {
  47. pr_err("FAIL: thread stack is not erased (checked %lu bytes)\n",
  48. i * sizeof(unsigned long));
  49. return;
  50. }
  51. pr_info("first %lu bytes are unpoisoned\n",
  52. (i - found) * sizeof(unsigned long));
  53. /* The rest of thread stack should be erased */
  54. for (; i < left; i++) {
  55. if (*(sp - i) != STACKLEAK_POISON) {
  56. pr_err("FAIL: thread stack is NOT properly erased\n");
  57. return;
  58. }
  59. }
  60. pr_info("OK: the rest of the thread stack is properly erased\n");
  61. return;
  62. }