Skip to content

[DML EP] Fix wide string handling in OpKernelInfoWrapper::GetWideName - #31656

Merged
fdwr merged 1 commit into
microsoft:mainfrom
miaobin:dml-wide-name
Aug 5, 2026
Merged

[DML EP] Fix wide string handling in OpKernelInfoWrapper::GetWideName#31656
fdwr merged 1 commit into
microsoft:mainfrom
miaobin:dml-wide-name

Conversation

@miaobin

@miaobin miaobin commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Description

GetWideName passes the source length explicitly to MultiByteToWideChar, so the API never appends a null terminator. When the converted node name needs exactly as many code units as the caller's buffer holds, the call succeeds, returns bufferSizeInChars, and does not set ERROR_INSUFFICIENT_BUFFER, so writing the terminator at outputName[charsCopiedIfSucceeded] lands one wchar_t past the end of the buffer.

Motivation and Context

Clamp the index so the terminator stays in bounds, truncating the last converted char in that case. Truncation is already part of the contract: both in-tree callers in DmlOperator.cpp use a fixed wchar_t[512] and note "might truncate name", and the ERROR_INSUFFICIENT_BUFFER branch below already writes to bufferSizeInChars - 1. The sibling GetUtf8Name reserves room the same way via bufferSizeInBytes - 1.

…eName

GetWideName passes the source length explicitly to MultiByteToWideChar, so
the API never appends a null terminator. When the converted node name needs
exactly as many code units as the caller's buffer holds, the call succeeds,
returns bufferSizeInChars, and does not set ERROR_INSUFFICIENT_BUFFER, so
writing the terminator at outputName[charsCopiedIfSucceeded] lands one
wchar_t past the end of the buffer.

Clamp the index so the terminator stays in bounds, truncating the last
converted char in that case. The sibling GetUtf8Name reservesroom the same
way via bufferSizeInBytes - 1.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@miaobin miaobin changed the title [DML EP] Fix OOB write in OpKernelInfoWrapper::GetWideName [DML EP] Fix wide string handling in OpKernelInfoWrapper::GetWideName Aug 5, 2026
@miaobin

miaobin commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

@fdwr PTAL, thanks!
cc/ @huningxin @ibelem @adrastogi

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 fixes a potential out-of-bounds write in the DML EP’s OpKernelInfoWrapper::GetWideName when converting UTF-8 node names to wide strings via MultiByteToWideChar. The issue occurs because MultiByteToWideChar does not append a null terminator when the source length is explicitly provided, so a successful conversion can exactly fill the caller’s buffer, and writing the terminator at outputName[charsCopied] can land one wchar_t past the end.

Changes:

  • Clamp the null-terminator write index to bufferSizeInChars - 1 on successful conversion, preserving the “may truncate name” contract while preventing OOB writes.
  • Add inline comments documenting the MultiByteToWideChar behavior and rationale for clamping/truncation.
Show a summary per file
File Description
onnxruntime/core/providers/dml/DmlExecutionProvider/src/MLOperatorAuthorImpl.cpp Prevents an off-by-one write when null-terminating the converted wide node name buffer.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

@fdwr fdwr 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.

So the case where this would happen is when the name is exactly 512 characters? 511 characters would be fine (since charsCopiedIfSucceeded == 511), and 513 characters would be fine too (falling through the ERROR_INSUFFICIENT_BEFFER path where bufferSizeInChars - 1 = 511). Thanks for fixing 🛠️.

@fdwr
fdwr merged commit 4e48876 into microsoft:main Aug 5, 2026
86 checks passed
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