skb_to_sgvec hardening
Subsystems (6)
v8 cover skb_to_sgvec hardening
The recent bug with macsec and historical one with virtio have indicated that letting skb_to_sgvec trounce all over an sglist without checking the length is probably a bad idea. And it's not necessary either: an sglist already explicitly marks its last item, and the initialization functions are diligent in doing so. Thus there's a clear way of avoiding future overflows.
So, this patchset, from a high level, makes skb_to_sgvec return a potential error code, and then adjusts all callers to check for the error code. There are two situations in which skb_to_sgvec might return such an error:
1) When the passed in sglist is too small; and
2) When the passed in skbuff is too deeply nested.
So, the first patch in this series handles the issues with skb_to_sgvec directly, and the remaining ones then handle the call sites.
Changes v7->v8:
- Added __must_check annotation.
- Rebased against latest upstream ipsec changes.
Jason A. Donenfeld (5):
skbuff: return -EMSGSIZE in skb_to_sgvec to prevent overflow
ipsec: check return value of skb_to_sgvec always
rxrpc: check return value of skb_to_sgvec always
macsec: check return value of skb_to_sgvec always
virtio_net: check return value of skb_to_sgvec always
drivers/net/macsec.c | 13 ++++++++-- drivers/net/virtio_net.c | 9 +++++-- include/linux/skbuff.h | 8 +++--- net/core/skbuff.c | 65 +++++++++++++++++++++++++++++++----------------- net/ipv4/ah4.c | 8 ++++-- net/ipv4/esp4.c | 20 +++++++++------ net/ipv6/ah6.c | 8 ++++-- net/ipv6/esp6.c | 20 +++++++++------ net/rxrpc/rxkad.c | 13 +++++++--- 9 files changed, 112 insertions(+), 52 deletions(-)
Signature · 1 line
2.13.0
PATCH v8 1/5 skbuff: return -EMSGSIZE in skb_to_sgvec to prevent overflow
This is a defense-in-depth measure in response to bugs like 4d6fa57b4dab ("macsec: avoid heap overflow in skb_to_sgvec"). There's not only a potential overflow of sglist items, but also a stack overflow potential, so we fix this by limiting the amount of recursion this function is allowed to do. Not actually providing a bounded base case is a future disaster that we can easily avoid here.
As a small matter of house keeping, we take this opportunity to move the documentation comment over the actual function the documentation is for.
While this could be implemented by using an explicit stack of skbuffs, when implementing this, the function complexity increased considerably, and I don't think such complexity and bloat is actually worth it. So, instead I built this and tested it on x86, x86_64, ARM, ARM64, and MIPS, and measured the stack usage there. I also reverted the recent MIPS changes that give it a separate IRQ stack, so that I could experience some worst-case situations. I found that limiting it to 24 layers deep yielded a good stack usage with room for safety, as well as being much deeper than any driver actually ever creates.
--- include/linux/skbuff.h | 8 +++---- net/core/skbuff.c | 65 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 27 deletions(-)
include/linux/skbuff.h +4 -4
@@ -1001,10 +1001,10 @@ struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom);struct sk_buff *skb_copy_expand (const struct sk_buff *skb,int newheadroom,int newtailroom,gfp_t priority); -int skb_to_sgvec_nomark (struct sk_buff *skb,struct scatterlist *sg, -int offset,int len); -int skb_to_sgvec (struct sk_buff *skb,struct scatterlist *sg,int offset, -int len); +int __must_check skb_to_sgvec_nomark (struct sk_buff *skb,struct scatterlist *sg, +int offset,int len); +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);#define dev_kfree_skb (a) consume_skb(a)
net/core/skbuff.c +42 -23
113 lines not on this page · raw message
Signature · 1 line
2.13.0
PATCH v8 2/5 ipsec: check return value of skb_to_sgvec always
In reply to Jason A. Donenfeld--- net/ipv4/ah4.c | 8 ++++++-- net/ipv4/esp4.c | 20 +++++++++++++------- net/ipv6/ah6.c | 8 ++++++-- net/ipv6/esp6.c | 20 +++++++++++++------- 4 files changed, 38 insertions(+), 18 deletions(-)
net/ipv4/ah4.c +6 -2
@@ -220,7 +220,9 @@ static int ah_output(struct xfrm_state *x, struct sk_buff *skb) ah->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low);sg_init_table (sg, nfrags + sglists); -skb_to_sgvec_nomark (skb, sg,0 , skb->len); + err =skb_to_sgvec_nomark (skb, sg,0 , skb->len); +if (unlikely (err <0 )) + goto out_free;if (x->props.flags &XFRM_STATE_ESN ) {/* Attach seqhi sg right after packet payload */
@@ -393,7 +395,9 @@ static int ah_input(struct xfrm_state *x, struct sk_buff *skb)skb_push (skb, ihl);sg_init_table (sg, nfrags + sglists); -skb_to_sgvec_nomark (skb, sg,0 , skb->len); + err =skb_to_sgvec_nomark (skb, sg,0 , skb->len); +if (unlikely (err <0 )) + goto out_free;if (x->props.flags &XFRM_STATE_ESN ) {/* Attach seqhi sg right after packet payload */
net/ipv4/esp4.c +13 -7
@@ -374,9 +374,11 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info * esp->esph = esph;sg_init_table (sg, esp->nfrags); -skb_to_sgvec (skb, sg, - (unsigned char *)esph - skb->data, - assoclen + ivlen + esp->clen + alen); + err =skb_to_sgvec (skb, sg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + esp->clen + alen); +if (unlikely (err <0 )) + goto error;if (!esp->inplace) {int allocsize;
@@ -400,9 +402,11 @@ int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *spin_unlock_bh (&x->lock);sg_init_table (dsg,skb_shinfo (skb)->nr_frags +1 ); -skb_to_sgvec (skb, dsg, - (unsigned char *)esph - skb->data, - assoclen + ivlen + esp->clen + alen); + err =skb_to_sgvec (skb, dsg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + esp->clen + alen); +if (unlikely (err <0 )) + goto error; }if ((x->props.flags &XFRM_STATE_ESN ))
@@ -687,7 +691,9 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)esp_input_set_header (skb, seqhi);sg_init_table (sg, nfrags); -skb_to_sgvec (skb, sg,0 , skb->len); + err =skb_to_sgvec (skb, sg,0 , skb->len); +if (unlikely (err <0 )) + goto out; skb->ip_summed =CHECKSUM_NONE ;
net/ipv6/ah6.c +6 -2
@@ -423,7 +423,9 @@ static int ah6_output(struct xfrm_state *x, struct sk_buff *skb) ah->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low);sg_init_table (sg, nfrags + sglists); -skb_to_sgvec_nomark (skb, sg,0 , skb->len); + err =skb_to_sgvec_nomark (skb, sg,0 , skb->len); +if (unlikely (err <0 )) + goto out_free;if (x->props.flags &XFRM_STATE_ESN ) {/* Attach seqhi sg right after packet payload */
@@ -606,7 +608,9 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb) ip6h->hop_limit =0 ;sg_init_table (sg, nfrags + sglists); -skb_to_sgvec_nomark (skb, sg,0 , skb->len); + err =skb_to_sgvec_nomark (skb, sg,0 , skb->len); +if (unlikely (err <0 )) + goto out_free;if (x->props.flags &XFRM_STATE_ESN ) {/* Attach seqhi sg right after packet payload */
net/ipv6/esp6.c +13 -7
@@ -346,9 +346,11 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info esph =esp_output_set_esn (skb, x,ip_esp_hdr (skb), seqhi);sg_init_table (sg, esp->nfrags); -skb_to_sgvec (skb, sg, - (unsigned char *)esph - skb->data, - assoclen + ivlen + esp->clen + alen); + err =skb_to_sgvec (skb, sg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + esp->clen + alen); +if (unlikely (err <0 )) + goto error;if (!esp->inplace) {int allocsize;
@@ -372,9 +374,11 @@ int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_infospin_unlock_bh (&x->lock);sg_init_table (dsg,skb_shinfo (skb)->nr_frags +1 ); -skb_to_sgvec (skb, dsg, - (unsigned char *)esph - skb->data, - assoclen + ivlen + esp->clen + alen); + err =skb_to_sgvec (skb, dsg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + esp->clen + alen); +if (unlikely (err <0 )) + goto error; }if ((x->props.flags &XFRM_STATE_ESN ))
@@ -618,7 +622,9 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)esp_input_set_header (skb, seqhi);sg_init_table (sg, nfrags); -skb_to_sgvec (skb, sg,0 , skb->len); + ret =skb_to_sgvec (skb, sg,0 , skb->len); +if (unlikely (ret <0 )) + goto out; skb->ip_summed =CHECKSUM_NONE ;
Signature · 1 line
2.13.0
PATCH v8 3/5 rxrpc: check return value of skb_to_sgvec always
In reply to Jason A. Donenfeld--- net/rxrpc/rxkad.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
net/rxrpc/rxkad.c +10 -3
@@ -227,7 +227,9 @@ static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, len &= ~(call->conn->size_align -1 );sg_init_table (sg, nsg); -skb_to_sgvec (skb, sg,0 , len); + err =skb_to_sgvec (skb, sg,0 , len); +if (unlikely (err <0 )) + goto out;skcipher_request_set_crypt (req, sg, sg, len, iv.x);crypto_skcipher_encrypt (req);
@@ -342,7 +344,8 @@ static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb, goto nomem;sg_init_table (sg, nsg); -skb_to_sgvec (skb, sg, offset,8 ); +if (unlikely (skb_to_sgvec (skb, sg, offset,8 ) <0 )) + goto nomem;/* start the decryption afresh */ memset (&iv,0 ,sizeof (iv));
@@ -434,7 +437,11 @@ static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb, }sg_init_table (sg, nsg); -skb_to_sgvec (skb, sg, offset, len); +if (unlikely (skb_to_sgvec (skb, sg, offset, len) <0 )) { +if (sg != _sg) +kfree (sg); + goto nomem; + }/* decrypt from the session key */ token = call->conn->params.key->payload.data[0 ];
Signature · 1 line
2.13.0
PATCH v8 4/5 macsec: check return value of skb_to_sgvec always
In reply to Jason A. Donenfeld--- drivers/net/macsec.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/net/macsec.c +11 -2
@@ -742,7 +742,12 @@ static struct sk_buff *macsec_encrypt(struct sk_buff *skb,macsec_fill_iv (iv, secy->sci, pn);sg_init_table (sg, ret); -skb_to_sgvec (skb, sg,0 , skb->len); + ret =skb_to_sgvec (skb, sg,0 , skb->len); +if (unlikely (ret <0 )) { +macsec_txsa_put (tx_sa); +kfree_skb (skb); +return ERR_PTR (ret); + }if (tx_sc->encrypt) { int len = skb->len - macsec_hdr_len(sci_present) -
@@ -952,7 +957,11 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,macsec_fill_iv (iv, sci,ntohl (hdr->packet_number));sg_init_table (sg, ret); -skb_to_sgvec (skb, sg,0 , skb->len); + ret =skb_to_sgvec (skb, sg,0 , skb->len); +if (unlikely (ret <0 )) { +kfree_skb (skb); +return ERR_PTR (ret); + }if (hdr->tci_an &MACSEC_TCI_E ) {/* confidentiality: ethernet + macsec header
Signature · 1 line
2.13.0
PATCH v8 5/5 virtio_net: check return value of skb_to_sgvec always
In reply to Jason A. Donenfeld--- drivers/net/virtio_net.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +7 -2
@@ -1150,7 +1150,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)struct virtio_net_hdr_mrg_rxbuf *hdr;const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;struct virtnet_info *vi = sq->vq->vdev->priv; -unsigned num_sg; +int num_sg;unsigned hdr_len = vi->hdr_len;bool can_push;
@@ -1177,11 +1177,16 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)if (can_push) {__skb_push (skb, hdr_len); num_sg =skb_to_sgvec (skb, sq->sg,0 , skb->len); +if (unlikely (num_sg <0 )) +return num_sg;/* Pull header back to avoid skew in tx bytes calculations. */ __skb_pull (skb, hdr_len); }else {sg_set_buf (sq->sg, hdr, hdr_len); - num_sg =skb_to_sgvec (skb, sq->sg +1 ,0 , skb->len) +1 ; + num_sg =skb_to_sgvec (skb, sq->sg +1 ,0 , skb->len); +if (unlikely (num_sg <0 )) +return num_sg; + num_sg++; }return virtqueue_add_outbuf (sq->vq, sq->sg, num_sg, skb,GFP_ATOMIC ); }
Signature · 1 line
2.13.0
David Howells
maintainerRe: [PATCH v8 3/5] rxrpc: check return value of skb_to_sgvec always
In reply to Jason A. Donenfeldnet/rxrpc/rxkad.c static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb, · line 443
Quoted 7 lines · Jason A. Donenfeld
net/rxrpc/rxkad.c +6
-0+if (unlikely (skb_to_sgvec (skb, sg, offset,8 ) <0 )) + goto nomem; +if (unlikely (skb_to_sgvec (skb, sg, offset, len) <0 )) { +if (sg != _sg) +kfree (sg); + goto nomem;
skb_to_sgvec() can return -EMSGSIZE in some circumstances. You shouldn't return -ENOMEM here in such a case.
David
David Howells
maintainerRe: [PATCH v8 1/5] skbuff: return -EMSGSIZE in skb_to_sgvec to prevent overflow
In reply to Jason A. DonenfeldReview
Quoted 3 lines · Jason A. Donenfeld
(quoted diff) +1
-1+int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) -int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
Is there a reason you moved skb_to_sgvec() in the file rather than just moving the comment to it (since you moved the comment anyway)?
David
Re: [PATCH v8 1/5] skbuff: return -EMSGSIZE in skb_to_sgvec to prevent overflow
Quoted 2 lines · David Howells
Is there a reason you moved
skb_to_sgvec()in the file rather than just moving the comment to it (since you moved the comment anyway)?
1) Because it's easier to understand skb_to_sgvec_nomark as a variant of skb_to_sgvec, so I'd rather skb_to_sgvec to be first when reading.
2) Because skb_to_sgvec relies on the return value of __skb_to_sgvec, and so when assessing it, it's sometimes nice to be able to look at why it will return different things. In that case, it's easier to have both functions within the same view without scrolling.
It's the little things that make life easier sometimes.
Re: [PATCH v8 3/5] rxrpc: check return value of skb_to_sgvec always
In reply to David HowellsQuoted 2 lines · David Howells
skb_to_sgvec()can return-EMSGSIZEin some circumstances. You shouldn't return-ENOMEMhere in such a case.
Noted. I'll fix this up for the next round.