2015-05-11 15:11:36 -07:00
all :
i n c l u d e . . / l i b . m k
.PHONY : all all_ 32 all_ 64 warn_ 32bit_failure clean
2015-04-06 23:11:06 -07:00
2016-06-28 14:35:39 +03:00
TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall test_mremap_vdso \
2017-02-22 07:36:17 -08:00
check_initial_reg_state sigreturn ldt_gdt iopl mpx-mini-test ioperm \
2016-11-16 10:23:28 -08:00
protection_keys test_vdso
x86/signal/64: Re-add support for SS in the 64-bit signal context
This is a second attempt to make the improvements from c6f2062935c8
("x86/signal/64: Fix SS handling for signals delivered to 64-bit
programs"), which was reverted by 51adbfbba5c6 ("x86/signal/64: Add
support for SS in the 64-bit signal context").
This adds two new uc_flags flags. UC_SIGCONTEXT_SS will be set for
all 64-bit signals (including x32). It indicates that the saved SS
field is valid and that the kernel supports the new behavior.
The goal is to fix a problems with signal handling in 64-bit tasks:
SS wasn't saved in the 64-bit signal context, making it awkward to
determine what SS was at the time of signal delivery and making it
impossible to return to a non-flat SS (as calling sigreturn clobbers
SS).
This also made it extremely difficult for 64-bit tasks to return to
fully-defined 16-bit contexts, because only the kernel can easily do
espfix64, but sigreturn was unable to set a non-flag SS:ESP.
(DOSEMU has a monstrous hack to partially work around this
limitation.)
If we could go back in time, the correct fix would be to make 64-bit
signals work just like 32-bit signals with respect to SS: save it
in signal context, reset it when delivering a signal, and restore
it in sigreturn.
Unfortunately, doing that (as I tried originally) breaks DOSEMU:
DOSEMU wouldn't reset the signal context's SS when clearing the LDT
and changing the saved CS to 64-bit mode, since it predates the SS
context field existing in the first place.
This patch is a bit more complicated, and it tries to balance a
bunch of goals. It makes most cases of changing ucontext->ss during
signal handling work as expected.
I do this by special-casing the interesting case. On sigreturn,
ucontext->ss will be honored by default, unless the ucontext was
created from scratch by an old program and had a 64-bit CS
(unfortunately, CRIU can do this) or was the result of changing a
32-bit signal context to 64-bit without resetting SS (as DOSEMU
does).
For the benefit of new 64-bit software that uses segmentation (new
versions of DOSEMU might), the new behavior can be detected with a
new ucontext flag UC_SIGCONTEXT_SS.
To avoid compilation issues, __pad0 is left as an alias for ss in
ucontext.
The nitty-gritty details are documented in the header file.
This patch also re-enables the sigreturn_64 and ldt_gdt_64 selftests,
as the kernel change allows both of them to pass.
Tested-by: Stas Sergeev <stsp@list.ru>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Acked-by: Borislav Petkov <bp@alien8.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Pavel Emelyanov <xemul@parallels.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/749149cbfc3e75cd7fcdad69a854b399d792cc6f.1455664054.git.luto@kernel.org
[ Small readability edit. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-02-16 15:09:03 -08:00
TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \
2016-01-11 17:23:32 -08:00
test_FCMOV test_FCOMI test_FISTTP \
2016-01-12 11:56:15 -08:00
vdso_restorer
2016-12-26 19:20:50 -08:00
TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip
2015-04-06 23:11:06 -07:00
2015-05-29 14:58:24 -07:00
TARGETS_C_32BIT_ALL := $( TARGETS_C_BOTHBITS) $( TARGETS_C_32BIT_ONLY)
2016-01-28 15:11:19 -08:00
TARGETS_C_64BIT_ALL := $( TARGETS_C_BOTHBITS) $( TARGETS_C_64BIT_ONLY)
2015-05-29 14:58:24 -07:00
BINARIES_32 := $( TARGETS_C_32BIT_ALL:%= %_32)
2016-01-28 15:11:19 -08:00
BINARIES_64 := $( TARGETS_C_64BIT_ALL:%= %_64)
2015-04-06 23:11:06 -07:00
2016-11-29 19:55:52 +08:00
BINARIES_32 := $( patsubst %,$( OUTPUT) /%,$( BINARIES_32) )
BINARIES_64 := $( patsubst %,$( OUTPUT) /%,$( BINARIES_64) )
2015-04-06 23:11:06 -07:00
CFLAGS := -O2 -g -std= gnu99 -pthread -Wall
2015-04-21 15:51:33 -07:00
UNAME_M := $( shell uname -m)
2015-05-11 15:11:36 -07:00
CAN_BUILD_I386 := $( shell ./check_cc.sh $( CC) trivial_32bit_program.c -m32)
CAN_BUILD_X86_64 := $( shell ./check_cc.sh $( CC) trivial_64bit_program.c)
2015-04-06 23:11:06 -07:00
2015-05-11 15:11:36 -07:00
i f e q ( $( CAN_BUILD_I 386) , 1 )
2015-04-06 23:11:06 -07:00
all : all_ 32
2015-05-11 15:11:35 -07:00
TEST_PROGS += $( BINARIES_32)
2015-05-11 15:11:36 -07:00
e n d i f
i f e q ( $( CAN_BUILD_X 86_ 64) , 1 )
2015-04-06 23:11:06 -07:00
all : all_ 64
2015-04-21 15:51:58 -07:00
TEST_PROGS += $( BINARIES_64)
2015-04-06 23:11:06 -07:00
e n d i f
2015-05-11 15:11:36 -07:00
all_32 : $( BINARIES_ 32)
2015-04-06 23:11:06 -07:00
all_64 : $( BINARIES_ 64)
2017-04-21 16:26:16 -06:00
EXTRA_CLEAN := $( BINARIES_32) $( BINARIES_64)
2015-04-06 23:11:06 -07:00
2016-11-29 19:55:52 +08:00
$(BINARIES_32) : $( OUTPUT ) /%_ 32: %.c
2015-09-17 15:02:13 +02:00
$( CC) -m32 -o $@ $( CFLAGS) $( EXTRA_CFLAGS) $^ -lrt -ldl -lm
2015-04-06 23:11:06 -07:00
2016-11-29 19:55:52 +08:00
$(BINARIES_64) : $( OUTPUT ) /%_ 64: %.c
2015-04-06 23:11:06 -07:00
$( CC) -m64 -o $@ $( CFLAGS) $( EXTRA_CFLAGS) $^ -lrt -ldl
2015-05-11 15:11:36 -07:00
# x86_64 users should be encouraged to install 32-bit libraries
i f e q ( $( CAN_BUILD_I 386) $( CAN_BUILD_X 86_ 64) , 0 1 )
all : warn_ 32bit_failure
warn_32bit_failure :
@echo "Warning: you seem to have a broken 32-bit build" 2>& 1; \
echo "environment. This will reduce test coverage of 64-bit" 2>& 1; \
echo "kernels. If you are using a Debian-like distribution," 2>& 1; \
echo "try:" ; 2>& 1; \
echo "" ; \
echo " apt-get install gcc-multilib libc6-i386 libc6-dev-i386" ; \
echo "" ; \
echo "If you are using a Fedora-like distribution, try:" ; \
echo "" ; \
echo " yum install glibc-devel.*i686" ; \
exit 0;
e n d i f
2015-04-24 15:09:19 -07:00
# Some tests have additional dependencies.
2016-11-29 19:55:52 +08:00
$(OUTPUT)/sysret_ss_attrs_64 : thunks .S
$(OUTPUT)/ptrace_syscall_32 : raw_syscall_helper_ 32.S
$(OUTPUT)/test_syscall_vdso_32 : thunks_ 32.S
2016-01-28 15:11:20 -08:00
# check_initial_reg_state is special: it needs a custom entry, and it
# needs to be static so that its interpreter doesn't destroy its initial
# state.
2016-11-29 19:55:52 +08:00
$(OUTPUT)/check_initial_reg_state_32 : CFLAGS += -Wl , -ereal_start -static
$(OUTPUT)/check_initial_reg_state_64 : CFLAGS += -Wl , -ereal_start -static