xref: /openbmc/linux/lib/list_debug.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1199a9afcSDave Jones /*
2199a9afcSDave Jones  * Copyright 2006, Red Hat, Inc., Dave Jones
3199a9afcSDave Jones  * Released under the General Public License (GPL).
4199a9afcSDave Jones  *
5*aebc7b0dSMarco Elver  * This file contains the linked list validation and error reporting for
6*aebc7b0dSMarco Elver  * LIST_HARDENED and DEBUG_LIST.
7199a9afcSDave Jones  */
8199a9afcSDave Jones 
98bc3bcc9SPaul Gortmaker #include <linux/export.h>
10199a9afcSDave Jones #include <linux/list.h>
1150af5eadSPaul Gortmaker #include <linux/bug.h>
12b116ee4dSPaul Gortmaker #include <linux/kernel.h>
13559f9badSDave Jones #include <linux/rculist.h>
14199a9afcSDave Jones 
15199a9afcSDave Jones /*
16d7c81673SKees Cook  * Check that the data structures for the list manipulations are reasonably
17d7c81673SKees Cook  * valid. Failures here indicate memory corruption (and possibly an exploit
18d7c81673SKees Cook  * attempt).
19199a9afcSDave Jones  */
20199a9afcSDave Jones 
21*aebc7b0dSMarco Elver __list_valid_slowpath
__list_add_valid_or_report(struct list_head * new,struct list_head * prev,struct list_head * next)22b16c42c8SMarco Elver bool __list_add_valid_or_report(struct list_head *new, struct list_head *prev,
23199a9afcSDave Jones 				struct list_head *next)
24199a9afcSDave Jones {
250cc011c5SGuenter Roeck 	if (CHECK_DATA_CORRUPTION(prev == NULL,
260cc011c5SGuenter Roeck 			"list_add corruption. prev is NULL.\n") ||
270cc011c5SGuenter Roeck 	    CHECK_DATA_CORRUPTION(next == NULL,
280cc011c5SGuenter Roeck 			"list_add corruption. next is NULL.\n") ||
290cc011c5SGuenter Roeck 	    CHECK_DATA_CORRUPTION(next->prev != prev,
3068c1f082SMatthew Wilcox 			"list_add corruption. next->prev should be prev (%px), but was %px. (next=%px).\n",
3185caa95bSKees Cook 			prev, next->prev, next) ||
32de54ebbeSKees Cook 	    CHECK_DATA_CORRUPTION(prev->next != next,
3368c1f082SMatthew Wilcox 			"list_add corruption. prev->next should be next (%px), but was %px. (prev=%px).\n",
3485caa95bSKees Cook 			next, prev->next, prev) ||
35de54ebbeSKees Cook 	    CHECK_DATA_CORRUPTION(new == prev || new == next,
3668c1f082SMatthew Wilcox 			"list_add double add: new=%px, prev=%px, next=%px.\n",
3785caa95bSKees Cook 			new, prev, next))
3885caa95bSKees Cook 		return false;
39de54ebbeSKees Cook 
40d7c81673SKees Cook 	return true;
41d7c81673SKees Cook }
42b16c42c8SMarco Elver EXPORT_SYMBOL(__list_add_valid_or_report);
43199a9afcSDave Jones 
44*aebc7b0dSMarco Elver __list_valid_slowpath
__list_del_entry_valid_or_report(struct list_head * entry)45b16c42c8SMarco Elver bool __list_del_entry_valid_or_report(struct list_head *entry)
463c18d4deSLinus Torvalds {
473c18d4deSLinus Torvalds 	struct list_head *prev, *next;
483c18d4deSLinus Torvalds 
493c18d4deSLinus Torvalds 	prev = entry->prev;
503c18d4deSLinus Torvalds 	next = entry->next;
513c18d4deSLinus Torvalds 
520cc011c5SGuenter Roeck 	if (CHECK_DATA_CORRUPTION(next == NULL,
530cc011c5SGuenter Roeck 			"list_del corruption, %px->next is NULL\n", entry) ||
540cc011c5SGuenter Roeck 	    CHECK_DATA_CORRUPTION(prev == NULL,
550cc011c5SGuenter Roeck 			"list_del corruption, %px->prev is NULL\n", entry) ||
560cc011c5SGuenter Roeck 	    CHECK_DATA_CORRUPTION(next == LIST_POISON1,
5768c1f082SMatthew Wilcox 			"list_del corruption, %px->next is LIST_POISON1 (%px)\n",
5885caa95bSKees Cook 			entry, LIST_POISON1) ||
59de54ebbeSKees Cook 	    CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
6068c1f082SMatthew Wilcox 			"list_del corruption, %px->prev is LIST_POISON2 (%px)\n",
6185caa95bSKees Cook 			entry, LIST_POISON2) ||
62de54ebbeSKees Cook 	    CHECK_DATA_CORRUPTION(prev->next != entry,
63a31f9336SZhen Lei 			"list_del corruption. prev->next should be %px, but was %px. (prev=%px)\n",
64a31f9336SZhen Lei 			entry, prev->next, prev) ||
65de54ebbeSKees Cook 	    CHECK_DATA_CORRUPTION(next->prev != entry,
66a31f9336SZhen Lei 			"list_del corruption. next->prev should be %px, but was %px. (next=%px)\n",
67a31f9336SZhen Lei 			entry, next->prev, next))
6885caa95bSKees Cook 		return false;
6985caa95bSKees Cook 
700cd340dcSKees Cook 	return true;
710cd340dcSKees Cook }
72b16c42c8SMarco Elver EXPORT_SYMBOL(__list_del_entry_valid_or_report);
73