Skip to content

Android drive enumeration returns escaped mount paths that cannot be used with filesystem APIs #137813

Closed

Description

@krwq

Description

DriveInfo.GetDrives(), Environment.GetLogicalDrives(), and Directory.GetLogicalDrives() return mount-table escape sequences instead of actual directory names on Android when a mount destination contains a space or backslash. Using those returned names then fails even when the real mount is accessible to the app.

This is a follow-up to the Linux path-decoding work in dotnet/runtime#137195, which fixed the desktop Linux report dotnet/runtime#123347. The Android case was found during follow-up inspection, not in that original customer report. No separate Android customer report was found in the issue search.

The Android condition has now been reproduced on an Android emulator with real bind mounts, real procfs data, Bionic native calls, and a normal application process. It is not just a Windows test with simulated OS checks. However, the mounts were deliberately created for this test using root-assisted setup. This does not establish that ordinary Android USB/SD-card mounts are affected: Android normally names those paths using filesystem UUIDs rather than user-facing volume labels.

The expanded checks also found costs in the proposed implementation: using managed mountinfo enumeration was approximately 1.8-1.9 times slower and allocated 3.6-4.2 times as much memory in the emulator measurements. It also throws if mountinfo exists but cannot be read, whereas the old native path can still enumerate through /proc/mounts. These trade-offs need review; the path-spelling fix should not be read as an unconditional recommendation to merge the one-line routing change.

Reproduction Steps

Environment

Component Configuration used
Host Windows x64, Windows Hypervisor Platform acceleration
Emulator Android Emulator 37.2.13.0
System images Official default AOSP x86_64 images: Android 16/API 36 revision 2 and Android 11/API 30 revision 11
Android 16 fingerprint Android/sdk_phone64_x86_64/emu64x:16/BE2A.250530.026.D1/13818094:userdebug/test-keys
Android 11 fingerprint Android/sdk_phone_x86_64/generic_x86_64:11/RSR1.210722.013.A2/10067904:userdebug/test-keys
.NET SDK 10.0.404 in an isolated installation
Android workload 36.1.118
Java Microsoft OpenJDK 17.0.14
App runtime .NET 10.0.15
Managed process Normal Android application, UID 10150 on API 36 and UID 10130 on API 30, SELinux untrusted_app domain
SELinux Enforcing throughout

The emulator was created specifically for the test. The app was launched as an ordinary Android activity. No storage permissions or SELinux policy changes were made to make filesystem access succeed.

Two complete public System.IO.FileSystem.DriveInfo assemblies were also rebuilt from the inspected repository's product sources, compiled against .NET 10 reference APIs for the installed Android runtime, and separately packaged into the app. Both had the expected assembly identity and public key token. They differed only in the enumeration guard. Their loaded module IDs were checked inside the Android app to ensure that the changed library, rather than a cached original DLL, really ran.

Set up accessible escaped destinations

Inside the app's private files directory, create a source directory containing marker.txt, plus three empty destination directories named:

  • USB Disk
  • Back\Slash
  • Literal\040Name, where \040 is literal filename text

Keep the app process running and obtain its PID. From the root-capable AOSP emulator's adb shell, enter that process's mount namespace and create bind mounts:

# Replace these with the PID and private directory of the test app.
pid=2495
root=/data/user/0/org.dotnet.mountprobe.ade8021/files/mount-probe

nsenter -t "$pid" -m -- mount -o bind "$root/source" "$root/USB Disk"
nsenter -t "$pid" -m -- mount -o bind "$root/source" "$root/Back\Slash"
nsenter -t "$pid" -m -- mount -o bind "$root/source" "$root/Literal\040Name"

Only mount setup uses root. The C# API calls must continue in the existing normal app process, not in a root or adb-shell process.

Query the paths from the app

Capture /proc/self/mountinfo and /proc/mounts from that same app process. Then enumerate all three public APIs and inspect each returned path:

string mountinfo = File.ReadAllText("/proc/self/mountinfo");
string mounts = File.ReadAllText("/proc/mounts");

string[] logicalDrives = Environment.GetLogicalDrives();
string[] directoryDrives = Directory.GetLogicalDrives();

foreach (DriveInfo drive in DriveInfo.GetDrives())
{
    Console.WriteLine(
        $"{drive.Name}: exists={Directory.Exists(drive.Name)}, " +
        $"ready={drive.IsReady}, type={drive.DriveType}, " +
        $"root={drive.RootDirectory.FullName}");

    // For the affected entries, these throw DriveNotFoundException:
    // drive.DriveFormat
    // drive.TotalSize
}

Compare the returned names with the actual bind destinations. Confirm that a correctly spelled destination exists and can read the source marker. Account for Android aliases: in this run, the files directory was reachable through /data/user/0/..., but procfs reported the canonical /data/data/... destination.

Expected behavior

Enumeration returns actual mount destinations, decoded once:

Destination Expected returned suffix
Directory containing a space USB Disk
Directory containing a backslash Back\Slash
Directory containing literal escape text Literal\040Name

An accessible mount should not become an inaccessible path merely because the returned name still contains procfs escaping. This does not mean every system mount is accessible to an Android app.

Actual behavior

The installed public APIs and the original shared enumeration helper returned:

Actual destination Returned suffix
USB Disk USB\040Disk
Back\Slash Back\134Slash
Literal\040Name Literal\134040Name

For all three returned paths, the same normal app observed:

  • Directory.Exists was false.
  • DriveInfo.IsReady was false.
  • DriveInfo.DriveType was NoRootDirectory.
  • DriveInfo.RootDirectory referred to the encoded, nonexistent spelling.
  • DriveInfo.DriveFormat and TotalSize threw DriveNotFoundException.

The actual decoded directories existed and the app could read marker.txt through each bind mount.

With the rebuilt changed public library installed, DriveInfo.GetDrives() itself returned the decoded names on both Android versions. The app could use those results directly: the accessible entries were ready, reported Fixed and ext4, returned a positive total size, and exposed the source marker. This goes beyond calling a source-linked internal helper.

The expanded cases also covered actual tab and newline characters, Unicode, two stacked mounts at the same destination, and a root-owned inaccessible directory. Stacked duplicate entries were retained and both resolved to the top layer's marker. Correcting a denied mount's name did not grant access: file reads and directory listing still threw UnauthorizedAccessException.

Regression?

The first affected Android release has not been determined.

The original desktop Linux report was a .NET 10 regression relative to .NET 8 and 9. That does not establish the same regression range on Android.

Known Workarounds

If the actual accessible mount destination is already known, construct DriveInfo with that correctly spelled path instead of using the escaped enumeration result.

Do not indiscriminately replace escape sequences in filenames or decode twice. For example, the mount-table text \134040 represents a literal backslash followed by 040, not a space.

Android storage APIs and the Storage Access Framework remain appropriate for storage discovery and access where required. Correcting mount-path spelling does not bypass scoped storage or SELinux.

Configuration

The controlled runtime reproduction used the Android and .NET versions listed above. It was not performed on a physical device or with an ordinary external-storage volume.

The source under investigation is dotnet/runtime commit a33eebbe8e6cc76c22f30ac9b9f166ac6469d088, plus a one-line local change. Here, "original" means that commit's Linux-only enumeration guard; "changed" means the same code with Android included.

The Android app used the installed .NET 10 Mono runtime. It ran both a source-linked internal-helper comparison and a separately rebuilt public DriveInfo library comparison. The public library was retargeted to the .NET 10 assembly contract for this test; this was not a complete .NET 12 runtime build.

CoreLib was not replaced. Consequently, the installed Environment.GetLogicalDrives() and Directory.GetLogicalDrives() remained original in all runs. Their failures were reproduced, but corrected versions of those two public entrypoints were not deployed.

Other information

Cause and proposed change

The native Android enumeration path reads /proc/mounts through Bionic's getmntent_r(). Bionic splits the fields but leaves octal path escapes intact. In contrast, the managed /proc/self/mountinfo parser decodes those paths after dotnet/runtime#137195.

Interop.Sys.GetAllMountPoints() selects the managed path only for OperatingSystem.IsLinux(), which is false on Android. Android already uses the shared mountinfo parser for filesystem-format lookup, following dotnet/runtime#123617.

The proposed product change is:

-if (OperatingSystem.IsLinux())
+if (OperatingSystem.IsLinux() || OperatingSystem.IsAndroid())

This reuses the existing parser. It does not introduce a new public API or change which native fallback is selected when mountinfo is unavailable. It does change performance and behavior when mountinfo is present but empty or unreadable, as measured below.

Validation completed

Check Result
Ordinary Android mount population Original and changed enumeration agreed: 160 mounts on Android 16 and 100 on Android 11.
Expanded controlled mounts Ten additional mount entries on each OS, including a stacked duplicate: 170 entries on Android 16 and 110 on Android 11. Only escaped destination spellings differed.
Installed public APIs All three original public enumeration APIs reproduced the incorrect encoded names.
Rebuilt public DriveInfo library, original versus changed DriveInfo.GetDrives() reproduced the failure with the original library and returned correct names with the changed library on both Android versions. Loaded module IDs confirmed the intended DLLs.
Changed shared helper on Android Passed on both OS versions with real OS checks, procfs and native-library bindings.
Usability of decoded results Accessible entries existed, read the source marker, were ready, reported Fixed/ext4, and returned a positive total size.
Literal escape preservation Literal\134040Name became Literal\040Name, not Literal Name.
Tabs, newlines and Unicode Preserved as actual characters in usable returned paths on both OS versions.
Stacked mounts and duplicates Duplicate destinations remained duplicated; the visible top-layer marker was read.
Permission boundary A denied mount remained unreadable; marker reads and directory enumeration still threw UnauthorizedAccessException.
Present but empty mountinfo Controlled Android 16 file bind: original native enumeration returned 161 entries; changed enumeration and changed public DriveInfo returned an empty array.
Present but unreadable mountinfo Controlled Android 16 file bind: original native enumeration returned 161 entries; changed enumeration and changed public DriveInfo threw UnauthorizedAccessException.
In-tree portable routing regression All 22 cases were discovered. Nine Android-routing cases failed against the exact original product source; all 22 passed with the change.
Full Common.Tests on Windows 1,779 passed, 0 failed or skipped.
Windows DriveInfo tests 24 passed, 0 failed or skipped.
Android-targeted managed DriveInfo implementation build Passed with 0 warnings or errors.

The portable regression uses an isolated, non-shipping test assembly that compiles the actual product helper and decoder with test-only platform and file substitutes. That is distinct from the emulator run, which used real Android bindings.

The broader multi-target Android cross-build encountered APICompat attribute errors that also reproduce with the one-line fix removed. No suppression was added.

Performance measurements

BenchmarkDotNet 0.15.8 ran inside the actual normal Android app using Mono 10.0.15, optimized Release builds, the in-process no-emit toolchain, three warmup iterations and eight measurement iterations with 512 calls per iteration. Each benchmark returned the actual original or changed helper's array. No mounts were changed during measurement.

These are emulator microbenchmarks of enumeration over ordinary mounts, not end-to-end application latency or physical-device performance:

Environment Mount entries Original native mean Changed managed mean Original allocated/call Changed allocated/call
Android 16/API 36 x64 160 253.1 microseconds 460.3 microseconds 18,392 B 66,800 B
Android 11/API 30 x64 100 201.0 microseconds 379.6 microseconds 10,256 B 43,488 B

The extra CPU and allocation cost is expected from reading and parsing the whole mountinfo file in managed code rather than receiving just mount destinations from the native reader. The measurements establish a trade-off, not an optimization. Decoding only the affected native results is a possible alternative to evaluate; it is not implemented or validated by this investigation.

Evidence boundaries

The public DriveInfo comparison used the repository's actual implementation files and resources, built into an assembly named System.IO.FileSystem.DriveInfo, Version=10.0.0.0, PublicKeyToken=b03f5f7f11d50a3a. The original module ID was ff16723d-db07-4131-b99e-c99174d884ed; the changed module ID was 9dde93ab-67f7-4190-a504-b033fc39e287. Both were observed from the app's loaded typeof(DriveInfo).Assembly, so the comparison did not rely only on filenames or a source patch.

The native runtime and CoreLib remained from the installed .NET 10 runtime. A complete matching .NET 12 CoreLib/runtime before/after deployment was not achieved. The separate Linux build attempt failed before compilation because its NuGet audit endpoint could not resolve. An available .NET 12 native runtime package was from a different source commit and was not treated as a matching replacement.

The experimental CoreCLR Android APK built, but aborted at launch with Missing pinvoke [email protected], including after a clean rebuild. A narrower enumeration-only CoreCLR APK also built; its additional run was blocked by emulator package-service failures. No successful CoreCLR result is claimed, and these attempts are not evidence that the one-line patch causes a CoreCLR regression.

An app-only namespace control intended to remove mountinfo also interfered with native /proc/mounts: original and changed code both returned empty arrays. It is therefore not counted as successful preservation of the native mount population. The original procfs view was restored and both implementations again returned 100 mounts. Native-fallback selection remains directly covered by the portable repository tests.

There is still no separately reported Android customer case, no physical-device result, and no evidence that simply giving an SD card or USB drive a label containing a space triggers this on stock Android.

Only x86_64 emulator execution is covered. No ARM hardware, NativeAOT Android execution, fully rebuilt CoreLib, broad device matrix or physical-device performance result is claimed.

Reproduction and cleanup notes

The probe and library were built with an isolated SDK; no system workload or Windows feature change was required. The signed library builds used the repository's product C# files, native interop files, decoder and generated resources. The only behavioral difference between their builds was the IsAndroid() guard.

The APK builds used Release configuration with AOT, trimming and ReadyToRun disabled for the probe. For the public-library comparison, an immutable private runtime-pack copy supplied the original or changed DriveInfo DLL; the pristine installed private SDK pack was not modified. Rebuilding the APK and checking the loaded module ID avoided stale assembly reuse.

All managed queries ran under the normal app UID with SELinux enforcing. Root entered that app's namespace only to create and remove controlled mounts. The mount tables were read by the managed app itself, not substituted with a fake procfs path in the successful reproduction.

The controlled bind mounts and procfs-file controls were removed after use. Raw JSON reports, app-captured mount tables, build logs, benchmark JSON and verification scripts were retained locally.

References

Note

Generated with GitHub Copilot.

Activity

  1. self-assigned this
    on Nov 18, 2026
  2. added
    untriaged
    on Nov 18, 2026
  3. removed
    untriaged
    on Nov 21, 2026
  4. added a commit that references this issue on Nov 21, 2026
    4a6fc8a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions