Skip to content
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

Fix double free error when using signed images without a secure boot. (IDFGH-4376) #6210

Conversation

Morozov-5F
Copy link
Contributor

@Morozov-5F Morozov-5F commented Dec 4, 2020

  • If the signed images are enabled and image verification occurrs after an upgrade in the app itself the the doulbe free takes place.
  • Double free occurs due to the fact that verify_secure_boot_signature() already frees the sha_handle pointer.
  • If the verify_secure_boot_signature() does not return ESP_OK (for example, when new image is not signed) then the code jumps to the err label which basically frees the sha_handle if it's not NULL causing a double-free.
  • I've decided to set the sha_handle to NULL after verify_secure_boot_signature() in the non-bootloader mode fixing the double-free. I didn't do this change for bootloader build because all the bootloader functions that use sha_handle expect it to be non-NULL.
  • I think the better solution is to pass a sha_handle by reference and NULL it inside the verify_secure_boot_signature() if needed but it's a risky change and I don't have all the required hardware to test it.

- If the signed images are enabled and image verification occurrs after
  an upgrade in the app itself the the doulbe free takes place.
- Double free occurs due to the fact that `verify_secure_boot_signature()` already frees
  the `sha_handle` pointer.
- If the `verify_secure_boot_signature()` does not return `ESP_OK` (for example, when
  new image is not signed) then the code jums to the `err` label which basically frees
  the `sha_handle` if it's not NULL causing a double-free.
- I've decided to set the `sha_handle` to NULL after `verify_secure_boot_signature()`
  in the non-bootloader mode fixing the double-free. I didn't do this change for
  bootloader build because all the bootloader functions that use `sha_handle` expect it
  to be non-NULL.
- I think the better solution is to pass a sha_handle by reference and NULL it inside the
  `verify_secure_boot_signature()` if needed but it's a risky change and I don't all the
  required hardware to test it.
@CLAassistant
Copy link

CLAassistant commented Dec 4, 2020

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot changed the title Fix double free error when using signed images without a secure boot. Fix double free error when using signed images without a secure boot. (IDFGH-4376) Dec 4, 2020
@Alvin1Zhang
Copy link
Collaborator

Thanks for your contribution.

@projectgus
Copy link
Contributor

Hi @Morozov-5F,

Thanks again for sending this fix. We've submitted the same fix internally for merging, but with a small tweak (applied for both bootloader & app). In the bootloader, a non-null sha_handle value means the calculation is still ongoing in the hardware so in this case it's correct for it to be nulled out at this point as well - the only difference is that the bootloader doesn't crash when it's incorrectly left as non-null.

This PR will be updated once the fix merges to master.

Angus

@Morozov-5F
Copy link
Contributor Author

Thanks @projectgus, that's a great news! I wasn't sure about the bootloader part so thank you for clarification too.

espressif-bot pushed a commit that referenced this pull request Jan 9, 2021
sha_handle is "finished" when verify_secure_boot_signature() returns and
should be nulled out.

Alternative version of fix submitted in #6210

Closes #6210

Signed-off-by: Angus Gratton <[email protected]>
espressif-bot pushed a commit that referenced this pull request Feb 25, 2021
sha_handle is "finished" when verify_secure_boot_signature() returns and
should be nulled out.

Alternative version of fix submitted in #6210

Closes #6210

Signed-off-by: Angus Gratton <[email protected]>
@Morozov-5F Morozov-5F deleted the bugfix/fix-double-free-if-signed-images-used-without-secure-boot branch February 26, 2021 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants