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 (some ?) memory alignment issues on the crypto part - resulting in crashes or strange behavior #4867

Merged
merged 6 commits into from
Sep 26, 2024

Conversation

TheMalkavien
Copy link
Contributor

@TheMalkavien TheMalkavien commented Sep 25, 2024

Hi,

As discussed into the #4855 issue, here is my humble PR.

It fixes #4855 by avoiding the use of non aligned pointers. On some hardware, we do not care at all, but for some the result is not guaranteed, and it makes some hardware crash (rp2040 crashes, Heltecv3 PKI_FAILED, ...).

There is 2 main changes :

  • Firstly, avoiding to dereference the extraNonce pointer, wich is pointing to a bytebuffer, at an address we cannot be sure is properly aligned (it depends on the size of the buffer, and the alignment of the buffer itself).
  • Secondly, the two crypto bytebuffers are now aligned at declaration, it makes the use of pointers inside, a little more safe (and it fixes multiples crashes during the crypto algorithm which use pointers to the buffers).

As discussed, these two changes are a quick way to fix crashes or bugs linked to the non-alignment of some objects. It will probably not fix all the alignment problems in the future (or even in the present), but it should be considered to avoid such difficult to debug behavior.
Maybe the compiler can provide some useful flags for that, and of course be very careful when using pointers (as usual ? :p ).

Tested and working on RP2040-lora and Heltecv3

Thanks !

First step to fix some Crypto crashes or strange behaviors
Fix #4855, and probably multiple Crypto problems depending on hardware
@CLAassistant
Copy link

CLAassistant commented Sep 25, 2024

CLA assistant check
All committers have signed the CLA.

@jp-bennett
Copy link
Collaborator

It looks sane to me, though memory alignment isn't something I'm used to worrying about (obviously). I should have a rp2040 platform by the end of today to test with.

@GUVWAF
Copy link
Member

GUVWAF commented Sep 26, 2024

I'm wondering whether we should align the radiobuf (used when receiving) as well:

uint8_t radiobuf[MAX_LORA_PAYLOAD_LEN + 1];

As we're doing pointer arithmetic for the payload afterwards:

const uint8_t *payload = radiobuf + sizeof(PacketHeader);

@TheMalkavien
Copy link
Contributor Author

Hi,

I'd say, every structure should be aligned for performance at least. I'm rusty, but I found it strange that the compiler does not align global variables by default.

Anyway, in your example, even if the start of the buffer is properly aligned, there is a risk that if sizeof(PacketHeader) is not modulo "word", your pointer will be not aligned.
I'ts not a problem if you use it byte by byte, but using it with a dereferrence will mostly cause problems.

Everything I say must be verified, I'm not myself an expert in memory alignment or gcc specificities.

@GUVWAF
Copy link
Member

GUVWAF commented Sep 26, 2024

@TheMalkavien Thanks, makes sense. We force sizeof(PacketHeader) to be 16, but that might thus be an issue as well. Not sure how far we should go with this, but at least this PR looks good to me.

@jp-bennett
Copy link
Collaborator

#4878 adds a test case that crashes the rp2040. I've not yet been able to trigger the Heltec v3 flaw. This patch does fix that crash and get the test suite passing again on rp2040.

@thebentern thebentern merged commit 4794cdb into meshtastic:master Sep 26, 2024
108 of 109 checks passed
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.

[Bug]: RP2040 - Decrypt or Encrypt (during remote admin for example) crashes the device
6 participants