Skip to content

Fix format-string misuse in PAL_GetTransportName - #137010

Merged
jkotas merged 1 commit into
dotnet:mainfrom
filipnavara:transport-name
Nov 5, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
filipnavara:transport-name

Conversation

@filipnavara

Copy link
Copy Markdown
Member

PAL_GetTransportName builds the IPC transport name (the diagnostics server socket dotnet-diagnostic-<pid>-<key>-socket and the debugger transport pipes) by concatenating the directory with "%s-%d-%llu-%s". The directory comes from GetTempPathA, meaning $TMPDIR, or on Apple with an application group ID, from the app-group container folder. The concatenated string was then used as the snprintf format:

strncat_s(formatBuffer, MAX_TRANSPORT_NAME_LENGTH, IpcNameFormat, strlen(IpcNameFormat));
int chars = snprintf(name, MAX_TRANSPORT_NAME_LENGTH, formatBuffer, prefix, id, disambiguationKey, suffix);

If the directory contains % (for example TMPDIR=/tmp/%s%n/), snprintf takes the wrong arguments: it can read an integer as a pointer for %s or write through %n. This code runs at every startup, from the diagnostics server (ds_rt_transport_get_default_name) and from the debugger pipe setup.

NativeAOT already does this correctly in ds_rt_aot_transport_get_default_name (src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp). It passes the directory as an argument to a constant format:

format_result = snprintf(name, name_len, "%s%s-%d-%llu-%s", format_buffer, prefix, id, (unsigned long long)disambiguation_key, suffix);

Changes

  • Pass the directory to snprintf as a %s argument with a literal format string, matching NativeAOT. The strncat_s step and the IpcNameFormat global are removed. Because the format is now a literal, -Wformat can check the arguments.
  • Cast the disambiguation key to unsigned long long for %llu.
  • Replace the debug-only _ASSERTE on the result with a real check. If the name is truncated or snprintf fails, name is left empty, which is the function's existing failure convention.
  • Read the directory through the PathCharString instead of the raw buffer pointer. On the Apple app-group path, GetApplicationContainerFolder can reallocate the string, which would leave that pointer pointing at freed memory. Also reject GetTempPathA results that leave no room for the name.

For directories without %, the generated name is byte-for-byte the same as before, so debuggers and diagnostic tools that compute it independently (dotnet-trace, dotnet-counters, dbgshim) still connect.

Testing

  • ./build.sh clr.runtime -c Debug on osx-arm64.
  • Started the runtime with DOTNET_DefaultDiagnosticPortSuspend=1 and listed the diagnostics socket:
    • Normal TMPDIR: dotnet-diagnostic-<pid>-<key>-socket, the same name as before.
    • TMPDIR=.../%s%n%x/ and TMPDIR=.../%s%s%s%s%s%s%s%s%s%s%n/: the runtime starts normally and the socket is created inside the literal % directory.

PAL_GetTransportName built the IPC transport name by concatenating the
directory (GetTempPathA, i.e. $TMPDIR, or the macOS app-group container
folder) with "%s-%d-%llu-%s" and then using the result as the snprintf
format string. A directory containing '%' (e.g. TMPDIR=/tmp/%s%n/) made
snprintf consume the wrong arguments, dereference integers as pointers,
or perform %n writes. This runs at every startup via the diagnostics
server and the debugger transport pipes.

Pass the directory as a "%s" argument instead, cast the disambiguation
key for %llu explicitly, and replace the debug-only assert with a real
truncation/error check that leaves the name empty on failure (the
existing failure convention for callers). The resulting name is
byte-for-byte identical for paths without '%', so debuggers and
diagnostic tools that compute it independently are unaffected.

Also read the directory through the PathCharString rather than the raw
buffer pointer, which could be stale after GetApplicationContainerFolder
reallocates, and reject GetTempPathA results that leave no room for the
name.
@filipnavara
filipnavara marked this pull request as ready for review November 5, 2026 10:32
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas
jkotas merged commit 129c63e into dotnet:main Nov 5, 2026
102 of 104 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
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.

2 participants