Skip to content

Treat a missing /etc like a missing /etc/resolv.conf in NetworkInformation - #135569

Merged
MihaZupan merged 2 commits into
dotnet:mainfrom
caraioniurie47:issue-135568
Oct 12, 2026
Merged

MihaZupan merged 2 commits into
dotnet:mainfrom
caraioniurie47:issue-135568

Conversation

@caraioniurie47

@caraioniurie47 caraioniurie47 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

On Linux, NetworkInterface.GetAllNetworkInterfaces() throws System.IO.DirectoryNotFoundException when /etc does not exist. /etc/resolv.conf is read with File.ReadAllText, which maps ENOENT to DirectoryNotFoundException when the parent directory is missing, and the reader catches only FileNotFoundException and UnauthorizedAccessException. The two /etc/resolv.conf readers in UnixIPInterfaceProperties, used by the BSD and Apple implementations, catch only FileNotFoundException.

The read now goes through one internal helper, StringParsingHelpers.ReadResolvConfFile, which returns null for FileNotFoundException, DirectoryNotFoundException and UnauthorizedAccessException. All three readers use it, so a missing /etc gives no DNS information, as a missing /etc/resolv.conf already did. UnauthorizedAccessException was ignored only by the Linux reader; the BSD/Apple readers now ignore it too. No public API change.

Tests: a new theory in DnsParsingTests checks that the helper returns null for a missing file and for a file under a missing directory; with the helper's catch narrowed to FileNotFoundException (a Windows run), the directory case fails with DirectoryNotFoundException. A new fact checks that the helper returns null when given a directory; with UnauthorizedAccessException removed from the helper's catch, it fails with that exception (a Windows run). The existing resolv.conf parsing tests now read their input through the helper. The System.Net.NetworkInformation functional tests passed on Windows; on Linux all passed except IPGlobalProperties_DomainName_ReturnsEmptyStringWhenNotSet, which fails with and without this change on the test machine (DomainName returns "localdomain" there). The repro from the issue passed with the fix, both with no /etc and with an /etc that has no resolv.conf. The BSD and Apple implementations were compiled but not run.

Resolves #135568

Note

AI-generated, written at my direction and reviewed by me before posting. Verified on dotnet/runtime 9bfbe1d with the first commit of this change applied. On Ubuntu 22.04.5 LTS x64 on WSL2 (Linux 6.18.40.1), after ./build.sh clr+libs -rc Release, with ./.dotnet/dotnet build src/libraries/System.Net.NetworkInformation/src, ./.dotnet/dotnet build src/libraries/System.Net.NetworkInformation/tests/FunctionalTests /t:Test, and the issue's repro commands run as root (dotnet build -c Release -o out with the repo's SDK, then ./run.sh "$(command -v dotnet)" out/repro.dll with artifacts/bin/testhost/net11.0-linux-Debug-x64 first on PATH). On Windows x64, after build.cmd clr+libs -rc checked, with dotnet.cmd build src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj /p:TargetOS=<os> for linux, freebsd and osx (each built every target framework of the library) and the functional tests via /t:Test; the new theory was also run with the helper's catch narrowed to FileNotFoundException, where its etc/resolv.conf case failed with DirectoryNotFoundException. These runs predate the review commit that moved the UnauthorizedAccessException catch into the helper; that commit was built on Windows for every target framework of the library, and the functional tests passed on Windows.

…ation

When /etc does not exist, reading /etc/resolv.conf throws
DirectoryNotFoundException rather than FileNotFoundException, because
ENOENT with a missing parent directory is mapped to
DirectoryNotFoundException. The resolv.conf readers only caught
FileNotFoundException, so NetworkInterface.GetAllNetworkInterfaces()
threw on Linux. The readers used by the BSD and Apple
IPInterfaceProperties had the same catch.

Move the read into a shared StringParsingHelpers.ReadResolvConfFile
helper that returns null for both FileNotFoundException and
DirectoryNotFoundException, and use it from all three readers. A
missing /etc now yields no DNS information, as a missing resolv.conf
already did. The Linux reader still also ignores
UnauthorizedAccessException, as before.

Fix dotnet#135568

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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 dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Only the Linux reader ignored UnauthorizedAccessException from
/etc/resolv.conf; the BSD and Apple readers let it escape. Handle it in
the shared helper so all three readers treat an unreadable resolv.conf
like a missing one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

This seems reasonable to me, thank you

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused implementation correctly addresses the reported failure with appropriate cross-platform consistency and regression coverage.

0 open findings

What changed in this PR

Fixes NetworkInterface.GetAllNetworkInterfaces() when /etc or resolv.conf is unavailable by centralizing resilient resolver configuration reads.

Changes:

  • Handles missing directories, missing files, and denied access consistently.
  • Updates Linux, BSD, and Apple call paths.
  • Adds regression coverage for unavailable resolver configuration.
File Description
DnsParsingTests.cs Tests missing and inaccessible resolver configuration.
UnixIPInterfaceProperties.cs Uses the shared resolver reader.
StringParsingHelpers.Dns.cs Adds the resilient file-reading helper.
LinuxNetworkInterface.cs Uses the helper for Linux DNS properties.

🧠 Review effort: Balanced

@MihaZupan
MihaZupan merged commit 4be74d1 into dotnet:main Oct 12, 2026
89 checks passed
@MihaZupan MihaZupan added this to the 12.0.0 milestone Oct 12, 2026
@caraioniurie47
caraioniurie47 deleted the issue-135568 branch October 12, 2026 20:49
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Net community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NetworkInterface.GetAllNetworkInterfaces throws DirectoryNotFoundException on Linux when /etc does not exist

3 participants