-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chip-cert tool: Fix OpenSSL Object Reuse and Double-Free #24166
chip-cert tool: Fix OpenSSL Object Reuse and Double-Free #24166
Conversation
Don't rely on d2i_X509 object reuse and fix double-free The chip-cert tool is relying on OpenSSL's "object reuse" mode in d2i_X509. d2i_X509 has a very bizarre type signature: X509 *d2i_X509(X509 **out, const unsigned char **inp, long len); The safest way to call this function is to pass NULL into out. The function then straightforwardly hands you a new X509 on success, or NULL on error. However, if out and *out are both NULL, OpenSSL tries to reuse the existing X509 object. This does not work, particular not in the way that chip-cert uses it. When d2i_X509 fails, even in this mode, it will free what's at *out and set *out to NULL. So when ReadCert's d2i_X509 call fails, it will silently free the cert parameter. But the caller doesn't know this and will double-free it!
PR #24166: Size comparison from e101730 to 0758a0f Increases (9 builds for bl702, nrfconnect, psoc6, qpg, telink)
Decreases (4 builds for bl602, k32w, telink)
Full report (53 builds for bl602, bl702, cc13x2_26x2, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, qpg, telink)
|
Minor correction (this was my fault; I had a typo in my description). This should have read:
|
Also worth noting, here's OpenSSL's documentation that discourages the object reuse mode.
https://www.openssl.org/docs/man1.1.1/man3/d2i_X509.html In addition to being slightly misleading with the error case, as described here, it can go wrong if the original object has some cached values in it. On the BoringSSL side, we're thinking of making it always free the old object and write in a fresh object, which is safer and less error-prone, but it will require a change like this one. |
Fixed #24165
Don't rely on d2i_X509 object reuse and fix double-free
The chip-cert tool is relying on OpenSSL's "object reuse" mode in d2i_X509. d2i_X509 has a very bizarre type signature:
X509 *d2i_X509(X509 **out, const unsigned char **inp, long len);
The safest way to call this function is to pass NULL into out. The function then straightforwardly hands you a new X509 on success, or NULL on error. However, if out and *out are both NULL, OpenSSL tries to reuse the existing X509 object.
This does not work, particular not in the way that chip-cert uses it. When d2i_X509 fails, even in this mode, it will free what's at *out and set *out to NULL. So when ReadCert's d2i_X509 call fails, it will silently free the cert parameter. But the caller doesn't know this and will double-free it!