* [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c
@ 2017-08-22 22:12 Florian Fainelli
2017-08-22 22:12 ` [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing Florian Fainelli
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Florian Fainelli @ 2017-08-22 22:12 UTC (permalink / raw)
To: netdev; +Cc: davem, vivien.didelot, Woojung.Huh, UNGLinuxDriver,
Florian Fainelli
This implements David's suggestion of providing low-level functions
to control whether skb_pad() and skb_put_padto() should be freeing
the passed skb.
We make use of it to fix a double free in net/dsa/tag_ksz.c that would
occur if we kept using skb_put_padto() in both places.
Florian Fainelli (2):
net: core: Specify skb_pad()/skb_put_padto() SKB freeing
net: dsa: skb_put_padto() already frees nskb
include/linux/skbuff.h | 41 +++++++++++++++++++++++++++++++++++++----
net/core/skbuff.c | 13 ++++++++-----
net/dsa/tag_ksz.c | 10 ++++++----
3 files changed, 51 insertions(+), 13 deletions(-)
--
2.9.3
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing 2017-08-22 22:12 [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c Florian Fainelli @ 2017-08-22 22:12 ` Florian Fainelli 2017-08-23 21:42 ` Woojung.Huh 2017-08-22 22:12 ` [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb Florian Fainelli 2017-08-24 3:37 ` [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c David Miller 2 siblings, 1 reply; 8+ messages in thread From: Florian Fainelli @ 2017-08-22 22:12 UTC (permalink / raw) To: netdev; +Cc: davem, vivien.didelot, Woojung.Huh, UNGLinuxDriver, Florian Fainelli Rename skb_pad() into __skb_pad() and make it take a third argument: free_on_error which controls whether kfree_skb() should be called or not, skb_pad() directly makes use of it and passes true to preserve its existing behavior. Do exactly the same thing with __skb_put_padto() and skb_put_padto(). Suggested-by: David Miller <davem@davemloft.net> Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> --- include/linux/skbuff.h | 41 +++++++++++++++++++++++++++++++++++++---- net/core/skbuff.c | 13 ++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index dbe29b6c9bd6..d67a8182e5eb 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -973,7 +973,23 @@ int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len); int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer); -int skb_pad(struct sk_buff *skb, int pad); +int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error); + +/** + * skb_pad - zero pad the tail of an skb + * @skb: buffer to pad + * @pad: space to pad + * + * Ensure that a buffer is followed by a padding area that is zero + * filled. Used by network drivers which may DMA or transfer data + * beyond the buffer end onto the wire. + * + * May return error in out of memory cases. The skb is freed on error. + */ +static inline int skb_pad(struct sk_buff *skb, int pad) +{ + return __skb_pad(skb, pad, true); +} #define dev_kfree_skb(a) consume_skb(a) int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, @@ -2825,25 +2841,42 @@ static inline int skb_padto(struct sk_buff *skb, unsigned int len) * skb_put_padto - increase size and pad an skbuff up to a minimal size * @skb: buffer to pad * @len: minimal length + * @free_on_error: free buffer on error * * Pads up a buffer to ensure the trailing bytes exist and are * blanked. If the buffer already contains sufficient data it * is untouched. Otherwise it is extended. Returns zero on - * success. The skb is freed on error. + * success. The skb is freed on error if @free_on_error is true. */ -static inline int skb_put_padto(struct sk_buff *skb, unsigned int len) +static inline int __skb_put_padto(struct sk_buff *skb, unsigned int len, + bool free_on_error) { unsigned int size = skb->len; if (unlikely(size < len)) { len -= size; - if (skb_pad(skb, len)) + if (__skb_pad(skb, len, free_on_error)) return -ENOMEM; __skb_put(skb, len); } return 0; } +/** + * skb_put_padto - increase size and pad an skbuff up to a minimal size + * @skb: buffer to pad + * @len: minimal length + * + * Pads up a buffer to ensure the trailing bytes exist and are + * blanked. If the buffer already contains sufficient data it + * is untouched. Otherwise it is extended. Returns zero on + * success. The skb is freed on error. + */ +static inline int skb_put_padto(struct sk_buff *skb, unsigned int len) +{ + return __skb_put_padto(skb, len, true); +} + static inline int skb_add_data(struct sk_buff *skb, struct iov_iter *from, int copy) { diff --git a/net/core/skbuff.c b/net/core/skbuff.c index f990eb8b30a9..e07556606284 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -1363,18 +1363,20 @@ struct sk_buff *skb_copy_expand(const struct sk_buff *skb, EXPORT_SYMBOL(skb_copy_expand); /** - * skb_pad - zero pad the tail of an skb + * __skb_pad - zero pad the tail of an skb * @skb: buffer to pad * @pad: space to pad + * @free_on_error: free buffer on error * * Ensure that a buffer is followed by a padding area that is zero * filled. Used by network drivers which may DMA or transfer data * beyond the buffer end onto the wire. * - * May return error in out of memory cases. The skb is freed on error. + * May return error in out of memory cases. The skb is freed on error + * if @free_on_error is true. */ -int skb_pad(struct sk_buff *skb, int pad) +int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) { int err; int ntail; @@ -1403,10 +1405,11 @@ int skb_pad(struct sk_buff *skb, int pad) return 0; free_skb: - kfree_skb(skb); + if (free_on_error) + kfree_skb(skb); return err; } -EXPORT_SYMBOL(skb_pad); +EXPORT_SYMBOL(__skb_pad); /** * pskb_put - add data to the tail of a potentially fragmented buffer -- 2.9.3 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* RE: [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing 2017-08-22 22:12 ` [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing Florian Fainelli @ 2017-08-23 21:42 ` Woojung.Huh 0 siblings, 0 replies; 8+ messages in thread From: Woojung.Huh @ 2017-08-23 21:42 UTC (permalink / raw) To: f.fainelli, netdev; +Cc: davem, vivien.didelot, UNGLinuxDriver > Cc: davem@davemloft.net; vivien.didelot@savoirfairelinux.com; Woojung > Huh - C21699; UNGLinuxDriver; Florian Fainelli > Subject: [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() > SKB freeing > > Rename skb_pad() into __skb_pad() and make it take a third argument: > free_on_error which controls whether kfree_skb() should be called or > not, skb_pad() directly makes use of it and passes true to preserve its > existing behavior. Do exactly the same thing with __skb_put_padto() and > skb_put_padto(). > > Suggested-by: David Miller <davem@davemloft.net> > Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Reviewed-by: Woojung Huh <Woojung.Huh@microchip.com> - Woojung ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb 2017-08-22 22:12 [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c Florian Fainelli 2017-08-22 22:12 ` [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing Florian Fainelli @ 2017-08-22 22:12 ` Florian Fainelli 2017-08-23 21:42 ` Woojung.Huh 2017-08-24 3:37 ` [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c David Miller 2 siblings, 1 reply; 8+ messages in thread From: Florian Fainelli @ 2017-08-22 22:12 UTC (permalink / raw) To: netdev; +Cc: davem, vivien.didelot, Woojung.Huh, UNGLinuxDriver, Florian Fainelli The first call of skb_put_padto() will free up the SKB on error, but we return NULL which tells dsa_slave_xmit() that the original SKB should be freed so this would lead to a double free here. The second skb_put_padto() already frees the passed sk_buff reference upon error, so calling kfree_skb() on it again is not necessary. Detected by CoverityScan, CID#1416687 ("USE_AFTER_FREE") Fixes: e71cb9e00922 ("net: dsa: ksz: fix skb freeing") Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> --- net/dsa/tag_ksz.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c index de66ca8e6201..3bd6e2a83125 100644 --- a/net/dsa/tag_ksz.c +++ b/net/dsa/tag_ksz.c @@ -42,7 +42,8 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev) padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len; if (skb_tailroom(skb) >= padlen + KSZ_INGRESS_TAG_LEN) { - if (skb_put_padto(skb, skb->len + padlen)) + /* Let dsa_slave_xmit() free skb */ + if (__skb_put_padto(skb, skb->len + padlen, false)) return NULL; nskb = skb; @@ -60,10 +61,11 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev) skb_transport_header(skb) - skb->head); skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len)); - if (skb_put_padto(nskb, nskb->len + padlen)) { - kfree_skb(nskb); + /* Let skb_put_padto() free nskb, and let dsa_slave_xmit() free + * skb + */ + if (skb_put_padto(nskb, nskb->len + padlen)) return NULL; - } kfree_skb(skb); } -- 2.9.3 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* RE: [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb 2017-08-22 22:12 ` [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb Florian Fainelli @ 2017-08-23 21:42 ` Woojung.Huh 0 siblings, 0 replies; 8+ messages in thread From: Woojung.Huh @ 2017-08-23 21:42 UTC (permalink / raw) To: f.fainelli, netdev; +Cc: davem, vivien.didelot, UNGLinuxDriver > The first call of skb_put_padto() will free up the SKB on error, but we > return NULL which tells dsa_slave_xmit() that the original SKB should be > freed so this would lead to a double free here. > > The second skb_put_padto() already frees the passed sk_buff reference > upon error, so calling kfree_skb() on it again is not necessary. > > Detected by CoverityScan, CID#1416687 ("USE_AFTER_FREE") > > Fixes: e71cb9e00922 ("net: dsa: ksz: fix skb freeing") > Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Reviewed-by: Woojung Huh <Woojung.Huh@microchip.com> - Woojung ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c 2017-08-22 22:12 [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c Florian Fainelli 2017-08-22 22:12 ` [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing Florian Fainelli 2017-08-22 22:12 ` [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb Florian Fainelli @ 2017-08-24 3:37 ` David Miller 2017-08-24 4:40 ` [PATCH net] net: dsa: use consume_skb() Eric Dumazet 2 siblings, 1 reply; 8+ messages in thread From: David Miller @ 2017-08-24 3:37 UTC (permalink / raw) To: f.fainelli; +Cc: netdev, vivien.didelot, Woojung.Huh, UNGLinuxDriver From: Florian Fainelli <f.fainelli@gmail.com> Date: Tue, 22 Aug 2017 15:12:13 -0700 > This implements David's suggestion of providing low-level functions > to control whether skb_pad() and skb_put_padto() should be freeing > the passed skb. > > We make use of it to fix a double free in net/dsa/tag_ksz.c that would > occur if we kept using skb_put_padto() in both places. Series applied and queued up for -stable, thanks. ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net] net: dsa: use consume_skb() 2017-08-24 3:37 ` [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c David Miller @ 2017-08-24 4:40 ` Eric Dumazet 2017-08-24 5:14 ` David Miller 0 siblings, 1 reply; 8+ messages in thread From: Eric Dumazet @ 2017-08-24 4:40 UTC (permalink / raw) To: David Miller Cc: f.fainelli, netdev, vivien.didelot, Woojung.Huh, UNGLinuxDriver From: Eric Dumazet <edumazet@google.com> Two kfree_skb() should be consume_skb(), to be friend with drop monitor (perf record ... -e skb:kfree_skb) Signed-off-by: Eric Dumazet <edumazet@google.com> --- net/dsa/tag_ksz.c | 2 +- net/dsa/tag_trailer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/net/dsa/tag_ksz.c b/net/dsa/tag_ksz.c index 3bd6e2a83125..fcd90f79458e 100644 --- a/net/dsa/tag_ksz.c +++ b/net/dsa/tag_ksz.c @@ -67,7 +67,7 @@ static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev) if (skb_put_padto(nskb, nskb->len + padlen)) return NULL; - kfree_skb(skb); + consume_skb(skb); } tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN); diff --git a/net/dsa/tag_trailer.c b/net/dsa/tag_trailer.c index b09e56214005..9c7b1d74a5c6 100644 --- a/net/dsa/tag_trailer.c +++ b/net/dsa/tag_trailer.c @@ -40,7 +40,7 @@ static struct sk_buff *trailer_xmit(struct sk_buff *skb, struct net_device *dev) skb_set_network_header(nskb, skb_network_header(skb) - skb->head); skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head); skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len)); - kfree_skb(skb); + consume_skb(skb); if (padlen) { skb_put_zero(nskb, padlen); ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH net] net: dsa: use consume_skb() 2017-08-24 4:40 ` [PATCH net] net: dsa: use consume_skb() Eric Dumazet @ 2017-08-24 5:14 ` David Miller 0 siblings, 0 replies; 8+ messages in thread From: David Miller @ 2017-08-24 5:14 UTC (permalink / raw) To: eric.dumazet Cc: f.fainelli, netdev, vivien.didelot, Woojung.Huh, UNGLinuxDriver From: Eric Dumazet <eric.dumazet@gmail.com> Date: Wed, 23 Aug 2017 21:40:32 -0700 > From: Eric Dumazet <edumazet@google.com> > > Two kfree_skb() should be consume_skb(), to be friend with drop monitor > (perf record ... -e skb:kfree_skb) > > Signed-off-by: Eric Dumazet <edumazet@google.com> Applied, thanks Eric. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2017-08-24 5:14 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2017-08-22 22:12 [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c Florian Fainelli 2017-08-22 22:12 ` [PATCH net v2 1/2] net: core: Specify skb_pad()/skb_put_padto() SKB freeing Florian Fainelli 2017-08-23 21:42 ` Woojung.Huh 2017-08-22 22:12 ` [PATCH net v2 2/2] net: dsa: skb_put_padto() already frees nskb Florian Fainelli 2017-08-23 21:42 ` Woojung.Huh 2017-08-24 3:37 ` [PATCH net v2 0/2] net: dsa: Fix tag_ksz.c David Miller 2017-08-24 4:40 ` [PATCH net] net: dsa: use consume_skb() Eric Dumazet 2017-08-24 5:14 ` David Miller
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).