Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 32518aee authored by Sami Tolvanen's avatar Sami Tolvanen Committed by Alistair Delva
Browse files

FROMLIST: scs: add support for stack usage debugging

Implements CONFIG_DEBUG_STACK_USAGE for shadow stacks. When enabled,
also prints out the highest shadow stack usage per process.

Bug: 145210207
Change-Id: I4c085b51e1432e8d52e54126ffd8bf7b6e35b529
(am from https://lore.kernel.org/patchwork/patch/1149056/

)
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarSami Tolvanen <samitolvanen@google.com>
parent 9c265248
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -184,6 +184,44 @@ int scs_prepare(struct task_struct *tsk, int node)
	return 0;
}

#ifdef CONFIG_DEBUG_STACK_USAGE
static inline unsigned long scs_used(struct task_struct *tsk)
{
	unsigned long *p = __scs_base(tsk);
	unsigned long *end = scs_magic(p);
	unsigned long s = (unsigned long)p;

	while (p < end && READ_ONCE_NOCHECK(*p))
		p++;

	return (unsigned long)p - s;
}

static void scs_check_usage(struct task_struct *tsk)
{
	static DEFINE_SPINLOCK(lock);
	static unsigned long highest;
	unsigned long used = scs_used(tsk);

	if (used <= highest)
		return;

	spin_lock(&lock);

	if (used > highest) {
		pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
			tsk->comm, task_pid_nr(tsk), used);
		highest = used;
	}

	spin_unlock(&lock);
}
#else
static inline void scs_check_usage(struct task_struct *tsk)
{
}
#endif

bool scs_corrupted(struct task_struct *tsk)
{
	unsigned long *magic = scs_magic(__scs_base(tsk));
@@ -200,6 +238,7 @@ void scs_release(struct task_struct *tsk)
		return;

	WARN_ON(scs_corrupted(tsk));
	scs_check_usage(tsk);

	scs_account(tsk, -1);
	task_set_scs(tsk, NULL);