Skip to content

osd: fix UBSan shift overflow in pg_pool_t::calc_pg_masks - #67458

Merged
SrinivasaBharath merged 1 commit into
ceph:mainfrom
baum:pgmask-ubsan-shift-overflow
Aug 10, 2026
Merged

SrinivasaBharath merged 1 commit into
ceph:mainfrom
baum:pgmask-ubsan-shift-overflow

Conversation

@baum

@baum baum commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

UBSan reports: shift exponent 32 is too large for 32-bit type 'int'

When pg_num or pgp_num is 0, pg_num-1 wraps to UINT_MAX (unsigned), cbits() returns 32, and (1 << 32) on 32-bit int is undefined behavior.

Fix: use 1ULL for the shift so the expression is well-defined; for cbits() returning 32, (1ULL << 32) - 1 equals UINT_MAX.

No unit test added: the bug is undefined behavior, not a deterministic wrong result — only UBSan reliably detects it.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

Comment thread src/osd/osd_types.cc Resolved
Comment thread src/test/crimson/CMakeLists.txt Resolved
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@github-actions github-actions Bot removed the stale label Jun 23, 2026
@baum
baum force-pushed the pgmask-ubsan-shift-overflow branch from fd58ada to c0469b6 Compare July 8, 2026 07:23
@baum
baum requested a review from a team as a code owner July 8, 2026 07:23
@baum
baum force-pushed the pgmask-ubsan-shift-overflow branch from c0469b6 to c692eb9 Compare July 8, 2026 07:37
@baum

baum commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@tchaikov thank you for review. could you take a second look?

@baum

baum commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

@tchaikov could you take another look please?

Comment thread src/test/osd/types.cc Resolved
Comment thread src/test/osd/types.cc Resolved
UBSan reports: shift exponent 32 is too large for 32-bit type 'int'

When pg_num or pgp_num is 0, pg_num-1 wraps to UINT_MAX (unsigned),
cbits() returns 32, and (1 << 32) on 32-bit int is undefined behavior.

Use 1ULL for the shift so the expression is well-defined; for cbits()
returning 32, (1ULL << 32) - 1 equals UINT_MAX.

Signed-off-by: Alexander Indenbaum <[email protected]>
@baum
baum force-pushed the pgmask-ubsan-shift-overflow branch from c692eb9 to a964f35 Compare July 23, 2026 03:14
Copilot AI review requested due to automatic review settings July 23, 2026 03:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes undefined behavior in pg_pool_t::calc_pg_masks() when PG counts are zero.

Changes:

  • Uses 64-bit shift operands to safely handle a 32-bit shift.
  • Documents the zero-count edge case.

@tchaikov tchaikov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath5-testing-20260804.033808-main.

@NitzanMordhai

Copy link
Copy Markdown
Contributor

@SrinivasaBharath
SrinivasaBharath merged commit 124dd00 into ceph:main Aug 10, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants