Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerating drive C in WSL fails with UnauthorizedAccessException #112035

Open
markusschaber opened this issue Jan 31, 2025 · 2 comments
Open

Enumerating drive C in WSL fails with UnauthorizedAccessException #112035

markusschaber opened this issue Jan 31, 2025 · 2 comments
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@markusschaber
Copy link

markusschaber commented Jan 31, 2025

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 31, 2025
Copy link
Contributor

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

@markusschaber
Copy link
Author

markusschaber commented Feb 3, 2025

Additional hint found by a teammate: Even explicitly setting the IngoreInaccessible EnumerationOption does not catch the exception, the following snipped aborts with the exception just as with the default settings.

        foreach (var info in dir.EnumerateFileSystemInfos("*", new EnumerationOptions{IgnoreInaccessible = true}))
            Console.WriteLine(info.Name);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

1 participant