fix: prevent offset+len overflow in flash log read#449
Open
MarkAtwood wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes a portability/security edge case in wh_NvmFlashLog_Read where offset + data_len could overflow on 16-bit-int targets, allowing an out-of-bounds read.
Changes:
- Forces 32-bit arithmetic in the bounds check by casting
offsetanddata_lentouint32_tprior to addition.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
In
wh_NvmFlashLog_Read(src/wh_nvm_flash_log.c:690), the bounds checkadds two
whNvmSize(uint16_t) values. On a platform whereintis 16 bits (standards-permissible; C only guaranteesint >= 16bits), the addition wraps modulo 65536. A craftedoffset=0xFFFF, data_len=11against a 10-byte object wraps to 10, passes the check, and the subsequentmemcpy(data, obj_data, data_len)at line 693 reads out of bounds.On mainstream >=32-bit-int targets (e.g. ARM Cortex-M) integer promotion computes the sum in 32-bit
int, so it does not wrap there — this is a latent portability / defense-in-depth defect for 16-bit-int embedded targets.Fix
Force 32-bit arithmetic with explicit casts so the sum never wraps regardless of
intwidth:This mirrors the already-remediated sibling check in
src/wh_server_nvm.c(which avoids the addition via subtraction).Build verification
Compiled the changed translation unit on Ubuntu 24.04 / gcc (32-bit int) against the repo's own test config (
test/config/wolfhsm_cfg.h,-DWOLFHSM_CFG) linked to a prebuilt wolfSSL install:gcc_exit=0, no warnings.Reported by static analysis (Fenrir finding 389).