Skip to content

Commit 026211c

Browse files
keesPeter Zijlstra
authored andcommitted
x86/cfi: Add "debug" option to "cfi=" bootparam
Add "debug" option for "cfi=" bootparam to get details on early CFI initialization steps so future Kees can find breakage easier. Signed-off-by: Kees Cook <kees@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20250904034656.3670313-5-kees@kernel.org
1 parent 9f303a3 commit 026211c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@
624624
bhi: Enable register poisoning to stop speculation
625625
across FineIBT. (Disabled by default.)
626626
warn: Do not enforce CFI checking: warn only.
627+
debug: Report CFI initialization details.
627628

628629
cgroup_disable= [KNL] Disable a particular controller or optional feature
629630
Format: {name of the controller(s) or feature(s) to disable}

arch/x86/kernel/alternative.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { }
11771177
#endif
11781178

11791179
enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT;
1180+
static bool cfi_debug __ro_after_init;
11801181

11811182
#ifdef CONFIG_FINEIBT_BHI
11821183
bool cfi_bhi __ro_after_init = false;
@@ -1259,6 +1260,8 @@ static __init int cfi_parse_cmdline(char *str)
12591260
} else if (!strcmp(str, "off")) {
12601261
cfi_mode = CFI_OFF;
12611262
cfi_rand = false;
1263+
} else if (!strcmp(str, "debug")) {
1264+
cfi_debug = true;
12621265
} else if (!strcmp(str, "kcfi")) {
12631266
cfi_mode = CFI_KCFI;
12641267
} else if (!strcmp(str, "fineibt")) {
@@ -1707,6 +1710,8 @@ static int cfi_rewrite_callers(s32 *start, s32 *end)
17071710
return 0;
17081711
}
17091712

1713+
#define pr_cfi_debug(X...) if (cfi_debug) pr_info(X)
1714+
17101715
static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
17111716
s32 *start_cfi, s32 *end_cfi, bool builtin)
17121717
{
@@ -1734,6 +1739,7 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
17341739
* rewrite them. This disables all CFI. If this succeeds but any of the
17351740
* later stages fails, we're without CFI.
17361741
*/
1742+
pr_cfi_debug("CFI: disabling all indirect call checking\n");
17371743
ret = cfi_disable_callers(start_retpoline, end_retpoline);
17381744
if (ret)
17391745
goto err;
@@ -1744,14 +1750,19 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
17441750
cfi_bpf_hash = cfi_rehash(cfi_bpf_hash);
17451751
cfi_bpf_subprog_hash = cfi_rehash(cfi_bpf_subprog_hash);
17461752
}
1753+
pr_cfi_debug("CFI: cfi_seed: 0x%08x\n", cfi_seed);
17471754

1755+
pr_cfi_debug("CFI: rehashing all preambles\n");
17481756
ret = cfi_rand_preamble(start_cfi, end_cfi);
17491757
if (ret)
17501758
goto err;
17511759

1760+
pr_cfi_debug("CFI: rehashing all indirect calls\n");
17521761
ret = cfi_rand_callers(start_retpoline, end_retpoline);
17531762
if (ret)
17541763
goto err;
1764+
} else {
1765+
pr_cfi_debug("CFI: rehashing disabled\n");
17551766
}
17561767

17571768
switch (cfi_mode) {
@@ -1761,6 +1772,7 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
17611772
return;
17621773

17631774
case CFI_KCFI:
1775+
pr_cfi_debug("CFI: re-enabling all indirect call checking\n");
17641776
ret = cfi_enable_callers(start_retpoline, end_retpoline);
17651777
if (ret)
17661778
goto err;
@@ -1771,17 +1783,20 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
17711783
return;
17721784

17731785
case CFI_FINEIBT:
1786+
pr_cfi_debug("CFI: adding FineIBT to all preambles\n");
17741787
/* place the FineIBT preamble at func()-16 */
17751788
ret = cfi_rewrite_preamble(start_cfi, end_cfi);
17761789
if (ret)
17771790
goto err;
17781791

17791792
/* rewrite the callers to target func()-16 */
1793+
pr_cfi_debug("CFI: rewriting indirect call sites to use FineIBT\n");
17801794
ret = cfi_rewrite_callers(start_retpoline, end_retpoline);
17811795
if (ret)
17821796
goto err;
17831797

17841798
/* now that nobody targets func()+0, remove ENDBR there */
1799+
pr_cfi_debug("CFI: removing old endbr insns\n");
17851800
cfi_rewrite_endbr(start_cfi, end_cfi);
17861801

17871802
if (builtin) {
@@ -2324,6 +2339,7 @@ void __init alternative_instructions(void)
23242339

23252340
__apply_fineibt(__retpoline_sites, __retpoline_sites_end,
23262341
__cfi_sites, __cfi_sites_end, true);
2342+
cfi_debug = false;
23272343

23282344
/*
23292345
* Rewrite the retpolines, must be done before alternatives since

0 commit comments

Comments
 (0)