net: hsr: fix shared-skb mutations in the forwarding path
Subsystems (4)
v2 cover net: hsr: fix shared-skb mutations in the forwarding path
During the review of the PRP RedBox v4 series (Simon Horman forwarding the sashiko AI review), shared-skb-data mutation issues in the existing HSR/PRP forwarding code were raised. They concern pre-existing code, and we promised independent fixes for net. An independent source audit then confirmed two distinct defect classes. This series fixes both.
Patch 1/3 (interlink address mutations): on an HSR RedBox, frames forwarded to the interlink arrive in hsr_xmit() as clones of a shared skb. A tagged multicast or broadcast from a slave is also delivered to the master through another clone of the same buffer, so the node MAC written by hsr_deliver_master() and the RedBox MAC written by hsr_xmit() land in the same six bytes; the local stack receives the RedBox MAC instead of the originating node's. Master-originated frames alias the original TX skb (taps may hold it), so the master-origin substitutions and the RedBox rewrite would share data too. The interlink-bound skb is now privatized with skb_cow() before any address mutation: for all master-originated frames, and for ring frames the master also consumes. On the hardware tag-insertion path, the selected master-originated and locally consumed ring frames are now isolated before the interlink address write. Ring frames the master does not consume retain their existing zero-copy behavior and pre-existing aliases; the pre-existing slave-side packet-tap alias is explicitly not addressed here.
Patch 2/3 (path/LAN ID alias): hsr_create_tagged_frame() and prp_create_tagged_frame() wrote the path/LAN ID into the received skb's shared buffer and then skb_clone()d it per slave, so the second slave's ID landed in the first slave's still-queued clone. With a 200 ms netem delay, all 200 injected frames left slave A carrying slave B's LAN ID. The helpers now clone first, privatize with skb_cow(), reacquire the pointer, then write.
Patch 3/3 adds a regression selftest covering both classes:
pre-tagged PRP injection (base: 200/200 wrong-lan; patched:
isolated) and an HSR RedBox tagged-multicast case (base: the master receives the RedBox MAC; patched: the master keeps the node MAC and the interlink keeps the RedBox MAC).
Each code patch carries its own independently identified Fixes:
commit (5055cccfc2d1, RedBox introduction, v6.10; 451d8123f897, PRP support, v5.9). The PRP RedBox v4 feature series is unaffected and benefits when rebased.
Relationship with the in-flight GRO/GSO series ("net: hsr: fix GRO/GSO super-packet handling", currently at v3,
https://lore.kernel.org/all/[email protected]/):
both series touch net/hsr/hsr_forward.c and the HSR selftest Makefile; they verify as independent - full series applied in both orders on current net (and the v2 selftest Makefile entry merges with GRO's at their respective sorted positions); no textual or semantic dependency.
Apply note: on net the series applies cleanly with plain git am. On net-next, which already contains hsr_prp_redbox.sh in the HSR selftest Makefile, the Makefile hunk of patch 3 needs git am's three-way fallback (git am -3); the automatic merge inserts hsr_shared_mutation.sh at its sorted position and the merged result passes the upstream Makefile format validation.
Validation:
- both shared-skb mutation tests: the frozen v2 script (SHA-256 9096d2c9050b052bc0355bce969fdea248382c90d92103f17b5e3cc32102a852), which differs from v1 only by the behavior-preserving CI edits listed in the changelog, failed on the base kernel and passed on the patched kernel
- independently reproduced and tagged on the v1 thread by Ali Ahmet Memis (Tested-by on the series, Reviewed-by on both code patches)
Changelog:
v1 -> v2 (patches 1/3 and 2/3 C payloads and technical descriptions unchanged; identical patch-ids):
- carried trailers from the v1 thread: Reviewed-by and Tested-by of Ali Ahmet Memis <[email protected]> on both code patches;
- patch 3/3 selftest CI fixes only (no behavior change):
- two SC2181 rewrites to direct exit-code checks,
- register the new test at its sorted TEST_PROGS position,
- rewrap the twelve >80-column lines reported by NIPA checkpatch;
- Cc the hsr_forward.c area authors and the v1 reviewer.
Previous postings (newest first):
v1: https://lore.kernel.org/all/[email protected]/
Xin Xie (3):
net: hsr: privatize interlink-bound skbs before address mutation
net: hsr: clone before updating path and LAN IDs in tagged frames
selftests: net: hsr: add shared-mutation regression test
net/hsr/hsr_forward.c | 51 +++- tools/testing/selftests/net/hsr/Makefile | 1 + .../selftests/net/hsr/hsr_shared_mutation.sh | 223 ++++++++++++++++++ 3 files changed, 270 insertions(+), 5 deletions(-) create mode 100755 tools/testing/selftests/net/hsr/hsr_shared_mutation.sh
base-commit: 2195424c3da2ef1829a63b807e3a900a90e57d85
Signature · 1 line
2.43.0
PATCH v2 1/3 net: hsr: privatize interlink-bound skbs before address mutation
On an HSR RedBox, frames forwarded to the interlink go through hsr_get_untagged_frame() and arrive in hsr_xmit() as skb_clone(frame->skb_std). Two reachable alias cases make the writes there unsafe:
(a) A tagged multicast or broadcast frame received on a slave is also delivered to the master through another clone of the same
buffer: hsr_deliver_master() writes the node MAC and hsr_xmit() writes hsr->macaddress_redbox into the same six source bytes, so the local stack receives the RedBox MAC instead of the originating node's - deterministic without backpressure.
(b) A master-originated frame forwarded to the interlink aliases the original TX skb, which packet taps or the TX path may still hold. The master-origin substitutions (hsr_addr_subst_dest() and the outgoing-slave source write) and the RedBox rewrite would all modify that shared data.
Privatize the interlink-bound skb with skb_cow() before any address mutation whenever it can alias a live consumer: for all master-originated frames, and for ring frames the master also consumes (is_local_dest && !is_local_exclusive - the exact inverse of the port loop's master skip rules, so the condition does not depend on port order). Every interlink skb reaching hsr_xmit() is a clone from get_untagged_frame(), so the selected cases always pay one private copy. On the hardware tag-insertion path, the selected master-originated and locally consumed ring frames are now isolated before the interlink address write: the interlink write can no longer change an already queued clone of the same frame. Ring frames the master does not consume (is_local_dest == false, e.g. unicast ring-to-SAN) keep the zero-copy path; aliases that already exist there - for example between the interlink clone and a peer slave's hardware-tag-insertion clone - are pre-existing and unchanged by this patch. On allocation failure the frame is dropped with the same accounting as a tagged-frame creation failure.
--- net/hsr/hsr_forward.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
net/hsr/hsr_forward.c +16 -0
@@ -420,6 +420,22 @@ static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev,static int hsr_xmit (struct sk_buff *skb,struct hsr_port *port,struct hsr_frame_info *frame) { +/* An interlink-bound skb from get_untagged_frame() can still alias +* another live consumer: for master-originated frames the clone +* shares the original TX skb (which taps or the TX path may still +* hold); for ring frames the master also consumes them when they +* are destined to the local node without being exclusive to it. +* Privatize before any address mutation. +*/ +if (port->type ==HSR_PT_INTERLINK && + (frame->port_rcv->type ==HSR_PT_MASTER || + (frame->is_local_dest && !frame->is_local_exclusive)) && +skb_cow (skb,0 )) { + frame->port_rcv->dev->stats.rx_dropped++; +kfree_skb (skb); +return NET_XMIT_DROP ; + } +if (frame->port_rcv->type ==HSR_PT_MASTER ) {hsr_addr_subst_dest (frame->node_src, skb, port);
Signature · 1 line
2.43.0
PATCH v2 2/3 net: hsr: clone before updating path and LAN IDs in tagged frames
In reply to Xin Xiehsr_create_tagged_frame() and prp_create_tagged_frame() update the path/LAN ID in the received skb data buffer and then skb_clone() it for the egress port. When the same frame is forwarded to both slave ports, the second port ID update lands in the same shared buffer the first port clone still points at; if that clone is still queued (qdisc backlog, NETEM, BQL), it is transmitted with the second port ID - a silent on-wire corruption.
One always-available trigger is the master transmit path: a pre-tagged frame transmitted on the master (for example injected locally via AF_PACKET with a valid RCT) passes prp_fill_frame_info() with frame->skb_prp set, the master-origin gate lets it through to both slaves, and both slaves run prp_set_lan_id() + skb_clone() on the same buffer. Tagged frames arriving on an HSR RedBox interlink take the analogous path through hsr_create_tagged_frame(). Normal traffic tests do not expose the race: the window between the first dev_queue_xmit() and the second ID write is only a few instructions and opens only under egress backpressure. With a 200 ms netem delay on one slave, all 200 injected frames left that slave carrying the other slave LAN ID in testing.
Reorder both helpers: clone first, privatize the clone with skb_cow(), reacquire the header/trailer pointer, then update the ID. skb_cow() is used rather than skb_cow_head() because privacy is needed for the whole linear area, not only the header: the HSR tag sits in the head, but the PRP RCT sits at the linear tail. skb_get_PRP_rct() computes the trailer from skb_tail_pointer(), so the returned pointer is always inside the linear area that pskb_expand_head() copies; frames with a nonlinear tail are mis-parsed by the existing helper regardless and are out of scope here. (Both wrappers currently reach pskb_expand_head(); they differ in the cloned-data predicate, and correctness here needs full data privacy, not only header privacy.) This adds one linear-head copy per tagged egress; untagged master traffic keeps the existing __pskb_copy() path and pays nothing from this patch.
--- net/hsr/hsr_forward.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-)
net/hsr/hsr_forward.c +30 -5
@@ -336,12 +336,24 @@ struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,int movelen;if (frame->skb_hsr) { -struct hsr_ethhdr *hsr_ethhdr = - (struct hsr_ethhdr *)skb_mac_header (frame->skb_hsr); +struct hsr_ethhdr *hsr_ethhdr; + +/* The original skb data may be shared with another egress +* clone. Make the clone data private before updating the +* path id so the update cannot corrupt the other copy. +*/ + skb =skb_clone (frame->skb_hsr,GFP_ATOMIC ); +if (!skb) +return NULL ; +if (skb_cow (skb,0 )) { +kfree_skb (skb); +return NULL ; + }/* set the lane id properly */ + hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header (skb);hsr_set_path_id (frame, hsr_ethhdr, port); -return skb_clone (frame->skb_hsr,GFP_ATOMIC ); +return skb; }else if (port->dev->features &NETIF_F_HW_HSR_TAG_INS ) {return skb_clone (frame->skb_std,GFP_ATOMIC ); }
@@ -377,15 +389,28 @@ struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,struct sk_buff *skb;if (frame->skb_prp) { -struct prp_rct *trailer =skb_get_PRP_rct (frame->skb_prp); +struct prp_rct *trailer; +/* Same sharing hazard as above: privatize the clone data +* before updating the LAN id. +*/ + skb =skb_clone (frame->skb_prp,GFP_ATOMIC ); +if (!skb) +return NULL ; +if (skb_cow (skb,0 )) { +kfree_skb (skb); +return NULL ; + } + + trailer =skb_get_PRP_rct (skb);if (trailer) {prp_set_lan_id (trailer, port); }else {WARN_ONCE (!trailer,"errored PRP skb" ); +kfree_skb (skb);return NULL ; } -return skb_clone (frame->skb_prp,GFP_ATOMIC ); +return skb; }else if (port->dev->features &NETIF_F_HW_HSR_TAG_INS ) {return skb_clone (frame->skb_std,GFP_ATOMIC ); }
Signature · 1 line
2.43.0
PATCH v2 3/3 selftests: net: hsr: add shared-mutation regression test
In reply to Xin XieCover both shared-skb mutation classes with one test. The PRP LAN-ID case injects 200 pre-tagged frames on the master with one slave egress delayed by netem and selects exactly the injected flow: on an affected kernel all 200 frames leave slave A with slave B LAN ID (FAIL); with the helpers privatizing clone data before the ID update both sides stay isolated (PASS). The HSR RedBox source-MAC case injects a tagged multicast data frame on a slave of an HSR RedBox:
on an affected kernel the local stack receives the RedBox MAC instead of the originating node MAC (FAIL); with the interlink-bound skb privatized before the rewrite, the master keeps the node MAC and the interlink keeps the RedBox MAC (PASS).
Capture filters select destination, post-strip EtherType, and the exact payload while leaving the asserted source MAC unfiltered. Capability probes (tc, python3, sch_netem, HSR/PRP support) exit ksft_skip and that status is propagated; real failures exit 1.
--- tools/testing/selftests/net/hsr/Makefile | 1 + .../selftests/net/hsr/hsr_shared_mutation.sh | 223 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100755 tools/testing/selftests/net/hsr/hsr_shared_mutation.sh
tools/testing/selftests/net/hsr/Makefile +1 -0
@@ -5,6 +5,7 @@ top_srcdir = ../../../../.. TEST_PROGS := \ hsr_ping.sh \ hsr_redbox.sh \ + hsr_shared_mutation.sh \ link_faults.sh \ prp_ping.sh \ # end of TEST_PROGS
tools/testing/selftests/net/hsr/hsr_shared_mutation.sh new file +223 -0
223 lines not on this page · raw message
Signature · 1 line
2.43.0
Paolo Abeni
maintainerRe: [PATCH net v2 2/3] net: hsr: clone before updating path and LAN IDs in tagged frames
In reply to Xin XieQuoted 36 lines · Xin Xie
hsr_create_tagged_frame()andprp_create_tagged_frame()update the path/LAN ID in the received skb data buffer and thenskb_clone()it for the egress port. When the same frame is forwarded to both slave ports, the second port ID update lands in the same shared buffer the first port clone still points at; if that clone is still queued (qdisc backlog, NETEM, BQL), it is transmitted with the second port ID - a silent on-wire corruption.One always-available trigger is the master transmit path: a pre-tagged frame transmitted on the master (for example injected locally via
AF_PACKETwith a valid RCT) passesprp_fill_frame_info()withframe->skb_prpset, the master-origin gate lets it through to both slaves, and both slaves runprp_set_lan_id()+skb_clone()on the same buffer. Tagged frames arriving on an HSR RedBox interlink take the analogous path throughhsr_create_tagged_frame(). Normal traffic tests do not expose the race: the window between the firstdev_queue_xmit()and the second ID write is only a few instructions and opens only under egress backpressure. With a 200 ms netem delay on one slave, all 200 injected frames left that slave carrying the other slave LAN ID in testing.Reorder both helpers: clone first, privatize the clone with
skb_cow(), reacquire the header/trailer pointer, then update the ID.skb_cow()is used rather thanskb_cow_head()because privacy is needed for the whole linear area, not only the header: the HSR tag sits in the head, but the PRP RCT sits at the linear tail.skb_get_PRP_rct()computes the trailer fromskb_tail_pointer(), so the returned pointer is always inside the linear area thatpskb_expand_head()copies; frames with a nonlinear tail are mis-parsed by the existing helper regardless and are out of scope here. (Both wrappers currently reachpskb_expand_head(); they differ in the cloned-data predicate, and correctness here needs full data privacy, not only header privacy.) This adds one linear-head copy per tagged egress; untagged master traffic keeps the existing__pskb_copy()path and pays nothing from this patch.
Quoted 4 lines
Fixes 451d8123f897 ("net: prp: add packet handling support")Reviewed-by Ali Ahmet Memis <[email protected]>Tested-by Ali Ahmet Memis <[email protected]>Signed-off-by Xin Xie <[email protected]>
Please try to condense a bit your commit message: too much text is alike no text at all.
Quoted 7 lines
net/hsr/hsr_forward.c +1
-1@@ -377,15 +389,28 @@ struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,struct sk_buff *skb;if (frame->skb_prp) { -struct prp_rct *trailer =skb_get_PRP_rct (frame->skb_prp); +struct prp_rct *trailer;
net/hsr/hsr_forward.c struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame, · line 403
Quoted 10 lines
net/hsr/hsr_forward.c +10
-0+/* Same sharing hazard as above: privatize the clone data +* before updating the LAN id. +*/ + skb =skb_clone (frame->skb_prp,GFP_ATOMIC ); +if (!skb) +return NULL ; +if (skb_cow (skb,0 )) { +kfree_skb (skb); +return NULL ; + }
This is the verbatim copy of the previous chunk; please create a shared helper instead.
While at it, sashiko notes this leaves the else branch open to the same issue:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260802202504.2962-1-xiexinet%40gmail.com
Note that the data race on the dev stats can be addressed later in separate series, as there are already many occurrences in the hsr code.
Also be aware of net-next commit c82ff94592fb.
/P