Skip to content

Commit 5294b83

Browse files
zx2c4davem330
authored andcommitted
macsec: dynamically allocate space for sglist
We call skb_cow_data, which is good anyway to ensure we can actually modify the skb as such (another error from prior). Now that we have the number of fragments required, we can safely allocate exactly that amount of memory. Fixes: c09440f ("macsec: introduce IEEE 802.1AE driver") Signed-off-by: Jason A. Donenfeld <[email protected]> Acked-by: Sabrina Dubroca <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b43bd72 commit 5294b83

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

drivers/net/macsec.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ static void macsec_encrypt_done(struct crypto_async_request *base, int err)
617617

618618
static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
619619
unsigned char **iv,
620-
struct scatterlist **sg)
620+
struct scatterlist **sg,
621+
int num_frags)
621622
{
622623
size_t size, iv_offset, sg_offset;
623624
struct aead_request *req;
@@ -629,7 +630,7 @@ static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
629630

630631
size = ALIGN(size, __alignof__(struct scatterlist));
631632
sg_offset = size;
632-
size += sizeof(struct scatterlist) * (MAX_SKB_FRAGS + 1);
633+
size += sizeof(struct scatterlist) * num_frags;
633634

634635
tmp = kmalloc(size, GFP_ATOMIC);
635636
if (!tmp)
@@ -649,6 +650,7 @@ static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
649650
{
650651
int ret;
651652
struct scatterlist *sg;
653+
struct sk_buff *trailer;
652654
unsigned char *iv;
653655
struct ethhdr *eth;
654656
struct macsec_eth_header *hh;
@@ -723,7 +725,14 @@ static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
723725
return ERR_PTR(-EINVAL);
724726
}
725727

726-
req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg);
728+
ret = skb_cow_data(skb, 0, &trailer);
729+
if (unlikely(ret < 0)) {
730+
macsec_txsa_put(tx_sa);
731+
kfree_skb(skb);
732+
return ERR_PTR(ret);
733+
}
734+
735+
req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
727736
if (!req) {
728737
macsec_txsa_put(tx_sa);
729738
kfree_skb(skb);
@@ -732,7 +741,7 @@ static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
732741

733742
macsec_fill_iv(iv, secy->sci, pn);
734743

735-
sg_init_table(sg, MAX_SKB_FRAGS + 1);
744+
sg_init_table(sg, ret);
736745
skb_to_sgvec(skb, sg, 0, skb->len);
737746

738747
if (tx_sc->encrypt) {
@@ -917,6 +926,7 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
917926
{
918927
int ret;
919928
struct scatterlist *sg;
929+
struct sk_buff *trailer;
920930
unsigned char *iv;
921931
struct aead_request *req;
922932
struct macsec_eth_header *hdr;
@@ -927,7 +937,12 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
927937
if (!skb)
928938
return ERR_PTR(-ENOMEM);
929939

930-
req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg);
940+
ret = skb_cow_data(skb, 0, &trailer);
941+
if (unlikely(ret < 0)) {
942+
kfree_skb(skb);
943+
return ERR_PTR(ret);
944+
}
945+
req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
931946
if (!req) {
932947
kfree_skb(skb);
933948
return ERR_PTR(-ENOMEM);
@@ -936,7 +951,7 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
936951
hdr = (struct macsec_eth_header *)skb->data;
937952
macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
938953

939-
sg_init_table(sg, MAX_SKB_FRAGS + 1);
954+
sg_init_table(sg, ret);
940955
skb_to_sgvec(skb, sg, 0, skb->len);
941956

942957
if (hdr->tci_an & MACSEC_TCI_E) {
@@ -2716,7 +2731,7 @@ static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
27162731
}
27172732

27182733
#define MACSEC_FEATURES \
2719-
(NETIF_F_SG | NETIF_F_HIGHDMA)
2734+
(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
27202735
static struct lock_class_key macsec_netdev_addr_lock_key;
27212736

27222737
static int macsec_dev_init(struct net_device *dev)

0 commit comments

Comments
 (0)