Skip to content

Commit 5cd8d46

Browse files
wdebruijdavem330
authored andcommitted
packet: copy user buffers before orphan or clone
tpacket_snd sends packets with user pages linked into skb frags. It notifies that pages can be reused when the skb is released by setting skb->destructor to tpacket_destruct_skb. This can cause data corruption if the skb is orphaned (e.g., on transmit through veth) or cloned (e.g., on mirror to another psock). Create a kernel-private copy of data in these cases, same as tun/tap zerocopy transmission. Reuse that infrastructure: mark the skb as SKBTX_ZEROCOPY_FRAG, which will trigger copy in skb_orphan_frags(_rx). Unlike other zerocopy packets, do not set shinfo destructor_arg to struct ubuf_info. tpacket_destruct_skb already uses that ptr to notify when the original skb is released and a timestamp is recorded. Do not change this timestamp behavior. The ubuf_info->callback is not needed anyway, as no zerocopy notification is expected. Mark destructor_arg as not-a-uarg by setting the lower bit to 1. The resulting value is not a valid ubuf_info pointer, nor a valid tpacket_snd frame address. Add skb_zcopy_.._nouarg helpers for this. The fix relies on features introduced in commit 5226779 ("sock: add MSG_ZEROCOPY"), so can be backported as is only to 4.14. Tested with from `./in_netns.sh ./txring_overwrite` from http://github.com/wdebruij/kerneltools/tests Fixes: 69e3c75 ("net: TX_RING and packet mmap") Reported-by: Anand H. Krishnan <anandhkrishnan@gmail.com> Signed-off-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 039e70a commit 5cd8d46

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,22 @@ static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg)
13261326
}
13271327
}
13281328

1329+
static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val)
1330+
{
1331+
skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL);
1332+
skb_shinfo(skb)->tx_flags |= SKBTX_ZEROCOPY_FRAG;
1333+
}
1334+
1335+
static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb)
1336+
{
1337+
return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL;
1338+
}
1339+
1340+
static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb)
1341+
{
1342+
return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL);
1343+
}
1344+
13291345
/* Release a reference on a zerocopy structure */
13301346
static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy)
13311347
{
@@ -1335,7 +1351,7 @@ static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy)
13351351
if (uarg->callback == sock_zerocopy_callback) {
13361352
uarg->zerocopy = uarg->zerocopy && zerocopy;
13371353
sock_zerocopy_put(uarg);
1338-
} else {
1354+
} else if (!skb_zcopy_is_nouarg(skb)) {
13391355
uarg->callback(uarg, zerocopy);
13401356
}
13411357

‎net/packet/af_packet.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ static void tpacket_destruct_skb(struct sk_buff *skb)
23942394
void *ph;
23952395
__u32 ts;
23962396

2397-
ph = skb_shinfo(skb)->destructor_arg;
2397+
ph = skb_zcopy_get_nouarg(skb);
23982398
packet_dec_pending(&po->tx_ring);
23992399

24002400
ts = __packet_set_timestamp(po, ph, skb);
@@ -2461,7 +2461,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
24612461
skb->mark = po->sk.sk_mark;
24622462
skb->tstamp = sockc->transmit_time;
24632463
sock_tx_timestamp(&po->sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
2464-
skb_shinfo(skb)->destructor_arg = ph.raw;
2464+
skb_zcopy_set_nouarg(skb, ph.raw);
24652465

24662466
skb_reserve(skb, hlen);
24672467
skb_reset_network_header(skb);

0 commit comments

Comments
 (0)