jedisct1 / libsodium

A modern, portable, easy to use crypto library.
https://libsodium.org
Other
12.06k stars 1.72k forks source link

[UB] memcpy could be called on null dst pointer in function escrypt_r #1324

Closed cchr-ledger closed 8 months ago

cchr-ledger commented 8 months ago

Hello,

At https://github.com/jedisct1/libsodium/blob/master/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c#L188, memcpy could be called with dst being NULL, if escrypt_r is itself called with its buf argument set to NULL.

jedisct1 commented 8 months ago

Static analysis tools are nice, but that argument is never expected to be NULL.

Not only it wouldn't make any sense to use the crypto_pwhash_*() functions that way, but all their arguments are tagged __attribute__ ((nonnull)), so the compiler is going to scream if you ever do that.

Also, the first thing these functions do is to zero the output buffer, so a bus error would happen way before the memcpy().

cchr-ledger commented 8 months ago

Fair enough, thanks for the explanation.