Restore SIGILL handler if sigprocmask fails

We've never encountered this case, but better safe then sorry.
This commit is contained in:
Jeffrey Walton 2020-02-17 14:15:02 -05:00
parent a76c29a11e
commit 6fa3e38e5c
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
11 changed files with 61 additions and 8 deletions

View File

@ -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))

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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))