Skip to content

Commit 1613252

Browse files
chleroympe
authored andcommitted
powerpc/32s: Rework Kernel Userspace Access Protection
On book3s/32, KUAP is provided by toggling Ks bit in segment registers. One segment register addresses 256M of virtual memory. At the time being, KUAP implements a complex logic to apply the unlock/lock on the exact number of segments covering the user range to access, with saving the boundaries of the range of segments in a member of thread struct. But most if not all user accesses are within a single segment. Rework KUAP with a different approach: - Open only one segment, the one corresponding to the starting address of the range to be accessed. - If a second segment is involved, it will generate a page fault. The segment will then be open by the page fault handler. The kuap member of thread struct will now contain: - The start address of the current on going user access, that will be used to know which segment to lock at the end of the user access. - ~0 when no user access is open - ~1 when additionnal segments are opened by a page fault. Then, at lock time - When only one segment is open, close it. - When several segments are open, close all user segments. Almost 100% of the time, only one segment will be involved. In interrupts, inline the function that unlock/lock all segments, because not inlining them implies a lot of register save/restore. With the patch, writing value 128 in userspace in perf_copy_attr() is done with 16 instructions: 3890: 93 82 04 dc stw r28,1244(r2) 3894: 7d 20 e5 26 mfsrin r9,r28 3898: 55 29 00 80 rlwinm r9,r9,0,2,0 389c: 7d 20 e1 e4 mtsrin r9,r28 38a0: 4c 00 01 2c isync 38a4: 39 20 00 80 li r9,128 38a8: 91 3c 00 00 stw r9,0(r28) 38ac: 81 42 04 dc lwz r10,1244(r2) 38b0: 39 00 ff ff li r8,-1 38b4: 91 02 04 dc stw r8,1244(r2) 38b8: 2c 0a ff fe cmpwi r10,-2 38bc: 41 82 00 88 beq 3944 <perf_copy_attr+0x36c> 38c0: 7d 20 55 26 mfsrin r9,r10 38c4: 65 29 40 00 oris r9,r9,16384 38c8: 7d 20 51 e4 mtsrin r9,r10 38cc: 4c 00 01 2c isync ... 3944: 48 00 00 01 bl 3944 <perf_copy_attr+0x36c> 3944: R_PPC_REL24 kuap_lock_all_ool Before the patch it was 118 instructions. In reality only 42 are executed in most cases, but GCC is not able to see that a properly aligned user access cannot involve more than one segment. 5060: 39 1d 00 04 addi r8,r29,4 5064: 3d 20 b0 00 lis r9,-20480 5068: 7c 08 48 40 cmplw r8,r9 506c: 40 81 00 08 ble 5074 <perf_copy_attr+0x2cc> 5070: 3d 00 b0 00 lis r8,-20480 5074: 39 28 ff ff addi r9,r8,-1 5078: 57 aa 00 06 rlwinm r10,r29,0,0,3 507c: 55 29 27 3e rlwinm r9,r9,4,28,31 5080: 39 29 00 01 addi r9,r9,1 5084: 7d 29 53 78 or r9,r9,r10 5088: 91 22 04 dc stw r9,1244(r2) 508c: 7d 20 ed 26 mfsrin r9,r29 5090: 55 29 00 80 rlwinm r9,r9,0,2,0 5094: 7c 08 50 40 cmplw r8,r10 5098: 40 81 00 c0 ble 5158 <perf_copy_attr+0x3b0> 509c: 7d 46 50 f8 not r6,r10 50a0: 7c c6 42 14 add r6,r6,r8 50a4: 54 c6 27 be rlwinm r6,r6,4,30,31 50a8: 7d 20 51 e4 mtsrin r9,r10 50ac: 3c ea 10 00 addis r7,r10,4096 50b0: 39 29 01 11 addi r9,r9,273 50b4: 7f 88 38 40 cmplw cr7,r8,r7 50b8: 55 29 02 06 rlwinm r9,r9,0,8,3 50bc: 40 9d 00 9c ble cr7,5158 <perf_copy_attr+0x3b0> 50c0: 2f 86 00 00 cmpwi cr7,r6,0 50c4: 41 9e 00 4c beq cr7,5110 <perf_copy_attr+0x368> 50c8: 2f 86 00 01 cmpwi cr7,r6,1 50cc: 41 9e 00 2c beq cr7,50f8 <perf_copy_attr+0x350> 50d0: 2f 86 00 02 cmpwi cr7,r6,2 50d4: 41 9e 00 14 beq cr7,50e8 <perf_copy_attr+0x340> 50d8: 7d 20 39 e4 mtsrin r9,r7 50dc: 39 29 01 11 addi r9,r9,273 50e0: 3c e7 10 00 addis r7,r7,4096 50e4: 55 29 02 06 rlwinm r9,r9,0,8,3 50e8: 7d 20 39 e4 mtsrin r9,r7 50ec: 39 29 01 11 addi r9,r9,273 50f0: 3c e7 10 00 addis r7,r7,4096 50f4: 55 29 02 06 rlwinm r9,r9,0,8,3 50f8: 7d 20 39 e4 mtsrin r9,r7 50fc: 3c e7 10 00 addis r7,r7,4096 5100: 39 29 01 11 addi r9,r9,273 5104: 7f 88 38 40 cmplw cr7,r8,r7 5108: 55 29 02 06 rlwinm r9,r9,0,8,3 510c: 40 9d 00 4c ble cr7,5158 <perf_copy_attr+0x3b0> 5110: 7d 20 39 e4 mtsrin r9,r7 5114: 39 29 01 11 addi r9,r9,273 5118: 3c c7 10 00 addis r6,r7,4096 511c: 55 29 02 06 rlwinm r9,r9,0,8,3 5120: 7d 20 31 e4 mtsrin r9,r6 5124: 39 29 01 11 addi r9,r9,273 5128: 3c c6 10 00 addis r6,r6,4096 512c: 55 29 02 06 rlwinm r9,r9,0,8,3 5130: 7d 20 31 e4 mtsrin r9,r6 5134: 39 29 01 11 addi r9,r9,273 5138: 3c c7 30 00 addis r6,r7,12288 513c: 55 29 02 06 rlwinm r9,r9,0,8,3 5140: 7d 20 31 e4 mtsrin r9,r6 5144: 3c e7 40 00 addis r7,r7,16384 5148: 39 29 01 11 addi r9,r9,273 514c: 7f 88 38 40 cmplw cr7,r8,r7 5150: 55 29 02 06 rlwinm r9,r9,0,8,3 5154: 41 9d ff bc bgt cr7,5110 <perf_copy_attr+0x368> 5158: 4c 00 01 2c isync 515c: 39 20 00 80 li r9,128 5160: 91 3d 00 00 stw r9,0(r29) 5164: 38 e0 00 00 li r7,0 5168: 90 e2 04 dc stw r7,1244(r2) 516c: 7d 20 ed 26 mfsrin r9,r29 5170: 65 29 40 00 oris r9,r9,16384 5174: 40 81 00 c0 ble 5234 <perf_copy_attr+0x48c> 5178: 7d 47 50 f8 not r7,r10 517c: 7c e7 42 14 add r7,r7,r8 5180: 54 e7 27 be rlwinm r7,r7,4,30,31 5184: 7d 20 51 e4 mtsrin r9,r10 5188: 3d 4a 10 00 addis r10,r10,4096 518c: 39 29 01 11 addi r9,r9,273 5190: 7c 08 50 40 cmplw r8,r10 5194: 55 29 02 06 rlwinm r9,r9,0,8,3 5198: 40 81 00 9c ble 5234 <perf_copy_attr+0x48c> 519c: 2c 07 00 00 cmpwi r7,0 51a0: 41 82 00 4c beq 51ec <perf_copy_attr+0x444> 51a4: 2c 07 00 01 cmpwi r7,1 51a8: 41 82 00 2c beq 51d4 <perf_copy_attr+0x42c> 51ac: 2c 07 00 02 cmpwi r7,2 51b0: 41 82 00 14 beq 51c4 <perf_copy_attr+0x41c> 51b4: 7d 20 51 e4 mtsrin r9,r10 51b8: 39 29 01 11 addi r9,r9,273 51bc: 3d 4a 10 00 addis r10,r10,4096 51c0: 55 29 02 06 rlwinm r9,r9,0,8,3 51c4: 7d 20 51 e4 mtsrin r9,r10 51c8: 39 29 01 11 addi r9,r9,273 51cc: 3d 4a 10 00 addis r10,r10,4096 51d0: 55 29 02 06 rlwinm r9,r9,0,8,3 51d4: 7d 20 51 e4 mtsrin r9,r10 51d8: 3d 4a 10 00 addis r10,r10,4096 51dc: 39 29 01 11 addi r9,r9,273 51e0: 7c 08 50 40 cmplw r8,r10 51e4: 55 29 02 06 rlwinm r9,r9,0,8,3 51e8: 40 81 00 4c ble 5234 <perf_copy_attr+0x48c> 51ec: 7d 20 51 e4 mtsrin r9,r10 51f0: 39 29 01 11 addi r9,r9,273 51f4: 3c ea 10 00 addis r7,r10,4096 51f8: 55 29 02 06 rlwinm r9,r9,0,8,3 51fc: 7d 20 39 e4 mtsrin r9,r7 5200: 39 29 01 11 addi r9,r9,273 5204: 3c e7 10 00 addis r7,r7,4096 5208: 55 29 02 06 rlwinm r9,r9,0,8,3 520c: 7d 20 39 e4 mtsrin r9,r7 5210: 39 29 01 11 addi r9,r9,273 5214: 3c ea 30 00 addis r7,r10,12288 5218: 55 29 02 06 rlwinm r9,r9,0,8,3 521c: 7d 20 39 e4 mtsrin r9,r7 5220: 3d 4a 40 00 addis r10,r10,16384 5224: 39 29 01 11 addi r9,r9,273 5228: 7c 08 50 40 cmplw r8,r10 522c: 55 29 02 06 rlwinm r9,r9,0,8,3 5230: 41 81 ff bc bgt 51ec <perf_copy_attr+0x444> 5234: 4c 00 01 2c isync Signed-off-by: Christophe Leroy <[email protected]> [mpe: Export the ool handlers to fix build errors] Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/d9121f96a7c4302946839a0771f5d1daeeb6968c.1622708530.git.christophe.leroy@csgroup.eu
1 parent 6b4d630 commit 1613252

File tree

4 files changed

+104
-73
lines changed

4 files changed

+104
-73
lines changed

arch/powerpc/include/asm/book3s/32/kup.h

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,69 @@ static inline void kuep_unlock(void)
4242

4343
#include <linux/sched.h>
4444

45-
static inline void kuap_update_sr(u32 sr, u32 addr, u32 end)
46-
{
47-
addr &= 0xf0000000; /* align addr to start of segment */
48-
barrier(); /* make sure thread.kuap is updated before playing with SRs */
49-
for (;;) {
50-
mtsr(sr, addr);
51-
addr += 0x10000000; /* address of next segment */
52-
if (addr >= end)
53-
break;
54-
sr += 0x111; /* next VSID */
55-
sr &= 0xf0ffffff; /* clear VSID overflow */
56-
}
45+
#define KUAP_NONE (~0UL)
46+
#define KUAP_ALL (~1UL)
47+
48+
static inline void kuap_lock_one(unsigned long addr)
49+
{
50+
mtsr(mfsr(addr) | SR_KS, addr);
51+
isync(); /* Context sync required after mtsr() */
52+
}
53+
54+
static inline void kuap_unlock_one(unsigned long addr)
55+
{
56+
mtsr(mfsr(addr) & ~SR_KS, addr);
57+
isync(); /* Context sync required after mtsr() */
58+
}
59+
60+
static inline void kuap_lock_all(void)
61+
{
62+
update_user_segments(mfsr(0) | SR_KS);
5763
isync(); /* Context sync required after mtsr() */
5864
}
5965

66+
static inline void kuap_unlock_all(void)
67+
{
68+
update_user_segments(mfsr(0) & ~SR_KS);
69+
isync(); /* Context sync required after mtsr() */
70+
}
71+
72+
void kuap_lock_all_ool(void);
73+
void kuap_unlock_all_ool(void);
74+
75+
static inline void kuap_lock(unsigned long addr, bool ool)
76+
{
77+
if (likely(addr != KUAP_ALL))
78+
kuap_lock_one(addr);
79+
else if (!ool)
80+
kuap_lock_all();
81+
else
82+
kuap_lock_all_ool();
83+
}
84+
85+
static inline void kuap_unlock(unsigned long addr, bool ool)
86+
{
87+
if (likely(addr != KUAP_ALL))
88+
kuap_unlock_one(addr);
89+
else if (!ool)
90+
kuap_unlock_all();
91+
else
92+
kuap_unlock_all_ool();
93+
}
94+
6095
static inline void kuap_save_and_lock(struct pt_regs *regs)
6196
{
6297
unsigned long kuap = current->thread.kuap;
63-
u32 addr = kuap & 0xf0000000;
64-
u32 end = kuap << 28;
6598

6699
if (kuap_is_disabled())
67100
return;
68101

69102
regs->kuap = kuap;
70-
if (unlikely(!kuap))
103+
if (unlikely(kuap == KUAP_NONE))
71104
return;
72105

73-
current->thread.kuap = 0;
74-
kuap_update_sr(mfsr(addr) | SR_KS, addr, end); /* Set Ks */
106+
current->thread.kuap = KUAP_NONE;
107+
kuap_lock(kuap, false);
75108
}
76109

77110
static inline void kuap_user_restore(struct pt_regs *regs)
@@ -80,28 +113,22 @@ static inline void kuap_user_restore(struct pt_regs *regs)
80113

81114
static inline void kuap_kernel_restore(struct pt_regs *regs, unsigned long kuap)
82115
{
83-
u32 addr = regs->kuap & 0xf0000000;
84-
u32 end = regs->kuap << 28;
85-
86116
if (kuap_is_disabled())
87117
return;
88118

89119
current->thread.kuap = regs->kuap;
90120

91-
if (unlikely(regs->kuap == kuap))
92-
return;
93-
94-
kuap_update_sr(mfsr(addr) & ~SR_KS, addr, end); /* Clear Ks */
121+
kuap_unlock(regs->kuap, false);
95122
}
96123

97124
static inline unsigned long kuap_get_and_assert_locked(void)
98125
{
99126
unsigned long kuap = current->thread.kuap;
100127

101128
if (kuap_is_disabled())
102-
return 0;
129+
return KUAP_NONE;
103130

104-
WARN_ON_ONCE(IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && kuap != 0);
131+
WARN_ON_ONCE(IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && kuap != KUAP_NONE);
105132

106133
return kuap;
107134
}
@@ -114,8 +141,6 @@ static inline void kuap_assert_locked(void)
114141
static __always_inline void allow_user_access(void __user *to, const void __user *from,
115142
u32 size, unsigned long dir)
116143
{
117-
u32 addr, end;
118-
119144
if (kuap_is_disabled())
120145
return;
121146

@@ -125,88 +150,71 @@ static __always_inline void allow_user_access(void __user *to, const void __user
125150
if (!(dir & KUAP_WRITE))
126151
return;
127152

128-
addr = (__force u32)to;
129-
130-
if (unlikely(addr >= TASK_SIZE || !size))
131-
return;
132-
133-
end = min(addr + size, TASK_SIZE);
134-
135-
current->thread.kuap = (addr & 0xf0000000) | ((((end - 1) >> 28) + 1) & 0xf);
136-
kuap_update_sr(mfsr(addr) & ~SR_KS, addr, end); /* Clear Ks */
153+
current->thread.kuap = (__force u32)to;
154+
kuap_unlock_one((__force u32)to);
137155
}
138156

139157
static __always_inline void prevent_user_access(void __user *to, const void __user *from,
140158
u32 size, unsigned long dir)
141159
{
142-
u32 addr, end;
160+
u32 kuap = current->thread.kuap;
143161

144162
if (kuap_is_disabled())
145163
return;
146164

147165
BUILD_BUG_ON(!__builtin_constant_p(dir));
148166

149-
if (dir & KUAP_CURRENT_WRITE) {
150-
u32 kuap = current->thread.kuap;
151-
152-
if (unlikely(!kuap))
153-
return;
154-
155-
addr = kuap & 0xf0000000;
156-
end = kuap << 28;
157-
} else if (dir & KUAP_WRITE) {
158-
addr = (__force u32)to;
159-
end = min(addr + size, TASK_SIZE);
160-
161-
if (unlikely(addr >= TASK_SIZE || !size))
162-
return;
163-
} else {
167+
if (!(dir & KUAP_WRITE))
164168
return;
165-
}
166169

167-
current->thread.kuap = 0;
168-
kuap_update_sr(mfsr(addr) | SR_KS, addr, end); /* set Ks */
170+
current->thread.kuap = KUAP_NONE;
171+
kuap_lock(kuap, true);
169172
}
170173

171174
static inline unsigned long prevent_user_access_return(void)
172175
{
173176
unsigned long flags = current->thread.kuap;
174-
unsigned long addr = flags & 0xf0000000;
175-
unsigned long end = flags << 28;
176-
void __user *to = (__force void __user *)addr;
177177

178178
if (kuap_is_disabled())
179-
return 0;
179+
return KUAP_NONE;
180180

181-
if (flags)
182-
prevent_user_access(to, to, end - addr, KUAP_READ_WRITE);
181+
if (flags != KUAP_NONE) {
182+
current->thread.kuap = KUAP_NONE;
183+
kuap_lock(flags, true);
184+
}
183185

184186
return flags;
185187
}
186188

187189
static inline void restore_user_access(unsigned long flags)
188190
{
189-
unsigned long addr = flags & 0xf0000000;
190-
unsigned long end = flags << 28;
191-
void __user *to = (__force void __user *)addr;
192-
193191
if (kuap_is_disabled())
194192
return;
195193

196-
if (flags)
197-
allow_user_access(to, to, end - addr, KUAP_READ_WRITE);
194+
if (flags != KUAP_NONE) {
195+
current->thread.kuap = flags;
196+
kuap_unlock(flags, true);
197+
}
198198
}
199199

200200
static inline bool
201201
bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
202202
{
203-
unsigned long begin = regs->kuap & 0xf0000000;
204-
unsigned long end = regs->kuap << 28;
203+
unsigned long kuap = regs->kuap;
205204

206205
if (kuap_is_disabled())
207206
return false;
208207

209-
return is_write && (address < begin || address >= end);
208+
if (!is_write || kuap == KUAP_ALL)
209+
return false;
210+
if (kuap == KUAP_NONE)
211+
return true;
212+
213+
/* If faulting address doesn't match unlocked segment, unlock all */
214+
if ((kuap ^ address) & 0xf0000000)
215+
regs->kuap = KUAP_ALL;
216+
217+
return false;
210218
}
211219

212220
#endif /* CONFIG_PPC_KUAP */

arch/powerpc/include/asm/processor.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,15 @@ struct thread_struct {
276276
#define SPEFSCR_INIT
277277
#endif
278278

279-
#ifdef CONFIG_PPC32
279+
#if defined(CONFIG_PPC_BOOK3S_32) && defined(CONFIG_PPC_KUAP)
280+
#define INIT_THREAD { \
281+
.ksp = INIT_SP, \
282+
.pgdir = swapper_pg_dir, \
283+
.kuap = ~0UL, /* KUAP_NONE */ \
284+
.fpexc_mode = MSR_FE0 | MSR_FE1, \
285+
SPEFSCR_INIT \
286+
}
287+
#elif defined(CONFIG_PPC32)
280288
#define INIT_THREAD { \
281289
.ksp = INIT_SP, \
282290
.pgdir = swapper_pg_dir, \

arch/powerpc/kernel/process.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,9 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
17451745
#ifdef CONFIG_ALTIVEC
17461746
p->thread.vr_save_area = NULL;
17471747
#endif
1748+
#if defined(CONFIG_PPC_BOOK3S_32) && defined(CONFIG_PPC_KUAP)
1749+
p->thread.kuap = KUAP_NONE;
1750+
#endif
17481751

17491752
setup_ksp_vsid(p, sp);
17501753

arch/powerpc/mm/book3s32/kuap.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66
struct static_key_false disable_kuap_key;
77
EXPORT_SYMBOL(disable_kuap_key);
88

9+
void kuap_lock_all_ool(void)
10+
{
11+
kuap_lock_all();
12+
}
13+
EXPORT_SYMBOL(kuap_lock_all_ool);
14+
15+
void kuap_unlock_all_ool(void)
16+
{
17+
kuap_unlock_all();
18+
}
19+
EXPORT_SYMBOL(kuap_unlock_all_ool);
20+
921
void __init setup_kuap(bool disabled)
1022
{
1123
if (!disabled)
12-
kuap_update_sr(mfsr(0) | SR_KS, 0, TASK_SIZE);
24+
kuap_lock_all_ool();
1325

1426
if (smp_processor_id() != boot_cpuid)
1527
return;

0 commit comments

Comments
 (0)