Skip to contentSkip to conversation

net: atlantic: Use kmalloc_array to prevent overflow of dynamic size calculation

PATCH In review Bhanu Seshu Kumar Valluri 3 participants 5 messages Sep 30, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: atlantic: Use kmalloc_array to prevent overflow of dynamic size calculation

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note: Patch is tested for compilation.

 drivers/net/ethernet/aquantia/atlantic/aq_ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +1 -1
index 5acb3e16b567..f445d449f80f 100644
@@ -182,7 +182,7 @@ static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring)
 
 static int aq_ptp_skb_ring_init(struct ptp_skb_ring *ring, unsigned int size)
 {
-	struct sk_buff **buff = kmalloc(sizeof(*buff) * size, GFP_KERNEL);
+	struct sk_buff **buff = kmalloc_array(sizeof(*buff), size, GFP_KERNEL);
 
 	if (!buff)
 		return -ENOMEM;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · Bhanu Seshu Kumar Valluri

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note: Patch is tested for compilation.

  drivers/net/ethernet/aquantia/atlantic/aq_ptp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +0 -0
index 5acb3e16b567..f445d449f80f 100644

drivers/net/ethernet/aquantia/atlantic/aq_ptp.c static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring) · line 186

Quoted 6 lines
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +1 -1
@@ -182,7 +182,7 @@ static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring)
 
  static int aq_ptp_skb_ring_init(struct ptp_skb_ring *ring, unsigned int size)
  {
-	struct sk_buff **buff = kmalloc(sizeof(*buff) * size, GFP_KERNEL);
+	struct sk_buff **buff = kmalloc_array(sizeof(*buff), size, GFP_KERNEL);
 

Shouldn't this be kmalloc_array(size, sizeof(*buff), GFP_KERNEL); ?

Quoted 2 lines
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +0 -0
  	if (!buff)
  		return -ENOMEM;
#2 lore raw
Quoted 10 lines · Sahil Chandna
Quoted 9 lines · Bhanu Seshu Kumar Valluri

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
  Note: Patch is tested for compilation. drivers/net/ethernet/aquantia/atlantic/aq_ptp.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines
Quoted 10 lines
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +1 -1
index 5acb3e16b567..f445d449f80f 100644
@@ -182,7 +182,7 @@ static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring)
     static int aq_ptp_skb_ring_init(struct ptp_skb_ring *ring, unsigned int size)
   {
-    struct sk_buff **buff = kmalloc(sizeof(*buff) * size, GFP_KERNEL);
+    struct sk_buff **buff = kmalloc_array(sizeof(*buff), size, GFP_KERNEL);

Shouldn't this be kmalloc_array(size, sizeof(*buff), GFP_KERNEL); ?

I didn't change the order of arguments to make it easier to review.

Quoted 4 lines
Quoted 3 lines

      if (!buff)
          return -ENOMEM;

#3 lore raw
Quoted 24 lines · bhanuseshukumar
Quoted 10 lines · Sahil Chandna
Quoted 9 lines · Bhanu Seshu Kumar Valluri

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note: Patch is tested for compilation.

  drivers/net/ethernet/aquantia/atlantic/aq_ptp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

drivers/net/ethernet/aquantia/atlantic/aq_ptp.c static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring) · line 185

Quoted 10 lines
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +1 -1
index 5acb3e16b567..f445d449f80f 100644
@@ -182,7 +182,7 @@ static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring)
    static int aq_ptp_skb_ring_init(struct ptp_skb_ring *ring, unsigned int size)
  {
-    struct sk_buff **buff = kmalloc(sizeof(*buff) * size, GFP_KERNEL);
+    struct sk_buff **buff = kmalloc_array(sizeof(*buff), size, GFP_KERNEL);
 

Shouldn't this be kmalloc_array(size, sizeof(*buff), GFP_KERNEL); ?

Quoted 1 line

I didn't change the order of arguments to make it easier to review.

I think this NIC limits the number of skbs per queue to less than 8192.

Also sizeof(struct sk_buff *) is quite small (a pointer is 8 or 4 bytes)

So your patch would probably target net-next as a cleanup, in about two weeks when the merge window is over.

More details in
https://www.kernel.org/doc/Documentation/process/maintainer-netdev.rst

Thanks.

#4 lore raw
Quoted 32 lines · Eric Dumazet
Quoted 24 lines · bhanuseshukumar
Quoted 10 lines · Sahil Chandna
Quoted 9 lines · Bhanu Seshu Kumar Valluri

Use kmalloc_array to avoid potential overflow during dynamic size calculation inside kmalloc.

---
Note: Patch is tested for compilation.

  drivers/net/ethernet/aquantia/atlantic/aq_ptp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

drivers/net/ethernet/aquantia/atlantic/aq_ptp.c static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring) · line 185

Quoted 10 lines
drivers/net/ethernet/aquantia/atlantic/aq_ptp.c +1 -1
index 5acb3e16b567..f445d449f80f 100644
@@ -182,7 +182,7 @@ static unsigned int aq_ptp_skb_buf_len(struct ptp_skb_ring *ring)
    static int aq_ptp_skb_ring_init(struct ptp_skb_ring *ring, unsigned int size)
  {
-    struct sk_buff **buff = kmalloc(sizeof(*buff) * size, GFP_KERNEL);
+    struct sk_buff **buff = kmalloc_array(sizeof(*buff), size, GFP_KERNEL);
 

Shouldn't this be kmalloc_array(size, sizeof(*buff), GFP_KERNEL); ?

Quoted 1 line

I didn't change the order of arguments to make it easier to review.

I think this NIC limits the number of skbs per queue to less than 8192.

Also sizeof(struct sk_buff *) is quite small (a pointer is 8 or 4 bytes)

Quoted 7 lines

So your patch would probably target net-next as a cleanup, in about two weeks when the merge window is over.

More details in
https://www.kernel.org/doc/Documentation/process/maintainer-netdev.rst

Thanks.

Thank you for sharing helpful documentation. I will send a cleanup patch designated with net-next once net-next reopens.

Thanks.