Skip to contentSkip to conversation

staging: android: ion: hisilicon: Use pointer to memory being allocated as the sizeof argument

PATCH In review sayli karnik 3 participants 5 messages Sep 21, 2016 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: android: ion: hisilicon: Use pointer to memory being allocated as the sizeof argument

This patch finds cases where the argument to sizeof is wrong in memory allocation functions by checking the type of the allocated memory when it is a double pointer and ensuring the sizeof argument takes a pointer to the memory being allocated.
Done using coccinelle:
@@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>

---
 drivers/staging/android/ion/hisilicon/hi6220_ion.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/android/ion/hisilicon/hi6220_ion.c +1 -1
index 659aa71..9f9b244 100644
@@ -57,7 +57,7 @@ static int hi6220_ion_probe(struct platform_device *pdev)
 		return PTR_ERR(ipdev->data);
 
 	ipdev->heaps = devm_kzalloc(&pdev->dev,
-				sizeof(struct ion_heap)*ipdev->data->nr,
+				sizeof(*ipdev->heaps) * ipdev->data->nr,
 				GFP_KERNEL);
 	if (!ipdev->heaps) {
 		ion_destroy_platform_data(ipdev->data);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 4 lines · sayli karnik

This patch finds cases where the argument to sizeof is wrong in memory allocation functions by checking the type of the allocated memory when it is a double pointer and ensuring the sizeof argument takes a pointer to the memory being allocated.

Are you sure?

Quoted 22 lines

Done using coccinelle:
@@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>

---
 drivers/staging/android/ion/hisilicon/hi6220_ion.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/android/ion/hisilicon/hi6220_ion.c +0 -0
index 659aa71..9f9b244 100644

drivers/staging/android/ion/hisilicon/hi6220_ion.c static int hi6220_ion_probe(struct platform_device *pdev) · line 60

Quoted 5 lines
drivers/staging/android/ion/hisilicon/hi6220_ion.c +1 -1
@@ -57,7 +57,7 @@ static int hi6220_ion_probe(struct platform_device *pdev)
 		return PTR_ERR(ipdev->data);
 
 	ipdev->heaps = devm_kzalloc(&pdev->dev,
-				sizeof(struct ion_heap)*ipdev->data->nr,
+				sizeof(*ipdev->heaps) * ipdev->data->nr,

Do we really want to allocate an array of pointers, or an array of structures? Which is it, as it's not obvious...

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 4 lines · sayli karnik

This patch finds cases where the argument to sizeof is wrong in memory allocation functions by checking the type of the allocated memory when it is a double pointer and ensuring the sizeof argument takes a pointer to the memory being allocated.

Are you sure?

This was detected by scripts/coccinelle/misc/badty.cocci, which specifies that a false positive only arises when the sizeof argument is not used in constructing the return value. Also the driver builds without errors. What do you think?

Quoted 35 lines
Quoted 23 lines

Done using coccinelle:
@@
type T;
T **x;
@@

x =
<+...sizeof(
- T
+ *x
)...+>

---
 drivers/staging/android/ion/hisilicon/hi6220_ion.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/android/ion/hisilicon/hi6220_ion.c +0 -0
index 659aa71..9f9b244 100644

drivers/staging/android/ion/hisilicon/hi6220_ion.c static int hi6220_ion_probe(struct platform_device *pdev) · line 60

Quoted 5 lines
drivers/staging/android/ion/hisilicon/hi6220_ion.c +1 -1
@@ -57,7 +57,7 @@ static int hi6220_ion_probe(struct platform_device *pdev)
              return PTR_ERR(ipdev->data);
 
      ipdev->heaps = devm_kzalloc(&pdev->dev,
-                             sizeof(struct ion_heap)*ipdev->data->nr,
+                             sizeof(*ipdev->heaps) * ipdev->data->nr,

Do we really want to allocate an array of pointers, or an array of structures? Which is it, as it's not obvious...

thanks,

greg k-h

#3 lore raw
Quoted 8 lines · sayli karnik
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · sayli karnik

This patch finds cases where the argument to sizeof is wrong in memory allocation functions by checking the type of the allocated memory when it is a double pointer and ensuring the sizeof argument takes a pointer to the memory being allocated.

Quoted 6 lines
Quoted 2 lines

Are you sure?

This was detected by scripts/coccinelle/misc/badty.cocci, which specifies that a false positive only arises when the sizeof argument is not used in constructing the return value. Also the driver builds without errors. What do you think?

Neither of these are convincing argument. For example, if you replaced the sizeof by 27, it would also build. And Coccinelle doesn't know anything aboout the intent of the code. So you would have to check it carefully.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Done using coccinelle:
@@
type T;
T **x;
@@

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

x =
<+...sizeof(
- T
+ *x

This change looks appropriate for a single pointer case, not a double pointer case. Ie where x has type T *x. Check the code again and think about what types are involved.

julia

Quoted 32 lines
Quoted 21 lines
Quoted 12 lines

)...+>

---
 drivers/staging/android/ion/hisilicon/hi6220_ion.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/android/ion/hisilicon/hi6220_ion.c +0 -0
index 659aa71..9f9b244 100644

drivers/staging/android/ion/hisilicon/hi6220_ion.c static int hi6220_ion_probe(struct platform_device *pdev) · line 60

Quoted 5 lines
drivers/staging/android/ion/hisilicon/hi6220_ion.c +1 -1
@@ -57,7 +57,7 @@ static int hi6220_ion_probe(struct platform_device *pdev)
              return PTR_ERR(ipdev->data);
 
      ipdev->heaps = devm_kzalloc(&pdev->dev,
-                             sizeof(struct ion_heap)*ipdev->data->nr,
+                             sizeof(*ipdev->heaps) * ipdev->data->nr,

Do we really want to allocate an array of pointers, or an array of structures? Which is it, as it's not obvious...

Quoted 3 lines

thanks,

greg k-h

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/CAKG5xWgV-0cKboce8i8LbLB7kBQPMYs_NjcredvggXX6Qb%3DUGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
In reply to sayli karnik
Quoted 8 lines · sayli karnik
Quoted 6 lines · Greg Kroah-Hartman
Quoted 4 lines · sayli karnik

This patch finds cases where the argument to sizeof is wrong in memory allocation functions by checking the type of the allocated memory when it is a double pointer and ensuring the sizeof argument takes a pointer to the memory being allocated.

Quoted 6 lines
Quoted 2 lines

Are you sure?

This was detected by scripts/coccinelle/misc/badty.cocci, which specifies that a false positive only arises when the sizeof argument is not used in constructing the return value. Also the driver builds without errors. What do you think?

building without errors is not the issue here at all, it's a number that is being passed in, you could replace that statement with "42" and it would still build.

But would it work when running? That's the question here :)

Please look at the surrounding code and see if it is correct or not, somehow it is working as-is, right? Are we just allocating too much memory?

Pointer math is tricky at times, I can understand coccinelle scripts getting confused here...

thanks,

greg k-h