Skip to content

Commit 69393cb

Browse files
Christopher M. Riedlmpe
authored andcommitted
powerpc/xmon: Restrict when kernel is locked down
Xmon should be either fully or partially disabled depending on the kernel lockdown state. Put xmon into read-only mode for lockdown=integrity and prevent user entry into xmon when lockdown=confidentiality. Xmon checks the lockdown state on every attempted entry: (1) during early xmon'ing (2) when triggered via sysrq (3) when toggled via debugfs (4) when triggered via a previously enabled breakpoint The following lockdown state transitions are handled: (1) lockdown=none -> lockdown=integrity set xmon read-only mode (2) lockdown=none -> lockdown=confidentiality clear all breakpoints, set xmon read-only mode, prevent user re-entry into xmon (3) lockdown=integrity -> lockdown=confidentiality clear all breakpoints, set xmon read-only mode, prevent user re-entry into xmon Suggested-by: Andrew Donnellan <[email protected]> Signed-off-by: Christopher M. Riedl <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 96664de commit 69393cb

File tree

3 files changed

+86
-21
lines changed

3 files changed

+86
-21
lines changed

arch/powerpc/xmon/xmon.c

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/nmi.h>
2626
#include <linux/ctype.h>
2727
#include <linux/highmem.h>
28+
#include <linux/security.h>
2829

2930
#include <asm/debugfs.h>
3031
#include <asm/ptrace.h>
@@ -187,6 +188,8 @@ static void dump_tlb_44x(void);
187188
static void dump_tlb_book3e(void);
188189
#endif
189190

191+
static void clear_all_bpt(void);
192+
190193
#ifdef CONFIG_PPC64
191194
#define REG "%.16lx"
192195
#else
@@ -283,10 +286,38 @@ Commands:\n\
283286
" U show uptime information\n"
284287
" ? help\n"
285288
" # n limit output to n lines per page (for dp, dpa, dl)\n"
286-
" zr reboot\n\
287-
zh halt\n"
289+
" zr reboot\n"
290+
" zh halt\n"
288291
;
289292

293+
#ifdef CONFIG_SECURITY
294+
static bool xmon_is_locked_down(void)
295+
{
296+
static bool lockdown;
297+
298+
if (!lockdown) {
299+
lockdown = !!security_locked_down(LOCKDOWN_XMON_RW);
300+
if (lockdown) {
301+
printf("xmon: Disabled due to kernel lockdown\n");
302+
xmon_is_ro = true;
303+
}
304+
}
305+
306+
if (!xmon_is_ro) {
307+
xmon_is_ro = !!security_locked_down(LOCKDOWN_XMON_WR);
308+
if (xmon_is_ro)
309+
printf("xmon: Read-only due to kernel lockdown\n");
310+
}
311+
312+
return lockdown;
313+
}
314+
#else /* CONFIG_SECURITY */
315+
static inline bool xmon_is_locked_down(void)
316+
{
317+
return false;
318+
}
319+
#endif
320+
290321
static struct pt_regs *xmon_regs;
291322

292323
static inline void sync(void)
@@ -438,7 +469,10 @@ static bool wait_for_other_cpus(int ncpus)
438469

439470
return false;
440471
}
441-
#endif /* CONFIG_SMP */
472+
#else /* CONFIG_SMP */
473+
static inline void get_output_lock(void) {}
474+
static inline void release_output_lock(void) {}
475+
#endif
442476

443477
static inline int unrecoverable_excp(struct pt_regs *regs)
444478
{
@@ -455,6 +489,7 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
455489
int cmd = 0;
456490
struct bpt *bp;
457491
long recurse_jmp[JMP_BUF_LEN];
492+
bool locked_down;
458493
unsigned long offset;
459494
unsigned long flags;
460495
#ifdef CONFIG_SMP
@@ -465,6 +500,8 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
465500
local_irq_save(flags);
466501
hard_irq_disable();
467502

503+
locked_down = xmon_is_locked_down();
504+
468505
if (!fromipi) {
469506
tracing_enabled = tracing_is_on();
470507
tracing_off();
@@ -518,7 +555,8 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
518555

519556
if (!fromipi) {
520557
get_output_lock();
521-
excprint(regs);
558+
if (!locked_down)
559+
excprint(regs);
522560
if (bp) {
523561
printf("cpu 0x%x stopped at breakpoint 0x%tx (",
524562
cpu, BP_NUM(bp));
@@ -570,10 +608,14 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
570608
}
571609
remove_bpts();
572610
disable_surveillance();
573-
/* for breakpoint or single step, print the current instr. */
574-
if (bp || TRAP(regs) == 0xd00)
575-
ppc_inst_dump(regs->nip, 1, 0);
576-
printf("enter ? for help\n");
611+
612+
if (!locked_down) {
613+
/* for breakpoint or single step, print curr insn */
614+
if (bp || TRAP(regs) == 0xd00)
615+
ppc_inst_dump(regs->nip, 1, 0);
616+
printf("enter ? for help\n");
617+
}
618+
577619
mb();
578620
xmon_gate = 1;
579621
barrier();
@@ -597,8 +639,9 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
597639
spin_cpu_relax();
598640
touch_nmi_watchdog();
599641
} else {
600-
cmd = cmds(regs);
601-
if (cmd != 0) {
642+
if (!locked_down)
643+
cmd = cmds(regs);
644+
if (locked_down || cmd != 0) {
602645
/* exiting xmon */
603646
insert_bpts();
604647
xmon_gate = 0;
@@ -635,13 +678,16 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
635678
"can't continue\n");
636679
remove_bpts();
637680
disable_surveillance();
638-
/* for breakpoint or single step, print the current instr. */
639-
if (bp || TRAP(regs) == 0xd00)
640-
ppc_inst_dump(regs->nip, 1, 0);
641-
printf("enter ? for help\n");
681+
if (!locked_down) {
682+
/* for breakpoint or single step, print current insn */
683+
if (bp || TRAP(regs) == 0xd00)
684+
ppc_inst_dump(regs->nip, 1, 0);
685+
printf("enter ? for help\n");
686+
}
642687
}
643688

644-
cmd = cmds(regs);
689+
if (!locked_down)
690+
cmd = cmds(regs);
645691

646692
insert_bpts();
647693
in_xmon = 0;
@@ -670,7 +716,10 @@ static int xmon_core(struct pt_regs *regs, int fromipi)
670716
}
671717
}
672718
#endif
673-
insert_cpu_bpts();
719+
if (locked_down)
720+
clear_all_bpt();
721+
else
722+
insert_cpu_bpts();
674723

675724
touch_nmi_watchdog();
676725
local_irq_restore(flags);
@@ -3768,6 +3817,11 @@ static void xmon_init(int enable)
37683817
#ifdef CONFIG_MAGIC_SYSRQ
37693818
static void sysrq_handle_xmon(int key)
37703819
{
3820+
if (xmon_is_locked_down()) {
3821+
clear_all_bpt();
3822+
xmon_init(0);
3823+
return;
3824+
}
37713825
/* ensure xmon is enabled */
37723826
xmon_init(1);
37733827
debugger(get_irq_regs());
@@ -3789,7 +3843,6 @@ static int __init setup_xmon_sysrq(void)
37893843
device_initcall(setup_xmon_sysrq);
37903844
#endif /* CONFIG_MAGIC_SYSRQ */
37913845

3792-
#ifdef CONFIG_DEBUG_FS
37933846
static void clear_all_bpt(void)
37943847
{
37953848
int i;
@@ -3807,18 +3860,22 @@ static void clear_all_bpt(void)
38073860
iabr = NULL;
38083861
dabr.enabled = 0;
38093862
}
3810-
3811-
printf("xmon: All breakpoints cleared\n");
38123863
}
38133864

3865+
#ifdef CONFIG_DEBUG_FS
38143866
static int xmon_dbgfs_set(void *data, u64 val)
38153867
{
38163868
xmon_on = !!val;
38173869
xmon_init(xmon_on);
38183870

38193871
/* make sure all breakpoints removed when disabling */
3820-
if (!xmon_on)
3872+
if (!xmon_on) {
38213873
clear_all_bpt();
3874+
get_output_lock();
3875+
printf("xmon: All breakpoints cleared\n");
3876+
release_output_lock();
3877+
}
3878+
38223879
return 0;
38233880
}
38243881

@@ -3844,7 +3901,11 @@ static int xmon_early __initdata;
38443901

38453902
static int __init early_parse_xmon(char *p)
38463903
{
3847-
if (!p || strncmp(p, "early", 5) == 0) {
3904+
if (xmon_is_locked_down()) {
3905+
xmon_init(0);
3906+
xmon_early = 0;
3907+
xmon_on = 0;
3908+
} else if (!p || strncmp(p, "early", 5) == 0) {
38483909
/* just "xmon" is equivalent to "xmon=early" */
38493910
xmon_init(1);
38503911
xmon_early = 1;

include/linux/security.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ enum lockdown_reason {
116116
LOCKDOWN_MODULE_PARAMETERS,
117117
LOCKDOWN_MMIOTRACE,
118118
LOCKDOWN_DEBUGFS,
119+
LOCKDOWN_XMON_WR,
119120
LOCKDOWN_INTEGRITY_MAX,
120121
LOCKDOWN_KCORE,
121122
LOCKDOWN_KPROBES,
122123
LOCKDOWN_BPF_READ,
123124
LOCKDOWN_PERF,
124125
LOCKDOWN_TRACEFS,
126+
LOCKDOWN_XMON_RW,
125127
LOCKDOWN_CONFIDENTIALITY_MAX,
126128
};
127129

security/lockdown/lockdown.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ static const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
3131
[LOCKDOWN_MODULE_PARAMETERS] = "unsafe module parameters",
3232
[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
3333
[LOCKDOWN_DEBUGFS] = "debugfs access",
34+
[LOCKDOWN_XMON_WR] = "xmon write access",
3435
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
3536
[LOCKDOWN_KCORE] = "/proc/kcore access",
3637
[LOCKDOWN_KPROBES] = "use of kprobes",
3738
[LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
3839
[LOCKDOWN_PERF] = "unsafe use of perf",
3940
[LOCKDOWN_TRACEFS] = "use of tracefs",
41+
[LOCKDOWN_XMON_RW] = "xmon read and write access",
4042
[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
4143
};
4244

0 commit comments

Comments
 (0)