Skip to contentSkip to conversation

Bluetooth: a2mp: Use struct_size() helper

PATCH In review Gustavo A. R. Silva 3 participants 5 messages Feb 8, 2019 – Feb 18, 2019

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

PATCH 1/1 Bluetooth: a2mp: Use struct_size() helper

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 = alloc(size, GFP_KERNEL)

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

size = struct_size(instance, entry, count);
instance = alloc(size, GFP_KERNEL)

This code was detected with the help of Coccinelle.

---
 net/bluetooth/a2mp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/a2mp.c +1 -1
index 58fc6333d412..5f918ea18b5a 100644
@@ -174,7 +174,7 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
 			num_ctrl++;
 	}
 
-	len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
+	len = struct_size(rsp, cl, num_ctrl);
 	rsp = kmalloc(len, GFP_ATOMIC);
 	if (!rsp) {
 		read_unlock(&hci_dev_list_lock);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 12 lines · 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 = alloc(size, GFP_KERNEL)

Quoted 7 lines

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

size = struct_size(instance, entry, count);
instance = alloc(size, GFP_KERNEL)

This code was detected with the help of Coccinelle.

[]

net/bluetooth/a2mp.c

Quoted 1 line
net/bluetooth/a2mp.c +0 -0

[]

net/bluetooth/a2mp.c static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb, · line 180

Quoted 9 lines
net/bluetooth/a2mp.c +1 -1
@@ -174,7 +174,7 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
 			num_ctrl++;
 	}
 
-	len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
+	len = struct_size(rsp, cl, num_ctrl);
 	rsp = kmalloc(len, GFP_ATOMIC);
 	if (!rsp) {
 		read_unlock(&hci_dev_list_lock);

At least a weakness in this code is len is u16
and struct_size is size_t so there's a size
truncation possible.

#2 lore raw
Quoted 27 lines · Joe Perches
Quoted 12 lines · 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 = alloc(size, GFP_KERNEL)

Quoted 7 lines

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

size = struct_size(instance, entry, count);
instance = alloc(size, GFP_KERNEL)

This code was detected with the help of Coccinelle.

[]

net/bluetooth/a2mp.c

Quoted 1 line
net/bluetooth/a2mp.c +0 -0

[]

Quoted 4 lines
net/bluetooth/a2mp.c +0 -0
@@ -174,7 +174,7 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
 			num_ctrl++;
 	}
 
Quoted 11 lines

net/bluetooth/a2mp.c static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb, · line 180

Quoted 5 lines
net/bluetooth/a2mp.c +1 -1
-	len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
+	len = struct_size(rsp, cl, num_ctrl);
 	rsp = kmalloc(len, GFP_ATOMIC);
 	if (!rsp) {
 		read_unlock(&hci_dev_list_lock);

At least a weakness in this code is len is u16
and struct_size is size_t so there's a size
truncation possible.

That's true. I didn't change the type to size_t because of the call to le16_to_cpu():

u16 len = le16_to_cpu(hdr->len);

I've been changing the type of the variable in other cases.

--
Gustavo

maintainer
#3 lore raw
In reply to Gustavo A. R. Silva

Hi Gustavo,

Quoted 18 lines

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 = alloc(size, GFP_KERNEL)

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

size = struct_size(instance, entry, count);
instance = alloc(size, GFP_KERNEL)

Quoted 6 lines

This code was detected with the help of Coccinelle.

---
net/bluetooth/a2mp.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

#4 lore raw
Quoted 20 lines · Marcel Holtmann

Hi Gustavo,

Quoted 18 lines

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 = alloc(size, GFP_KERNEL)

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

size = struct_size(instance, entry, count);
instance = alloc(size, GFP_KERNEL)

Quoted 9 lines
Quoted 6 lines

This code was detected with the help of Coccinelle.

---
net/bluetooth/a2mp.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

patch has been applied to bluetooth-next tree.

Thanks Marcel.

--
Gustavo