Skip to content

Commit 5bbaf9d

Browse files
samitolvanenwilldeacon
authored andcommitted
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. Signed-off-by: Sami Tolvanen <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Will Deacon <[email protected]> [will: rewrote most of scs_check_usage()] Signed-off-by: Will Deacon <[email protected]>
1 parent 628d06a commit 5bbaf9d

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

kernel/scs.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,37 @@ int scs_prepare(struct task_struct *tsk, int node)
6363
task_scs(tsk) = s;
6464
task_scs_offset(tsk) = 0;
6565
scs_account(tsk, 1);
66-
6766
return 0;
6867
}
6968

69+
static void scs_check_usage(struct task_struct *tsk)
70+
{
71+
static unsigned long highest;
72+
73+
unsigned long *p, prev, curr = highest, used = 0;
74+
75+
if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
76+
return;
77+
78+
for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
79+
if (!READ_ONCE_NOCHECK(*p))
80+
break;
81+
used++;
82+
}
83+
84+
while (used > curr) {
85+
prev = cmpxchg_relaxed(&highest, curr, used);
86+
87+
if (prev == curr) {
88+
pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
89+
tsk->comm, task_pid_nr(tsk), used);
90+
break;
91+
}
92+
93+
curr = prev;
94+
}
95+
}
96+
7097
void scs_release(struct task_struct *tsk)
7198
{
7299
void *s = task_scs(tsk);
@@ -75,6 +102,7 @@ void scs_release(struct task_struct *tsk)
75102
return;
76103

77104
WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");
105+
scs_check_usage(tsk);
78106
scs_account(tsk, -1);
79107
scs_free(s);
80108
}

0 commit comments

Comments
 (0)