Skip to content

Throw PNSE if insufficient privileges when custom Ping payload was specified #64625

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

Merged
merged 5 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libraries/System.Net.Ping/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@
<data name="SystemNetPing_PlatformNotSupported" xml:space="preserve">
<value>System.Net.Ping is not supported on this platform.</value>
</data>
</root>
<data name="net_ping_utility_custom_payload" xml:space="preserve">
<value>Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ private Process GetPingProcess(IPAddress address, byte[] buffer, int timeout, Pi
throw new PlatformNotSupportedException(SR.net_ping_utility_not_found);
}

// although the ping utility supports custom pattern via -p option, it supports
// specifying only up to 16B pattern which repeats in the payload. The option also might
// not be present in all distributions, so we forbid ping payload in general.
if (buffer != DefaultSendBuffer && buffer != Array.Empty<byte>())
{
throw new PlatformNotSupportedException(SR.net_ping_utility_custom_payload);
}

UnixCommandLinePing.PingFragmentOptions fragmentOption = UnixCommandLinePing.PingFragmentOptions.Default;
if (options != null && address.AddressFamily == AddressFamily.InterNetwork)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public PingReply Send(IPAddress address, int timeout, byte[] buffer, PingOptions
{
return SendPingCore(addressSnapshot, buffer, timeout, options);
}
catch (Exception e)
catch (Exception e) when (e is not PlatformNotSupportedException)
{
throw new PingException(SR.net_ping, e);
}
Expand Down Expand Up @@ -336,7 +336,7 @@ private async Task<PingReply> SendPingAsyncInternal(IPAddress address, int timeo
Task<PingReply> pingReplyTask = SendPingAsyncCore(addressSnapshot, buffer, timeout, options);
return await pingReplyTask.ConfigureAwait(false);
}
catch (Exception e)
catch (Exception e) when (e is not PlatformNotSupportedException)
{
throw new PingException(SR.net_ping, e);
}
Expand Down Expand Up @@ -388,7 +388,7 @@ private PingReply GetAddressAndSend(string hostNameOrAddress, int timeout, byte[
IPAddress[] addresses = Dns.GetHostAddresses(hostNameOrAddress);
return SendPingCore(addresses[0], buffer, timeout, options);
}
catch (Exception e)
catch (Exception e) when (e is not PlatformNotSupportedException)
{
throw new PingException(SR.net_ping, e);
}
Expand All @@ -407,7 +407,7 @@ private async Task<PingReply> GetAddressAndSendAsync(string hostNameOrAddress, i
Task<PingReply> pingReplyTask = SendPingAsyncCore(addresses[0], buffer, timeout, options);
return await pingReplyTask.ConfigureAwait(false);
}
catch (Exception e)
catch (Exception e) when (e is not PlatformNotSupportedException)
{
throw new PingException(SR.net_ping, e);
}
Expand Down
Loading