linux: fix out-of-bounds access when parsing power supply type - #2065
Merged
Conversation
📝 WalkthroughWalkthrough
Possibly related PRs
Poem
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
When reading /sys/class/power_supply/<name>/type, the trailing-newline trimming loop in Platform_Battery_getSysData started at &buffer[ret - 1] and decremented the pointer while *buf == '\n', without checking that the pointer remained within the buffer. If the type file contains only newline bytes, the loop walks past buffer[0] and performs an out-of-bounds read on the stack. If the OOB byte happens to be '\n' (0x0a), the loop also writes '\0' into it, corrupting the stack below the buffer for as many consecutive newline bytes as are present. Run the loop forward instead and sanitize control characters and spaces. Assisted-by: lilu <lilu@kylinos.cn> Co-authored-by: BenBE <BenBE@geshi.org>
BenBE
force-pushed
the
fix-battery-type-oob
branch
from
August 11, 2026 16:12
82172e6 to
4329f54
Compare
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.
Problem
Platform_Battery_getSysData()inlinux/Platform.creads/sys/class/power_supply/<name>/typeinto a stack buffer and trims trailing newlines with:Compat_readfileat()reads up tocount - 1bytes (reserving one for the'\0'terminator), so for a 32-byte buffer it can return up to 31 bytes. If thetypefile contains only newline bytes (e.g. 31'\n'), the loop walks the pointer frombuffer[30]down tobuffer[0]and then — because there is no lower-bound check — performs an out-of-bounds read atbuffer[-1]to re-evaluate*buf == '\n'.The loop only stops there if the OOB byte is not
'\n'. If the OOB byte happens to be0x0a(e.g. leftover stack content), the loop continues:'\0'intobuffer[-1](OOB write), andbuffer[-2],buffer[-3], …corrupting the stack below the buffer for as many consecutive
'\n'bytes as are present, until a non-'\n'byte or an unmapped page is reached.Trigger
A malicious or buggy
power_supplydevice whosetypeattribute contains only newlines. Such devices can be created via USB/ACPI/container power supply class registrations (e.g./sys/class/power_supply/<name>/type). Any entry whose name does not start withBATorACreaches this code path.Impact
typefile is all newlines.'\n'.This is reachable from an unprivileged trigger (a crafted
power_supplysysfs entry) and can corrupt the stack of thePlatform_Battery_getSysDataframe.Fix
Add a lower-bound check so the loop stops at
buffer[0]:One line, one file.
Verification
Reproduced the OOB with a standalone canary test that mimics
Compat_readfileatreturning 31 newlines into a 32-byte buffer sandwiched between guard regions:buffer[0]set to a non-'\n'value, the loop performs an OOB read atbuffer[-1](final pointer offset-1) and stops only because that byte is not'\n'.'\n'bytes placed belowbuffer[0], the loop performs 3 OOB writes, clobbering the guard bytes from0x0ato0x00, and walks to offset-4.With this patch applied, the loop stops at
buffer[0]and no OOB access occurs.htoprebuilds cleanly with-Wall -Wextra.Notes
type-file probing for entries not prefixed withBAT/AC.Assisted-by: lilu lilu@kylinos.cn