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

Just a bit of security hygiene. #4313

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,23 @@ void Router::enqueueReceivedMessage(meshtastic_MeshPacket *p)
// FIXME, move this someplace better
PacketId generatePacketId()
{
static uint32_t i; // Note: trying to keep this in noinit didn't help for working across reboots
static uint32_t rollingPacketId; // Note: trying to keep this in noinit didn't help for working across reboots
static bool didInit = false;

uint32_t numPacketId = UINT32_MAX;

if (!didInit) {
didInit = true;

// pick a random initial sequence number at boot (to prevent repeated reboots always starting at 0)
// Note: we mask the high order bit to ensure that we never pass a 'negative' number to random
i = random(numPacketId & 0x7fffffff);
LOG_DEBUG("Initial packet id %u, numPacketId %u\n", i, numPacketId);
rollingPacketId = random(UINT32_MAX & 0x7fffffff);
LOG_DEBUG("Initial packet id %u\n", rollingPacketId);
}

i++;
PacketId id = (i % numPacketId) + 1; // return number between 1 and numPacketId (ie - never zero)
rollingPacketId++;

rollingPacketId &= UINT32_MAX >> 22; // Mask out the top 22 bits
PacketId id = rollingPacketId | random(UINT32_MAX & 0x7fffffff) << 10; // top 22 bits
LOG_DEBUG("Partially randomized packet id %u\n", id);
return id;
}

Expand Down
4 changes: 4 additions & 0 deletions src/platform/esp32/main-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ void enableSlowCLK()

void esp32Setup()
{
/* We explicitly don't want to do call randomSeed,
// as that triggers the esp32 core to use a less secure pseudorandom function.
uint32_t seed = esp_random();
LOG_DEBUG("Setting random seed %u\n", seed);
randomSeed(seed);
jp-bennett marked this conversation as resolved.
Show resolved Hide resolved
*/

LOG_DEBUG("Total heap: %d\n", ESP.getHeapSize());
LOG_DEBUG("Free heap: %d\n", ESP.getFreeHeap());
Expand Down
Loading