fix(ec2): reject a CreateSubnet whose CIDR overlaps an existing subnet (#3171)

* fix(ec2): reject a CreateSubnet whose CIDR overlaps an existing subnet

AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the
same VPC with InvalidSubnet.Conflict. CreateSubnet has no idempotency
token, so this check is also what stops an SDK transport retry after a
lost response from recording a second subnet at the same CIDR.

Ported from lex00/floci#191.

* fix(ec2): hold the VPC lock across the subnet conflict scan and store

Two overlapping CreateSubnet requests in flight together could both pass
the scan before either subnet was stored, so the retry case the check
targets could still record a duplicate. The scan and the put now run
under the VPC's stripe lock, the way the other per-resource mutators do.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-07 09:27:23 -04:00
1 parent 2541f1b68d
commit 614166fe1f
6 files changed
+160 -1

No files matched your search

@@ -3309,7 +3309,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
subnet.setOwnerId(accountId);
subnet.setRegion(region);
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, accountId, "subnet/" + subnetId).toString());
subnets.put(key(region, subnetId), subnet);
// The conflict scan and the store must be one step under the VPC's lock, or two
// overlapping creates in flight together both pass the scan before either is stored.
synchronized (lockFor(key(region, vpcId))) {
rejectConflictingSubnetCidr(region, vpcId, cidrBlock);
subnets.put(key(region, subnetId), subnet);
}
// Every subnet starts associated with its VPC's default NACL. ReplaceNetworkAclAssociation
// later moves it onto a custom NACL, so this association must exist for that lookup to work.
@@ -3325,6 +3330,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
return subnet;
}
/**
* AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the same VPC. CreateSubnet
* carries no idempotency token, so this check is also what stops an SDK transport retry after
* a lost response from producing a second, unrecorded subnet at the same CIDR. Requests with
* no IPv4 CIDR are not checked here.
*/
private void rejectConflictingSubnetCidr(String region, String vpcId, String cidrBlock) {
if (!Ipv4Cidrs.isIpv4(cidrBlock)) {
return;
}
boolean conflict = subnets.scan(k -> true).stream()
.filter(s -> region.equals(s.getRegion()) && vpcId.equals(s.getVpcId()))
.filter(s -> Ipv4Cidrs.isIpv4(s.getCidrBlock()))
.anyMatch(s -> Ipv4Cidrs.overlaps(cidrBlock, s.getCidrBlock()));
if (conflict) {
throw new AwsException("InvalidSubnet.Conflict",
"The CIDR '" + cidrBlock + "' conflicts with another subnet", 400);
}
}
public List<Subnet> describeSubnets(String region, List<String> subnetIds, Map<String, List<String>> filters) {
ensureDefaultResources(region);
return subnets.scan(k -> true).stream()
@@ -26,6 +26,16 @@ final class Ipv4Cidrs {
return x[0] <= y[1] && y[0] <= x[1];
}
/** True when {@code cidr} parses as an IPv4 block, so the other checks can be applied to it. */
static boolean isIpv4(String cidr) {
try {
parse(cidr);
return true;
} catch (AwsException e) {
return false;
}
}
/**
* The first /{@code netmask} block inside any of the {@code provisioned}
* CIDRs (in order) that overlaps none of the {@code occupied} CIDRs, or