Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network.threat.manager): Fix flooding protection backport #4830

Merged
merged 2 commits into from
Sep 8, 2023
Merged
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
4 changes: 2 additions & 2 deletions kura/distrib/config/kura.build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ org.eclipse.kura.linux.gpio.version=1.3.0
org.eclipse.kura.linux.position.version=1.3.0
org.eclipse.kura.linux.usb.version=1.3.0
org.eclipse.kura.linux.watchdog.version=1.3.0
org.eclipse.kura.net.admin.version=1.3.0
org.eclipse.kura.net.admin.version=1.3.1-SNAPSHOT
org.eclipse.kura.nm.version=1.0.1
org.eclipse.kura.net.configuration.version=1.0.0
org.eclipse.kura.net.admin.firewall.version=1.0.0
org.eclipse.kura.net.admin.firewall.version=1.0.1-SNAPSHOT
org.eclipse.kura.util.version=1.4.0
org.eclipse.kura.protocol.can.version=3.3.0
org.eclipse.kura.protocol.modbus.version=2.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -60,6 +61,12 @@
public class FirewallConfigurationServiceImpl implements FirewallConfigurationService, SelfConfiguringComponent {

private static final Logger logger = LoggerFactory.getLogger(FirewallConfigurationServiceImpl.class);
private static final String[] FP_FILTER_RULES = {
"-A input-kura -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset",
"-A input-kura -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT",
"-A input-kura -p tcp --tcp-flags RST RST -j DROP",
"-A input-kura -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT",
"-A input-kura -p tcp -m conntrack --ctstate NEW -j DROP" };

private EventAdmin eventAdmin;
private LinuxFirewall firewall;
Expand Down Expand Up @@ -404,8 +411,16 @@ private NetworkPair getNetworkPair00() throws UnknownHostException {

@Override
public void addFloodingProtectionRules(Set<String> floodingRules) {
// Since the flooding protection rules passed as a parameter
// is only for the mangle table, a default set of rules for the
// filter tables is added.
try {
this.firewall.setAdditionalRules(new HashSet<>(), new HashSet<>(), floodingRules);
if (floodingRules == null || floodingRules.isEmpty()) {
this.firewall.setAdditionalRules(new HashSet<>(), new HashSet<>(), new HashSet<>());
} else {
this.firewall.setAdditionalRules(new HashSet<>(Arrays.asList(FP_FILTER_RULES)), new HashSet<>(),
floodingRules);
}
} catch (KuraException e) {
logger.error("Failed to set Firewall Flooding Protection Configuration", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -60,6 +61,12 @@
public class FirewallConfigurationServiceImpl implements FirewallConfigurationService, SelfConfiguringComponent {

private static final Logger logger = LoggerFactory.getLogger(FirewallConfigurationServiceImpl.class);
private static final String[] FP_FILTER_RULES = {
"-A input-kura -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset",
"-A input-kura -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT",
"-A input-kura -p tcp --tcp-flags RST RST -j DROP",
"-A input-kura -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT",
"-A input-kura -p tcp -m conntrack --ctstate NEW -j DROP" };

private EventAdmin eventAdmin;
private LinuxFirewall firewall;
Expand Down Expand Up @@ -404,8 +411,16 @@ private NetworkPair getNetworkPair00() throws UnknownHostException {

@Override
public void addFloodingProtectionRules(Set<String> floodingRules) {
// Since the flooding protection rules passed as a parameter
// is only for the mangle table, a default set of rules for the
// filter tables is added.
try {
this.firewall.setAdditionalRules(new HashSet<>(), new HashSet<>(), floodingRules);
if (floodingRules == null || floodingRules.isEmpty()) {
this.firewall.setAdditionalRules(new HashSet<>(), new HashSet<>(), new HashSet<>());
} else {
this.firewall.setAdditionalRules(new HashSet<>(Arrays.asList(FP_FILTER_RULES)), new HashSet<>(),
floodingRules);
}
} catch (KuraException e) {
logger.error("Failed to set Firewall Flooding Protection Configuration", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,7 @@ public synchronized void updated(Map<String, Object> properties) {
ComponentContext mockContext = mock(ComponentContext.class);
svc.activate(mockContext, new HashMap<String, Object>());

String[] floodingRules = {
"-A prerouting-kura -m conntrack --ctstate INVALID -j DROP",
String[] floodingRulesMangle = { "-A prerouting-kura -m conntrack --ctstate INVALID -j DROP",
"-A prerouting-kura -p tcp ! --syn -m conntrack --ctstate NEW -j DROP",
"-A prerouting-kura -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP",
"-A prerouting-kura -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP",
Expand All @@ -551,11 +550,50 @@ public synchronized void updated(Map<String, Object> properties) {
"-A prerouting-kura -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP",
"-A prerouting-kura -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP",
"-A prerouting-kura -p icmp -j DROP", "-A prerouting-kura -f -j DROP" };
Set<String> floodingRuleMangleSet = new HashSet<>(Arrays.asList(floodingRulesMangle));
String[] floodingRulesFilter = {
"-A input-kura -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset",
"-A input-kura -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT",
"-A input-kura -p tcp --tcp-flags RST RST -j DROP",
"-A input-kura -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT",
"-A input-kura -p tcp -m conntrack --ctstate NEW -j DROP" };
Set<String> floodingRuleFilterSet = new HashSet<>(Arrays.asList(floodingRulesFilter));

svc.addFloodingProtectionRules(new HashSet<>(Arrays.asList(floodingRules)));
svc.addFloodingProtectionRules(floodingRuleMangleSet);

try {
verify(mockFirewall, times(1)).setAdditionalRules(any(), any(), any());
verify(mockFirewall, times(1)).setAdditionalRules(floodingRuleFilterSet, new HashSet<>(),
floodingRuleMangleSet);
} catch (KuraException e) {
assert (false);
}
}

@Test
public void removeFloodingProtectionRulesTest() {
final LinuxFirewall mockFirewall = mock(LinuxFirewall.class);

FirewallConfigurationServiceImpl svc = new FirewallConfigurationServiceImpl() {

@Override
protected LinuxFirewall getLinuxFirewall() {
return mockFirewall;
}

@Override
public synchronized void updated(Map<String, Object> properties) {
// don't care about the properties in this test
// update is not called when adding flooding protection rules,
// it is called just during activate
}
};

ComponentContext mockContext = mock(ComponentContext.class);
svc.activate(mockContext, new HashMap<String, Object>());
svc.addFloodingProtectionRules(new HashSet<>());

try {
verify(mockFirewall, times(1)).setAdditionalRules(new HashSet<>(), new HashSet<>(), new HashSet<>());
} catch (KuraException e) {
assert (false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,31 +511,30 @@ protected void addLocalRules(ArrayList<LocalRule> localRules) throws KuraExcepti
svc.setFirewallOpenPortConfiguration(firewallConfiguration);

}

@Test
public void addFloodingProtectionRulesTest() {
final LinuxFirewall mockFirewall = mock(LinuxFirewall.class);

FirewallConfigurationServiceImpl svc = new FirewallConfigurationServiceImpl() {

@Override
protected LinuxFirewall getLinuxFirewall() {
return mockFirewall;
}

@Override
public synchronized void updated(Map<String, Object> properties) {
// don't care about the properties in this test
// update is not called when adding flooding protection rules,
// it is called just during activate
}
};

ComponentContext mockContext = mock(ComponentContext.class);
svc.activate(mockContext, new HashMap<String, Object>());

String[] floodingRules = {
"-A prerouting-kura -m conntrack --ctstate INVALID -j DROP",

String[] floodingRulesMangle = { "-A prerouting-kura -m conntrack --ctstate INVALID -j DROP",
"-A prerouting-kura -p tcp ! --syn -m conntrack --ctstate NEW -j DROP",
"-A prerouting-kura -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP",
"-A prerouting-kura -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP",
Expand All @@ -551,13 +550,52 @@ public synchronized void updated(Map<String, Object> properties) {
"-A prerouting-kura -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP",
"-A prerouting-kura -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP",
"-A prerouting-kura -p icmp -j DROP", "-A prerouting-kura -f -j DROP" };

svc.addFloodingProtectionRules(new HashSet<>(Arrays.asList(floodingRules)));

Set<String> floodingRuleMangleSet = new HashSet<>(Arrays.asList(floodingRulesMangle));
String[] floodingRulesFilter = {
"-A input-kura -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset",
"-A input-kura -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT",
"-A input-kura -p tcp --tcp-flags RST RST -j DROP",
"-A input-kura -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT",
"-A input-kura -p tcp -m conntrack --ctstate NEW -j DROP" };
Set<String> floodingRuleFilterSet = new HashSet<>(Arrays.asList(floodingRulesFilter));

svc.addFloodingProtectionRules(floodingRuleMangleSet);

try {
verify(mockFirewall, times(1)).setAdditionalRules(floodingRuleFilterSet, new HashSet<>(),
floodingRuleMangleSet);
} catch (KuraException e) {
assert (false);
}
}

@Test
public void removeFloodingProtectionRulesTest() {
final LinuxFirewall mockFirewall = mock(LinuxFirewall.class);

FirewallConfigurationServiceImpl svc = new FirewallConfigurationServiceImpl() {

@Override
protected LinuxFirewall getLinuxFirewall() {
return mockFirewall;
}

@Override
public synchronized void updated(Map<String, Object> properties) {
// don't care about the properties in this test
// update is not called when adding flooding protection rules,
// it is called just during activate
}
};

ComponentContext mockContext = mock(ComponentContext.class);
svc.activate(mockContext, new HashMap<String, Object>());
svc.addFloodingProtectionRules(new HashSet<>());

try {
verify(mockFirewall, times(1)).setAdditionalRules(any(), any(), any());
} catch(KuraException e) {
assert(false);
verify(mockFirewall, times(1)).setAdditionalRules(new HashSet<>(), new HashSet<>(), new HashSet<>());
} catch (KuraException e) {
assert (false);
}
}

Expand Down