lscpu: fix NULL+offset dereference in cpuinfo_parse_cache()#4433
Open
rawrmonster17 wants to merge 1 commit into
Open
lscpu: fix NULL+offset dereference in cpuinfo_parse_cache()#4433rawrmonster17 wants to merge 1 commit into
rawrmonster17 wants to merge 1 commit into
Conversation
strstr() returns NULL when the needle is absent. In two places the return value had a constant offset added before the NULL guard: p = strstr(data, "scope=") + 6; if (!p ...) /* dead: p is (char*)6 if absent */ p = strstr(data, "type=") + 5; if (!p || !*p) /* same: p is (char*)5 if absent */ When the field is missing from the cpuinfo cache line, p becomes a low non-NULL address (CWE-476 / CWE-823). The !p guard is dead, so strncmp() or the *p dereference immediately faults. Fix by moving the arithmetic after the NULL check, matching the correct pattern already used for "level=", "size=", "line_size=", and "associativity=" in the same function.
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
cpuinfo_parse_cache()insys-utils/lscpu-cputype.cparses thes390-style
/proc/cpuinfocache lines (format:cache<nr> : level=<lvl> type=<type> scope=<scope> size=<size> ...).Two fields are fetched with an offset applied before the NULL guard:
When
strstr()returnsNULL(field absent from the cache line),pbecomes
(char *)6or(char *)5— a non-NULL near-zero pointer thatpasses the
!pguard and immediately faults on the subsequentstrncmp()or*pdereference (CWE-476 / CWE-823).Reachable via
lscpu -s <sysroot>with a craftedproc/cpuinfo, oron real hardware/firmware that emits a malformed s390 cache line.
The same function already uses the correct pattern for
level=,size=,line_size=, andassociativity=—strstr()result checked first,arithmetic applied after:
Fix
Move the offset arithmetic to after the NULL check for both affected
fields, matching the established pattern:
No behaviour change when the fields are present. When absent, the
function now returns 0 safely instead of crashing.
Testing
Built with
./configure --disable-all-programs --enable-libsmartcols --enable-lscpu && make lscpu— zero errors, zero warnings on the changed translation unit.Ran a dedicated self-check covering all six cases (missing field,
scope=Private,scope=Shared, missingtype=,type=Unified,type=Data) — all pass.