Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933577AbdCWJUZ (ORCPT ); Thu, 23 Mar 2017 05:20:25 -0400 Received: from terminus.zytor.com ([65.50.211.136]:33033 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752070AbdCWJUX (ORCPT ); Thu, 23 Mar 2017 05:20:23 -0400 Date: Thu, 23 Mar 2017 02:13:06 -0700 From: tip-bot for Andy Lutomirski Message-ID: Cc: jpoimboe@redhat.com, dvlasenk@redhat.com, tglx@linutronix.de, luto@kernel.org, torvalds@linux-foundation.org, jgross@suse.com, thgarnie@google.com, boris.ostrovsky@oracle.com, hpa@zytor.com, brgerst@gmail.com, linux-kernel@vger.kernel.org, bp@alien8.de, mingo@kernel.org, peterz@infradead.org Reply-To: mingo@kernel.org, bp@alien8.de, brgerst@gmail.com, linux-kernel@vger.kernel.org, peterz@infradead.org, thgarnie@google.com, jgross@suse.com, torvalds@linux-foundation.org, hpa@zytor.com, boris.ostrovsky@oracle.com, luto@kernel.org, jpoimboe@redhat.com, tglx@linutronix.de, dvlasenk@redhat.com In-Reply-To: References: To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/mm] selftests/x86/ldt_gdt_32: Work around a glibc sigaction() bug Git-Commit-ID: 65973dd3fd31151823f4b8c289eebbb3fb7e6bc0 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3423 Lines: 108 Commit-ID: 65973dd3fd31151823f4b8c289eebbb3fb7e6bc0 Gitweb: http://git.kernel.org/tip/65973dd3fd31151823f4b8c289eebbb3fb7e6bc0 Author: Andy Lutomirski AuthorDate: Wed, 22 Mar 2017 14:32:29 -0700 Committer: Ingo Molnar CommitDate: Thu, 23 Mar 2017 08:25:07 +0100 selftests/x86/ldt_gdt_32: Work around a glibc sigaction() bug i386 glibc is buggy and calls the sigaction syscall incorrectly. This is asymptomatic for normal programs, but it blows up on programs that do evil things with segmentation. The ldt_gdt self-test is an example of such an evil program. This doesn't appear to be a regression -- I think I just got lucky with the uninitialized memory that glibc threw at the kernel when I wrote the test. This hackish fix manually issues sigaction(2) syscalls to undo the damage. Without the fix, ldt_gdt_32 segfaults; with the fix, it passes for me. See: https://sourceware.org/bugzilla/show_bug.cgi?id=21269 Signed-off-by: Andy Lutomirski Cc: Boris Ostrovsky Cc: Borislav Petkov Cc: Brian Gerst Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Josh Poimboeuf Cc: Juergen Gross Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Garnier Cc: Thomas Gleixner Cc: stable@vger.kernel.org Link: http://lkml.kernel.org/r/aaab0f9f93c9af25396f01232608c163a760a668.1490218061.git.luto@kernel.org Signed-off-by: Ingo Molnar --- tools/testing/selftests/x86/ldt_gdt.c | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tools/testing/selftests/x86/ldt_gdt.c b/tools/testing/selftests/x86/ldt_gdt.c index f612161..b9a22f1 100644 --- a/tools/testing/selftests/x86/ldt_gdt.c +++ b/tools/testing/selftests/x86/ldt_gdt.c @@ -409,6 +409,51 @@ static void *threadproc(void *ctx) } } +#ifdef __i386__ + +#ifndef SA_RESTORE +#define SA_RESTORER 0x04000000 +#endif + +/* + * The UAPI header calls this 'struct sigaction', which conflicts with + * glibc. Sigh. + */ +struct fake_ksigaction { + void *handler; /* the real type is nasty */ + unsigned long sa_flags; + void (*sa_restorer)(void); + unsigned char sigset[8]; +}; + +static void fix_sa_restorer(int sig) +{ + struct fake_ksigaction ksa; + + if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) { + /* + * glibc has a nasty bug: it sometimes writes garbage to + * sa_restorer. This interacts quite badly with anything + * that fiddles with SS because it can trigger legacy + * stack switching. Patch it up. See: + * + * https://sourceware.org/bugzilla/show_bug.cgi?id=21269 + */ + if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) { + ksa.sa_restorer = NULL; + if (syscall(SYS_rt_sigaction, sig, &ksa, NULL, + sizeof(ksa.sigset)) != 0) + err(1, "rt_sigaction"); + } + } +} +#else +static void fix_sa_restorer(int sig) +{ + /* 64-bit glibc works fine. */ +} +#endif + static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), int flags) { @@ -420,6 +465,7 @@ static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), if (sigaction(sig, &sa, 0)) err(1, "sigaction"); + fix_sa_restorer(sig); } static jmp_buf jmpbuf;