Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ protected Map<PortAndProto, Set<String>> generateRulesForVM(Long userVmId, Secur
cidr = cidr + "/32";
cidrs.add(cidr);
if (defaultNic.getIPv6Address() != null) {
cidrs.add(defaultNic.getIPv6Address() + "/64");
cidrs.add(defaultNic.getIPv6Address() + "/128");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens to vms that pick their own random ipv6 address, like windows does by default? looks like their traffic wont match this single address anymore

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this, it's a good thing to pin down. I went and looked at the agent side to be sure, and I think we might actually be covered here, though let me know if I'm missing a case.

From what I can see, a VM can't really put a self-chosen address on the wire in an SG network anyway. The KVM agent installs a source anti-spoof rule in security_group.py (the -m set ! --match-set <vmipset6> src -j DROP near the end of the IPv6 default chain), and that ipset only gets the addresses CloudStack assigned to the NIC. So a Windows temporary/privacy address wouldn't be in the set, and its traffic would get dropped at that VM's own vNIC before the receiving VM's member rule is ever looked at. If that reading is right, then /64 wasn't really letting those packets through either, they just died earlier in the path.

Where /64 did make a difference, I think, is that it also let any other VM sharing that /64 reach the protected VM, member or not, and narrowing to /128 lines it up with what the IPv4 side already does at /32. Does that match how you understand it, or is there a setup where a VM ends up sending from an address CloudStack didn't assign?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, the agent drops anything from an address it didnt assign so /64 wasnt helping there. thanks for checking

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ protected Map<PortAndProto, Set<String>> generateRulesForVM(Long userVmId, Secur
String cidr = ngmapVO.getGuestIpAddress() + "/32";
cidrs.add(cidr);
if (ngmapVO.getGuestIpv6Address() != null) {
cidrs.add(ngmapVO.getGuestIpv6Address() + "/64");
cidrs.add(ngmapVO.getGuestIpv6Address() + "/128");
}
}
} else if (rule.getAllowedSourceIpCidr() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network.security;

import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.network.security.SecurityGroupManagerImpl.PortAndProto;
import com.cloud.network.security.SecurityRule.SecurityRuleType;
import com.cloud.network.security.dao.SecurityGroupRuleDao;
import com.cloud.network.security.dao.SecurityGroupVMMapDao;
import com.cloud.vm.VirtualMachine.State;

@RunWith(MockitoJUnitRunner.Silent.class)
public class SecurityGroupManagerImpl2Ipv6RuleTest {

@Mock
SecurityGroupRuleDao _securityGroupRuleDao;
@Mock
SecurityGroupVMMapDao _securityGroupVMMapDao;

@InjectMocks
SecurityGroupManagerImpl2 manager = new SecurityGroupManagerImpl2();

@Test
public void securityGroupMemberRuleUsesExactIpv6HostCidr() {
Long vmId = 1L;
SecurityRuleType type = SecurityRuleType.IngressRule;

// The VM belongs to security group 10, which has one rule referencing another security group (20).
SecurityGroupVMMapVO groupMap = Mockito.mock(SecurityGroupVMMapVO.class);
when(groupMap.getSecurityGroupId()).thenReturn(10L);
when(_securityGroupVMMapDao.listByInstanceId(vmId)).thenReturn(Collections.singletonList(groupMap));

SecurityGroupRuleVO rule = Mockito.mock(SecurityGroupRuleVO.class);
when(rule.getProtocol()).thenReturn("tcp");
when(rule.getStartPort()).thenReturn(80);
when(rule.getEndPort()).thenReturn(80);
when(rule.getAllowedNetworkId()).thenReturn(20L);
when(_securityGroupRuleDao.listBySecurityGroupId(10L, type)).thenReturn(Collections.singletonList(rule));

// Unlike the superclass, Impl2 reads the member addresses straight from the VO join, not the nics table.
SecurityGroupVMMapVO memberMap = Mockito.mock(SecurityGroupVMMapVO.class);
when(memberMap.getGuestIpAddress()).thenReturn("10.1.1.5");
when(memberMap.getGuestIpv6Address()).thenReturn("2001:db8::5");
when(_securityGroupVMMapDao.listBySecurityGroup(20L, State.Running)).thenReturn(Collections.singletonList(memberMap));

Map<PortAndProto, Set<String>> allowed = manager.generateRulesForVM(vmId, type);

Assert.assertEquals(1, allowed.size());
Set<String> cidrs = allowed.values().iterator().next();
// The member must be authorized as an exact host, matching the IPv4 /32 behaviour.
Assert.assertTrue("IPv4 member should be pinned to /32", cidrs.contains("10.1.1.5/32"));
Assert.assertTrue("IPv6 member should be pinned to the exact /128 host", cidrs.contains("2001:db8::5/128"));
Assert.assertFalse("IPv6 member must not open the whole /64 subnet", cidrs.contains("2001:db8::5/64"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network.security;

import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.network.NetworkModel;
import com.cloud.network.security.SecurityGroupManagerImpl.PortAndProto;
import com.cloud.network.security.SecurityRule.SecurityRuleType;
import com.cloud.network.security.dao.SecurityGroupRuleDao;
import com.cloud.network.security.dao.SecurityGroupVMMapDao;
import com.cloud.vm.Nic;
import com.cloud.vm.VirtualMachine.State;

@RunWith(MockitoJUnitRunner.Silent.class)
public class SecurityGroupManagerImplIpv6RuleTest {

@Mock
SecurityGroupRuleDao _securityGroupRuleDao;
@Mock
SecurityGroupVMMapDao _securityGroupVMMapDao;
@Mock
NetworkModel _networkModel;

@InjectMocks
SecurityGroupManagerImpl manager = new SecurityGroupManagerImpl();

@Test
public void securityGroupMemberRuleUsesExactIpv6HostCidr() {
Long vmId = 1L;
SecurityRuleType type = SecurityRuleType.IngressRule;

// The VM belongs to security group 10, which has one rule referencing another security group (20).
SecurityGroupVMMapVO groupMap = Mockito.mock(SecurityGroupVMMapVO.class);
when(groupMap.getSecurityGroupId()).thenReturn(10L);
when(_securityGroupVMMapDao.listByInstanceId(vmId)).thenReturn(Collections.singletonList(groupMap));

SecurityGroupRuleVO rule = Mockito.mock(SecurityGroupRuleVO.class);
when(rule.getProtocol()).thenReturn("tcp");
when(rule.getStartPort()).thenReturn(80);
when(rule.getEndPort()).thenReturn(80);
when(rule.getAllowedNetworkId()).thenReturn(20L);
when(_securityGroupRuleDao.listBySecurityGroupId(10L, type)).thenReturn(Collections.singletonList(rule));

// Group 20 has one running member VM with both an IPv4 and an IPv6 address.
SecurityGroupVMMapVO memberMap = Mockito.mock(SecurityGroupVMMapVO.class);
when(memberMap.getInstanceId()).thenReturn(2L);
when(_securityGroupVMMapDao.listBySecurityGroup(20L, State.Running)).thenReturn(Collections.singletonList(memberMap));

Nic nic = Mockito.mock(Nic.class);
when(nic.getIPv4Address()).thenReturn("10.1.1.5");
when(nic.getIPv6Address()).thenReturn("2001:db8::5");
when(_networkModel.getDefaultNic(2L)).thenReturn(nic);

Map<PortAndProto, Set<String>> allowed = manager.generateRulesForVM(vmId, type);

Assert.assertEquals(1, allowed.size());
Set<String> cidrs = allowed.values().iterator().next();
// The member must be authorized as an exact host, matching the IPv4 /32 behaviour.
Assert.assertTrue("IPv4 member should be pinned to /32", cidrs.contains("10.1.1.5/32"));
Assert.assertTrue("IPv6 member should be pinned to the exact /128 host", cidrs.contains("2001:db8::5/128"));
Assert.assertFalse("IPv6 member must not open the whole /64 subnet", cidrs.contains("2001:db8::5/64"));
}
}
Loading