Skip to content

Guard CUDA LayerNorm/RMSNorm int32 offset range - #31650

Merged
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
msrc/cuda-layernorm-offset-overflow-fix
Aug 5, 2026
Merged

Guard CUDA LayerNorm/RMSNorm int32 offset range#31650
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
msrc/cuda-layernorm-offset-overflow-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds input validation checks to prevent integer overflow issues during CUDA kernel indexing in the LayerNorm and RMSNorm CUDA operators. The main goal is to ensure that the product of num_rows and norm_size does not exceed INT_MAX, which could lead to incorrect behavior or crashes.

Input validation for CUDA kernel indexing:

  • Added a check in LayerNorm::ComputeInternal (in layer_norm.cc) to return an error if num_rows * norm_size exceeds INT_MAX, preventing integer overflow during CUDA kernel indexing.
  • Added a similar check in RMSNorm::ComputeInternal (in rms_norm.cc) to ensure the input size does not exceed CUDA kernel indexing limits.

Code maintenance:

  • Included the <limits> header in both layer_norm.cc and rms_norm.cc to support the new input validation logic. [1] [2]

Reject inputs where num_rows * norm_size exceeds INT_MAX before launching kernels that use int32 row offsets.

This prevents overflow in per-row pointer offset arithmetic in LayerNormalization and RMSNormalization CUDA paths.

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

This PR adds input-size validation to the CUDA implementations of LayerNormalization and RMSNormalization to prevent int32 overflow in CUDA kernel indexing (notably around num_rows * norm_size).

Changes:

  • Added a pre-launch guard in LayerNorm::ComputeInternal to reject inputs where num_rows * norm_size exceeds INT_MAX.
  • Added a similar guard in RMSNorm::ComputeInternal.
  • Included <limits> to support the new bounds checks.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/core/providers/cuda/nn/layer_norm.cc Adds an INT_MAX-based size guard before launching the CUDA LayerNorm kernel.
onnxruntime/core/providers/cuda/nn/rms_norm.cc Adds the same INT_MAX-based size guard for CUDA RMSNorm (LayerNorm-based) execution.

Comment thread onnxruntime/core/providers/cuda/nn/rms_norm.cc Outdated
Comment thread onnxruntime/core/providers/cuda/nn/layer_norm.cc
…tion

This commit addresses the security vulnerability identified in PR #31650 review comments
from Copilot reviewer regarding integer overflow in host-side CUDA kernel indexing
calculations.

SECURITY FIX (Copilot Comments on LayerNorm and RMSNorm):
- Enhanced validation to prevent integer overflow in HostApplyLayerNorm calculations
- Original validation allowed norm_size == INT_MAX when num_rows == 1
- This caused overflow in host arithmetic: (n2 + 4 * warp_size - 1)
- New validation uses two-pronged approach:
  * Checks that norm_size doesn't exceed INT_MAX - 256 (accounts for warp_size overhead)
  * Still checks that num_rows * norm_size doesn't exceed INT_MAX
  * Either check failing rejects the input

DETAILED CHANGES:

LayerNormalization (layer_norm.cc):
- Added constexpr MAX_WARP_FACTOR = 256 (conservative upper bound for 4 * warp_size)
- Compute MAX_NORM_SIZE = INT_MAX - MAX_WARP_FACTOR
- Validate: norm_size <= MAX_NORM_SIZE AND norm_size <= INT_MAX / num_rows
- Prevents: (n2 + 4 * warp_size - 1) overflow for n2 = norm_size
- Prevents: num_rows * norm_size > INT_MAX overflow

RMSNormalization (rms_norm.cc):
- Applied identical validation as LayerNormalization
- Both use the same HostApplyLayerNorm function, same vulnerability pattern
- Ensures consistency across both normalization operators

BENEFITS:
✅ Prevents undefined behavior from integer overflow
✅ Blocks exploitation attempts using extreme input sizes
✅ Maintains safety margin for host-side arithmetic calculations
✅ Conservative bounds ensure correctness

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@apsonawane
Akshay Sonawane (apsonawane) merged commit 24f1aed into main Aug 5, 2026
93 of 94 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the msrc/cuda-layernorm-offset-overflow-fix branch August 5, 2026 23:02
Tianlei Wu (tianleiwu) pushed a commit that referenced this pull request Aug 6, 2026
This pull request adds input validation checks to prevent integer
overflow issues during CUDA kernel indexing in the LayerNorm and RMSNorm
CUDA operators. The main goal is to ensure that the product of
`num_rows` and `norm_size` does not exceed `INT_MAX`, which could lead
to incorrect behavior or crashes.

Input validation for CUDA kernel indexing:

* Added a check in `LayerNorm::ComputeInternal` (in `layer_norm.cc`) to
return an error if `num_rows * norm_size` exceeds `INT_MAX`, preventing
integer overflow during CUDA kernel indexing.
* Added a similar check in `RMSNorm::ComputeInternal` (in `rms_norm.cc`)
to ensure the input size does not exceed CUDA kernel indexing limits.

Code maintenance:

* Included the `<limits>` header in both `layer_norm.cc` and
`rms_norm.cc` to support the new input validation logic.
[[1]](diffhunk://#diff-ebda3d3b7054f5d14c679ebe8e6520a2c84a5a558d8fdcc0798c08e7032345feR9)
[[2]](diffhunk://#diff-adebea99f15800767eb7b84d40be4873e81ca4df99ffe9dc7f849eabe0a3c563R9)

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit 24f1aed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants