Skip to contentSkip to conversation

cxgb3/l2t: Fix undefined behaviour and use struct_size() helper

PATCH In review Gustavo A. R. Silva 2 participants 5 messages Mar 29, 2019 – Apr 1, 2019

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

cover cxgb3/l2t: Fix undefined behaviour and use struct_size() helper

Hi all,

This patchset aims to fix an undefined behaviour when using a zero-sized array and, add the use of the struct_size() helper in kvzalloc().

You might consider the first patch in this series for stable.

More details in the commit logs.

Thanks

Gustavo A. R. Silva (2):
cxgb3/l2t: Fix undefined behaviour
cxgb3/l2t: Use struct_size() in kvzalloc()

 drivers/net/ethernet/chelsio/cxgb3/l2t.c | 4 ++--
 drivers/net/ethernet/chelsio/cxgb3/l2t.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/2 cxgb3/l2t: Fix undefined behaviour

The use of zero-sized array causes undefined behaviour when it is not the last member in a structure. As it happens to be in this case.

Also, the current code makes use of a language extension to the C90 standard, but the preferred mechanism to declare variable-length types such as this one is a flexible array member, introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning in case the flexible array does not occur last. Which is beneficial to cultivate a high-quality code.

Fixes e48f129c2f20 ("[SCSI] cxgb3i: convert cdev->l2opt to use rcu to prevent NULL dereference")
---
 drivers/net/ethernet/chelsio/cxgb3/l2t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/chelsio/cxgb3/l2t.h +1 -1
index c2fd323c4078..ea75f275023f 100644
@@ -75,8 +75,8 @@ struct l2t_data {
 	struct l2t_entry *rover;	/* starting point for next allocation */
 	atomic_t nfree;		/* number of free entries */
 	rwlock_t lock;
-	struct l2t_entry l2tab[0];
 	struct rcu_head rcu_head;	/* to handle rcu cleanup */
+	struct l2t_entry l2tab[];
 };
 
 typedef void (*arp_failure_handler_func)(struct t3cdev * dev,
Signature · 1 line
2.21.0
#2 lore raw

PATCH 2/2 cxgb3/l2t: Use struct_size() in kvzalloc()

In reply to Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo); instance = kvzalloc(size, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kvzalloc(struct_size(instance, entry, count), GFP_KERNEL);

Notice that, in this case, variable size is not necessary, hence it is removed.

This code was detected with the help of Coccinelle.

---
 drivers/net/ethernet/chelsio/cxgb3/l2t.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/chelsio/cxgb3/l2t.c +2 -2
index 0e9182d3f02c..b3e4118a15e7 100644
@@ -443,9 +443,9 @@ void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh)
 struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
 {
 	struct l2t_data *d;
-	int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
+	int i;
 
-	d = kvzalloc(size, GFP_KERNEL);
+	d = kvzalloc(struct_size(d, l2tab, l2t_capacity), GFP_KERNEL);
 	if (!d)
 		return NULL;
 
Signature · 1 line
2.21.0
maintainer
#3 lore raw
In reply to Gustavo A. R. Silva

From: "Gustavo A. R. Silva" <[email protected]>
Date: Fri, 29 Mar 2019 10:26:16 -0500

Quoted 8 lines

Hi all,

This patchset aims to fix an undefined behaviour when using a zero-sized array and, add the use of the struct_size() helper in kvzalloc().

You might consider the first patch in this series for stable.

More details in the commit logs.

Series applied, honestly I don't think this is -stable material.

And if it was you should have targetted 'net' instead of 'net-next'.

#4 lore raw
Quoted 5 lines · David Miller

From: "Gustavo A. R. Silva" <[email protected]>
Date: Fri, 29 Mar 2019 10:26:16 -0500

Quoted 2 lines

Hi all,

Quoted 9 lines
Quoted 6 lines

This patchset aims to fix an undefined behaviour when using a zero-sized array and, add the use of the struct_size() helper in kvzalloc().

You might consider the first patch in this series for stable.

More details in the commit logs.

Series applied, honestly I don't think this is -stable material.

Okay.

Quoted 2 lines

And if it was you should have targetted 'net' instead of 'net-next'.

Got it.

Thanks, Dave.
--
Gustavo