Skip to content

Commit 10248dc

Browse files
chleroympe
authored andcommitted
powerpc/44x: Implement Kernel Userspace Exec Protection (KUEP)
Powerpc 44x has two bits for exec protection in TLBs: one for user (UX) and one for superviser (SX). Clear SX on user pages in TLB miss handlers to provide KUEP. Signed-off-by: Christophe Leroy <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/169310e08152aa1d96c979770291d165ec6896ae.1622616032.git.christophe.leroy@csgroup.eu
1 parent c0ca0fe commit 10248dc

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

arch/powerpc/include/asm/nohash/32/mmu-44x.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ typedef struct {
113113

114114
/* patch sites */
115115
extern s32 patch__tlb_44x_hwater_D, patch__tlb_44x_hwater_I;
116+
extern s32 patch__tlb_44x_kuep, patch__tlb_47x_kuep;
116117

117118
#endif /* !__ASSEMBLY__ */
118119

arch/powerpc/kernel/head_44x.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@ finish_tlb_load_44x:
532532
andi. r10,r12,_PAGE_USER /* User page ? */
533533
beq 1f /* nope, leave U bits empty */
534534
rlwimi r11,r11,3,26,28 /* yes, copy S bits to U */
535+
#ifdef CONFIG_PPC_KUEP
536+
0: rlwinm r11,r11,0,~PPC44x_TLB_SX /* Clear SX if User page */
537+
patch_site 0b, patch__tlb_44x_kuep
538+
#endif
535539
1: tlbwe r11,r13,PPC44x_TLB_ATTRIB /* Write ATTRIB */
536540

537541
/* Done...restore registers and get out of here.
@@ -743,6 +747,10 @@ finish_tlb_load_47x:
743747
andi. r10,r12,_PAGE_USER /* User page ? */
744748
beq 1f /* nope, leave U bits empty */
745749
rlwimi r11,r11,3,26,28 /* yes, copy S bits to U */
750+
#ifdef CONFIG_PPC_KUEP
751+
0: rlwinm r11,r11,0,~PPC47x_TLB2_SX /* Clear SX if User page */
752+
patch_site 0b, patch__tlb_47x_kuep
753+
#endif
746754
1: tlbwe r11,r13,2
747755

748756
/* Done...restore registers and get out of here.

arch/powerpc/mm/nohash/44x.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,16 @@ void __init mmu_init_secondary(int cpu)
239239
}
240240
}
241241
#endif /* CONFIG_SMP */
242+
243+
#ifdef CONFIG_PPC_KUEP
244+
void __init setup_kuep(bool disabled)
245+
{
246+
if (disabled)
247+
patch_instruction_site(&patch__tlb_44x_kuep, ppc_inst(PPC_RAW_NOP()));
248+
else
249+
pr_info("Activating Kernel Userspace Execution Prevention\n");
250+
251+
if (IS_ENABLED(CONFIG_PPC_47x) && disabled)
252+
patch_instruction_site(&patch__tlb_47x_kuep, ppc_inst(PPC_RAW_NOP()));
253+
}
254+
#endif

arch/powerpc/platforms/Kconfig.cputype

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ config 44x
6161
select 4xx_SOC
6262
select HAVE_PCI
6363
select PHYS_64BIT
64+
select PPC_HAVE_KUEP
6465

6566
endchoice
6667

0 commit comments

Comments
 (0)