Skip to content

parse-util: fix ineffective overflow guards in store_loadavg_fixed_point() - #43753

Merged
cdown merged 1 commit into
systemd:mainfrom
Ar-maan05:fix/loadavg-fixed-point-overflow-guards
Sep 14, 2026
Merged

cdown merged 1 commit into
systemd:mainfrom
Ar-maan05:fix/loadavg-fixed-point-overflow-guards

Conversation

@Ar-maan05

Copy link
Copy Markdown
Contributor

store_loadavg_fixed_point() guarded both of its << LOADAVG_PRECISION_BITS shifts against overflow, but neither guard was effective:

  • The integer part was rejected with i >= (~0UL << LOADAVG_PRECISION_BITS). The shift direction is inverted: that bound is 0xFFFFFFFFFFFFF800, so it only rejects the topmost 2048 values of the range, while the smallest value that actually overflows i << 11 is 2^53. Everything from 2^53 up shifted out of the word and was accepted.

  • The fractional part was shifted first and range-checked afterwards, so a large f wrapped to a small value and then passed the f >= LOADAVG_FIXED_POINT_1_0 test.

Concretely, on 64 bit:

parse_loadavg_fixed_point("9007199254740992.00", &fp)  ->  0, fp == 0     (load reported as 0.00)
parse_loadavg_fixed_point("1.9007199254740992",  &fp)  ->  0, fp == 1.00

Both should be -ERANGE. The existing ASSERT_ERROR(..."18446744073709551615.5"..., ERANGE) passes only because ~0UL happens to fall inside the narrow band the broken guard does catch.

Validate both parts before shifting instead. Rejecting f >= 100 is exactly equivalent to the old post-shift comparison for every input that previously succeeded, since DIV_ROUND_UP(99 << 11, 100) == 2028 and DIV_ROUND_UP(100 << 11, 100) == 2048, so there is no behaviour change for valid input; only the values that used to wrap are now rejected.

Note this is a latent bug rather than a live one: the only caller of parse_loadavg_fixed_point() is read_resource_pressure(), which parses kernel-generated /proc/pressure/* and cgroup memory.pressure files where values are capped at 100.00, and every direct store_loadavg_fixed_point() caller passes permyriad-derived values already clamped to 0...10000. Fixing the guards keeps the helper correct for future callers.

Covered by new cases in test-parse-util, including the largest integer part that must still be accepted (2^53-1).

@github-actions github-actions Bot added util-lib tests please-review PR is ready for (re-)review by a maintainer labels Sep 14, 2026
@Ar-maan05
Ar-maan05 force-pushed the fix/loadavg-fixed-point-overflow-guards branch from 9646026 to efdc4b0 Compare September 14, 2026 00:27
@cdown
cdown merged commit d04aa85 into systemd:main Sep 14, 2026
59 of 62 checks passed
@github-actions github-actions Bot removed the please-review PR is ready for (re-)review by a maintainer label Sep 14, 2026
@bluca

bluca commented Sep 14, 2026

Copy link
Copy Markdown
Member

@cdown the clang-tidy job was failing in this PR, in the future please have a quick look at the CI results before merging, and when in doubt if something is an expected failure or not, please feel free to ask

#43757

@cdown

cdown commented Sep 14, 2026

Copy link
Copy Markdown
Member

@bluca My apologies! I looked but unfortunately missed it in the others

@bluca

bluca commented Sep 14, 2026

Copy link
Copy Markdown
Member

No worries, easy to fix

@Ar-maan05
Ar-maan05 deleted the fix/loadavg-fixed-point-overflow-guards branch September 14, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants