list_debug.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2006, Red Hat, Inc., Dave Jones
  3. * Released under the General Public License (GPL).
  4. *
  5. * This file contains the linked list validation and error reporting for
  6. * LIST_HARDENED and DEBUG_LIST.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/list.h>
  10. #include <linux/bug.h>
  11. #include <linux/kernel.h>
  12. #include <linux/rculist.h>
  13. /*
  14. * Check that the data structures for the list manipulations are reasonably
  15. * valid. Failures here indicate memory corruption (and possibly an exploit
  16. * attempt).
  17. */
  18. __list_valid_slowpath
  19. bool __list_add_valid_or_report(struct list_head *new, struct list_head *prev,
  20. struct list_head *next)
  21. {
  22. if (CHECK_DATA_CORRUPTION(prev == NULL, NULL,
  23. "list_add corruption. prev is NULL.\n") ||
  24. CHECK_DATA_CORRUPTION(next == NULL, NULL,
  25. "list_add corruption. next is NULL.\n") ||
  26. CHECK_DATA_CORRUPTION(next->prev != prev, next,
  27. "list_add corruption. next->prev should be prev (%px), but was %px. (next=%px).\n",
  28. prev, next->prev, next) ||
  29. CHECK_DATA_CORRUPTION(prev->next != next, prev,
  30. "list_add corruption. prev->next should be next (%px), but was %px. (prev=%px).\n",
  31. next, prev->next, prev) ||
  32. CHECK_DATA_CORRUPTION(new == prev || new == next, NULL,
  33. "list_add double add: new=%px, prev=%px, next=%px.\n",
  34. new, prev, next))
  35. return false;
  36. return true;
  37. }
  38. EXPORT_SYMBOL(__list_add_valid_or_report);
  39. __list_valid_slowpath
  40. bool __list_del_entry_valid_or_report(struct list_head *entry)
  41. {
  42. struct list_head *prev, *next;
  43. prev = entry->prev;
  44. next = entry->next;
  45. if (CHECK_DATA_CORRUPTION(next == NULL, NULL,
  46. "list_del corruption, %px->next is NULL\n", entry) ||
  47. CHECK_DATA_CORRUPTION(prev == NULL, NULL,
  48. "list_del corruption, %px->prev is NULL\n", entry) ||
  49. CHECK_DATA_CORRUPTION(next == LIST_POISON1, next,
  50. "list_del corruption, %px->next is LIST_POISON1 (%px)\n",
  51. entry, LIST_POISON1) ||
  52. CHECK_DATA_CORRUPTION(prev == LIST_POISON2, prev,
  53. "list_del corruption, %px->prev is LIST_POISON2 (%px)\n",
  54. entry, LIST_POISON2) ||
  55. CHECK_DATA_CORRUPTION(prev->next != entry, prev,
  56. "list_del corruption. prev->next should be %px, but was %px. (prev=%px)\n",
  57. entry, prev->next, prev) ||
  58. CHECK_DATA_CORRUPTION(next->prev != entry, next,
  59. "list_del corruption. next->prev should be %px, but was %px. (next=%px)\n",
  60. entry, next->prev, next))
  61. return false;
  62. return true;
  63. }
  64. EXPORT_SYMBOL(__list_del_entry_valid_or_report);