162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * This code tests that the current task stack is properly erased (filled
462306a36Sopenharmony_ci * with STACKLEAK_POISON).
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Authors:
762306a36Sopenharmony_ci *   Alexander Popov <alex.popov@linux.com>
862306a36Sopenharmony_ci *   Tycho Andersen <tycho@tycho.ws>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "lkdtm.h"
1262306a36Sopenharmony_ci#include <linux/stackleak.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#if defined(CONFIG_GCC_PLUGIN_STACKLEAK)
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Check that stackleak tracks the lowest stack pointer and erases the stack
1762306a36Sopenharmony_ci * below this as expected.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * To prevent the lowest stack pointer changing during the test, IRQs are
2062306a36Sopenharmony_ci * masked and instrumentation of this function is disabled. We assume that the
2162306a36Sopenharmony_ci * compiler will create a fixed-size stack frame for this function.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * Any non-inlined function may make further use of the stack, altering the
2462306a36Sopenharmony_ci * lowest stack pointer and/or clobbering poison values. To avoid spurious
2562306a36Sopenharmony_ci * failures we must avoid printing until the end of the test or have already
2662306a36Sopenharmony_ci * encountered a failure condition.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistatic void noinstr check_stackleak_irqoff(void)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	const unsigned long task_stack_base = (unsigned long)task_stack_page(current);
3162306a36Sopenharmony_ci	const unsigned long task_stack_low = stackleak_task_low_bound(current);
3262306a36Sopenharmony_ci	const unsigned long task_stack_high = stackleak_task_high_bound(current);
3362306a36Sopenharmony_ci	const unsigned long current_sp = current_stack_pointer;
3462306a36Sopenharmony_ci	const unsigned long lowest_sp = current->lowest_stack;
3562306a36Sopenharmony_ci	unsigned long untracked_high;
3662306a36Sopenharmony_ci	unsigned long poison_high, poison_low;
3762306a36Sopenharmony_ci	bool test_failed = false;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	/*
4062306a36Sopenharmony_ci	 * Check that the current and lowest recorded stack pointer values fall
4162306a36Sopenharmony_ci	 * within the expected task stack boundaries. These tests should never
4262306a36Sopenharmony_ci	 * fail unless the boundaries are incorrect or we're clobbering the
4362306a36Sopenharmony_ci	 * STACK_END_MAGIC, and in either casee something is seriously wrong.
4462306a36Sopenharmony_ci	 */
4562306a36Sopenharmony_ci	if (current_sp < task_stack_low || current_sp >= task_stack_high) {
4662306a36Sopenharmony_ci		instrumentation_begin();
4762306a36Sopenharmony_ci		pr_err("FAIL: current_stack_pointer (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
4862306a36Sopenharmony_ci		       current_sp, task_stack_low, task_stack_high - 1);
4962306a36Sopenharmony_ci		test_failed = true;
5062306a36Sopenharmony_ci		goto out;
5162306a36Sopenharmony_ci	}
5262306a36Sopenharmony_ci	if (lowest_sp < task_stack_low || lowest_sp >= task_stack_high) {
5362306a36Sopenharmony_ci		instrumentation_begin();
5462306a36Sopenharmony_ci		pr_err("FAIL: current->lowest_stack (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
5562306a36Sopenharmony_ci		       lowest_sp, task_stack_low, task_stack_high - 1);
5662306a36Sopenharmony_ci		test_failed = true;
5762306a36Sopenharmony_ci		goto out;
5862306a36Sopenharmony_ci	}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/*
6162306a36Sopenharmony_ci	 * Depending on what has run prior to this test, the lowest recorded
6262306a36Sopenharmony_ci	 * stack pointer could be above or below the current stack pointer.
6362306a36Sopenharmony_ci	 * Start from the lowest of the two.
6462306a36Sopenharmony_ci	 *
6562306a36Sopenharmony_ci	 * Poison values are naturally-aligned unsigned longs. As the current
6662306a36Sopenharmony_ci	 * stack pointer might not be sufficiently aligned, we must align
6762306a36Sopenharmony_ci	 * downwards to find the lowest known stack pointer value. This is the
6862306a36Sopenharmony_ci	 * high boundary for a portion of the stack which may have been used
6962306a36Sopenharmony_ci	 * without being tracked, and has to be scanned for poison.
7062306a36Sopenharmony_ci	 */
7162306a36Sopenharmony_ci	untracked_high = min(current_sp, lowest_sp);
7262306a36Sopenharmony_ci	untracked_high = ALIGN_DOWN(untracked_high, sizeof(unsigned long));
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	/*
7562306a36Sopenharmony_ci	 * Find the top of the poison in the same way as the erasing code.
7662306a36Sopenharmony_ci	 */
7762306a36Sopenharmony_ci	poison_high = stackleak_find_top_of_poison(task_stack_low, untracked_high);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/*
8062306a36Sopenharmony_ci	 * Check whether the poisoned portion of the stack (if any) consists
8162306a36Sopenharmony_ci	 * entirely of poison. This verifies the entries that
8262306a36Sopenharmony_ci	 * stackleak_find_top_of_poison() should have checked.
8362306a36Sopenharmony_ci	 */
8462306a36Sopenharmony_ci	poison_low = poison_high;
8562306a36Sopenharmony_ci	while (poison_low > task_stack_low) {
8662306a36Sopenharmony_ci		poison_low -= sizeof(unsigned long);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		if (*(unsigned long *)poison_low == STACKLEAK_POISON)
8962306a36Sopenharmony_ci			continue;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci		instrumentation_begin();
9262306a36Sopenharmony_ci		pr_err("FAIL: non-poison value %lu bytes below poison boundary: 0x%lx\n",
9362306a36Sopenharmony_ci		       poison_high - poison_low, *(unsigned long *)poison_low);
9462306a36Sopenharmony_ci		test_failed = true;
9562306a36Sopenharmony_ci		goto out;
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	instrumentation_begin();
9962306a36Sopenharmony_ci	pr_info("stackleak stack usage:\n"
10062306a36Sopenharmony_ci		"  high offset: %lu bytes\n"
10162306a36Sopenharmony_ci		"  current:     %lu bytes\n"
10262306a36Sopenharmony_ci		"  lowest:      %lu bytes\n"
10362306a36Sopenharmony_ci		"  tracked:     %lu bytes\n"
10462306a36Sopenharmony_ci		"  untracked:   %lu bytes\n"
10562306a36Sopenharmony_ci		"  poisoned:    %lu bytes\n"
10662306a36Sopenharmony_ci		"  low offset:  %lu bytes\n",
10762306a36Sopenharmony_ci		task_stack_base + THREAD_SIZE - task_stack_high,
10862306a36Sopenharmony_ci		task_stack_high - current_sp,
10962306a36Sopenharmony_ci		task_stack_high - lowest_sp,
11062306a36Sopenharmony_ci		task_stack_high - untracked_high,
11162306a36Sopenharmony_ci		untracked_high - poison_high,
11262306a36Sopenharmony_ci		poison_high - task_stack_low,
11362306a36Sopenharmony_ci		task_stack_low - task_stack_base);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciout:
11662306a36Sopenharmony_ci	if (test_failed) {
11762306a36Sopenharmony_ci		pr_err("FAIL: the thread stack is NOT properly erased!\n");
11862306a36Sopenharmony_ci	} else {
11962306a36Sopenharmony_ci		pr_info("OK: the rest of the thread stack is properly erased\n");
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci	instrumentation_end();
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic void lkdtm_STACKLEAK_ERASING(void)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	unsigned long flags;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	local_irq_save(flags);
12962306a36Sopenharmony_ci	check_stackleak_irqoff();
13062306a36Sopenharmony_ci	local_irq_restore(flags);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci#else /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
13362306a36Sopenharmony_cistatic void lkdtm_STACKLEAK_ERASING(void)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_HAVE_ARCH_STACKLEAK)) {
13662306a36Sopenharmony_ci		pr_err("XFAIL: stackleak is not enabled (CONFIG_GCC_PLUGIN_STACKLEAK=n)\n");
13762306a36Sopenharmony_ci	} else {
13862306a36Sopenharmony_ci		pr_err("XFAIL: stackleak is not supported on this arch (HAVE_ARCH_STACKLEAK=n)\n");
13962306a36Sopenharmony_ci	}
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci#endif /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistatic struct crashtype crashtypes[] = {
14462306a36Sopenharmony_ci	CRASHTYPE(STACKLEAK_ERASING),
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_cistruct crashtype_category stackleak_crashtypes = {
14862306a36Sopenharmony_ci	.crashtypes = crashtypes,
14962306a36Sopenharmony_ci	.len	    = ARRAY_SIZE(crashtypes),
15062306a36Sopenharmony_ci};
151