net: hsr: fix shared-skb mutations in the forwarding path
Subsystems (4)
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 v2 series ("net: hsr: fix GRO/GSO super-packet handling",
https://lore.kernel.org/all/[email protected]/):
full revised series vs full GRO v2 series verified applying cleanly in both orders (selftest Makefile merges identically either way); no textual or semantic dependency.
Validation (raw logs in the series' evidence archive):
- both shared-skb mutation tests: base failed, patched passed
- all executable HSR/PRP selftests of the base: pass
- create/delete loop x10: clean
- checkpatch --strict: 0 errors (one routine new-file MAINTAINERS note)
- W=1 base-vs-patched: allmodconfig + allyesconfig, 0 new warnings
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 | 213 ++++++++++++++++++ 3 files changed, 260 insertions(+), 5 deletions(-) create mode 100755 tools/testing/selftests/net/hsr/hsr_shared_mutation.sh
base-commit: 53658c6f3682967a5e76ed4bc7462c4bdcddaec3
Signature · 1 line
2.43.0
PATCH 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
Re: [PATCH net 1/3] net: hsr: privatize interlink-bound skbs before address mutation
The skb_cow() is placed before the address mutation and, thanks to the short-circuit, only runs when the interlink skb can alias a live
consumer: master-originated frames, or ring frames the master also consumes (is_local_dest and not is_local_exclusive). The zero-copy ring paths keep their behavior. On allocation failure the frame is dropped with rx_dropped accounting, matching the tagged-frame creation failure path. Reads correctly to me.
Jakub Kicinski
maintainerRe: [PATCH net 1/3] net: hsr: privatize interlink-bound skbs before address mutation
Quoted 9 lines · On Sat, 1 Aug 2026 03:29:31 +0300 Ali Ahmet Memis wrote:
The
skb_cow()is placed before the address mutation and, thanks to the short-circuit, only runs when the interlink skb can alias a live
consumer: master-originated frames, or ring frames the master also consumes (is_local_destand notis_local_exclusive). The zero-copy ring paths keep their behavior. On allocation failure the frame is dropped withrx_droppedaccounting, matching the tagged-frame creation failure path. Reads correctly to me.Reviewed-by Ali Ahmet Memis <[email protected]>
How are you producing this review?
I've never seen this sort of blurb human-written.
Re: [PATCH net 1/3] net: hsr: privatize interlink-bound skbs before address mutation
Quoted 2 lines · On Mon, 3 Aug 2026 12:43:05 -0700 Jakub Kicinski wrote:
How are you producing this review?
I've never seen this sort of blurb human-written.
I write them myself. English is not my first language, so I draft a comment and then keep cutting and rearranging it until it says exactly what I mean. What comes out is denser than how people normally write on a list, and I can see how that reads as machine output.
The content is what I went through in hsr_forward.c. hsr_deliver_master() calls hsr_addr_subst_source(), which writes into the header, so the copy has to happen before that call rather than after it. It only matters when the master can be looking at the same skb as another consumer, and in hsr_forward_do() that is decided by these two:
if (port->type == HSR_PT_MASTER && !frame->is_local_dest) continue; if (port->type != HSR_PT_MASTER && frame->is_local_exclusive) continue;
so a ring frame reaches the master and a slave port together only when is_local_dest is set and is_local_exclusive is not. That is the case I meant by the skb aliasing a live consumer. The accounting remark was about the create_tagged_frame() failure just below, which does frame->port_rcv->dev->stats.rx_dropped++, and the new failure path does the same.
If the phrasing is what bothers you I can write these plainly from now on. I only tag patches I have read through myself.
PATCH 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
Re: [PATCH net 2/3] net: hsr: clone before updating path and LAN IDs in tagged frames
Clone first, privatize with skb_cow(), reacquire the header or trailer pointer, then write the id: this removes the shared-buffer mutation for both hsr_create_tagged_frame() and prp_create_tagged_frame(). The error paths (clone failure, cow failure, NULL trailer) all free the clone and return NULL, and adding kfree_skb() on the NULL-trailer path also closes a leak. Using skb_cow() rather than skb_cow_head() is right since the PRP RCT sits at the linear tail.
PATCH 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 | 213 ++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100755 tools/testing/selftests/net/hsr/hsr_shared_mutation.sh
tools/testing/selftests/net/hsr/Makefile +1 -0
@@ -7,6 +7,7 @@ TEST_PROGS := \ hsr_redbox.sh \ link_faults.sh \ prp_ping.sh \ + hsr_shared_mutation.sh \ # end of TEST_PROGS TEST_FILES += hsr_common.sh
tools/testing/selftests/net/hsr/hsr_shared_mutation.sh new file +213 -0
213 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH net 3/3] selftests: net: hsr: add shared-mutation regression test
The test covers both classes: the PRP LAN-ID race under a netem delay on one slave, and the HSR RedBox source-MAC alias with a tagged multicast on a slave. The capability probes (tc, python3, sch_netem, HSR/PRP support) exit ksft_skip and propagate that status, while real failures exit 1. The capture filters keying on destination, post-strip EtherType and payload while leaving the asserted source MAC unfiltered look correct.
I reproduced both classes and tested the series under virtme-ng (QEMU/KVM).
Running hsr_shared_mutation.sh on an unpatched kernel fails both cases: in the PRP LAN-ID case all 200 frames leave slave A carrying slave B's LAN id, and in the HSR RedBox case the master receives the RedBox MAC instead of the originating node's MAC. With the series applied both are isolated: slave A keeps its own LAN id on all 200 frames, and the master keeps the node MAC while the interlink keeps the RedBox MAC. The test passes.
Reviewed-by for the individual patches sent separately.