162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2006, Red Hat, Inc., Dave Jones
362306a36Sopenharmony_ci * Released under the General Public License (GPL).
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This file contains the linked list validation and error reporting for
662306a36Sopenharmony_ci * LIST_HARDENED and DEBUG_LIST.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/export.h>
1062306a36Sopenharmony_ci#include <linux/list.h>
1162306a36Sopenharmony_ci#include <linux/bug.h>
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/rculist.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Check that the data structures for the list manipulations are reasonably
1762306a36Sopenharmony_ci * valid. Failures here indicate memory corruption (and possibly an exploit
1862306a36Sopenharmony_ci * attempt).
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci__list_valid_slowpath
2262306a36Sopenharmony_cibool __list_add_valid_or_report(struct list_head *new, struct list_head *prev,
2362306a36Sopenharmony_ci				struct list_head *next)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	if (CHECK_DATA_CORRUPTION(prev == NULL,
2662306a36Sopenharmony_ci			"list_add corruption. prev is NULL.\n") ||
2762306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(next == NULL,
2862306a36Sopenharmony_ci			"list_add corruption. next is NULL.\n") ||
2962306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(next->prev != prev,
3062306a36Sopenharmony_ci			"list_add corruption. next->prev should be prev (%px), but was %px. (next=%px).\n",
3162306a36Sopenharmony_ci			prev, next->prev, next) ||
3262306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(prev->next != next,
3362306a36Sopenharmony_ci			"list_add corruption. prev->next should be next (%px), but was %px. (prev=%px).\n",
3462306a36Sopenharmony_ci			next, prev->next, prev) ||
3562306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(new == prev || new == next,
3662306a36Sopenharmony_ci			"list_add double add: new=%px, prev=%px, next=%px.\n",
3762306a36Sopenharmony_ci			new, prev, next))
3862306a36Sopenharmony_ci		return false;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	return true;
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ciEXPORT_SYMBOL(__list_add_valid_or_report);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci__list_valid_slowpath
4562306a36Sopenharmony_cibool __list_del_entry_valid_or_report(struct list_head *entry)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	struct list_head *prev, *next;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	prev = entry->prev;
5062306a36Sopenharmony_ci	next = entry->next;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	if (CHECK_DATA_CORRUPTION(next == NULL,
5362306a36Sopenharmony_ci			"list_del corruption, %px->next is NULL\n", entry) ||
5462306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(prev == NULL,
5562306a36Sopenharmony_ci			"list_del corruption, %px->prev is NULL\n", entry) ||
5662306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(next == LIST_POISON1,
5762306a36Sopenharmony_ci			"list_del corruption, %px->next is LIST_POISON1 (%px)\n",
5862306a36Sopenharmony_ci			entry, LIST_POISON1) ||
5962306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(prev == LIST_POISON2,
6062306a36Sopenharmony_ci			"list_del corruption, %px->prev is LIST_POISON2 (%px)\n",
6162306a36Sopenharmony_ci			entry, LIST_POISON2) ||
6262306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(prev->next != entry,
6362306a36Sopenharmony_ci			"list_del corruption. prev->next should be %px, but was %px. (prev=%px)\n",
6462306a36Sopenharmony_ci			entry, prev->next, prev) ||
6562306a36Sopenharmony_ci	    CHECK_DATA_CORRUPTION(next->prev != entry,
6662306a36Sopenharmony_ci			"list_del corruption. next->prev should be %px, but was %px. (next=%px)\n",
6762306a36Sopenharmony_ci			entry, next->prev, next))
6862306a36Sopenharmony_ci		return false;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	return true;
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ciEXPORT_SYMBOL(__list_del_entry_valid_or_report);
73