-
Notifications
You must be signed in to change notification settings - Fork 361
smex: harden ELF parsing against malformed input #10922
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
Merged
+85
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46703e0
smex: validate ELF string section index and size
lrgirdwo a12a275
smex: clear freed section buffer pointer on error
lrgirdwo 497c960
smex: bound section name offsets to the string table
lrgirdwo 4363677
smex: reject undersized fw-ready section
lrgirdwo c8aab4f
smex: bound the extended manifest walk
lrgirdwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,13 @@ static int fw_version_copy(const struct elf_module *src, | |
| if (section_size < 0) | ||
| return section_size; | ||
|
|
||
| if ((size_t)section_size < sizeof(struct sof_ipc_fw_ready)) { | ||
| fprintf(stderr, "error: .fw_ready section too small: %d\n", | ||
| section_size); | ||
| free(buffer); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| memcpy(&header->version, | ||
| &((struct sof_ipc_fw_ready *)buffer)->version, | ||
| sizeof(header->version)); | ||
|
|
@@ -50,13 +57,37 @@ static int fw_version_copy(const struct elf_module *src, | |
| return section_size; | ||
|
|
||
| ext_hdr = (struct ext_man_elem_header *)buffer; | ||
| while ((uintptr_t)ext_hdr < (uintptr_t)buffer + section_size) { | ||
| while ((uintptr_t)ext_hdr + sizeof(*ext_hdr) <= | ||
| (uintptr_t)buffer + section_size) { | ||
| if (ext_hdr->type == EXT_MAN_ELEM_DBG_ABI) { | ||
| /* make sure the whole dbg-abi element is within the | ||
| * section before reading it | ||
| */ | ||
| if (ext_hdr->elem_size < sizeof(struct ext_man_dbg_abi) || | ||
| (uintptr_t)ext_hdr + sizeof(struct ext_man_dbg_abi) > | ||
| (uintptr_t)buffer + section_size) { | ||
| fprintf(stderr, "error: %s truncated dbg-abi element\n", | ||
| src->elf_file); | ||
| free(buffer); | ||
| return -ENOEXEC; | ||
| } | ||
| header->version.abi_version = | ||
| ((struct ext_man_dbg_abi *) | ||
| ext_hdr)->dbg_abi.abi_dbg_version; | ||
| break; | ||
| } | ||
|
Comment on lines
+60
to
78
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed — before reading the dbg-abi element it now checks |
||
| /* a malformed element size would loop forever (0) or advance | ||
| * the cursor past the section; reject the image rather than | ||
| * silently stopping | ||
| */ | ||
| if (ext_hdr->elem_size == 0 || | ||
| (uintptr_t)ext_hdr + ext_hdr->elem_size > | ||
| (uintptr_t)buffer + section_size) { | ||
| fprintf(stderr, "error: %s malformed ext-manifest element\n", | ||
| src->elf_file); | ||
| free(buffer); | ||
| return -ENOEXEC; | ||
| } | ||
| //move to the next entry | ||
| ext_hdr = (struct ext_man_elem_header *) | ||
| ((uint8_t *)ext_hdr + ext_hdr->elem_size); | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SHN_XINDEX(extended section numbering) only applies when a file has >= 0xff00 sections, which doesn't occur for SOF firmware images (they have a handful of sections). Supporting it would mean reading the real index fromsection[0].sh_link, which is out of scope here — rejecting is the safe behaviour for these inputs. I can add an explicitSHN_XINDEXdetection + clearer message if you'd prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets do the explicit detection and clearer messaging.