Skip to content

Enumerating drive C in WSL fails with UnauthorizedAccessException #112035

Open
@markusschaber

Description

@markusschaber

Description

When trying to enumerate the C drive of the Windows host in WSL 2, the enumeration aborts with an UnauthorizedAccessException as soon as it hits certain entries, for example DumpStack.log.tmp, hiberfil.sys, pagefile.sys or swapfile.sys.

Reproduction Steps

namespace WslFileSystemException;

static class Program
{
    static void Main(string[] args)
    {
        var dir = new DirectoryInfo(args.SingleOrDefault("/mnt/c"));

        // Simple reproduction
        Console.WriteLine($"Trying to list {dir.FullName}");
        foreach (var info in dir.EnumerateFileSystemInfos())
            Console.WriteLine(info.Name);      
    }
}

Expected behavior

The directory is listed, returning an Entry object for each entry in the file system.

Actual behavior

Unhandled exception. System.UnauthorizedAccessException: Access to the path '/mnt/c/DumpStack.log.tmp' is denied.
 ---> System.IO.IOException: Permission denied
   --- End of inner exception stack trace ---
   at System.IO.FileStatus.ThrowOnCacheInitializationError(ReadOnlySpan`1 path)
   at System.IO.FileSystemInfo.Create(String fullPath, String fileName, Boolean asDirectory, FileStatus& fileStatus)
   at System.IO.Enumeration.FileSystemEnumerator`1.MoveNext()
   at WslFileSystemException.Program.Main(String[] args) in /mnt/d/WorkingCopies/playgrounds/SinglePlaygrounds/WslFileSystemException/Program.cs:line 11

Regression?

No response

Known Workarounds

Wrap the enumeration in your own code, catching the exception in the MoveNext()-Method, and resume iteration. (Relying on this behavior might be a violation of the iterator protocol, but it seems to work for now. The "forbidden" entries are skipped, but the rest of the drive contents show up.)

        using var enumerator = dir.EnumerateFileSystemInfos().GetEnumerator();
        while (true)
        {
            try
            {
                if (!enumerator.MoveNext())
                    break;
                Console.WriteLine(enumerator.Current.Name);
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine($"Caught {ex.Message}, trying to resume...");
            }
        }

[...]
OneDriveTemp
Caught Access to the path '/mnt/c/pagefile.sys' is denied., trying to resume...
PerfLogs
Program Files
Program Files (x86)
ProgramData
Programme
Recovery
Caught Access to the path '/mnt/c/swapfile.sys' is denied., trying to resume...
System Volume Information
[...]

Configuration

WSL-Version: 2.3.26.0
Kernelversion: 5.15.167.4-1
WSLg-Version: 1.0.65
MSRDC-Version: 1.2.5620
Direct3D-Version: 1.611.1-81528511
DXCore-Version: 10.0.26100.1-240331-1435.ge-release
Windows-Version: 10.0.19045.5371

/proc/version: Linux version 5.15.167.4-microsoft-standard-WSL2 (root@f9c826d3017f) (gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.37) #1 SMP Tue Nov 5 00:21:55 UTC 2024

Ubuntu 22.04.5 LTS
Dotnet 9.0.102 (but also reproducible with .NET 8)

Other information

The 'ls' command in Linux also has problems showing the entries, but at least it doesn't fail completely. The entries in question show up as:

-????????? ? ? ? ? ? DumpStack.log.tmp
-????????? ? ? ? ? ? hiberfil.sys
-????????? ? ? ? ? ? pagefile.sys
-????????? ? ? ? ? ? swapfile.sys

Metadata

Metadata

Assignees

Labels

area-System.IOuntriagedNew issue has not been triaged by the area owner

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions