TExcelTAD
Home › Network Security: Discovery, Segmentation, Firewalls, VPN & IDS › Rule Order, Shadowing and Redundancy

Rule Order, Shadowing and Redundancy

Firewalls read rules top to bottom and stop at the first rule with a terminal verdict. That makes order part of the policy: the same rules in a different order are a different firewall. Rulesets that grow over years develop three order problems a reviewer must find:

  • Shadowing: a rule can never match, because earlier rules already decide every packet it would match. If the shadowed rule was meant to block something, that thing is allowed; if it was meant to allow, someone's access is broken (and they will soon ask for a broader rule).
  • Redundancy: a rule is covered by an earlier rule with the same verdict. Harmless for traffic, harmful for reviews: removing the earlier rule later silently changes what the redundant one does.
  • Correlation (partial overlap): two rules with different verdicts overlap without one containing the other, so the order decides the overlapping part. Not necessarily wrong, but the intent must be checked.

Shadowing in the lab

The lab's "shadowed rules" set is a small ruleset with two classic mistakes. Run the new connections through it:

chain input { type filter hook input priority 0; policy drop; } on 10.10.0.101tcp dport 22 drop2 pkts2ip saddr 10.10.0.0/24 tcp dport 22 acceptshadowed by 10 pkts3tcp dport 443 accept1 pkt4tcp dport 1024-65535 accept2 pkts5tcp dport 8443 dropshadowed by 40 pkts—policy drop (no rule matched)3 pktspackets (source → protocol/destination port, conntrack state)#1203.0.113.7 → tcp/443 newHTTPS visitor✓ accept (rule 3)#210.10.0.21 → tcp/22 newadmin SSH✕ drop (rule 1)#3198.51.100.9 → tcp/22 newSSH from outside✕ drop (rule 1)#4192.0.2.50 → icmp newping✕ drop (policy)#5198.51.100.9 → tcp/3389 newRDP probe✓ accept (rule 4)#6203.0.113.7 → udp/53 newDNS query✕ drop (policy)#7198.51.100.9 → tcp/8443 newadmin panel probe✓ accept (rule 4)#8203.0.113.8 → tcp/80 newHTTP visitor✕ drop (policy)
text
1  tcp dport 22 drop
2  ip saddr 10.10.0.0/24 tcp dport 22 accept      # shadowed by 1: admins locked out
3  tcp dport 443 accept
4  tcp dport 1024-65535 accept
5  tcp dport 8443 drop                             # shadowed by 4: the admin panel is open

The counters tell the story: rule 2 and rule 5 stay at 0 packets. The admin's SSH from 10.10.0.21 is dropped by rule 1 (somebody wanted "no SSH from outside" and wrote "no SSH"), and the probe to the admin panel on 8443 is accepted by rule 4 (somebody wanted "allow high ports for an application" and added the 8443 block below it). Of the eight packets, three are accepted: HTTPS (rule 3), and two that should have been dropped, RDP on 3389 and the admin-panel probe on 8443, both by rule 4.

Finding shadowing in code

Pairwise shadowing is a containment test: rule j is shadowed by an earlier rule i if every packet that matches j also matches i. The lab does exactly this. A real analyser must also catch rules hidden by several earlier rules together, which no single pair reveals. The program below checks both, for rules over protocol, source prefix and destination port ranges, by splitting the port space at every rule boundary and asking, for each piece, which rule matches first.

python · try it yourself
import ipaddress

def R(text, src="0.0.0.0/0", ports=(0, 65535), action="accept"):
    return {"text": text, "src": ipaddress.ip_network(src), "ports": ports, "action": action}

rules = [
    R("tcp dport 22 drop",                         ports=(22, 22), action="drop"),
    R("ip saddr 10.10.0.0/24 tcp dport 22 accept", src="10.10.0.0/24", ports=(22, 22)),
    R("tcp dport 443 accept",                      ports=(443, 443)),
    R("tcp dport 1024-65535 accept",               ports=(1024, 65535)),
    R("tcp dport 8443 drop",                       ports=(8443, 8443), action="drop"),
    R("tcp dport 1024-4999 drop",                  ports=(1024, 4999), action="drop"),
    R("tcp dport 5000-65535 drop",                 ports=(5000, 65535), action="drop"),
    R("ip saddr 10.10.0.0/24 tcp dport 2000-6000 accept", src="10.10.0.0/24", ports=(2000, 6000)),
]

def covers(a, b):
    return b["src"].subnet_of(a["src"]) and a["ports"][0] <= b["ports"][0] and b["ports"][1] <= a["ports"][1]

def pieces(rule):
    """Split the rule's (source, port range) into pieces on which every rule matches all or nothing."""
    cuts = {rule["ports"][0], rule["ports"][1] + 1}
    for r in rules:
        for edge in (r["ports"][0], r["ports"][1] + 1):
            if rule["ports"][0] < edge <= rule["ports"][1]:
                cuts.add(edge)
    cuts = sorted(cuts)
    srcs = {rule["src"]} | {r["src"] for r in rules if r["src"].subnet_of(rule["src"])}
    for lo, hi in zip(cuts, cuts[1:]):
        for s in srcs:
            yield s, lo, hi - 1

for j, rj in enumerate(rules):
    pair = next((i for i in range(j) if covers(rules[i], rj)), None)
    deciders = set()
    for s, lo, hi in pieces(rj):
        first = next(i for i, r in enumerate(rules)
                     if s.subnet_of(r["src"]) and r["ports"][0] <= lo and hi <= r["ports"][1])
        deciders.add(first)
    if j not in deciders:
        kind = f"shadowed by rule {pair + 1}" if pair is not None else \
               f"shadowed by rules {', '.join(str(d + 1) for d in sorted(deciders))} together"
        same = all(rules[d]["action"] == rj["action"] for d in deciders)
        print(f"rule {j + 1}: {rj['text']:48} {kind} ({'redundant' if same else 'CONFLICT: verdict differs'})")
    elif len(deciders) > 1:
        print(f"rule {j + 1}: {rj['text']:48} partly decided earlier by rules {', '.join(str(d + 1) for d in sorted(deciders - {j}))}")
rule 2: ip saddr 10.10.0.0/24 tcp dport 22 accept        shadowed by rule 1 (CONFLICT: verdict differs)
rule 5: tcp dport 8443 drop                              shadowed by rule 4 (CONFLICT: verdict differs)
rule 6: tcp dport 1024-4999 drop                         shadowed by rule 4 (CONFLICT: verdict differs)
rule 7: tcp dport 5000-65535 drop                        shadowed by rule 4 (CONFLICT: verdict differs)
rule 8: ip saddr 10.10.0.0/24 tcp dport 2000-6000 accept shadowed by rule 4 (redundant)

Rules 6 and 7 are new, added "to close the high ports again" after someone noticed rule 4. Neither is ever reached: rule 4 accepts every port they cover, so they are shadowed with a conflicting verdict. Rule 8 (lab clients to 2000–6000) is decided entirely by earlier rules too, and because its verdict agrees with rule 4's, it is merely redundant: delete rule 4 one day and rule 8 starts to matter, with a different effect than today. A pairwise checker finds all five here, because rule 4 alone covers each of them; the "together" case appears when a range is split across two earlier rules, which you can try in the practice.

How shadowing is detected in practice

  • Hit counters: a rule that stays at zero for a full business cycle is either stale or shadowed. Firewall management tools show "unused rules".
  • Rule analysers: commercial firewall-policy tools and open-source checkers compute shadowing, redundancy and correlation for vendor rule formats.
  • Tests: a table of "packet → expected verdict" pairs run against the ruleset (in a lab VM, or with a simulator like this lab) on every change. If the admin-SSH test fails, the change doesn't ship.
  • Review of every change in context: a new rule is reviewed together with the rules above and below it, not alone.

How it is fixed

  1. Order from specific to general: exceptions (admin SSH from the lab net) before the general rule (no SSH).
  2. Group by service, one block per destination service, so related rules sit together.
  3. Prefer allow lists with a default drop to mixes of allows and drops: with only accepts above a drop policy, shadowing can only cause redundancy, never a security hole.
  4. Delete shadowed and redundant rules after confirming intent; don't leave dead rules "just in case".
  5. Keep the ruleset in version control with tests, and generate it from the policy matrix where possible (Zones, DMZs and Trust Boundaries).

The corrected lab ruleset is shorter and has no order traps:

text
ct state established,related accept
ip saddr 10.10.0.0/24 tcp dport 22 accept
tcp dport 443 accept
tcp dport <the application's actual port> accept      # instead of 1024-65535
# policy drop: SSH from elsewhere, 8443, RDP and everything else

Pitfalls

  • Fixing a shadowed allow by adding an even broader allow above it.
  • Reordering rules on a device managed centrally, so the next push restores the old order.
  • Forgetting that NAT rules have their own order and are evaluated before filtering on some platforms.
  • Assuming a pairwise tool finds everything: combined shadowing needs a full analysis.

Practice

1. In the lab, switch the policy to accept. Which counters change and why?

Only the policy counter and the accepted total: rules 1–5 decide the same packets as before. The ping, the DNS query and the HTTP visitor now fall to an accepting policy, so 6 of 8 are accepted. Rule 2 and rule 5 still never match.

2. Remove the old rule 4 and put three rules at the top: R("tcp dport 1024-8443 accept", ports=(1024, 8443)), R("tcp dport 8444-65535 accept", ports=(8444, 65535)) and R("tcp dport 8000-9000 drop", ports=(8000, 9000), action="drop"). What does the program report for the new drop rule?

rule 3: tcp dport 8000-9000 drop shadowed by rules 1, 2 together (CONFLICT: verdict differs). Neither accept rule alone covers 8000–9000, so a pairwise checker (like the lab's) misses it, but together they decide every port in the range. The old tcp dport 5000-65535 drop (now rule 9) is reported the same way.

3. Why does "only accept rules above a drop policy" make shadowing harmless for security?

A shadowed accept is covered by an earlier accept, so its packets are accepted anyway: at worst the rule is redundant. Shadowing becomes dangerous only when the shadowed rule has the opposite verdict, which requires drop rules among the accepts.

Practice problems in 21 languages: Firewall First Match, Merge Port Ranges.

Test yourself

In the lab's shadowed set, why is the admin's SSH from 10.10.0.21 dropped?

  1. The policy is drop
  2. Rule 1 tcp dport 22 drop matches first, before the admin accept in rule 2
  3. The address is outside 10.10.0.0/24
  4. SSH is disabled

What is the difference between a shadowed rule and a redundant rule?

  1. None
  2. A shadowed rule never matches; it is redundant if the earlier rules have the same verdict, a conflict if they differ
  3. Redundant rules match more
  4. Shadowed rules are faster

Which observation most directly points to a shadowed rule on a live firewall?

  1. High CPU
  2. Its hit counter stays at zero over a full business cycle
  3. It has a comment
  4. It uses a set

How many of the eight new-connection packets does the shadowed set accept with policy drop?

  1. 2
  2. 3
  3. 5
  4. 8