Skip to content

Commit 0b38b5e

Browse files
svens-s390Vasily Gorbik
authored andcommitted
s390: prevent leaking kernel address in BEAR
When userspace executes a syscall or gets interrupted, BEAR contains a kernel address when returning to userspace. This make it pretty easy to figure out where the kernel is mapped even with KASLR enabled. To fix this, add lpswe to lowcore and always execute it there, so userspace sees only the lowcore address of lpswe. For this we have to extend both critical_cleanup and the SWITCH_ASYNC macro to also check for lpswe addresses in lowcore. Fixes: b2d24b9 ("s390/kernel: add support for kernel address space layout randomization (KASLR)") Cc: <stable@vger.kernel.org> # v5.2+ Reviewed-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> Signed-off-by: Sven Schnelle <svens@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent d2abfbe commit 0b38b5e

9 files changed

Lines changed: 62 additions & 27 deletions

File tree

‎arch/s390/include/asm/lowcore.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ struct lowcore {
141141

142142
/* br %r1 trampoline */
143143
__u16 br_r1_trampoline; /* 0x0400 */
144-
__u8 pad_0x0402[0x0e00-0x0402]; /* 0x0402 */
144+
__u32 return_lpswe; /* 0x0402 */
145+
__u32 return_mcck_lpswe; /* 0x0406 */
146+
__u8 pad_0x040a[0x0e00-0x040a]; /* 0x040a */
145147

146148
/*
147149
* 0xe00 contains the address of the IPL Parameter Information

‎arch/s390/include/asm/processor.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ typedef struct thread_struct thread_struct;
162162
#define INIT_THREAD { \
163163
.ksp = sizeof(init_stack) + (unsigned long) &init_stack, \
164164
.fpu.regs = (void *) init_task.thread.fpu.fprs, \
165+
.last_break = 1, \
165166
}
166167

167168
/*

‎arch/s390/include/asm/setup.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/bits.h>
1010
#include <uapi/asm/setup.h>
11+
#include <linux/build_bug.h>
1112

1213
#define EP_OFFSET 0x10008
1314
#define EP_STRING "S390EP"
@@ -162,6 +163,12 @@ static inline unsigned long kaslr_offset(void)
162163
return __kaslr_offset;
163164
}
164165

166+
static inline u32 gen_lpswe(unsigned long addr)
167+
{
168+
BUILD_BUG_ON(addr > 0xfff);
169+
return 0xb2b20000 | addr;
170+
}
171+
165172
#else /* __ASSEMBLY__ */
166173

167174
#define IPL_DEVICE (IPL_DEVICE_OFFSET)

‎arch/s390/kernel/asm-offsets.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ int main(void)
124124
OFFSET(__LC_EXT_DAMAGE_CODE, lowcore, external_damage_code);
125125
OFFSET(__LC_MCCK_FAIL_STOR_ADDR, lowcore, failing_storage_address);
126126
OFFSET(__LC_LAST_BREAK, lowcore, breaking_event_addr);
127+
OFFSET(__LC_RETURN_LPSWE, lowcore, return_lpswe);
128+
OFFSET(__LC_RETURN_MCCK_LPSWE, lowcore, return_mcck_lpswe);
127129
OFFSET(__LC_RST_OLD_PSW, lowcore, restart_old_psw);
128130
OFFSET(__LC_EXT_OLD_PSW, lowcore, external_old_psw);
129131
OFFSET(__LC_SVC_OLD_PSW, lowcore, svc_old_psw);

‎arch/s390/kernel/entry.S‎

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,29 @@ _LPP_OFFSET = __LC_LPP
115115

116116
.macro SWITCH_ASYNC savearea,timer
117117
tmhh %r8,0x0001 # interrupting from user ?
118-
jnz 1f
118+
jnz 2f
119119
lgr %r14,%r9
120+
cghi %r14,__LC_RETURN_LPSWE
121+
je 0f
120122
slg %r14,BASED(.Lcritical_start)
121123
clg %r14,BASED(.Lcritical_length)
122-
jhe 0f
124+
jhe 1f
125+
0:
123126
lghi %r11,\savearea # inside critical section, do cleanup
124127
brasl %r14,cleanup_critical
125128
tmhh %r8,0x0001 # retest problem state after cleanup
126-
jnz 1f
127-
0: lg %r14,__LC_ASYNC_STACK # are we already on the target stack?
129+
jnz 2f
130+
1: lg %r14,__LC_ASYNC_STACK # are we already on the target stack?
128131
slgr %r14,%r15
129132
srag %r14,%r14,STACK_SHIFT
130-
jnz 2f
133+
jnz 3f
131134
CHECK_STACK \savearea
132135
aghi %r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
133-
j 3f
134-
1: UPDATE_VTIME %r14,%r15,\timer
136+
j 4f
137+
2: UPDATE_VTIME %r14,%r15,\timer
135138
BPENTER __TI_flags(%r12),_TIF_ISOLATE_BP
136-
2: lg %r15,__LC_ASYNC_STACK # load async stack
137-
3: la %r11,STACK_FRAME_OVERHEAD(%r15)
139+
3: lg %r15,__LC_ASYNC_STACK # load async stack
140+
4: la %r11,STACK_FRAME_OVERHEAD(%r15)
138141
.endm
139142

140143
.macro UPDATE_VTIME w1,w2,enter_timer
@@ -401,7 +404,7 @@ ENTRY(system_call)
401404
stpt __LC_EXIT_TIMER
402405
mvc __VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
403406
lmg %r11,%r15,__PT_R11(%r11)
404-
lpswe __LC_RETURN_PSW
407+
b __LC_RETURN_LPSWE(%r0)
405408
.Lsysc_done:
406409

407410
#
@@ -608,43 +611,50 @@ ENTRY(pgm_check_handler)
608611
BPOFF
609612
stmg %r8,%r15,__LC_SAVE_AREA_SYNC
610613
lg %r10,__LC_LAST_BREAK
611-
lg %r12,__LC_CURRENT
614+
srag %r11,%r10,12
615+
jnz 0f
616+
/* if __LC_LAST_BREAK is < 4096, it contains one of
617+
* the lpswe addresses in lowcore. Set it to 1 (initial state)
618+
* to prevent leaking that address to userspace.
619+
*/
620+
lghi %r10,1
621+
0: lg %r12,__LC_CURRENT
612622
lghi %r11,0
613623
larl %r13,cleanup_critical
614624
lmg %r8,%r9,__LC_PGM_OLD_PSW
615625
tmhh %r8,0x0001 # test problem state bit
616-
jnz 2f # -> fault in user space
626+
jnz 3f # -> fault in user space
617627
#if IS_ENABLED(CONFIG_KVM)
618628
# cleanup critical section for program checks in sie64a
619629
lgr %r14,%r9
620630
slg %r14,BASED(.Lsie_critical_start)
621631
clg %r14,BASED(.Lsie_critical_length)
622-
jhe 0f
632+
jhe 1f
623633
lg %r14,__SF_SIE_CONTROL(%r15) # get control block pointer
624634
ni __SIE_PROG0C+3(%r14),0xfe # no longer in SIE
625635
lctlg %c1,%c1,__LC_USER_ASCE # load primary asce
626636
larl %r9,sie_exit # skip forward to sie_exit
627637
lghi %r11,_PIF_GUEST_FAULT
628638
#endif
629-
0: tmhh %r8,0x4000 # PER bit set in old PSW ?
630-
jnz 1f # -> enabled, can't be a double fault
639+
1: tmhh %r8,0x4000 # PER bit set in old PSW ?
640+
jnz 2f # -> enabled, can't be a double fault
631641
tm __LC_PGM_ILC+3,0x80 # check for per exception
632642
jnz .Lpgm_svcper # -> single stepped svc
633-
1: CHECK_STACK __LC_SAVE_AREA_SYNC
643+
2: CHECK_STACK __LC_SAVE_AREA_SYNC
634644
aghi %r15,-(STACK_FRAME_OVERHEAD + __PT_SIZE)
635-
# CHECK_VMAP_STACK branches to stack_overflow or 4f
636-
CHECK_VMAP_STACK __LC_SAVE_AREA_SYNC,4f
637-
2: UPDATE_VTIME %r14,%r15,__LC_SYNC_ENTER_TIMER
645+
# CHECK_VMAP_STACK branches to stack_overflow or 5f
646+
CHECK_VMAP_STACK __LC_SAVE_AREA_SYNC,5f
647+
3: UPDATE_VTIME %r14,%r15,__LC_SYNC_ENTER_TIMER
638648
BPENTER __TI_flags(%r12),_TIF_ISOLATE_BP
639649
lg %r15,__LC_KERNEL_STACK
640650
lgr %r14,%r12
641651
aghi %r14,__TASK_thread # pointer to thread_struct
642652
lghi %r13,__LC_PGM_TDB
643653
tm __LC_PGM_ILC+2,0x02 # check for transaction abort
644-
jz 3f
654+
jz 4f
645655
mvc __THREAD_trap_tdb(256,%r14),0(%r13)
646-
3: stg %r10,__THREAD_last_break(%r14)
647-
4: lgr %r13,%r11
656+
4: stg %r10,__THREAD_last_break(%r14)
657+
5: lgr %r13,%r11
648658
la %r11,STACK_FRAME_OVERHEAD(%r15)
649659
stmg %r0,%r7,__PT_R0(%r11)
650660
# clear user controlled registers to prevent speculative use
@@ -663,14 +673,14 @@ ENTRY(pgm_check_handler)
663673
stg %r13,__PT_FLAGS(%r11)
664674
stg %r10,__PT_ARGS(%r11)
665675
tm __LC_PGM_ILC+3,0x80 # check for per exception
666-
jz 5f
676+
jz 6f
667677
tmhh %r8,0x0001 # kernel per event ?
668678
jz .Lpgm_kprobe
669679
oi __PT_FLAGS+7(%r11),_PIF_PER_TRAP
670680
mvc __THREAD_per_address(8,%r14),__LC_PER_ADDRESS
671681
mvc __THREAD_per_cause(2,%r14),__LC_PER_CODE
672682
mvc __THREAD_per_paid(1,%r14),__LC_PER_ACCESS_ID
673-
5: REENABLE_IRQS
683+
6: REENABLE_IRQS
674684
xc __SF_BACKCHAIN(8,%r15),__SF_BACKCHAIN(%r15)
675685
larl %r1,pgm_check_table
676686
llgh %r10,__PT_INT_CODE+2(%r11)
@@ -775,7 +785,7 @@ ENTRY(io_int_handler)
775785
mvc __VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
776786
.Lio_exit_kernel:
777787
lmg %r11,%r15,__PT_R11(%r11)
778-
lpswe __LC_RETURN_PSW
788+
b __LC_RETURN_LPSWE(%r0)
779789
.Lio_done:
780790

781791
#
@@ -1214,7 +1224,7 @@ ENTRY(mcck_int_handler)
12141224
stpt __LC_EXIT_TIMER
12151225
mvc __VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
12161226
0: lmg %r11,%r15,__PT_R11(%r11)
1217-
lpswe __LC_RETURN_MCCK_PSW
1227+
b __LC_RETURN_MCCK_LPSWE
12181228

12191229
.Lmcck_panic:
12201230
lg %r15,__LC_NODAT_STACK
@@ -1271,6 +1281,8 @@ ENDPROC(stack_overflow)
12711281
#endif
12721282

12731283
ENTRY(cleanup_critical)
1284+
cghi %r9,__LC_RETURN_LPSWE
1285+
je .Lcleanup_lpswe
12741286
#if IS_ENABLED(CONFIG_KVM)
12751287
clg %r9,BASED(.Lcleanup_table_sie) # .Lsie_gmap
12761288
jl 0f
@@ -1424,6 +1436,7 @@ ENDPROC(cleanup_critical)
14241436
mvc __LC_RETURN_PSW(16),__PT_PSW(%r9)
14251437
mvc 0(64,%r11),__PT_R8(%r9)
14261438
lmg %r0,%r7,__PT_R0(%r9)
1439+
.Lcleanup_lpswe:
14271440
1: lmg %r8,%r9,__LC_RETURN_PSW
14281441
BR_EX %r14,%r11
14291442
.Lcleanup_sysc_restore_insn:

‎arch/s390/kernel/process.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp,
106106
p->thread.system_timer = 0;
107107
p->thread.hardirq_timer = 0;
108108
p->thread.softirq_timer = 0;
109+
p->thread.last_break = 1;
109110

110111
frame->sf.back_chain = 0;
111112
/* new return point is ret_from_fork */

‎arch/s390/kernel/setup.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include <asm/nospec-branch.h>
7474
#include <asm/mem_detect.h>
7575
#include <asm/uv.h>
76+
#include <asm/asm-offsets.h>
7677
#include "entry.h"
7778

7879
/*
@@ -450,6 +451,8 @@ static void __init setup_lowcore_dat_off(void)
450451
lc->spinlock_index = 0;
451452
arch_spin_lock_setup(0);
452453
lc->br_r1_trampoline = 0x07f1; /* br %r1 */
454+
lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
455+
lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
453456

454457
set_prefix((u32)(unsigned long) lc);
455458
lowcore_ptr[0] = lc;

‎arch/s390/kernel/smp.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
212212
lc->spinlock_lockval = arch_spin_lockval(cpu);
213213
lc->spinlock_index = 0;
214214
lc->br_r1_trampoline = 0x07f1; /* br %r1 */
215+
lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
216+
lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
215217
if (nmi_alloc_per_cpu(lc))
216218
goto out_async;
217219
if (vdso_alloc_per_cpu(lc))

‎arch/s390/mm/vmem.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ void __init vmem_map_init(void)
415415
SET_MEMORY_RO | SET_MEMORY_X);
416416
__set_memory(__stext_dma, (__etext_dma - __stext_dma) >> PAGE_SHIFT,
417417
SET_MEMORY_RO | SET_MEMORY_X);
418+
419+
/* we need lowcore executable for our LPSWE instructions */
420+
set_memory_x(0, 1);
421+
418422
pr_info("Write protected kernel read-only data: %luk\n",
419423
(unsigned long)(__end_rodata - _stext) >> 10);
420424
}

0 commit comments

Comments
 (0)