Skip to content

VPC ACL: apply every CIDR of a multi-CIDR rule, in order - #14350

Open
bhouse-nexthop wants to merge 2 commits into
apache:4.22from
bhouse-nexthop:fix-vpc-acl-multi-cidr
Open

bhouse-nexthop wants to merge 2 commits into
apache:4.22from
bhouse-nexthop:fix-vpc-acl-multi-cidr

Conversation

@bhouse-nexthop

@bhouse-nexthop bhouse-nexthop commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A VPC network ACL rule can carry several CIDRs. On the VR those rules are either applied in the wrong order or not applied at all, and nothing is reported back to the UI or API.

Fixes #12668

Why it breaks

IPv4, iptables (normal VPC): AclDevice.process() turns each ACL rule into one fw entry, e.g. -A ACL_INBOUND_eth3 -s 1.2.3.4/32,2.3.4.5/32 -p tcp --dport 22 -j ACCEPT. iptables expands a comma-separated address into one rule per address, so that line creates two rules. CsNetfilters.compare() inserts each ACL rule at the chain's current rule count (just ahead of the trailing DROP), and bumps that count by one per fw entry, not by the number of rules iptables actually created. After a multi-CIDR rule the count is short by n-1, so every later rule is inserted between the expanded rules. All but the last CIDR of each multi-CIDR rule drift down behind every rule that follows, including an explicit deny.

The effect: an allow silently stops working for every CIDR but one, and a multi-CIDR deny placed ahead of an allow (e.g. "block RFC1918, then allow 0.0.0.0/0") stops denying. Single-CIDR rules are unaffected. A lone multi-CIDR rule also looks fine, since there is nothing after it to land in the gap, which is why this can be hard to reproduce. Both ingress (filter) and egress (mangle) chains are affected.

IPv6, and IPv4 in routed mode (nftables): __process_ip6() and __process_routing_ip4() build ip6 saddr a,b ... / ip saddr a,b .... nft rejects a bare comma list:

$ nft add rule ip6 t c ip6 saddr 2001:db8:1::/64,2001:db8:2::/64 tcp dport 22 accept
Error: syntax error, unexpected /, expecting end of file or newline or semicolon

The rules are added one nft add rule at a time and the failure is only logged, so each multi-CIDR rule is silently missing while the rest of the list loads.

How it is fixed

  • iptables: AclDevice.process() emits one fw entry per CIDR, in list order, skipping any empty element. Each entry is then exactly one iptables rule, so the insert position compare() tracks stays correct. (This is simpler and safer than teaching compare() how iptables expands -s/-d. It also lifts a limit: a single -s with several hundred CIDRs fails outright with iptables: Message too long.)
  • nftables: a multi-CIDR match is rendered as a set, ip saddr { a, b }, via a small nftAddrSet() helper. The routed-network firewall rules (add_routing_rules) and CsIpv6Firewall in the same file already use nft sets for source_cidr_list. The set is written with spaces so that no shell can brace-expand it on its way to nft. nft also accepts overlapping or duplicate elements in it and merges them.

How to reproduce the old behaviour

  1. In a VPC, create a network ACL list with these ingress rules and attach it to a tier:
    # Protocol CIDR list Port Action
    1 TCP 1.2.3.4/32,2.3.4.5/32 22 Allow
    2 TCP 3.4.5.6/32 443 Allow
    3 All 0.0.0.0/0 – Deny
  2. On the VR, run iptables -S ACL_INBOUND_ethX for that tier's interface.

Before (the first CIDR of rule 1 sits behind the explicit deny and never matches):

-A ACL_INBOUND_eth3 -d 225.0.0.50/32 -j ACCEPT
-A ACL_INBOUND_eth3 -d 224.0.0.18/32 -j ACCEPT
-A ACL_INBOUND_eth3 -s 2.3.4.5/32 -p tcp -m tcp --dport 22 -j ACCEPT
-A ACL_INBOUND_eth3 -s 3.4.5.6/32 -p tcp -m tcp --dport 443 -j ACCEPT
-A ACL_INBOUND_eth3 -j DROP
-A ACL_INBOUND_eth3 -s 1.2.3.4/32 -p tcp -m tcp --dport 22 -j ACCEPT
-A ACL_INBOUND_eth3 -j DROP

After:

-A ACL_INBOUND_eth3 -d 225.0.0.50/32 -j ACCEPT
-A ACL_INBOUND_eth3 -d 224.0.0.18/32 -j ACCEPT
-A ACL_INBOUND_eth3 -s 1.2.3.4/32 -p tcp -m tcp --dport 22 -j ACCEPT
-A ACL_INBOUND_eth3 -s 2.3.4.5/32 -p tcp -m tcp --dport 22 -j ACCEPT
-A ACL_INBOUND_eth3 -s 3.4.5.6/32 -p tcp -m tcp --dport 443 -j ACCEPT
-A ACL_INBOUND_eth3 -j DROP
-A ACL_INBOUND_eth3 -j DROP

For the IPv6 side, give the tier an IPv6 CIDR and add a rule with two IPv6 CIDRs. Before the fix it is absent from nft list chain ip6 ip6_acl ethX_ingress_policy, and /var/log/cloud.log shows the nft add rule failing with a non-zero exit status (nft's syntax error itself goes to stderr). After the fix it is listed as ip6 saddr { a, b } ....

Until this lands, the workaround is one CIDR per ACL rule.

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

How Has This Been Tested?

  • New systemvm/test/TestCsAcl.py covers iptables ingress and egress, the IPv4 and IPv6 halves of a mixed-family rule, routed IPv4, and empty list elements. The multi-CIDR tests fail on the current 4.22 code and all pass with this change. (Nothing in .github/workflows runs systemvm/test, so these run via systemvm/test/runtests.sh.)
  • The before/after chains above are real output, not hand-written. They come from feeding the rules AclDevice generates through the unmodified CsNetfilters().compare() against real iptables (nf_tables backend) in a network namespace. Re-applying the same ACL a second time leaves the chain unchanged.
  • The nft set syntax was checked with real nft add rule for both ip and ip6 on nft 1.0.6 (the version in the Debian 12 systemvm template) and 1.0.9, including overlapping, duplicate and 0.0.0.0/0 / ::/0 elements. The old bare-list form was confirmed to fail.
  • pycodestyle and pylint (as in systemvm/test/runtests.sh) report nothing new.

How did you try to break this feature and the system with this change?

  • Mixed IPv4/IPv6 CIDR lists still split the same way as before (removeUndesiredCidrs). Each family only gets its own CIDRs, and a single-CIDR rule produces exactly the same output as before.
  • Rules with no CIDR (all) are unchanged.
  • Duplicate CIDRs, within one rule or across rules, are applied and cleaned up correctly on re-apply.
  • Upgrade case: a VR that already has the old scrambled chain is put back in order on the next apply.

A VPC network ACL rule may carry several CIDRs, which reach the VR as a
comma-separated string. Both ways the VR renders them are broken.

iptables (the normal VPC path): the rule went out as a single
"-A ACL_INBOUND_ethX -s a,b ..." line. iptables expands that into one rule
per address, but CsNetfilters.compare() counts the entry as one rule when it
works out where to insert the next ACL rule ahead of the chain's DROP. Every
later rule is therefore inserted one slot too high, between the expanded
rules, and all but the last CIDR of each multi-CIDR rule drift down behind
any explicit deny. An allow silently stops matching for those CIDRs; a
multi-CIDR deny placed ahead of an allow stops denying.

nftables (IPv6, and IPv4 in routed mode): the rule was built as
"ip6 saddr a,b ...", which nft rejects with a syntax error. The failure is
only logged, so the rule is simply missing.

Emit one iptables rule per CIDR so the count stays right, and render a
multi-CIDR nft match as a set, "{a,b}", the way the firewall rules in the same
file already do.

Fixes: apache#12668

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

@vladimirpetrov @sureshanaparti could you take a look at this one? We'd like this fix to make it into the upcoming 4.22.2 release.

@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 18.02%. Comparing base (2974af8) to head (f22753f).
⚠️ Report is 31 commits behind head on 4.22.

Additional details and impacted files
@@             Coverage Diff              @@
##               4.22   #14350      +/-   ##
============================================
- Coverage     18.02%   18.02%   -0.01%     
+ Complexity    16250    16245       -5     
============================================
  Files          5936     5936              
  Lines        535823   535823              
  Branches      65612    65612              
============================================
- Hits          96582    96556      -26     
- Misses       428242   428270      +28     
+ Partials      10999    10997       -2     
Flag Coverage Δ
uitests 4.04% <ø> (ø)
unittests 19.09% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

An empty element in the cidr list ("a,,b") would now become its own
iptables rule, fail, and still be counted by CsNetfilters.compare(),
pushing every later ACL rule behind the DROP. The API never produces one,
but skip it rather than rely on that.

Render the nft set as "{ a, b }" rather than "{a,b}": the unspaced form is
only safe because /bin/sh on the VR is dash, and bash would brace-expand it
into a syntax error.

Also cover the egress path and the IPv4 half of a mixed-family rule in the
tests.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@fermosan

fermosan commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Hello, this seems to do the work. Is there any chance for this to be applied to systemvm for 4.23 ? As far as i know there is no 4.23 systemvm. Maybe a 4.22 image with only this patch could make it to a 4.23?

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

@fermosan thanks for confirming. You shouldn't need a new systemvm template for this. configure.py and the rest of the VR scripts are shipped with the management server and pushed to the router, not baked into the template image. Once the fix is in a release your management server runs, existing routers pick it up either when they are recreated, or in place with restartVPC id=<vpc> livepatch=true (with cleanup=false), which live-patches the router's scripts before restarting it.

This PR targets 4.22. Fixes on the 4.22 branch are merged forward into main, so it should reach 4.23 the same way; the release managers decide which release. Until then, one CIDR per ACL rule avoids it.

@fermosan

fermosan commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for the clarification. We could temporarily backport the change then. When next release is up probably the patch will be already merged. I understand it can work one by one, but we have a case with a specific user that wants to create a lot of rules and doing it one by one generates around 2.000 rules.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

We have to run our own cloudstack builds ourselves because of all the pending PRs we need incorporated. Hopefully at some point that won't be true ...

@bhouse-nexthop

bhouse-nexthop commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

@DaanHoogland this one is in the same VPC ACL area as #14355 and #14352, which you merged today. Could you or @weizhouapache take a look and add it to 4.22.2? Today a multi-CIDR rule is applied on the VR in the wrong order or not at all, with nothing reported back; this applies every CIDR, in order (fixes #12668). I understand 4.22.2 is due out late this week or early next.

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

code lgtm

not tested

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Test report: multi-CIDR VPC ACL rules on a running router (iptables, IPv6 nft, routed nft)

Environment

  • A CloudStack 24.0 (main) build with this PR applied, alongside some of my other open PRs.
  • Why the result carries over to 4.22:
    • Main still ships system VM template 4.22.0, and the parts of configure.py this PR touches are the same on 4.22 and main.
    • The VPC offering has no Firewall service, as on 4.22.
  • Three KVM hosts on Ubuntu 24.04, nested virtualisation; Ceph RBD primary storage.
  • System VM template 4.22.0 (Debian 12, iptables 1.8.9 (nf_tables), nft 1.0.6). The routers were created on this build and run its configure.py.
  • A dual-stack VPC:
    • tier T1 holds the server V1 (Ubuntu 24.04), with the test list;
    • tier T2 holds the client V2.
  • A routed-mode VPC with one tier and a small VM.
  • An external host on the public network (IPv4 and IPv6).
  • For the "without the PR" runs, I copied apache/main's configure.py onto the router, re-applied the list, and then restored the PR's script with restartVPC livepatch=true.

1. The table from the PR description

The list:

  • rule 1: tcp 22 from 1.2.3.4/32,<T2>;
  • rule 2: tcp 8080 from 3.4.5.6/32;
  • rule 3: all, deny.
Router chain ssh (tcp 22) from V2
with the PR -s 1.2.3.4/32 --dport 22 ACCEPT, -s 172.16.2.0/24 --dport 22 ACCEPT, -s 3.4.5.6/32 --dport 8080 ACCEPT, DROP, DROP connects
stock -s 172.16.2.0/24 ...22 ACCEPT, -s 3.4.5.6/32 ...8080 ACCEPT, DROP, -s 1.2.3.4/32 ...22 ACCEPT, DROP connects (its CIDR is the last one, which stock keeps in place)

So that the traffic shows the bug, I put the client's CIDR first: rule 1 = <T2>,1.2.3.4/32.

Router chain ssh from V2
with the PR -s 172.16.2.0/24 ...22 ACCEPT, -s 1.2.3.4/32 ...22 ACCEPT, -s 3.4.5.6/32 ...8080 ACCEPT, DROP, DROP connects
stock -s 1.2.3.4/32 ...22 ACCEPT, -s 3.4.5.6/32 ...8080 ACCEPT, DROP, -s 172.16.2.0/24 ...22 ACCEPT, DROP times out
after restartVPC livepatch=true back in list order, as in the first row connects

So a chain that the old script had already scrambled is repaired once the new script re-applies the list.

2. Deny ahead of allow

  • rule 1: deny tcp 8080 from <T2>,192.168.0.0/16;
  • rule 2: allow all.

The router shows:

-A ACL_INBOUND_eth2 -s 172.16.2.0/24 -p tcp -m tcp --dport 8080 -j DROP
-A ACL_INBOUND_eth2 -s 192.168.0.0/16 -p tcp -m tcp --dport 8080 -j DROP
-A ACL_INBOUND_eth2 -j ACCEPT

From V2, curl V1:8080 fails (dropped), and tcp 22 still connects.

3. IPv6 (dual-stack tier)

I added tcp 22 from <external /64>,2001:db8:1::/64, with the reachable prefix listed first. The rule renders as:

chain eth2_ingress_policy {
    ip6 saddr { 2001:db8:1::/64, fd00:30::/64 } tcp dport 22 accept
    counter packets 0 bytes 0 drop
}

SSH (tcp 22) from the external host over IPv6 connects, and another port is dropped. The router's cloud.log shows the nft add rule ... { fd00:30::/64, 2001:db8:1::/64 } ... line and no failed nft add rule.

4. Routed IPv4 (nft)

The rule tcp 22 from 192.0.2.0/24,<external host>/32 renders in ip ip4_acl as:

ip saddr { 172.30.0.30, 192.0.2.0/24 } tcp dport 22 accept
  • From the external host, tcp 22 reaches the VM, which answers with a reset (its ssh daemon was not listening yet), and tcp 23 is dropped.
  • From a host not in the rule, tcp 22 is dropped.
  • With the stock script the rule doesn't load. cloud.log shows:
    ERROR Command 'nft add rule ip ip4_acl eth2_ingress_policy ip saddr 192.0.2.0/24,172.30.0.30/32 tcp dport 22 accept' returned non-zero exit status 1.
    
    The chain is left with only the drop, and the external host's tcp 22 times out.

5. A rule with 300 CIDRs

I created a rule for tcp 443 from 300 /32 addresses in 198.18.0.0/15.

  • All 300 lines are in list order, after the earlier rules and ahead of the final DROP: iptables -S | grep -c 198.18. is 300.
  • The first apply took about 1.3 s in the router's cloud.log, and a re-apply about 5.9 s. This is in line with the PR's 1.0 s and 6.6 s.
  • There was no Message too long.

6. Re-apply is stable

After restartVPC cleanup=false, the chain was identical (diff was empty) for the three-rule list and for the 300-CIDR list.

Result: pass.

  • Each CIDR of a rule becomes its own rule, in list order, ahead of the DROP.
  • The traffic results match the rules.
  • The nft sets load for IPv6 and for routed IPv4.
  • The 300-CIDR rule applies, re-apply is stable, and a chain scrambled by the old script is repaired.

Not covered

  • Egress multi-CIDR rules (only ingress was tested).
  • Redundant VPC routers.
  • Templates older than 4.22.0.

@fermosan

Copy link
Copy Markdown
Contributor

@DaanHoogland @RosiKyu @nvazquez Hello guys, any chance this can be included in upcoming 24 release ? We have independently verified it and use it for a decent amount of time in a production environment with thousands of acl rules.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

@fermosan thanks, good to hear it has held up in production with that many rules.

Could you post a short summary as a review (Review changes -> Approve)? Version, router type (VPC, IPv4/IPv6, nft or iptables) and roughly how many rules/CIDRs would be enough. That would count as the independent test review. Together with @weizhouapache's approval, it then meets the merge rule.

@RosiKyu @nvazquez could you consider this for 24.0? It is against 4.22, so merging it before the freeze this week also takes it into 24.0.

@fermosan

Copy link
Copy Markdown
Contributor

Hello, @bhouse-nexthop. The most "heavy" router currently runs version 4.22.1 for a VPC with 10 dual stack networks. After rule consolidation it has 1220 ipv4 rules and 1412 ipv6.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Thanks @fermosan, that's a good stress case: 1220 IPv4 and 1412 IPv6 rules.

To count it under the merge rule it has to be a review rather than a comment: Files changed -> Review changes -> Approve. A one-liner like "running on 4.22.1, VPC with 10 dual-stack networks, ~2600 rules" is enough.

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

running on 4.22.1, VPC with 10 dual-stack networks, ~2600 rules

@weizhouapache

Copy link
Copy Markdown
Member

@fermosan thanks, good to hear it has held up in production with that many rules.

Could you post a short summary as a review (Review changes -> Approve)? Version, router type (VPC, IPv4/IPv6, nft or iptables) and roughly how many rules/CIDRs would be enough. That would count as the independent test review. Together with @weizhouapache's approval, it then meets the merge rule.

@RosiKyu @nvazquez could you consider this for 24.0? It is against 4.22, so merging it before the freeze this week also takes it into 24.0.

@bhouse-nexthop
this is ready for merge. however, 4.22 is still in code freeze as far as I understand, so .. may be in 4.22.3

@fermosan

Copy link
Copy Markdown
Contributor

@weizhouapache I think there is still limited time to include it in upcoming 24 as well.

@weizhouapache

Copy link
Copy Markdown
Member

@weizhouapache I think there is still limited time to include it in upcoming 24 as well.

this can be merged as soon as 4.22.2.0 is released.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Thanks @fermosan for the review, and @weizhouapache for confirming it's ready. Understood that it waits for 4.22.2.0 to be released.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants