cheatfate / nimcrypto

Nim cryptographic library
MIT License
189 stars 23 forks source link

`nimcrypto/utils` has UB #66

Closed tersec closed 11 months ago

tersec commented 11 months ago
import nimcrypto/utils

var x: array[9, byte]

discard beLoad32(x, 1)
discard leLoad32(x, 1)
discard beLoad64(x, 1)
discard leLoad64(x, 1)
beStore32(x, 1, 0'u32)
beStore64(x, 1, 0'u64)
leStore32(x, 1, 0'u32)
leStore64(x, 1, 0'u64)
$ nim c -r --passC="-fsanitize=undefined" --passL="-fsanitize=undefined" c
...
.cache/nim/c_d/@mc.nim.c:120:25: runtime error: load of misaligned address 0x557e0a19ad41 for type 'NU32', which requires 4 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:127:25: runtime error: load of misaligned address 0x557e0a19ad41 for type 'NU32', which requires 4 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:131:25: runtime error: load of misaligned address 0x557e0a19ad41 for type 'NU64', which requires 8 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:138:26: runtime error: load of misaligned address 0x557e0a19ad41 for type 'NU64', which requires 8 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:141:42: runtime error: store to misaligned address 0x557e0a19ad41 for type 'NU32', which requires 4 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:143:42: runtime error: store to misaligned address 0x557e0a19ad41 for type 'NU64', which requires 8 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:145:42: runtime error: store to misaligned address 0x557e0a19ad41 for type 'NU32', which requires 4 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 
.cache/nim/c_d/@mc.nim.c:147:42: runtime error: store to misaligned address 0x557e0a19ad41 for type 'NU64', which requires 8 byte alignment
0x557e0a19ad41: note: pointer points here
 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
              ^ 

These come up running UBSAN in nimbus-eth2 make test:


Suite] Eth2 specific discovery tests
nimbus-eth2/vendor/nimcrypto/nimcrypto/utils.nim:349:106: runtime error: load of misaligned address 0x7f15ea0e37f7 for type 'NU64', which requires 8 byte alignment
0x7f15ea0e37f7: note: pointer points here
 7d fd 73 16 bd  03 be 9a 90 87 ea 5e f8  ac 3c 66 be c0 08 f6 3e  cd df 64 f7 25 9d 16 c7  df 03 04
             ^
nimbus-eth2/vendor/nimcrypto/nimcrypto/bcmode.nim:923:279: runtime error: load of misaligned address 0x7f15ea0e37ff for type 'NU64', which requires 8 byte alignment
0x7f15ea0e37ff: note: pointer points here
 90 87 ea 5e f8  ac 3c 66 be c0 08 f6 3e  cd df 64 f7 25 9d 16 c7  df 03 04 07 5f b4 a3 8c  9c da 53
             ^
  [OK] Subnet query
  [OK] Invalid attnets field
  [OK] Subnet query after ENR update
``
cheatfate commented 11 months ago

Fixed in #67.