Skip to contentSkip to conversation

net: openvswitch: limit the recursions from action sets

PATCH Superseded Aaron Conole 2 participants 7 messages Feb 6, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Aaron Conole

author maintainer
#0 lore raw

cover net: openvswitch: limit the recursions from action sets

Open vSwitch module accepts actions as a list from the netlink socket and then creates a copy which it uses in the action set processing. During processing of the action list on a packet, the module keeps a count of the execution depth and exits processing if the action depth goes too high.

However, during netlink processing the recursion depth isn't checked anywhere, and the copy trusts that kernel has large enough stack to accommodate it. The OVS sample action was the original action which could perform this kinds of recursion, and it originally checked that it didn't exceed the sample depth limit. However, when sample became optimized to provide the clone() semantics, the recursion limit was dropped.

This series adds a depth limit during the __ovs_nla_copy_actions() call that will ensure we don't exceed the max that the OVS userspace could generate for a clone().

Additionally, this series provides a selftest in 2/2 that can be used to determine if the OVS module is allowing unbounded access. It can be safely omitted where the ovs selftest framework isn't available.

Aaron Conole (2):
net: openvswitch: limit the number of recursions from action sets
selftests: openvswitch: Add validation for the recursion test

 net/openvswitch/flow_netlink.c                | 33 +++++++--
 .../selftests/net/openvswitch/openvswitch.sh  | 13 ++++
 .../selftests/net/openvswitch/ovs-dpctl.py    | 71 +++++++++++++++----
 3 files changed, 97 insertions(+), 20 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 net: openvswitch: limit the number of recursions from action sets

The ovs module allows for some actions to recursively contain an action list for complex scenarios, such as sampling, checking lengths, etc. When these actions are copied into the internal flow table, they are evaluated to validate that such actions make sense, and these calls happen recursively.

The ovs-vswitchd userspace won't emit more than 16 recursion levels deep. However, the module has no such limit and will happily accept limits larger than 16 levels nested. Prevent this by tracking the number of recursions happening and manually limiting it to 16 levels nested.

The initial implementation of the sample action would track this depth and prevent more than 3 levels of recursion, but this was removed to support the clone use case, rather than limited at the current userspace limit.

Fixes 798c166173ff ("openvswitch: Optimize sample action for the clone use cases")
---
 net/openvswitch/flow_netlink.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
net/openvswitch/flow_netlink.c +28 -5
index 88965e2068ac..ba5cfa67a720 100644
@@ -48,6 +48,9 @@ struct ovs_len_tbl {
 
 #define OVS_ATTR_NESTED -1
 #define OVS_ATTR_VARIABLE -2
+#define OVS_COPY_ACTIONS_MAX_DEPTH 16
+
+static DEFINE_PER_CPU(int, copy_actions_depth);
 
 static bool actions_may_change_flow(const struct nlattr *actions)
 {
@@ -3148,11 +3151,11 @@ static int copy_action(const struct nlattr *from,
 	return 0;
 }
 
-static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
-				  const struct sw_flow_key *key,
-				  struct sw_flow_actions **sfa,
-				  __be16 eth_type, __be16 vlan_tci,
-				  u32 mpls_label_count, bool log)
+static int ___ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+				   const struct sw_flow_key *key,
+				   struct sw_flow_actions **sfa,
+				   __be16 eth_type, __be16 vlan_tci,
+				   u32 mpls_label_count, bool log)
 {
 	u8 mac_proto = ovs_key_mac_proto(key);
 	const struct nlattr *a;
@@ -3478,6 +3481,26 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
 	return 0;
 }
 
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+				  const struct sw_flow_key *key,
+				  struct sw_flow_actions **sfa,
+				  __be16 eth_type, __be16 vlan_tci,
+				  u32 mpls_label_count, bool log)
+{
+	int level = this_cpu_read(copy_actions_depth);
+	int ret;
+
+	if (level > OVS_COPY_ACTIONS_MAX_DEPTH)
+		return -EOVERFLOW;
+
+	__this_cpu_inc(copy_actions_depth);
+	ret = ___ovs_nla_copy_actions(net, attr, key, sfa, eth_type,
+				      vlan_tci, mpls_label_count, log);
+	__this_cpu_dec(copy_actions_depth);
+
+	return ret;
+}
+
 /* 'key' must be the masked key. */
 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
 			 const struct sw_flow_key *key,
Signature · 1 line
2.41.0
author maintainer
#6 lore raw

PATCH 2/2 selftests: openvswitch: Add validation for the recursion test

In reply to Aaron Conole

Add a test case into the netlink checks that will show the number of nested action recursions won't exceed 16. Going to 17 on a small clone call isn't enough to exhaust the stack on (most) systems, so it should be safe to run even on systems that don't have the fix applied.

---
NOTE: This patch may be safely omitted for trees that don't
include the selftests

 .../selftests/net/openvswitch/openvswitch.sh  | 13 ++++
 .../selftests/net/openvswitch/ovs-dpctl.py    | 71 +++++++++++++++----
 2 files changed, 69 insertions(+), 15 deletions(-)
tools/testing/selftests/net/openvswitch/openvswitch.sh +13 -0
index f8499d4c87f3..30cb9d3b035d 100755
@@ -502,7 +502,20 @@ test_netlink_checks () {
 	    wc -l) == 2 ] || \
 	      return 1
 
+	info "Checking clone depth"
 	ERR_MSG="Flow actions may not be safe on all matching packets"
+	PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
+	ovs_add_flow "test_netlink_checks" nv0 \
+		'in_port(1),eth(),eth_type(0x800),ipv4()' \
+		'clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(drop)))))))))))))))))' \
+		&> /dev/null && return 1
+	POST_TEST=$(dmesg | grep -c "${ERR_MSG}")
+
+	if [ "$PRE_TEST" == "$POST_TEST" ]; then
+		info "failed - clone depth too large"
+		return 1
+	fi
+
 	PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
 	ovs_add_flow "test_netlink_checks" nv0 \
 		'in_port(1),eth(),eth_type(0x0806),arp()' 'drop(0),2' \
tools/testing/selftests/net/openvswitch/ovs-dpctl.py +56 -15

137 lines not on this page · raw message

Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH net 1/2] net: openvswitch: limit the number of recursions from action sets

In reply to Aaron Conole
Quoted 61 lines · Aaron Conole

The ovs module allows for some actions to recursively contain an action list for complex scenarios, such as sampling, checking lengths, etc. When these actions are copied into the internal flow table, they are evaluated to validate that such actions make sense, and these calls happen recursively.

The ovs-vswitchd userspace won't emit more than 16 recursion levels deep. However, the module has no such limit and will happily accept limits larger than 16 levels nested. Prevent this by tracking the number of recursions happening and manually limiting it to 16 levels nested.

The initial implementation of the sample action would track this depth and prevent more than 3 levels of recursion, but this was removed to support the clone use case, rather than limited at the current userspace limit.

Fixes 798c166173ff ("openvswitch: Optimize sample action for the clone use cases")
---
 net/openvswitch/flow_netlink.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
net/openvswitch/flow_netlink.c +10 -5
index 88965e2068ac..ba5cfa67a720 100644
@@ -48,6 +48,9 @@ struct ovs_len_tbl {
 
 #define OVS_ATTR_NESTED -1
 #define OVS_ATTR_VARIABLE -2
+#define OVS_COPY_ACTIONS_MAX_DEPTH 16
+
+static DEFINE_PER_CPU(int, copy_actions_depth);
 
 static bool actions_may_change_flow(const struct nlattr *actions)
 {
@@ -3148,11 +3151,11 @@ static int copy_action(const struct nlattr *from,
        return 0;
 }
 
-static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
-                                 const struct sw_flow_key *key,
-                                 struct sw_flow_actions **sfa,
-                                 __be16 eth_type, __be16 vlan_tci,
-                                 u32 mpls_label_count, bool log)
+static int ___ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+                                  const struct sw_flow_key *key,
+                                  struct sw_flow_actions **sfa,
+                                  __be16 eth_type, __be16 vlan_tci,
+                                  u32 mpls_label_count, bool log)
 {
        u8 mac_proto = ovs_key_mac_proto(key);
        const struct nlattr *a;
@@ -3478,6 +3481,26 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
        return 0;
 }
 
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+                                 const struct sw_flow_key *key,

net/openvswitch/flow_netlink.c static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · line 3495

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+                                 struct sw_flow_actions **sfa,
+                                 __be16 eth_type, __be16 vlan_tci,
+                                 u32 mpls_label_count, bool log)
+{
+       int level = this_cpu_read(copy_actions_depth);
+       int ret;
+
+       if (level > OVS_COPY_ACTIONS_MAX_DEPTH)
+               return -EOVERFLOW;
+

This code seems to run in process context.

Using per cpu limit would not work, unless you disabled migration ?

Quoted 14 lines
net/openvswitch/flow_netlink.c +8 -1
+       __this_cpu_inc(copy_actions_depth);
+       ret = ___ovs_nla_copy_actions(net, attr, key, sfa, eth_type,
+                                     vlan_tci, mpls_label_count, log);
+       __this_cpu_dec(copy_actions_depth);
+
+       return ret;
+}
+
 /* 'key' must be the masked key. */
 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                         const struct sw_flow_key *key,
--

2.41.0

author maintainer
#3 lore raw

Re: [PATCH net 1/2] net: openvswitch: limit the number of recursions from action sets

Quoted 75 lines · Eric Dumazet
Quoted 61 lines · Aaron Conole

The ovs module allows for some actions to recursively contain an action list for complex scenarios, such as sampling, checking lengths, etc. When these actions are copied into the internal flow table, they are evaluated to validate that such actions make sense, and these calls happen recursively.

The ovs-vswitchd userspace won't emit more than 16 recursion levels deep. However, the module has no such limit and will happily accept limits larger than 16 levels nested. Prevent this by tracking the number of recursions happening and manually limiting it to 16 levels nested.

The initial implementation of the sample action would track this depth and prevent more than 3 levels of recursion, but this was removed to support the clone use case, rather than limited at the current userspace limit.

Fixes 798c166173ff ("openvswitch: Optimize sample action for the clone use cases")
---
 net/openvswitch/flow_netlink.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
net/openvswitch/flow_netlink.c +10 -5
index 88965e2068ac..ba5cfa67a720 100644
@@ -48,6 +48,9 @@ struct ovs_len_tbl {
 
 #define OVS_ATTR_NESTED -1
 #define OVS_ATTR_VARIABLE -2
+#define OVS_COPY_ACTIONS_MAX_DEPTH 16
+
+static DEFINE_PER_CPU(int, copy_actions_depth);
 
 static bool actions_may_change_flow(const struct nlattr *actions)
 {
@@ -3148,11 +3151,11 @@ static int copy_action(const struct nlattr *from,
        return 0;
 }
 
-static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
-                                 const struct sw_flow_key *key,
-                                 struct sw_flow_actions **sfa,
-                                 __be16 eth_type, __be16 vlan_tci,
-                                 u32 mpls_label_count, bool log)
+static int ___ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+                                  const struct sw_flow_key *key,
+                                  struct sw_flow_actions **sfa,
+                                  __be16 eth_type, __be16 vlan_tci,
+                                  u32 mpls_label_count, bool log)
 {
        u8 mac_proto = ovs_key_mac_proto(key);
        const struct nlattr *a;
@@ -3478,6 +3481,26 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
        return 0;
 }
 
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
+                                 const struct sw_flow_key *key,

net/openvswitch/flow_netlink.c static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · line 3495

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+                                 struct sw_flow_actions **sfa,
+                                 __be16 eth_type, __be16 vlan_tci,
+                                 u32 mpls_label_count, bool log)
+{
+       int level = this_cpu_read(copy_actions_depth);
+       int ret;
+
+       if (level > OVS_COPY_ACTIONS_MAX_DEPTH)
+               return -EOVERFLOW;
+

This code seems to run in process context.

Quoted 1 line

Using per cpu limit would not work, unless you disabled migration ?

Oops - I didn't consider it.

Given that, maybe the best approach would not to rely on per-cpu counter. I'll respin in the next series with a depth counter that I pass to the function instead and compare that. I guess that should address migration and eliminate the need for per-cpu counter.

Does it make sense?

Quoted 14 lines
Quoted 14 lines
net/openvswitch/flow_netlink.c +8 -1
+       __this_cpu_inc(copy_actions_depth);
+       ret = ___ovs_nla_copy_actions(net, attr, key, sfa, eth_type,
+                                     vlan_tci, mpls_label_count, log);
+       __this_cpu_dec(copy_actions_depth);
+
+       return ret;
+}
+
 /* 'key' must be the masked key. */
 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                         const struct sw_flow_key *key,
--

2.41.0

#4 lore raw

Re: [PATCH net 1/2] net: openvswitch: limit the number of recursions from action sets

Quoted 10 lines · Aaron Conole

Oops - I didn't consider it.

Given that, maybe the best approach would not to rely on per-cpu counter. I'll respin in the next series with a depth counter that I pass to the function instead and compare that. I guess that should address migration and eliminate the need for per-cpu counter.

Does it make sense?

Sure, a depth parameter would work much better ;)

author maintainer
#5 lore raw

Re: [PATCH net 1/2] net: openvswitch: limit the number of recursions from action sets

Quoted 5 lines · Eric Dumazet
Quoted 4 lines · Aaron Conole

Oops - I didn't consider it.

Quoted 8 lines
Quoted 6 lines

Given that, maybe the best approach would not to rely on per-cpu counter. I'll respin in the next series with a depth counter that I pass to the function instead and compare that. I guess that should address migration and eliminate the need for per-cpu counter.

Does it make sense?

Sure, a depth parameter would work much better ;)

Okay - I'll give time for others to comment and resubmit in ~24 hours unless there's a reason to submit sooner.