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

Updater - fixed signature verification for compressed binaries #9109

Merged
merged 5 commits into from
Mar 27, 2024

Conversation

bakadave
Copy link
Contributor

Overview

This pull request introduces a fix to the signature verification, ensuring that the hash for compressed signed binaries is correctly calculated.

Problem Description

Previously, Arduino Core attempted to read from flash memory without proper consideration for the 4-byte alignment requirement when calculating the hash for the signature verification. This did not present an issue when uncompressed binaries are checked as all compiled binaries are 4-aligned (unconfirmed, just an educated guess), and signature verification appears to work well in these cases.
When uploading a compressed binary (based on this) the gzip algorithm makes no attempt to produce a 4-aligned file. The rest of the signing results in a valid signed binary regardless, however when calculating the hash for the verification process there is a ~75% chance that the hash will include some bytes from the signature, thus compromising the whole signature verification process.

Solution

  • Implementing a conditional check to identify unaligned read requests.
    • For aligned reads, operations proceed as normal.
    • For unaligned reads, the read size is adjusted to the nearest 4-byte boundary, ensuring alignment. Only the relevant bytes (excluding the padded ones for alignment) are processed after reading into a temporary buffer.
  • These adjustments ensure ESP.flashRead operates within the requirements set by the ESP SDK.

@mcspr
Copy link
Collaborator

mcspr commented Mar 26, 2024

...or it can be swapped with u8 flashRead variant that does this?

The gist of the issue here is that flashRead cannot use unaligned flash size, which fails?

@bakadave
Copy link
Contributor Author

bakadave commented Mar 26, 2024

The gist of the issue here is that flashRead cannot use unaligned flash size, which fails?

Yes.

...or it can be swapped with u8 flashRead variant that does this?

If there is one that would also work. EDIT: yep looks promising, let me test it.

@bakadave
Copy link
Contributor Author

So far it seems like @mcspr's take on this is correct, simply dropping the reinterpret cast causes the u8 flashRead method to be used instead of the u32 and the whole problem goes away.

I'm somewhat inexperienced with pull requests on such a large project, what's the next step here?

@d-a-v
Copy link
Collaborator

d-a-v commented Mar 26, 2024

what's the next step here?

Your last commit is enough.

@bakadave
Copy link
Contributor Author

just realised that the alignas specifier was redundant

@bakadave
Copy link
Contributor Author

@mcspr thank you for pointing it out

@mcspr mcspr changed the title Fixed signature verification for compressed binaries Updater - fixed signature verification for compressed binaries Mar 27, 2024
@mcspr mcspr merged commit d7c50f7 into esp8266:master Mar 27, 2024
28 checks passed
@mcspr
Copy link
Collaborator

mcspr commented Mar 27, 2024

tnx!

hasenradball pushed a commit to hasenradball/Arduino that referenced this pull request Nov 18, 2024
…66#9109)

Previously, Arduino Core attempted to read from flash memory without proper consideration for the 4-byte alignment requirement when calculating the hash for the signature verification. This did not present an issue when uncompressed binaries are checked as all compiled binaries are 4-aligned (unconfirmed, just an educated guess), and signature verification appears to work well in these cases.

When uploading a compressed binary (based on this) the gzip algorithm makes no attempt to produce a 4-aligned file. The rest of the signing results in a valid signed binary regardless, however when calculating the hash for the verification process there is a ~75% chance that the hash will include some bytes from the signature, thus compromising the whole signature verification process.

editorial note: ESP.flashRead for u8 arrays (aka byte arrays) was already updated to properly handle both aligned and unaligned target buffer and / or length, while u32 expects that its arguments are already aligned. Since array pointer in Updater is already aligned, this properly handles unaligned size case.
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.

3 participants