diff --git a/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.qhelp b/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.qhelp new file mode 100644 index 00000000..8ff084ca --- /dev/null +++ b/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.qhelp @@ -0,0 +1,66 @@ + + + + +

+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 standard query for constant comparisons, and follows +comparisons of the host name against non-constant values, such as a configured host name. +

+
+ + +

+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. +

+
+ + +

+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. +

+ + + +

+Instead, base the decision on the peer address itself: +

+ + +
+ + +
  • +MITRE: CWE-350: Reliance on Reverse DNS Resolution for a Security-Critical Action. +
  • +
  • +MITRE: CWE-807: Reliance on Untrusted Inputs in a Security Decision. +
  • +
  • +MITRE: CWE-247: Reliance on DNS Lookups in a Security Decision. +
  • +
    +
    diff --git a/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql b/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql new file mode 100644 index 00000000..48baefbd --- /dev/null +++ b/csharp/src/security/CWE-350/ReverseDnsConditionalBypass.ql @@ -0,0 +1,27 @@ +/** + * @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 githubsecuritylab/cs/reverse-dns-user-controlled-bypass + * @tags security + * experimental + * 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" diff --git a/csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll b/csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll new file mode 100644 index 00000000..a5c89717 --- /dev/null +++ b/csharp/src/security/CWE-350/ReverseDnsConditionalBypassQuery.qll @@ -0,0 +1,73 @@ +/** + * 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; diff --git a/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsAccessControl.cs b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsAccessControl.cs new file mode 100644 index 00000000..333fcc16 --- /dev/null +++ b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsAccessControl.cs @@ -0,0 +1,107 @@ +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 + if (hostInfo.HostName.Equals(TrustedHost)) // BAD: reported + { + 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 + if (hostInfo.HostName.EndsWith(".nonexistenttld")) // BAD: reported + { + 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 + if (hostInfo.HostName == TrustedHost) // BAD: reported + { + 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 +#pragma warning restore CS0618 + if (hostInfo.HostName == TrustedHost) // BAD: reported + { + 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 + if (hostInfo.HostName == configuredHost) // BAD: reported + { + 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) { } +} diff --git a/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.expected b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.expected new file mode 100644 index 00000000..ca41bdca --- /dev/null +++ b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.expected @@ -0,0 +1,67 @@ +#select +| 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 | 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 | +| ReverseDnsLocalComparison.cs:27:13:27:52 | call to method Equals | ReverseDnsLocalComparison.cs:26:32:26:62 | call to method GetHostEntry : IPHostEntry | ReverseDnsLocalComparison.cs:27:13:27:52 | call to method Equals | This condition guards a sensitive $@, but a $@ controls it. | ReverseDnsLocalComparison.cs:29:13:29:31 | call to method AuthorizeUser | action | ReverseDnsLocalComparison.cs:26:32:26:62 | call to method GetHostEntry | reverse DNS host name | +edges +| 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:1 | +| 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:1 | +| 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 | 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:1 | +| 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:1 | +| 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:1 | +| ReverseDnsAccessControl.cs:56:13:56:29 | access to property HostName : String | ReverseDnsAccessControl.cs:56:13:56:47 | ... == ... | provenance | Config | +| ReverseDnsLocalComparison.cs:26:21:26:28 | access to local variable hostInfo : IPHostEntry | ReverseDnsLocalComparison.cs:27:13:27:20 | access to local variable hostInfo : IPHostEntry | provenance | | +| ReverseDnsLocalComparison.cs:26:32:26:62 | call to method GetHostEntry : IPHostEntry | ReverseDnsLocalComparison.cs:26:21:26:28 | access to local variable hostInfo : IPHostEntry | provenance | | +| ReverseDnsLocalComparison.cs:27:13:27:20 | access to local variable hostInfo : IPHostEntry | ReverseDnsLocalComparison.cs:27:13:27:29 | access to property HostName : String | provenance | MaD:1 | +| ReverseDnsLocalComparison.cs:27:13:27:29 | access to property HostName : String | ReverseDnsLocalComparison.cs:27:13:27:52 | call to method Equals | provenance | Config | +models +| 1 | Summary: System.Net; IPHostEntry; false; get_HostName; (); ; Argument[this]; ReturnValue; taint; manual | +nodes +| 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 | semmle.label | call to method GetHostEntryAsync : Task | +| 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 | ... == ... | +| ReverseDnsLocalComparison.cs:26:21:26:28 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry | +| ReverseDnsLocalComparison.cs:26:32:26:62 | call to method GetHostEntry : IPHostEntry | semmle.label | call to method GetHostEntry : IPHostEntry | +| ReverseDnsLocalComparison.cs:27:13:27:20 | access to local variable hostInfo : IPHostEntry | semmle.label | access to local variable hostInfo : IPHostEntry | +| ReverseDnsLocalComparison.cs:27:13:27:29 | access to property HostName : String | semmle.label | access to property HostName : String | +| ReverseDnsLocalComparison.cs:27:13:27:52 | call to method Equals | semmle.label | call to method Equals | +subpaths diff --git a/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.qlref b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.qlref new file mode 100644 index 00000000..2585ae56 --- /dev/null +++ b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsConditionalBypass.qlref @@ -0,0 +1,2 @@ +query: security/CWE-350/ReverseDnsConditionalBypass.ql +postprocess: TestUtilities/PrettyPrintModels.ql diff --git a/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsLocalComparison.cs b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsLocalComparison.cs new file mode 100644 index 00000000..07ffb3ce --- /dev/null +++ b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/ReverseDnsLocalComparison.cs @@ -0,0 +1,46 @@ +using System; +using System.Net; + +// A reverse lookup compared with a host name held in a local variable. +public class ReverseDnsLocalComparison +{ + // BAD: the guarded action is not sensitive, so this is a known false negative. + 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)) // BAD: not reported (false negative) + { + Console.WriteLine("Access granted."); + } + } + + // BAD: 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 + if (hostInfo.HostName.Equals(secretHostname)) // BAD: reported + { + AuthorizeUser(user); + } + } + + // GOOD: the host name is only printed, no sensitive action depends on it. + 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) { } +} diff --git a/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/options b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/options new file mode 100644 index 00000000..5a44fb73 --- /dev/null +++ b/csharp/test/security/CWE-350/ReverseDnsConditionalBypass/options @@ -0,0 +1 @@ +semmle-extractor-options: /r:System.Net.Primitives.dll /r:System.Net.NameResolution.dll /r:System.Runtime.Extensions.dll