SciresM / hactool

hactool is a tool to view information about, decrypt, and extract common file formats for the Nintendo Switch, especially Nintendo Content Archives.
ISC License
982 stars 151 forks source link

fix mbedtls_cipher_finish invocations #122

Open justinkb opened 7 months ago

justinkb commented 7 months ago

those mbedtls_cipher_finish calls never did anything like this, since we were passing a null pointer for the third argument and the old included mbedtls just returns an error code (which we completely ignore) if you do that, refer to https://github.com/SciresM/hactool/blob/master/mbedtls/library/cipher.c#L684 for that.

I ran into this when I was building hactool against mbedtls 3.5.1 instead of the repo included one, it segfaults with that version (on dereferencing that third argument pointer), since they removed null pointer checks along the way to go with their stricter requirements on what you can pass as arguments.

you can argue this doesn't really meaningfully change or fix the operation of hactool, since all that crypto stuff is just transient anyway and any potential bad state issue evaporates once the program stops execution, but still I thought it was worth submitting this change.

&out_len reuse as output parameter for the mbedtls_cipher_finish is fine, since we never need that actual value from mbedtls_cipher_update invocation(s) anyway except now with this change to cleanly do the mbedtls_cipher_finish with the correct 2nd argument pointer