> For legacy and compatibility reasons I have to use an older version of the
> Windows DDK to compile certain libraries, including OpenSSL.  I am
> compiling it with version 3790.1830.

In other words compiler version 13.x.

> I spent the morning tracing a crash in BN_nist_mod_384 which I believe is
> caused by an optimisation by the DDK compiler, but is easily fixable.
> 
> If looking at the crypto/bn/bn_nist.c file distributed with version 1.0.1c
> of the library, on lines 1029-1030:
> 
> res = (BN_ULONG *)(((PTR_SIZE_INT)c_d&~mask) |
>     ((PTR_SIZE_INT)r_d&mask));
> 
> Because c_d is a stack array and not an actual pointer, the DDK compiler is
> retrieving the value of the first item in the stack array (in my case it's
> 0x01)

You make it sound like the fact that c_d is a stack array is an excuse
for doing wrong:-) I mean it's clearly a compiler bug and reasoning as
above does not make it understandable/justifiable/excusable. Formally
it's not OpenSSL bug and shouldn't be reported as one...

> and AND'ing it with (~mask), which is 0xffffffff.  This results in a
> crash later on when *(0x00000001) is dereferenced.
> 
> In assembly it looks like:
> 
> MOV eax, DWORD PTR [mask]
> NOT eax
> AND eax, DWORD PTR [c_d]
> 
> At this point [eax] contains 0x01.
> 
> Note that this only happens in Debug on Win32; it does not happen in
> Release,

Wow! I.e. code generated *without* any optimizations is wrong, but not
optimized one. Must be first occurrence...

> and does not happen in x64 at all.  I haven't spent much time to
> investigate why that is.
> 
> The fix (in my case) is very simple:
> 
> // top of function:
> // Name a temporary variable to hold the pointer to c_d
> PVOID tmpC;
> 
> // new line 1029:
> tmpC = (PVOID)((PTR_SIZE_INT) c_d);
> res = (BN_ULONG *)(((PTR_SIZE_INT)tmpC&~mask) |
>     ((PTR_SIZE_INT)r_d&mask));

You don't need any extra variable, just do 'res = c_d;' and then 'res =
...res&~mask...'


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to