Skip to content

C#: add a query for a security decision based on a reverse DNS lookup #276

In progress

Description

@felickz

What to do

Add a C# query that reports a sensitive action that runs only when a host name from a reverse DNS lookup passes
a check (CWE-350, CWE-807). The standard cs/user-controlled-bypass misses it in two ways:

  1. Source: it treats only Dns.GetHostByAddress as a reverse lookup, not Dns.GetHostEntry or
    Dns.GetHostEntryAsync called with an IPAddress.
  2. Comparison: its taint step through == and Equals applies only when the other operand is a constant, so
    a comparison with a variable loses the flow.

Report this:

IPHostEntry hostInfo = Dns.GetHostEntry(peer);
if (hostInfo.HostName.Equals(trustedHostName))
{
    AuthorizeUser(user);
}

Do not report this:

// The decision uses the address, not a reverse lookup.
if (peer.Equals(IPAddress.Loopback)) { Login(user, password); }

// A forward lookup of a trusted name.
IPHostEntry trusted = Dns.GetHostEntry(TrustedHost);
if (Array.IndexOf(trusted.AddressList, peer) >= 0) { Login(user, password); }

The files below are a draft of this query from another workspace. It was written for another layout and has never run in this repository. Use it as a starting point: port it, prove it with its test, and change it wherever the test or this repository's conventions disagree with it.

Decisions in the draft

  • A separate query with its own id, which reuses the sinks and sanitizers of cs/user-controlled-bypass, so
    that it can run next to the standard suites. A sensitive action is what that query treats as one: login and
    authorization methods, and socket Send. A flow from Dns.GetHostByAddress is reported by both queries.
  • Sources: Dns.GetHostByAddress (every overload), and Dns.GetHostEntry and Dns.GetHostEntryAsync when
    their first parameter is an IPAddress.
  • Extra step: taint flows from either operand of a comparison to its result, even when the other operand is
    not a constant. It applies to this query only.
  • Known gaps: a check that guards an action that is not sensitive, such as writing a message, is not
    reported, because the safe variant of the same code makes the same comparison (the test marks it
    MISSING: Alert). GetHostEntry(string) called with an address literal, and the BeginGetHostEntry and
    EndGetHostEntry pair, are not modelled.
  • Metadata: @precision high, @security-severity 7.5, CWE-350, CWE-807 and CWE-247. The draft id
    cs/reverse-dns-user-controlled-bypass needs this repository's id format.
  • Test: inline expectations. The draft's options file references framework assemblies. If they do not
    resolve, use the shared stubs as the other tests here do.

Files

The paths below follow this repository's layout but are suggestions. The draft was written for another layout, so its .qlref and options files, and its .md query help, need adapting (most C# queries here use .qhelp).

csharp/src/security/CWE-350/ReverseDnsConditionalBypass.md

Host names returned by a reverse DNS lookup of an IP address are controlled by whoever controls the DNS (PTR) records for that address. An attacker can make their own IP address resolve to a trusted host name, so a condition that compares a reverse DNS host name with an expected value must not decide whether a sensitive action (such as logging a user in) is performed.

This query extends `cs/user-controlled-bypass` with reverse DNS sources it does not model: `Dns.GetHostEntry(IPAddress)` and `Dns.GetHostEntryAsync(IPAddress)`. It also models `Dns.GetHostByAddress`, which overlaps with the stock query for constant comparisons, and follows comparisons of the host name against non-constant values, such as a configured host name.

## Recommendation

Do not use reverse DNS to make security decisions. Authenticate the peer with a cryptographic mechanism (for example TLS client certificates), or, if network location must be used, compare the peer IP address against an allow-list of addresses.

## Example

In the following example, the user is logged in only when the reverse DNS host name of the peer matches a trusted host name. An attacker who controls the PTR record of their address can bypass this check.

```csharp
IPHostEntry hostInfo = Dns.GetHostEntry(peerAddress);
if (hostInfo.HostName.Equals("trusted.example.com"))
{
    Login(user, password);
}
```

Instead, base the decision on the peer address itself:

```csharp
if (AllowedAddresses.Contains(peerAddress))
{
    Login(user, password);
}
```

## References

- MITRE: [CWE-350: Reliance on Reverse DNS Resolution for a Security-Critical Action](https://cwe.mitre.org/data/definitions/350.html).
- MITRE: [CWE-807: Reliance on Untrusted Inputs in a Security Decision](https://cwe.mitre.org/data/definitions/807.html).

csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql

/**
 * @name Sensitive action guarded by reverse DNS lookup
 * @description Deciding whether to perform a sensitive action based on the host name returned
 *              by a reverse DNS lookup allows an attacker who controls the DNS (PTR) record of
 *              their IP address to bypass the check.
 * @kind path-problem
 * @problem.severity error
 * @security-severity 7.5
 * @precision high
 * @id cs/reverse-dns-user-controlled-bypass
 * @tags security
 *       external/cwe/cwe-350
 *       external/cwe/cwe-807
 *       external/cwe/cwe-247
 */

import csharp
import semmle.code.csharp.security.dataflow.ConditionalBypassQuery as ConditionalBypassQuery
import ReverseDnsConditionalBypassQuery
import ReverseDnsConditionalBypass::PathGraph

from ReverseDnsConditionalBypass::PathNode source, ReverseDnsConditionalBypass::PathNode sink
where ReverseDnsConditionalBypass::flowPath(source, sink)
select sink.getNode(), source, sink, "This condition guards a sensitive $@, but a $@ controls it.",
  sink.getNode().(ConditionalBypassQuery::Sink).getSensitiveMethodCall(), "action",
  source.getNode(), "reverse DNS host name"

csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll

/**
 * Provides a taint-tracking configuration for reasoning about sensitive actions that are
 * guarded by a condition on the result of a reverse DNS lookup (CWE-350).
 *
 * This reuses the sinks and sanitizers of `cs/user-controlled-bypass`, but adds reverse DNS
 * sources that are not modeled there (`Dns.GetHostEntry(IPAddress)` and
 * `Dns.GetHostEntryAsync(IPAddress)`). It also includes `Dns.GetHostByAddress`, whose constant
 * comparison coverage overlaps with the stock query, to detect non-constant host-name comparisons.
 */

import csharp
private import semmle.code.csharp.commons.ComparisonTest
private import semmle.code.csharp.frameworks.system.Net
private import semmle.code.csharp.security.dataflow.ConditionalBypassQuery as ConditionalBypassQuery

/**
 * A call to a `System.Net.Dns` method that performs a reverse DNS lookup of an IP address.
 * The host names in the resulting `IPHostEntry` are controlled by whoever controls the PTR
 * record of that address, so they must not be trusted for security decisions.
 */
class ReverseDnsLookupCall extends MethodCall {
  ReverseDnsLookupCall() {
    exists(SystemNetDnsClass dns, Method m | m = this.getTarget() and m.getDeclaringType() = dns |
      // `GetHostByAddress(string)` and `GetHostByAddress(IPAddress)` are always reverse lookups.
      m = dns.getGetHostByAddressMethod()
      or
      // `GetHostEntry(IPAddress)`, `GetHostEntryAsync(IPAddress)` and their overloads.
      m.hasName(["GetHostEntry", "GetHostEntryAsync"]) and
      m.getParameter(0).getType().(Class).hasFullyQualifiedName("System.Net", "IPAddress")
    )
  }
}

/** A reverse DNS lookup result, which may be controlled by an attacker. */
class ReverseDnsSource extends DataFlow::ExprNode {
  ReverseDnsSource() { this.getExpr() instanceof ReverseDnsLookupCall }
}

/**
 * A taint-tracking configuration for sensitive actions guarded by a condition on
 * the result of a reverse DNS lookup.
 */
private module ReverseDnsConditionalBypassConfig implements DataFlow::ConfigSig {
  predicate isSource(DataFlow::Node source) { source instanceof ReverseDnsSource }

  predicate isSink(DataFlow::Node sink) { sink instanceof ConditionalBypassQuery::Sink }

  predicate isBarrier(DataFlow::Node node) { node instanceof ConditionalBypassQuery::Sanitizer }

  /**
   * Holds if `pred` is an operand of a comparison `succ`. The default taint steps only
   * propagate through a comparison when the other operand is a constant, but any comparison
   * of a reverse DNS host name (for example against an allow-list held in a variable or a
   * field) is a decision based on reverse DNS.
   */
  predicate isAdditionalFlowStep(DataFlow::Node pred, DataFlow::Node succ) {
    exists(ComparisonTest ct |
      ct.getExpr() = succ.asExpr() and
      ct.getAnArgument() = pred.asExpr()
    )
  }

  predicate observeDiffInformedIncrementalMode() { any() }

  Location getASelectedSinkLocation(DataFlow::Node sink) {
    result = sink.getLocation()
    or
    result = sink.(ConditionalBypassQuery::Sink).getSensitiveMethodCall().getLocation()
  }
}

/** Taint-tracking flow for sensitive actions guarded by a reverse DNS lookup result. */
module ReverseDnsConditionalBypass = TaintTracking::Global<ReverseDnsConditionalBypassConfig>;

csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsAccessControl.cs

using System;
using System.Net;
using System.Threading.Tasks;

public class ReverseDnsAccessControl
{
    private const string TrustedHost = "www.domain.nonexistenttld";

    // BAD: reverse DNS of the peer address (GetHostEntry(IPAddress)) decides whether to log in.
    public void GetHostEntryBad(IPAddress peer, string user, string password)
    {
        IPHostEntry hostInfo = Dns.GetHostEntry(peer); // $ Source=r1
        if (hostInfo.HostName.Equals(TrustedHost)) // $ Alert=r1
        {
            Login(user, password);
        }
    }

    // BAD: reverse DNS with a substring comparison on the host name.
    public void GetHostEntryEndsWithBad(string user, string password)
    {
        IPAddress peer = IPAddress.Parse("127.0.0.1");
        IPHostEntry hostInfo = Dns.GetHostEntry(peer); // $ Source=r2
        if (hostInfo.HostName.EndsWith(".nonexistenttld")) // $ Alert=r2
        {
            Login(user, password);
        }
    }

    // BAD: asynchronous reverse DNS lookup.
    public async Task GetHostEntryAsyncBad(IPAddress peer, string user, string password)
    {
        IPHostEntry hostInfo = await Dns.GetHostEntryAsync(peer); // $ Source=r3
        if (hostInfo.HostName == TrustedHost) // $ Alert=r3
        {
            Login(user, password);
        }
    }

    // BAD: obsolete reverse DNS API (already covered by cs/user-controlled-bypass).
    public void GetHostByAddressBad(IPAddress peer, string user, string password)
    {
#pragma warning disable CS0618
        IPHostEntry hostInfo = Dns.GetHostByAddress(peer); // $ Source=r4
#pragma warning restore CS0618
        if (hostInfo.HostName == TrustedHost) // $ Alert=r4
        {
            Login(user, password);
        }
    }

    // BAD: reverse DNS host name compared with a non-constant (configured) host name.
    public void GetHostEntryConfiguredHostBad(IPAddress peer, string configuredHost, string user, string password)
    {
        IPHostEntry hostInfo = Dns.GetHostEntry(peer); // $ Source=r5
        if (hostInfo.HostName == configuredHost) // $ Alert=r5
        {
            Login(user, password);
        }
    }

    // GOOD: the reverse DNS host name is only logged, the sensitive action does not depend on it.
    public void GetHostEntryLogOnlyGood(IPAddress peer, string user, string password)
    {
        IPHostEntry hostInfo = Dns.GetHostEntry(peer);
        if (hostInfo.HostName.Equals(TrustedHost))
        {
            Console.WriteLine(hostInfo.HostName);
        }
        Login(user, password);
    }

    // GOOD: authorization decision is based on the peer IP address, not on reverse DNS.
    public void IpAddressAllowListGood(IPAddress peer, string user, string password)
    {
        if (peer.Equals(IPAddress.Loopback))
        {
            Login(user, password);
        }
    }

    // GOOD: forward DNS lookup of a constant host name, compared with the peer address.
    public void ForwardLookupGood(IPAddress peer, string user, string password)
    {
        IPHostEntry trusted = Dns.GetHostEntry(TrustedHost);
        if (Array.IndexOf(trusted.AddressList, peer) >= 0)
        {
            Login(user, password);
        }
    }

    // GOOD: the same sensitive action happens on both branches.
    public void BothBranchesGood(IPAddress peer, string user, string password)
    {
        IPHostEntry hostInfo = Dns.GetHostEntry(peer);
        if (hostInfo.HostName == TrustedHost)
        {
            Login(user, password);
        }
        else
        {
            Login(user, password);
        }
    }

    private static void Login(string user, string password) { }
}

csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.expected

#select
| ReverseDnsLocalComparison.cs:26:13:26:52 | call to method Equals | ReverseDnsLocalComparison.cs:25:32:25:62 | call to method GetHostEntry : IPHostEntry | ReverseDnsLocalComparison.cs:26:13:26:52 | call to method Equals | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsLocalComparison.cs:28:13:28:31 | call to method AuthorizeUser | action | ReverseDnsLocalComparison.cs:25:32:25:62 | call to method GetHostEntry | reverse DNS host name |
| ReverseDnsAccessControl.cs:13:13:13:49 | call to method Equals | ReverseDnsAccessControl.cs:12:32:12:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:13:13:13:49 | call to method Equals | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsAccessControl.cs:15:13:15:33 | call to method Login | action | ReverseDnsAccessControl.cs:12:32:12:53 | call to method GetHostEntry | reverse DNS host name |
| ReverseDnsAccessControl.cs:24:13:24:29 | access to property HostName | ReverseDnsAccessControl.cs:23:32:23:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:24:13:24:29 | access to property HostName | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsAccessControl.cs:26:13:26:33 | call to method Login | action | ReverseDnsAccessControl.cs:23:32:23:53 | call to method GetHostEntry | reverse DNS host name |
| ReverseDnsAccessControl.cs:34:13:34:44 | ... == ... | ReverseDnsAccessControl.cs:33:38:33:64 | call to method GetHostEntryAsync : Task<IPHostEntry> | ReverseDnsAccessControl.cs:34:13:34:44 | ... == ... | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsAccessControl.cs:36:13:36:33 | call to method Login | action | ReverseDnsAccessControl.cs:33:38:33:64 | call to method GetHostEntryAsync | reverse DNS host name |
| ReverseDnsAccessControl.cs:46:13:46:44 | ... == ... | ReverseDnsAccessControl.cs:44:32:44:57 | call to method GetHostByAddress : IPHostEntry | ReverseDnsAccessControl.cs:46:13:46:44 | ... == ... | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsAccessControl.cs:48:13:48:33 | call to method Login | action | ReverseDnsAccessControl.cs:44:32:44:57 | call to method GetHostByAddress | reverse DNS host name |
| ReverseDnsAccessControl.cs:56:13:56:47 | ... == ... | ReverseDnsAccessControl.cs:55:32:55:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:56:13:56:47 | ... == ... | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsAccessControl.cs:58:13:58:33 | call to method Login | action | ReverseDnsAccessControl.cs:55:32:55:53 | call to method GetHostEntry | reverse DNS host name |
edges
| ReverseDnsLocalComparison.cs:25:21:25:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsLocalComparison.cs:26:13:26:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsLocalComparison.cs:25:32:25:62 | call to method GetHostEntry : IPHostEntry | ReverseDnsLocalComparison.cs:25:21:25:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsLocalComparison.cs:26:13:26:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsLocalComparison.cs:26:13:26:29 | access to property HostName : String | provenance | MaD:1892 |
| ReverseDnsLocalComparison.cs:26:13:26:29 | access to property HostName : String | ReverseDnsLocalComparison.cs:26:13:26:52 | call to method Equals | provenance | Config |
| ReverseDnsAccessControl.cs:12:21:12:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:13:13:13:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:12:32:12:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:12:21:12:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:13:13:13:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:13:13:13:29 | access to property HostName : String | provenance | MaD:1892 |
| ReverseDnsAccessControl.cs:13:13:13:29 | access to property HostName : String | ReverseDnsAccessControl.cs:13:13:13:49 | call to method Equals | provenance |  |
| ReverseDnsAccessControl.cs:13:13:13:29 | access to property HostName : String | ReverseDnsAccessControl.cs:13:13:13:49 | call to method Equals | provenance | Config |
| ReverseDnsAccessControl.cs:23:21:23:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:24:13:24:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:23:32:23:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:23:21:23:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:24:13:24:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:24:13:24:29 | access to property HostName | provenance | MaD:1892 |
| ReverseDnsAccessControl.cs:33:21:33:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:34:13:34:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:33:38:33:64 | call to method GetHostEntryAsync : Task<IPHostEntry> | ReverseDnsAccessControl.cs:33:21:33:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:34:13:34:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:34:13:34:29 | access to property HostName : String | provenance | MaD:1892 |
| ReverseDnsAccessControl.cs:34:13:34:29 | access to property HostName : String | ReverseDnsAccessControl.cs:34:13:34:44 | ... == ... | provenance |  |
| ReverseDnsAccessControl.cs:34:13:34:29 | access to property HostName : String | ReverseDnsAccessControl.cs:34:13:34:44 | ... == ... | provenance | Config |
| ReverseDnsAccessControl.cs:44:21:44:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:46:13:46:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:44:32:44:57 | call to method GetHostByAddress : IPHostEntry | ReverseDnsAccessControl.cs:44:21:44:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:46:13:46:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:46:13:46:29 | access to property HostName : String | provenance | MaD:1892 |
| ReverseDnsAccessControl.cs:46:13:46:29 | access to property HostName : String | ReverseDnsAccessControl.cs:46:13:46:44 | ... == ... | provenance |  |
| ReverseDnsAccessControl.cs:46:13:46:29 | access to property HostName : String | ReverseDnsAccessControl.cs:46:13:46:44 | ... == ... | provenance | Config |
| ReverseDnsAccessControl.cs:55:21:55:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:56:13:56:20 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:55:32:55:53 | call to method GetHostEntry : IPHostEntry | ReverseDnsAccessControl.cs:55:21:55:28 | access to local variable hostInfo : IPHostEntry | provenance |  |
| ReverseDnsAccessControl.cs:56:13:56:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsAccessControl.cs:56:13:56:29 | access to property HostName : String | provenance | MaD:1892 |
| ReverseDnsAccessControl.cs:56:13:56:29 | access to property HostName : String | ReverseDnsAccessControl.cs:56:13:56:47 | ... == ... | provenance | Config |
nodes
| ReverseDnsLocalComparison.cs:25:21:25:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsLocalComparison.cs:25:32:25:62 | call to method GetHostEntry : IPHostEntry | semmle.label | call to method GetHostEntry : IPHostEntry |
| ReverseDnsLocalComparison.cs:26:13:26:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsLocalComparison.cs:26:13:26:29 | access to property HostName : String | semmle.label | access to property HostName : String |
| ReverseDnsLocalComparison.cs:26:13:26:52 | call to method Equals | semmle.label | call to method Equals |
| ReverseDnsAccessControl.cs:12:21:12:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:12:32:12:53 | call to method GetHostEntry : IPHostEntry | semmle.label | call to method GetHostEntry : IPHostEntry |
| ReverseDnsAccessControl.cs:13:13:13:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:13:13:13:29 | access to property HostName : String | semmle.label | access to property HostName : String |
| ReverseDnsAccessControl.cs:13:13:13:49 | call to method Equals | semmle.label | call to method Equals |
| ReverseDnsAccessControl.cs:23:21:23:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:23:32:23:53 | call to method GetHostEntry : IPHostEntry | semmle.label | call to method GetHostEntry : IPHostEntry |
| ReverseDnsAccessControl.cs:24:13:24:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:24:13:24:29 | access to property HostName | semmle.label | access to property HostName |
| ReverseDnsAccessControl.cs:33:21:33:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:33:38:33:64 | call to method GetHostEntryAsync : Task<IPHostEntry> | semmle.label | call to method GetHostEntryAsync : Task<IPHostEntry> |
| ReverseDnsAccessControl.cs:34:13:34:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:34:13:34:29 | access to property HostName : String | semmle.label | access to property HostName : String |
| ReverseDnsAccessControl.cs:34:13:34:44 | ... == ... | semmle.label | ... == ... |
| ReverseDnsAccessControl.cs:44:21:44:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:44:32:44:57 | call to method GetHostByAddress : IPHostEntry | semmle.label | call to method GetHostByAddress : IPHostEntry |
| ReverseDnsAccessControl.cs:46:13:46:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:46:13:46:29 | access to property HostName : String | semmle.label | access to property HostName : String |
| ReverseDnsAccessControl.cs:46:13:46:44 | ... == ... | semmle.label | ... == ... |
| ReverseDnsAccessControl.cs:55:21:55:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:55:32:55:53 | call to method GetHostEntry : IPHostEntry | semmle.label | call to method GetHostEntry : IPHostEntry |
| ReverseDnsAccessControl.cs:56:13:56:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry |
| ReverseDnsAccessControl.cs:56:13:56:29 | access to property HostName : String | semmle.label | access to property HostName : String |
| ReverseDnsAccessControl.cs:56:13:56:47 | ... == ... | semmle.label | ... == ... |
subpaths

csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.qlref

query: ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.ql
postprocess:
  - utils/test/InlineExpectationsTestQuery.ql

csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsLocalComparison.cs

using System;
using System.Net;

// A reverse lookup compared with a host name held in a local variable.
public class ReverseDnsLocalComparison
{
    public void Bad()
    {
        string secretHostname = "www.domain.nonexistenttld";
        IPAddress hostIPAddress = IPAddress.Parse("127.0.0.1");
        IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress);
        // The guarded action only prints a message, which is not modeled as a sensitive
        // action by `cs/user-controlled-bypass`, so the condition is not a sink.
        if (hostInfo.HostName.Equals(secretHostname)) // $ MISSING: Alert
        {
            Console.WriteLine("Access granted.");
        }
    }

    // Same source, but the guarded action is an authorization method.
    public void BadWithAuthorize(string user)
    {
        string secretHostname = "www.domain.nonexistenttld";
        IPAddress hostIPAddress = IPAddress.Parse("127.0.0.1");
        IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress); // $ Source=j1
        if (hostInfo.HostName.Equals(secretHostname)) // $ Alert=j1
        {
            AuthorizeUser(user);
        }
    }

    public void Good()
    {
        string secretHostname = "www.domain.nonexistenttld";
        IPAddress hostIPAddress = IPAddress.Parse("127.0.0.1");
        IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress);
        if (hostInfo.HostName.Equals(secretHostname))
        {
            Console.WriteLine(hostInfo.HostName);
        }
    }

    private static void AuthorizeUser(string user) { }
}

csharp/test/security/CWE-350/ReverseDnsConditionalBypass/options

semmle-extractor-options: /r:System.Net.Primitives.dll /r:System.Net.NameResolution.dll /r:System.Runtime.Extensions.dll

How to check it

  1. Port the files and run the test of the new directory. Compare the results with what the draft expects (its
    inline $ Alert and $ MISSING: Alert comments and its .expected file), and explain every difference in
    the pull request.
  2. Then run the other gates of the agent profile: format, compile with warnings as errors, query help, suite
    selection and id uniqueness.

Rules for the change

  • Add only the new query, its library file if it has one, its query help and its test directory.
  • Pull request title: [New Query - <id>] - <what it detects>.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions