Skip to content

Fix filesystem type detection for stacked Linux mounts - #137748

Merged
krwq merged 1 commit into
mainfrom
krwq-stacked-mount-selection
Nov 20, 2026
Merged

krwq merged 1 commit into
mainfrom
krwq-stacked-mount-selection

Conversation

@krwq

@krwq krwq commented Nov 17, 2026

Copy link
Copy Markdown
Member

Follow-up to #137195, which fixed escaped mount paths reported in #123347. This addresses offline feedback about stacked-mount selection, separately from escaped-path decoding.

Problem

Linux allows another filesystem to be mounted at a directory that is already a mount point. File access uses the new mount, but /proc/self/mountinfo still lists the earlier one. The current lookup stops at the first exact-path match, so DriveInfo can report the hidden filesystem instead of the one the path actually reaches.

Choosing the longest matching path is not sufficient either: mounting a new filesystem at a parent directory also hides mounts below the old parent.

Both cases were reproduced with real mounts on Ubuntu 24.04.5, Linux 6.12.115-adc.2 x64, using installed .NET 11.0.0:

Setup Queried path Actual DriveFormat / DriveType Expected
ext4 bind mount, then tmpfs at the same target target ext4 / Fixed tmpfs / Ram
Same setup target/child tmpfs / Ram tmpfs / Ram
An additional ext4 child mount created before the parent tmpfs mount New target/child inside tmpfs ext4 / Fixed tmpfs / Ram

Native statfs reported tmpfs for every queried path. The proposed selector also returned tmpfs for every path when run against the same live mount table.

Why fix this

This affects behavior, not just the filesystem name displayed to a user. For example, code filtering on DriveType.Fixed accepts the tmpfs mount even though it should be classified as Ram. The directory and its child can also disagree despite accessing the same filesystem.

The issue was identified through offline feedback and confirmed with a controlled Linux reproduction, rather than an exact matching customer report. This is a correctness fix for the metadata returned by the API; it does not change mounts, file access, or kernel permissions.

Change

Follow mount IDs and parent IDs to select the filesystem that is actually reachable. This handles both same-directory stacks and child mounts hidden by a newer parent mount, without assuming record order or selecting the largest mount ID.

Mount identifiers remain spans in the shared parser and are converted only for matching paths. Mount enumeration, escaped-path decoding, and native interop are unchanged. No newer kernel feature is required.

Validation

Results so far

  • Installed .NET 11.0.0: both live mount setups above reproduced the incorrect public API results. All test mounts were private and removed afterward; the original namespace's mount table was unchanged.
  • Proposed selector: compiled into a standalone harness and returned the native filesystem type for all queried paths in both live setups.
  • Windows tests: 1,833 Common.Tests, 24 DriveInfo tests, and 15 RuntimeInformation tests passed. Coverage includes 64 selection cases with reordered records and 12 identifier-preservation cases.
  • Linux-targeted managed builds of DriveInfo and Process, including shared CoreLib, cross-compiled on Windows: passed, with no warnings or errors.

The live comparison exercised the proposed helper, not a rebuilt patched public DriveInfo assembly. That end-to-end patched API run remains outstanding. .NET 10, autofs, and optical media were not exercised.

Reproduce the public API bug

No PR, CI, or runtime build is needed. The following recipe was run successfully with the installed .NET 11 SDK/runtime.

Use a new empty directory on a non-tmpfs/non-ramfs filesystem. This requires Linux, Bash, GNU coreutils, util-linux, the .NET 11 SDK/targeting pack, and permission to create a private mount namespace and mounts (CAP_SYS_ADMIN). Root inside a restricted container may still lack that permission. The script refuses to mount anything in the original namespace.

Self-contained reproduction

Save this as Program.cs:

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Runtime.InteropServices;

Console.WriteLine($"Runtime: {RuntimeInformation.FrameworkDescription}");
Console.WriteLine($"Namespace: {new FileInfo("/proc/self/ns/mnt").LinkTarget}");
foreach (string path in args)
{
    DriveInfo drive = new(path);
    Console.WriteLine($"{drive.Name}: {drive.DriveFormat}/{drive.DriveType}");
}

Save this as repro.sh. The local SDK and build configuration files keep the standalone project independent of the runtime repository's build settings.

#!/usr/bin/env bash
set -euo pipefail
case "$(stat -f -c %T .)" in
    tmpfs|ramfs) echo 'Unsupported: use a non-tmpfs/non-ramfs backing directory.' >&2; exit 1 ;;
esac

printf '{"sdk":{"version":"11.0.100","rollForward":"latestFeature"}}\n' > global.json
printf '<Project />\n' | tee Directory.Build.props > Directory.Build.targets
printf '<configuration><packageSources><clear /></packageSources></configuration>\n' > NuGet.Config
printf '<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net11.0</TargetFramework></PropertyGroup></Project>\n' > StackedMountRepro.csproj
export DOTNET_CLI_TELEMETRY_OPTOUT=1 DOTNET_NOLOGO=1 DOTNET_CLI_HOME="$PWD/.dotnet-home"
dotnet build StackedMountRepro.csproj -c Release

unshare --mount --propagation private bash -s -- "$(readlink /proc/self/ns/mnt)" "${HIDDEN_CHILD:-0}" <<'MOUNTS'
set -euo pipefail
test "$(readlink /proc/self/ns/mnt)" != "$1" || { echo 'Refusing original mount namespace.' >&2; exit 99; }
mount --make-rprivate /
mkdir -- lower target hidden-lower
mkdir -- lower/child
mounts=()
cleanup() {
    result=$?
    trap - EXIT
    for ((i=${#mounts[@]}-1; i>=0; i--)); do
        umount -- "${mounts[i]}" || result=1
    done
    rmdir -- target lower/child lower hidden-lower || result=1
    exit "$result"
}
trap cleanup EXIT

mount --bind "$PWD/lower" "$PWD/target"
mounts+=("$PWD/target")
if [[ "$2" == 1 ]]; then
    mount --bind "$PWD/hidden-lower" "$PWD/target/child"
    mounts+=("$PWD/target/child")
fi
mount -t tmpfs -o size=4m,nosuid,nodev tmpfs "$PWD/target"
mounts+=("$PWD/target")
mkdir -- target/child
stat -f -c '%n: %T' target target/child
dotnet "$PWD/bin/Release/net11.0/StackedMountRepro.dll" "$PWD/target" "$PWD/target/child"
MOUNTS

Run the entire script for each setup:

bash ./repro.sh
HIDDEN_CHILD=1 bash ./repro.sh

On the tested ext4 host, the first run printed ext4/Fixed for the target and tmpfs/Ram for the child. The second printed ext4/Fixed for both. In both runs, stat -f reported tmpfs for both paths.

The correct public API result is tmpfs/Ram for both paths in both setups. The incorrectly selected lower type depends on the backing filesystem. A zero script exit code means the setup and cleanup succeeded; compare the printed API results with stat -f to detect the bug.

Validate the patch

From the repository root on Linux, build the normal component baseline and run the regression suites:

./build.sh clr+libs -rc Release
./dotnet.sh build src/libraries/Common/tests/Common.Tests.csproj /t:Test /p:RuntimeConfiguration=Release
./dotnet.sh build src/libraries/System.IO.FileSystem.DriveInfo/tests/System.IO.FileSystem.DriveInfo.Tests.csproj /t:Test /p:RuntimeConfiguration=Release
./dotnet.sh build src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj /t:Test /p:RuntimeConfiguration=Release

For the remaining end-to-end check, run the same probe inside the same private mount setup using the patched repository testhost instead of the installed runtime. Confirm that it loads the rebuilt DriveInfo assembly and returns tmpfs/Ram for both paths in both setups.

Tradeoff

Exact-path lookups now scan the whole mount table to account for other mounts at the same directory or above it, rather than stopping at the first match. The selector retains only matching path prefixes and adds no native calls. This is a correctness change, not a performance optimization.

Fixes #137745

Note

This pull request was opened with GitHub Copilot.

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

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

LGTM, thank you for fixing it @krwq !

@krwq

krwq commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-android

@krwq

krwq commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-androidemulator

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@krwq

krwq commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

CI failures analysis (Copilot generated)

No reviewed failure was attributed to this PR. New trackers: 3 for failed CI + 2 for non-blocking warnings.

What failed CI impact Issue status Issue / related work
macOS agents disconnected during the build Failed CI Existing issue dotnet/dnceng#1886
Replacement macOS agents never connected within 45 minutes Failed CI New issue dotnet/dnceng#6784; same error text as closed dotnet/dnceng#3435
iOS test apps could not be installed (devicectl error 49) Failed CI Existing issue #123796
Windows x86 HTTP metrics test recorded an unexpected HttpRequestException tag Failed CI New issue #137980; same test/assertion as closed #96407
Android expired-certificate test got NoError instead of NotTimeValid Failed CI Existing issue #133314
Android ARM64 DNS tests resolved localhost. to a non-loopback address Failed CI New issue #137979; existing fix proposal #128771
Android x64 emulator HTTP suite timed out after 30 minutes Failed CI Existing issue #137701
Windows compiler-cache restore could not extract its archive (4 jobs) Warning only; build continued New issue dotnet/dnceng#6783
Windows ARM64 post-build compiler-cache step exited with code 57005 Warning only; build continued New issue dotnet/dnceng#6782
Red pipeline, Helix monitor and Build Analysis/Insights checks Summaries of the failures above No separate issue Not additional defects; do not count them again

@krwq

krwq commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

/ba-g unrelated failures, issues are tracked as in the table above

@krwq
krwq merged commit cfe5c10 into main Nov 20, 2026
110 of 116 checks passed
@krwq
krwq deleted the krwq-stacked-mount-selection branch November 20, 2026 08:36
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 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.

DriveInfo reports ext4/Fixed instead of tmpfs/Ram for stacked Linux mounts

2 participants