NLnetLabs / ldns

LDNS is a DNS library that facilitates DNS tool programming
https://nlnetlabs.nl/ldns
BSD 3-Clause "New" or "Revised" License
292 stars 97 forks source link

Wrong SHA256 under gcc 11 -O2 #142

Closed RinCat closed 2 years ago

RinCat commented 2 years ago

Not sure what caused it, but the SHA256 result is wrong under gcc 11 (11.2). The SHA1 result is correct.

Example key: Krincat.ch.+013+18046.key:

 rincat.ch.      IN      DNSKEY  257 3 13 SNo9lghJXno9gSdNgdAK5nkuIeQsgBrzEFdXwhwHpV/oi4NhUkw84ZGgdBYZIiTQ7D3rlZ93WeBIhIR7Ea9pEg== ;{id = 18046 (ksk), size = 256b}

Under clang (12.0):

ldns-key2ds -2 -n Krincat.ch.+013+18046.key 
rincat.ch.      3600    IN      DS      18046 13 2 0603e36c8a11e8775ea70dd40acd52b159cb3460268b6907e2234c4c3cf88f71

Under gcc (11.2):

ldns-key2ds -2 -n Krincat.ch.+013+18046.key
rincat.ch.      3600    IN      DS      18046 13 2 1b10d6e2924cc31c849ad9a5e9a5a7fa0fb5118cd3a343c510ffddae5eb6a281
RinCat commented 2 years ago

The error only happened in -O2. x86_64-pc-linux-gnu-11.2.0 x86_64-gentoo-linux-musl-11.2.0

RinCat commented 2 years ago

I found that this problem can also be reproduced under glibc, so the problem is in gcc 11.

RinCat commented 2 years ago

Just before the line https://github.com/NLnetLabs/ldns/blob/4d2057f0b5220487882be1b19c302833b84cffe3/sha2.c#L601 The states are the same, but the last of a few bytes in buffer changed, and there is no corresponding code.

gcc (-O2):

context.buffer [64]
0x3d 0xeb 0x95 0x9f 0x77 0x59 0xe0 0x48 0x84 0x84 0x7b 0x11 0xaf 0x69 0x12 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x07 0x27 0xdd 0x4a 0xba 0x35 0x90 0xd4

clang, and gcc(-O1):

context.buffer [64]
0x3d 0xeb 0x95 0x9f 0x77 0x59 0xe0 0x48 0x84 0x84 0x7b 0x11 0xaf 0x69 0x12 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x78
wtoorop commented 2 years ago

Thanks @RinCat , I think your issue might be the same as in #131 Could you try compiling with -fno-strict-aliasing?

RinCat commented 2 years ago

@wtoorop Hi, I confirm use -fno-strict-aliasing fixed it. Also the commit https://github.com/NLnetLabs/ldns/commit/4d2057f0b5220487882be1b19c302833b84cffe3 works.

wtoorop commented 2 years ago

Resolved in ldns-1.8.0