net: openvswitch: fix a feature leak on a failed change
Subsystems (5)
v7 cover net: openvswitch: fix a feature leak on a failed change
ovs_dp_change() records the requested features in dp->user_features before it updates the per-cpu upcall port IDs, and that update fails with -EINVAL when the per-cpu-pids attribute is empty or its length is not a multiple of four. The request fails with the new features already recorded, while the code that takes the reference for TC_RECIRC_SHARING never ran, so a later request that clears the bit drops a reference that was never taken.
The first two patches are the fix, split so that the single read of dp->user_features in queue_userspace_packet() stands on its own: it closes a separate and older oops and carries its own Fixes: tag. The third patch adds the selftest. The series is three patches; v5 had two.
These issues were found by code inspection with LLM assistance. The failed-feature update was reproduced on the unfixed parent 2a324a58a084:
# the rejected set changed the features TEST: ovsdp: failed set keeps bits [FAIL]
The upcall race and the reference-count warning were not triggered; those parts rest on code inspection, not on a runtime trace.
Tested on x86_64 with CONFIG_NET_TC_SKB_EXT=y. Both new tests and the full 21-test OVS suite pass on the patched kernel. The baseline selftest scripts also pass all 19 cases on that same kernel.
v7:
- Say what the comment in queue_userspace_packet() guards: the choice of hlen, not head room. Sashiko.
- Correct the commit message of patch 2. Clearing the bit in the reset path drops this datapath's reference to a kernel wide key; other holders keep the key enabled, so the packet path is unaffected and what was lost is a reference. Sashiko.
- Narrow the comment on ovs_dp_set_user_features() and the @user_features kernel-doc to what is actually marked, and name the readers that still use a plain load. Sashiko.
- ovs_add_dp() returned the status of registering its cleanup command rather than the status of the add-dp it ran, so a datapath that was never created was reported as a feature mismatch. It now returns that status. Sashiko.
- Use f-strings in the prints this series adds, so the selftest patch adds no new ruff errors. NIPA.
- Cc Willem de Bruijn, Jamal Hadi Salim and linux-kselftest, which cc_maintainers asked for. NIPA.
- Rebased onto net 28a371228d4e.
v6: https://lore.kernel.org/netdev/[email protected]/
- Split the single read of dp->user_features out of the fix. It closes a crash that aea0bb4f8ee5 introduced in 2014, and folding it into a patch whose Fixes: tag points at 35d39fecbc24 hid that from a stable backport.
- Move Fixes: and Cc: into the trailer block; they had prose below them.
- Read the field with READ_ONCE() in ovs_dp_set_user_features(), now that every write to it is a single WRITE_ONCE(), and clear only the recirc bit through one helper instead of repeating the mask at two sites.
- Carry a changelog per patch in the notes under ---, as Matthieu asked.
- Quote the variables the new selftest helpers pass to subprocesses, and name both conditions that make the kernel reject the per-cpu-pids attribute.
- Move Reported-by: to the kernel fix, with the link to the report.
v5: https://lore.kernel.org/netdev/[email protected]/
- Use an empty per-cpu-pids attribute to make the update fail, instead of relying on the masks cache path. Suggested by Matthieu Baerts.
- Report the datapath that cannot be read as that, and not as a feature mismatch.
v4: https://lore.kernel.org/netdev/[email protected]/
- Take and drop the reference in one setter, and call it from every path that changes or clears the recirc bit, including ovs_dp_reset_user_features() and __dp_destroy().
- Clear only the recirc bit in __dp_destroy(), so an upcall that is reading the other bits keeps seeing them.
v3: https://lore.kernel.org/netdev/[email protected]/
- Describe the user namespace path in the commit message.
- Check the feature bit and the reference count in the selftest instead of only the return value.
v2: https://lore.kernel.org/netdev/[email protected]/
- Split the selftest into its own patch.
- Add the ovs-dpctl set-dp command the selftest needs.
v1: https://lore.kernel.org/netdev/[email protected]/
Minxi Hou (3):
net: openvswitch: read the datapath features once when sending an
upcall
net: openvswitch: do not record user features when the change fails selftests/net/openvswitch: check that a failed set keeps the features
net/openvswitch/datapath.c | 72 +++++-- net/openvswitch/datapath.h | 10 +- .../testing/selftests/net/openvswitch/config | 3 + .../selftests/net/openvswitch/openvswitch.sh | 201 ++++++++++++++++++ .../selftests/net/openvswitch/ovs-dpctl.py | 74 +++++++ 5 files changed, 342 insertions(+), 18 deletions(-)
base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f
Signature · 1 line
2.56.1
PATCH v7 1/3 net: openvswitch: read the datapath features once when sending an upcall
queue_userspace_packet() decides from OVS_DP_F_UNALIGNED whether to copy the packet with skb_zerocopy(), and pad_packet() reads the same field again after the copy. A concurrent OVS_DP_CMD_SET can clear the bit between the two reads, so the copy leaves a nonlinear skb and the padding calls skb_put_zero() on it. skb_put() asserts that the skb is linear, so the kernel oopses, and a host built with panic_on_oops goes down.
OVS_DP_CMD_SET needs CAP_NET_ADMIN, which a user namespace that owns a network namespace provides, so the window is reachable without privileges on the host.
Read the field once and pass that value to pad_packet().
---
v7: the comment now says which decision it guards. The code below it picks hlen, the number of bytes skb_zerocopy() copies into the linear part, and never chooses head room. Sashiko pointed that out.
net/openvswitch/datapath.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/openvswitch/datapath.c +12 -4
@@ -439,9 +439,9 @@ static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,return size; } -static void pad_packet (struct datapath *dp,struct sk_buff *skb) +static void pad_packet (struct sk_buff *skb,u32 features) { -if (!(dp->user_features &OVS_DP_F_UNALIGNED )) { +if (!(features &OVS_DP_F_UNALIGNED )) {size_t plen =NLA_ALIGN (skb->len) - skb->len;if (plen >0 )
@@ -461,6 +461,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,size_t msg_size;size_t skb_len;unsigned int hlen; +u32 features;int err, dp_ifindex;u64 hash;
@@ -494,11 +495,18 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, (err = skb_csum_hwoffload_help(skb, 0))) goto out; +/* A concurrent change to the features can land between the linear copy +* length (hlen) below and the padding after the copy, which would pad a +* nonlinear skb and trip SKB_LINEAR_ASSERT. Read the field once and use +* that value for both. +*/ + features =READ_ONCE (dp->user_features); +/* Older versions of OVS user space enforce alignment of the last * Netlink attribute to NLA_ALIGNTO which would require extensive * padding logic. Only perform zerocopy if padding is not required. */ -if (dp->user_features &OVS_DP_F_UNALIGNED ) +if (features &OVS_DP_F_UNALIGNED ) hlen =min (skb_zerocopy_headlen (skb), cutlen);else hlen = skb_len;
@@ -598,7 +606,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, goto out;/* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ -pad_packet (dp, user_skb); +pad_packet (user_skb, features); ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
Signature · 1 line
2.56.1
PATCH v7 2/3 net: openvswitch: do not record user features when the change fails
In reply to Minxi Houovs_dp_change() stores the new features in dp->user_features before it updates the per-cpu upcall port IDs, and that update fails when the per-cpu-pids attribute is empty or its length is not a multiple of four. The update runs only when the same request also sets OVS_DP_F_DISPATCH_UPCALL_PER_CPU, which is what user space that wants per-cpu upcalls sends.
The request returns -EINVAL with the new features already recorded, while the code that enables or disables the tc skb extension key never ran.
The datapath then claims TC_RECIRC_SHARING without holding the reference the bit stands for. A later request that clears the bit drops a reference that was never taken, and a datapath destroyed with the bit clear never drops the one it did take. The key is shared with the other users of recirc sharing, so the count can reach zero while one of them still reads it, and a disable on a count of zero warns from static_key_dec_not_one(), which panics a host that runs with panic_on_warn.
The request only needs CAP_NET_ADMIN, which a user namespace that owns a network namespace provides. datapath_policy has no entry for OVS_DP_ATTR_PER_CPU_PIDS and the operation skips strict validation, so an empty attribute reaches the check that fails.
Record the features last, in one setter that also takes and drops the reference, and use it from every place that changes or clears the bit. Two of those places leaked a reference: a datapath that fails after ovs_dp_change() enabled the key was freed without a disable, and ovs_dp_reset_user_features() cleared the bit without one. The reset runs when an outdated user space instance sends OVS_DP_CMD_NEW for a name that already exists. Clearing the bit there drops this datapath's reference to a kernel wide key. Other holders keep the key enabled, so this datapath's packets keep using the tc skb extension; what was lost is the reference, and the count no longer matches the number of holders.
__dp_destroy() clears the bit before the ports are detached and before the datapath leaves the list, so an upcall can still be reading the other bits from the receive path, and the clear goes through the same setter.
---
v7: two corrections from Sashiko.
The commit message said that after the reset the datapath's packets no longer share the tc skb extension. The key is kernel wide: clearing the bit drops this datapath's reference, and other holders keep the key enabled, so the packet path is unaffected and the loss is the reference itself.
The comment on ovs_dp_set_user_features() claimed the WRITE_ONCE() covers every lockless reader. Only this helper and queue_userspace_packet() are marked; ovs_dp_process_packet(), output_userspace() and ovs_vport_get_upcall_portids() still use a plain load on an aligned u32 and test one bit. The comment and the @user_features kernel-doc now say that.
net/openvswitch/datapath.c | 56 +++++++++++++++++++++++++++++--------- net/openvswitch/datapath.h | 10 ++++++- 2 files changed, 52 insertions(+), 14 deletions(-)
net/openvswitch/datapath.c +43 -13
@@ -1712,6 +1712,38 @@ static struct datapath *lookup_datapath(struct net *net,return dp ? dp :ERR_PTR (-ENODEV ); } +/* Called with ovs_mutex, which every writer holds. The store is a single +* WRITE_ONCE() because the field is read without a lock from the packet path; +* this helper reads it with READ_ONCE() for the same reason. Readers that +* still use a plain load -- ovs_dp_process_packet(), output_userspace() and +* ovs_vport_get_upcall_portids() -- read the u32 once and test one bit, so +* they are not marked here. The store happens before the reference is taken +* or dropped, the order ovs_dp_change() used. +*/ +static void ovs_dp_set_user_features (struct datapath *dp,u32 features) +{ +u32 previous =READ_ONCE (dp->user_features); +bool was = previous &OVS_DP_F_TC_RECIRC_SHARING ; +bool now = features &OVS_DP_F_TC_RECIRC_SHARING ; + +WRITE_ONCE (dp->user_features, features); + +if (now && !was) +tc_skb_ext_tc_enable (); +else if (!now && was) +tc_skb_ext_tc_disable (); +} + +/* Give up the reference for the recirc bit without touching the other bits, so +* an upcall that is reading them keeps seeing what it saw. +*/ +static void ovs_dp_clear_recirc_sharing (struct datapath *dp) +{ +u32 features =READ_ONCE (dp->user_features); + +ovs_dp_set_user_features (dp, features & ~OVS_DP_F_TC_RECIRC_SHARING ); +} +static void ovs_dp_reset_user_features (struct sk_buff *skb,struct genl_info *info) {
@@ -1724,7 +1756,7 @@ static void ovs_dp_reset_user_features(struct sk_buff *skb,pr_warn ("%s: Dropping previously announced user features\n" ,ovs_dp_name (dp)); - dp->user_features =0 ; +ovs_dp_set_user_features (dp,0 ); } static int ovs_dp_set_upcall_portids(struct datapath *dp,
@@ -1781,7 +1813,7 @@ u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id)static int ovs_dp_change (struct datapath *dp,struct nlattr *a[]) { -u32 user_features =0 , old_features = dp->user_features; +u32 user_features =0 ;int err;if (a[OVS_DP_ATTR_USER_FEATURES ]) {
@@ -1809,9 +1841,7 @@ static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])return err; } - dp->user_features = user_features; - -if (dp->user_features &OVS_DP_F_DISPATCH_UPCALL_PER_CPU && +if (user_features &OVS_DP_F_DISPATCH_UPCALL_PER_CPU && a[OVS_DP_ATTR_PER_CPU_PIDS ]) {/* Upcall Netlink Port IDs have been updated */ err = ovs_dp_set_upcall_portids(dp,
@@ -1820,12 +1850,7 @@ static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])return err; } -if ((dp->user_features &OVS_DP_F_TC_RECIRC_SHARING ) && - !(old_features &OVS_DP_F_TC_RECIRC_SHARING )) -tc_skb_ext_tc_enable (); -else if (!(dp->user_features &OVS_DP_F_TC_RECIRC_SHARING ) && - (old_features &OVS_DP_F_TC_RECIRC_SHARING )) -tc_skb_ext_tc_disable (); +ovs_dp_set_user_features (dp, user_features);return 0 ; }
@@ -1947,6 +1972,8 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) err_destroy_portids:kfree (rcu_dereference_raw (dp->upcall_portids)); err_unlock_and_destroy_meters: +/* The datapath is freed here and never reaches __dp_destroy(). */ +ovs_dp_clear_recirc_sharing (dp);ovs_unlock ();ovs_meters_exit (dp); err_destroy_ports:
@@ -1969,8 +1996,11 @@ static void __dp_destroy(struct datapath *dp)struct flow_table *table = &dp->table;int i; -if (dp->user_features &OVS_DP_F_TC_RECIRC_SHARING ) -tc_skb_ext_tc_disable (); +/* Only the recirc bit goes here. The datapath is still on the list and +* its ports are still attached, so an upcall can be reading the other +* bits from the receive path while this runs. +*/ +ovs_dp_clear_recirc_sharing (dp);for (i =0 ; i <DP_VPORT_HASH_BUCKETS ; i++) {struct vport *vport;
net/openvswitch/datapath.h +9 -1
@@ -76,7 +76,15 @@ struct dp_nlsk_pids {* ovs_mutex and RCU. * @stats_percpu: Per-CPU datapath statistics. * @net: Reference to net namespace. -* @user_features: Bitmap of enabled %OVS_DP_F_* features. +* @user_features: Bitmap of enabled %OVS_DP_F_* features. A datapath +* that has %OVS_DP_F_TC_RECIRC_SHARING set holds one reference to the tc +* skb extension, so this is a reference count on a kernel wide key rather +* than a switch for the datapath's own packet path: the bit can be cleared +* while the key stays enabled for other holders. +* ovs_dp_set_user_features() keeps the bit and the reference in step, and +* every change goes through it with ovs_mutex held. ovs_dp_change() calls +* it after the per-cpu pid update, which can fail, so a request that fails +* leaves the features alone. * @max_headroom: The maximum headroom of all vports in this datapath; it will * be used by all the internal vports in this dp. * @meter_tbl: Meter table.
Signature · 1 line
2.56.1
PATCH v7 3/3 selftests/net/openvswitch: check that a failed set keeps the features
In reply to Minxi HouAdd a set-dp command to ovs-dpctl for the kernel's datapath SET command, and two tests that use it.
The first test clears the features of a fresh datapath, sets the dispatch bit with a valid pid list, and clears them again. It then sends a set that would set that bit with an empty per-cpu-pids. The request has to fail with EINVAL, and the datapath has to keep reporting the features it had, which a kernel that records them before the change reads as 0x8 instead of 0x0. A second request sends OVS_DP_CMD_NEW for that name from an outdated client, which makes the kernel drop the features, and a further set has to work.
The second test covers the recirc bit. It sets that bit on a fresh datapath and sends a set that would set the dispatch bit with an empty per-cpu-pids. The rejected request has to leave the recirc bit in place. A kernel that records the features first reads 0x8 there instead of 0x4. A datapath holds that bit only when the tc skb extension is compiled in, so the test prints the reason and skips when the kernel answers EOPNOTSUPP, and any other failure of that request fails the test. CONFIG_NET_TC_SKB_EXT depends on CONFIG_NET_CLS_ACT and both sit under CONFIG_NET_SCHED, so the selftest config fragment selects all three.
The reference count itself is not visible from user space, so the steps that use an outdated client only show that those paths run and that a datapath keeps working across them.
set-dp sends the features it is given. When --features is left out it sends the features the datapath reports, and it refuses to send the request when the datapath reports none. A pid list that is not a list of numbers is reported as such, and empty entries are dropped.
A failing run deletes debug.log, and ovs_sbx sends a command's stdout into that same file, so the tests report a failure reason on stderr. stderr still reaches the test runner from inside ovs_sbx, and unlike a duplicated stdout it is not a second handle on the runner's pipe. The daemons the suite starts outlive the script, and one that inherits such a handle keeps the pipe open after the script exits, which leaves the run hanging until the harness times out.
Every step that can fail says why it did. A datapath that cannot be read is reported as that rather than as a feature mismatch: ovs-dpctl prints a missing datapath and exits 0, and it leaves the features line out when the kernel does not report them, so the check reads both cases from the output. ovs_add_dp() registers the cleanup command before it returns, which used to make it return the status of that registration rather than the status of the add-dp it ran; it now keeps that status and returns it, so a datapath that was never created is reported as such.
---
v7: two fixes from review and CI.
ovs_add_dp() ran add-dp and then registered the cleanup command, so the function returned the status of that registration. A datapath that was never created therefore surfaced as "the datapath features could not be cleared". It now keeps add-dp's status and returns it, and both tests say when the datapath was not created. Sashiko.
The prints added here used percent formatting, which ruff counts as new errors in this file. They use f-strings now.
.../testing/selftests/net/openvswitch/config | 3 + .../selftests/net/openvswitch/openvswitch.sh | 201 ++++++++++++++++++ .../selftests/net/openvswitch/ovs-dpctl.py | 74 +++++++ 3 files changed, 278 insertions(+)
tools/testing/selftests/net/openvswitch/config +3 -0
@@ -2,10 +2,13 @@ CONFIG_GENEVE=m CONFIG_INET_DIAG=y CONFIG_IP_SCTP=y CONFIG_IPV6=y +CONFIG_NET_CLS_ACT=y CONFIG_NETFILTER=y CONFIG_NETFILTER_ADVANCED=y CONFIG_NET_IPGRE=m CONFIG_NET_IPGRE_DEMUX=m +CONFIG_NET_SCHED=y +CONFIG_NET_TC_SKB_EXT=y CONFIG_NF_CONNTRACK=m CONFIG_NF_CONNTRACK_OVS=y CONFIG_NF_CT_PROTO_SCTP=y
tools/testing/selftests/net/openvswitch/openvswitch.sh +201 -0
224 lines not on this page · raw message
tools/testing/selftests/net/openvswitch/ovs-dpctl.py +74 -0
92 lines not on this page · raw message
Signature · 1 line
2.56.1