macsec: a few fixes
Subsystems (3)
Sabrina Dubroca
maintainercover macsec: a few fixes
Some small fixes for the macsec driver:
- possible NULL pointer dereferences
- netlink dumps fixes: RTNL locking, consistent dumps
- a reference counting bug
Sabrina Dubroca (5):
macsec: add missing NULL check after kmalloc
macsec: take rtnl lock before for_each_netdev
macsec: don't put a NULL rxsa
macsec: fix rx_sa refcounting with decrypt callback
macsec: add consistency check to netlink dumps
drivers/net/macsec.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-)
Signature · 1 line
2.8.0
Sabrina Dubroca
maintainerPATCH 1/5 macsec: add missing NULL check after kmalloc
--- drivers/net/macsec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/macsec.c +2 -2
@@ -1622,8 +1622,8 @@ static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info) } rx_sa =kmalloc (sizeof (*rx_sa),GFP_KERNEL ); -if (init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), secy->key_len, - secy->icv_len)) { +if (!rx_sa ||init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), + secy->key_len, secy->icv_len)) {rtnl_unlock ();return -ENOMEM ; }
Signature · 1 line
2.8.0
Sabrina Dubroca
maintainerPATCH 2/5 macsec: take rtnl lock before for_each_netdev
In reply to Sabrina Dubroca--- drivers/net/macsec.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/macsec.c +2 -5
@@ -2268,8 +2268,6 @@ static int dump_secy(struct macsec_secy *secy, struct net_device *dev,if (!hdr)return -EMSGSIZE ; -rtnl_lock (); -if (nla_put_u32 (skb,MACSEC_ATTR_IFINDEX , dev->ifindex)) goto nla_put_failure;
@@ -2429,14 +2427,11 @@ static int dump_secy(struct macsec_secy *secy, struct net_device *dev,nla_nest_end (skb, rxsc_list); -rtnl_unlock (); -genlmsg_end (skb, hdr);return 0 ; nla_put_failure: -rtnl_unlock ();genlmsg_cancel (skb, hdr);return -EMSGSIZE ; }
@@ -2450,6 +2445,7 @@ static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb) dev_idx = cb->args[0 ]; d =0 ; +rtnl_lock ();for_each_netdev (net, dev) {struct macsec_secy *secy;
@@ -2467,6 +2463,7 @@ next: } done: +rtnl_unlock (); cb->args[0 ] = d;return skb->len; }
Signature · 1 line
2.8.0
Sabrina Dubroca
maintainerPATCH 3/5 macsec: don't put a NULL rxsa
In reply to Sabrina DubrocaThe "deliver:" path of macsec_handle_frame can be called with rx_sa == NULL. Check rx_sa != NULL before calling macsec_rxsa_put().
--- drivers/net/macsec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/macsec.c +2 -1
@@ -1161,7 +1161,8 @@ deliver: macsec_extra_len(macsec_skb_cb(skb)->has_sci));macsec_reset_skb (skb, secy->netdev); -macsec_rxsa_put (rx_sa); +if (rx_sa) +macsec_rxsa_put (rx_sa);count_rx (dev, skb->len);rcu_read_unlock ();
Signature · 1 line
2.8.0
Sabrina Dubroca
maintainerPATCH 4/5 macsec: fix rx_sa refcounting with decrypt callback
In reply to Sabrina DubrocaThe decrypt callback macsec_decrypt_done needs a reference on the rx_sa and releases it before returning, but macsec_handle_frame already put that reference after macsec_decrypt returned NULL.
Change macsec_decrypt to return error codes, and use -EINPROGRESS as an indication that macsec_handle_frame must not release the reference.
--- drivers/net/macsec.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/net/macsec.c +9 -7
@@ -880,12 +880,12 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,macsec_skb_cb (skb)->valid = false; skb =skb_share_check (skb,GFP_ATOMIC );if (!skb) -return NULL ; +return ERR_PTR (-ENOMEM ); req =aead_request_alloc (rx_sa->key.tfm,GFP_ATOMIC );if (!req) {kfree_skb (skb); -return NULL ; +return ERR_PTR (-ENOMEM ); } hdr = (struct macsec_eth_header *)skb->data;
@@ -905,7 +905,7 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb, skb =skb_unshare (skb,GFP_ATOMIC );if (!skb) {aead_request_free (req); -return NULL ; +return ERR_PTR (-ENOMEM ); } }else {/* integrity only: all headers + data authenticated */
@@ -921,14 +921,14 @@ static struct sk_buff *macsec_decrypt(struct sk_buff *skb,dev_hold (dev); ret =crypto_aead_decrypt (req);if (ret == -EINPROGRESS ) { -return NULL ; +return ERR_PTR (ret); }else if (ret !=0 ) {/* decryption/authentication failed * 10.6 if validateFrames is disabled, deliver anyway */ if (ret != -EBADMSG ) {kfree_skb (skb); - skb =NULL ; + skb =ERR_PTR (ret); } }else {macsec_skb_cb (skb)->valid = true;
@@ -1146,8 +1146,10 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb) secy->validate_frames != MACSEC_VALIDATE_DISABLED) skb =macsec_decrypt (skb, dev, rx_sa, sci, secy); -if (!skb) { -macsec_rxsa_put (rx_sa); +if (IS_ERR (skb)) { +/* the decrypt callback needs the reference */ +if (PTR_ERR (skb) != -EINPROGRESS ) +macsec_rxsa_put (rx_sa);rcu_read_unlock (); *pskb =NULL ;return RX_HANDLER_CONSUMED ;
Signature · 1 line
2.8.0
Sabrina Dubroca
maintainerPATCH 5/5 macsec: add consistency check to netlink dumps
In reply to Sabrina DubrocaUse genl_dump_check_consistent in dump_secy.
--- drivers/net/macsec.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/net/macsec.c +11 -0
@@ -2271,6 +2271,8 @@ static int dump_secy(struct macsec_secy *secy, struct net_device *dev,if (!hdr)return -EMSGSIZE ; +genl_dump_check_consistent (cb, hdr, &macsec_fam); +if (nla_put_u32 (skb,MACSEC_ATTR_IFINDEX , dev->ifindex)) goto nla_put_failure;
@@ -2439,6 +2441,8 @@ nla_put_failure:return -EMSGSIZE ; } +static int macsec_generation =1 ;/* protected by RTNL */ +static int macsec_dump_txsc (struct sk_buff *skb,struct netlink_callback *cb) {struct net *net =sock_net (skb->sk);
@@ -2449,6 +2453,9 @@ static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb) d =0 ;rtnl_lock (); + + cb->seq = macsec_generation; +for_each_netdev (net, dev) {struct macsec_secy *secy;
@@ -2920,6 +2927,8 @@ static void macsec_dellink(struct net_device *dev, struct list_head *head)struct net_device *real_dev = macsec->real_dev;struct macsec_rxh_data *rxd =macsec_data_rtnl (real_dev); + macsec_generation++; +unregister_netdevice_queue (dev, head);list_del_rcu (&macsec->secys);if (list_empty (&rxd->secys))
@@ -3066,6 +3075,8 @@ static int macsec_newlink(struct net *net, struct net_device *dev,if (err <0 ) goto del_dev; + macsec_generation++; +dev_hold (real_dev);return 0 ;
Signature · 1 line
2.8.0
Re: [PATCH net 1/5] macsec: add missing NULL check after kmalloc
In reply to Sabrina Dubroca----- Original Message -----
Quoted 20 lines
From: "Sabrina Dubroca" <[email protected]>
To: [email protected]
Cc: "Hannes Frederic Sowa" <[email protected]>, "Johannes Berg" <[email protected]>, "Dan Carpenter" <[email protected]>, "Sabrina Dubroca" <[email protected]>
Sent: Tuesday, April 19, 2016 1:36:38 PM
Subject: [PATCH net 1/5] macsec: add missing NULL check after kmallocFixes c09440f7dcb3 ("macsec: introduce IEEE 802.1AE driver")Reported-by Dan Carpenter <[email protected]>Signed-off-by Sabrina Dubroca <[email protected]>--- drivers/net/macsec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/macsec.c +0
-0
drivers/net/macsec.c static int macsec_add_rxsa(struct sk_buff *skb, struct · line 1627
Quoted 8 lines
drivers/net/macsec.c +2
-2@@ -1622,8 +1622,8 @@ static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info) } rx_sa =kmalloc (sizeof (*rx_sa),GFP_KERNEL ); -if (init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), secy->key_len, - secy->icv_len)) { +if (!rx_sa ||init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), + secy->key_len, secy->icv_len)) {
Doesn't this leak rx_sa if kmalloc() succeeds but init_rx_sa fails?
Quoted 7 lines
drivers/net/macsec.c +0
-0rtnl_unlock ();return -ENOMEM ;}
--
2.8.0
Sabrina Dubroca
maintainerRe: [PATCH net 1/5] macsec: add missing NULL check after kmalloc
Quoted 24 lines · Lance Richardson
----- Original Message -----
Quoted 23 lines
From: "Sabrina Dubroca" <[email protected]>
To: [email protected]
Cc: "Hannes Frederic Sowa" <[email protected]>, "Johannes Berg" <[email protected]>, "Dan Carpenter" <[email protected]>, "Sabrina Dubroca" <[email protected]>
Sent: Tuesday, April 19, 2016 1:36:38 PM
Subject: [PATCH net 1/5] macsec: add missing NULL check after kmallocFixes c09440f7dcb3 ("macsec: introduce IEEE 802.1AE driver")Reported-by Dan Carpenter <[email protected]>Signed-off-by Sabrina Dubroca <[email protected]>--- drivers/net/macsec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/macsec.c +0
-0@@ -1622,8 +1622,8 @@ static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info) }
Quoted 7 lines
drivers/net/macsec.c static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info) · line 1626
Quoted 5 lines
drivers/net/macsec.c +2
-2rx_sa =kmalloc (sizeof (*rx_sa),GFP_KERNEL ); -if (init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), secy->key_len, - secy->icv_len)) { +if (!rx_sa ||init_rx_sa (rx_sa,nla_data (tb_sa[MACSEC_SA_ATTR_KEY ]), + secy->key_len, secy->icv_len)) {Doesn't this leak
rx_saifkmalloc()succeeds butinit_rx_safails?
Yeah, you're right. And there's the same code around init_tx_sa. I'll send v2 tomorrow with this and another fix.
Thanks!
Quoted 7 lines
Quoted 7 lines
drivers/net/macsec.c +0
-0rtnl_unlock ();return -ENOMEM ;}
--
2.8.0
Signature · 1 line
Sabrina