Skip to content

Commit 5bb7b21

Browse files
committed
Merge tag 'x86-urgent-2021-09-26' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 fixes from Thomas Gleixner: "A set of fixes for X86: - Prevent sending the wrong signal when protection keys are enabled and the kernel handles a fault in the vsyscall emulation. - Invoke early_reserve_memory() before invoking e820_memory_setup() which is required to make the Xen dom0 e820 hooks work correctly. - Use the correct data type for the SETZ operand in the EMQCMDS instruction wrapper. - Prevent undefined behaviour to the potential unaligned accesss in the instruction decoder library" * tag 'x86-urgent-2021-09-26' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/insn, tools/x86: Fix undefined behavior due to potential unaligned accesses x86/asm: Fix SETZ size enqcmds() build failure x86/setup: Call early_reserve_memory() earlier x86/fault: Fix wrong signal when vsyscall fails with pkey
2 parents 3a398ac + 5ba1071 commit 5bb7b21

File tree

7 files changed

+39
-27
lines changed

7 files changed

+39
-27
lines changed

arch/x86/include/asm/pkeys.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#ifndef _ASM_X86_PKEYS_H
33
#define _ASM_X86_PKEYS_H
44

5-
#define ARCH_DEFAULT_PKEY 0
6-
75
/*
86
* If more than 16 keys are ever supported, a thorough audit
97
* will be necessary to ensure that the types that store key

arch/x86/include/asm/special_insns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static inline int enqcmds(void __iomem *dst, const void *src)
275275
{
276276
const struct { char _[64]; } *__src = src;
277277
struct { char _[64]; } __iomem *__dst = dst;
278-
int zf;
278+
bool zf;
279279

280280
/*
281281
* ENQCMDS %(rdx), rax

arch/x86/kernel/setup.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,20 @@ void __init setup_arch(char **cmdline_p)
830830

831831
x86_init.oem.arch_setup();
832832

833+
/*
834+
* Do some memory reservations *before* memory is added to memblock, so
835+
* memblock allocations won't overwrite it.
836+
*
837+
* After this point, everything still needed from the boot loader or
838+
* firmware or kernel text should be early reserved or marked not RAM in
839+
* e820. All other memory is free game.
840+
*
841+
* This call needs to happen before e820__memory_setup() which calls the
842+
* xen_memory_setup() on Xen dom0 which relies on the fact that those
843+
* early reservations have happened already.
844+
*/
845+
early_reserve_memory();
846+
833847
iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
834848
e820__memory_setup();
835849
parse_setup_data();
@@ -876,18 +890,6 @@ void __init setup_arch(char **cmdline_p)
876890

877891
parse_early_param();
878892

879-
/*
880-
* Do some memory reservations *before* memory is added to
881-
* memblock, so memblock allocations won't overwrite it.
882-
* Do it after early param, so we could get (unlikely) panic from
883-
* serial.
884-
*
885-
* After this point everything still needed from the boot loader or
886-
* firmware or kernel text should be early reserved or marked not
887-
* RAM in e820. All other memory is free game.
888-
*/
889-
early_reserve_memory();
890-
891893
#ifdef CONFIG_MEMORY_HOTPLUG
892894
/*
893895
* Memory used by the kernel cannot be hot-removed because Linux

arch/x86/lib/insn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
3838

3939
#define __get_next(t, insn) \
40-
({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
40+
({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
4141

4242
#define __peek_nbyte_next(t, insn, n) \
43-
({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); })
43+
({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); })
4444

4545
#define get_next(t, insn) \
4646
({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })

arch/x86/mm/fault.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ page_fault_oops(struct pt_regs *regs, unsigned long error_code,
710710

711711
static noinline void
712712
kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
713-
unsigned long address, int signal, int si_code)
713+
unsigned long address, int signal, int si_code,
714+
u32 pkey)
714715
{
715716
WARN_ON_ONCE(user_mode(regs));
716717

@@ -735,8 +736,12 @@ kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code,
735736

736737
set_signal_archinfo(address, error_code);
737738

738-
/* XXX: hwpoison faults will set the wrong code. */
739-
force_sig_fault(signal, si_code, (void __user *)address);
739+
if (si_code == SEGV_PKUERR) {
740+
force_sig_pkuerr((void __user *)address, pkey);
741+
} else {
742+
/* XXX: hwpoison faults will set the wrong code. */
743+
force_sig_fault(signal, si_code, (void __user *)address);
744+
}
740745
}
741746

742747
/*
@@ -798,7 +803,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
798803
struct task_struct *tsk = current;
799804

800805
if (!user_mode(regs)) {
801-
kernelmode_fixup_or_oops(regs, error_code, address, pkey, si_code);
806+
kernelmode_fixup_or_oops(regs, error_code, address,
807+
SIGSEGV, si_code, pkey);
802808
return;
803809
}
804810

@@ -930,7 +936,8 @@ do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
930936
{
931937
/* Kernel mode? Handle exceptions or die: */
932938
if (!user_mode(regs)) {
933-
kernelmode_fixup_or_oops(regs, error_code, address, SIGBUS, BUS_ADRERR);
939+
kernelmode_fixup_or_oops(regs, error_code, address,
940+
SIGBUS, BUS_ADRERR, ARCH_DEFAULT_PKEY);
934941
return;
935942
}
936943

@@ -1396,7 +1403,8 @@ void do_user_addr_fault(struct pt_regs *regs,
13961403
*/
13971404
if (!user_mode(regs))
13981405
kernelmode_fixup_or_oops(regs, error_code, address,
1399-
SIGBUS, BUS_ADRERR);
1406+
SIGBUS, BUS_ADRERR,
1407+
ARCH_DEFAULT_PKEY);
14001408
return;
14011409
}
14021410

@@ -1416,15 +1424,17 @@ void do_user_addr_fault(struct pt_regs *regs,
14161424
return;
14171425

14181426
if (fatal_signal_pending(current) && !user_mode(regs)) {
1419-
kernelmode_fixup_or_oops(regs, error_code, address, 0, 0);
1427+
kernelmode_fixup_or_oops(regs, error_code, address,
1428+
0, 0, ARCH_DEFAULT_PKEY);
14201429
return;
14211430
}
14221431

14231432
if (fault & VM_FAULT_OOM) {
14241433
/* Kernel mode? Handle exceptions or die: */
14251434
if (!user_mode(regs)) {
14261435
kernelmode_fixup_or_oops(regs, error_code, address,
1427-
SIGSEGV, SEGV_MAPERR);
1436+
SIGSEGV, SEGV_MAPERR,
1437+
ARCH_DEFAULT_PKEY);
14281438
return;
14291439
}
14301440

include/linux/pkeys.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <linux/mm.h>
66

7+
#define ARCH_DEFAULT_PKEY 0
8+
79
#ifdef CONFIG_ARCH_HAS_PKEYS
810
#include <asm/pkeys.h>
911
#else /* ! CONFIG_ARCH_HAS_PKEYS */

tools/arch/x86/lib/insn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
3838

3939
#define __get_next(t, insn) \
40-
({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
40+
({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); })
4141

4242
#define __peek_nbyte_next(t, insn, n) \
43-
({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); })
43+
({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); })
4444

4545
#define get_next(t, insn) \
4646
({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })

0 commit comments

Comments
 (0)