fs/romfs: reject negative resulting position in romfs_seek()#19420
Open
94xhn wants to merge 1 commit into
Open
fs/romfs: reject negative resulting position in romfs_seek()#1942094xhn wants to merge 1 commit into
94xhn wants to merge 1 commit into
Conversation
romfs_seek() clamps the computed position to the file size when it exceeds rf_size, but never checks for a negative result. lseek(fd, offset, SEEK_SET/SEEK_CUR/SEEK_END) with an offset that produces a negative position (e.g. a negative SEEK_SET offset, or a SEEK_CUR/ SEEK_END offset more negative than the current position/file size) is written straight into filep->f_pos. The subsequent romfs_read() computes `rf->rf_startoffset + filep->f_pos` into a uint32_t, so a negative f_pos wraps around to a huge unsigned offset, and romfs_hwread()'s XIP path memcpy()s from rm_xipbase plus that offset -- an out-of-bounds read far past the mapped flash region. Add the same "if (position < 0) return -EINVAL" guard already used by fs/fat/fs_fat32.c's seek function, before the existing end-of-file clamp. Assisted-by: Claude:claude-sonnet-5
|
xiaoxiang781216
approved these changes
Jul 13, 2026
Contributor
|
@94xhn please fix: |
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.
Summary
romfs_seek()clamps the computedpositionto the file size when it exceedsrf->rf_size, but never checks for a negative result.lseek(fd, offset, SEEK_SET/SEEK_CUR/SEEK_END)calls that produce a negativeposition(e.g. a negativeSEEK_SEToffset, or aSEEK_CUR/SEEK_ENDoffset more negative than the current position/file size) write that negative value straight intofilep->f_pos.The subsequent
romfs_read()computesrf->rf_startoffset + filep->f_posinto auint32_t(fs/romfs/fs_romfs.c), so a negativef_poswraps around to a huge unsigned offset, andromfs_hwread()'s XIP pathmemcpy()s fromrm_xipbaseplus that offset — an out-of-bounds read far past the mapped flash region.Fix
Add the same
if (position < 0) return -EINVALguard already used byfs/fat/fs_fat32.c's seek function, before the existing end-of-file clamp.Testing
Wrote a standalone host-side C reproduction transcribing
romfs_seek(), the downstreamromfs_read()address computation, andromfs_hwread()'s XIPmemcpy()verbatim from the current source, with aSIGSEGVhandler around a deliberately small (1 MiB)rm_xipbasemapping:lseek(fd, -600, SEEK_SET)returns -1 but with the wrong errno (leaks the raw negative value instead ofEINVAL), leavesf_poscorrupted at -600, and the downstream address computation wraps to0xFFFFFDC8(~4 GB out of bounds) —romfs_hwread_XIP()reading from that offset triggers a realSIGSEGVagainst the mapped buffer.errno == EINVAL, andf_posis left unchanged.SEEK_SET/SEEK_CUR/SEEK_ENDseeks, EOF clamping (seeking past end-of-file), and the exact-zero boundary case (aSEEK_CURthat lands exactly at 0, which must not be rejected) all produce identical results before and after the fix.Assisted-by: Claude:claude-sonnet-5