list_debug.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 for DEBUG_LIST.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/list.h>
  9. #include <linux/bug.h>
  10. #include <linux/kernel.h>
  11. #include <linux/rculist.h>
  12. /*
  13. * Check that the data structures for the list manipulations are reasonably
  14. * valid. Failures here indicate memory corruption (and possibly an exploit
  15. * attempt).
  16. */
  17. bool __list_add_valid(struct list_head *new, struct list_head *prev,
  18. struct list_head *next)
  19. {
  20. if (CHECK_DATA_CORRUPTION(prev == NULL,
  21. "list_add corruption. prev is NULL.\n") ||
  22. CHECK_DATA_CORRUPTION(next == NULL,
  23. "list_add corruption. next is NULL.\n") ||
  24. CHECK_DATA_CORRUPTION(next->prev != prev,
  25. "list_add corruption. next->prev should be prev (%px), but was %px. (next=%px).\n",
  26. prev, next->prev, next) ||
  27. CHECK_DATA_CORRUPTION(prev->next != next,
  28. "list_add corruption. prev->next should be next (%px), but was %px. (prev=%px).\n",
  29. next, prev->next, prev) ||
  30. CHECK_DATA_CORRUPTION(new == prev || new == next,
  31. "list_add double add: new=%px, prev=%px, next=%px.\n",
  32. new, prev, next))
  33. return false;
  34. return true;
  35. }
  36. EXPORT_SYMBOL(__list_add_valid);
  37. bool __list_del_entry_valid(struct list_head *entry)
  38. {
  39. struct list_head *prev, *next;
  40. prev = entry->prev;
  41. next = entry->next;
  42. if (CHECK_DATA_CORRUPTION(next == NULL,
  43. "list_del corruption, %px->next is NULL\n", entry) ||
  44. CHECK_DATA_CORRUPTION(prev == NULL,
  45. "list_del corruption, %px->prev is NULL\n", entry) ||
  46. CHECK_DATA_CORRUPTION(next == LIST_POISON1,
  47. "list_del corruption, %px->next is LIST_POISON1 (%px)\n",
  48. entry, LIST_POISON1) ||
  49. CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
  50. "list_del corruption, %px->prev is LIST_POISON2 (%px)\n",
  51. entry, LIST_POISON2) ||
  52. CHECK_DATA_CORRUPTION(prev->next != entry,
  53. "list_del corruption. prev->next should be %px, but was %px. (prev=%px)\n",
  54. entry, prev->next, prev) ||
  55. CHECK_DATA_CORRUPTION(next->prev != entry,
  56. "list_del corruption. next->prev should be %px, but was %px. (next=%px)\n",
  57. entry, next->prev, next))
  58. return false;
  59. return true;
  60. }
  61. EXPORT_SYMBOL(__list_del_entry_valid);