From 6fa3e38e5c26820396d11a5a155fc83790d17c72 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 17 Feb 2020 14:15:02 -0500 Subject: [PATCH] Restore SIGILL handler if sigprocmask fails We've never encountered this case, but better safe then sorry. --- cpu.cpp | 8 +++++--- crc_simd.cpp | 3 +++ gcm_simd.cpp | 6 ++++++ neon_simd.cpp | 8 +++++++- ppc_power7.cpp | 3 +++ ppc_power8.cpp | 12 ++++++++++++ ppc_power9.cpp | 6 ++++++ ppc_simd.cpp | 3 +++ rijndael_simd.cpp | 3 +++ sha_simd.cpp | 6 ++++++ sse_simd.cpp | 11 +++++++---- 11 files changed, 61 insertions(+), 8 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 95561742..da9f298c 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -72,9 +72,8 @@ extern "C" { extern "C" { static jmp_buf s_jmpNoCPUID; - static void SigIllHandlerCPUID(int unused) + static void SigIllHandler(int) { - CRYPTOPP_UNUSED(unused); longjmp(s_jmpNoCPUID, 1); } } @@ -358,14 +357,17 @@ bool CpuId(word32 func, word32 subfunc, word32 output[4]) // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854 volatile bool result = true; - volatile SigHandler oldHandler = signal(SIGILL, SigIllHandlerCPUID); + volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler); if (oldHandler == SIG_ERR) return false; # ifndef __MINGW32__ volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask) != 0) + { + signal(SIGILL, oldHandler); return false; + } # endif if (setjmp(s_jmpNoCPUID)) diff --git a/crc_simd.cpp b/crc_simd.cpp index 35509335..93ab9bc9 100644 --- a/crc_simd.cpp +++ b/crc_simd.cpp @@ -87,7 +87,10 @@ bool CPU_ProbeCRC32() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/gcm_simd.cpp b/gcm_simd.cpp index ef9e2ebf..c8375f5c 100644 --- a/gcm_simd.cpp +++ b/gcm_simd.cpp @@ -123,7 +123,10 @@ bool CPU_ProbePMULL() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -174,7 +177,10 @@ bool CPU_ProbePMULL() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/neon_simd.cpp b/neon_simd.cpp index 12473d99..602ba0e9 100644 --- a/neon_simd.cpp +++ b/neon_simd.cpp @@ -77,7 +77,10 @@ bool CPU_ProbeARMv7() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -160,7 +163,10 @@ bool CPU_ProbeNEON() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -176,7 +182,7 @@ bool CPU_ProbeNEON() uint32x4_t y = vshlq_n_u32(x, 4); word32 z[4]; vst1q_u32(z, y); - return (z[0] & z[1] & z[2] & z[3]) == 16; + result = (z[0] & z[1] & z[2] & z[3]) == 16; } sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR); diff --git a/ppc_power7.cpp b/ppc_power7.cpp index 49c97cee..84a7275a 100644 --- a/ppc_power7.cpp +++ b/ppc_power7.cpp @@ -55,7 +55,10 @@ bool CPU_ProbePower7() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/ppc_power8.cpp b/ppc_power8.cpp index 7ed65ebb..7e287ee5 100644 --- a/ppc_power8.cpp +++ b/ppc_power8.cpp @@ -56,7 +56,10 @@ bool CPU_ProbePower8() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -109,7 +112,10 @@ bool CPU_ProbeAES() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -158,7 +164,10 @@ bool CPU_ProbeSHA256() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -202,7 +211,10 @@ bool CPU_ProbeSHA512() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/ppc_power9.cpp b/ppc_power9.cpp index c842d46d..8870023f 100644 --- a/ppc_power9.cpp +++ b/ppc_power9.cpp @@ -55,7 +55,10 @@ bool CPU_ProbePower9() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -102,7 +105,10 @@ bool CPU_ProbeDARN() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/ppc_simd.cpp b/ppc_simd.cpp index b8182ad5..081d0515 100644 --- a/ppc_simd.cpp +++ b/ppc_simd.cpp @@ -58,7 +58,10 @@ bool CPU_ProbeAltivec() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/rijndael_simd.cpp b/rijndael_simd.cpp index c40df9c6..b7f830f4 100644 --- a/rijndael_simd.cpp +++ b/rijndael_simd.cpp @@ -118,7 +118,10 @@ bool CPU_ProbeAES() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/sha_simd.cpp b/sha_simd.cpp index 419e183b..277c9362 100644 --- a/sha_simd.cpp +++ b/sha_simd.cpp @@ -113,7 +113,10 @@ bool CPU_ProbeSHA1() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; @@ -180,7 +183,10 @@ bool CPU_ProbeSHA256() volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } if (setjmp(s_jmpSIGILL)) result = false; diff --git a/sse_simd.cpp b/sse_simd.cpp index bd4d59c2..96c80a27 100644 --- a/sse_simd.cpp +++ b/sse_simd.cpp @@ -34,7 +34,7 @@ extern const char SSE_SIMD_FNAME[] = __FILE__; NAMESPACE_BEGIN(CryptoPP) -#ifndef CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY +#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY extern "C" { typedef void (*SigHandler)(int); } @@ -42,12 +42,12 @@ extern "C" { extern "C" { static jmp_buf s_jmpNoSSE2; - static void SigIllHandlerSSE2(int) + static void SigIllHandler(int) { longjmp(s_jmpNoSSE2, 1); } } -#endif // Not CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY +#endif // CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY bool CPU_ProbeSSE2() { @@ -78,14 +78,17 @@ bool CPU_ProbeSSE2() // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854 volatile bool result = true; - volatile SigHandler oldHandler = signal(SIGILL, SigIllHandlerSSE2); + volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler); if (oldHandler == SIG_ERR) return false; # ifndef __MINGW32__ volatile sigset_t oldMask; if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask)) + { + signal(SIGILL, oldHandler); return false; + } # endif if (setjmp(s_jmpNoSSE2))