Skip to contentSkip to conversation

staging: lustre: libcfs: Prefer kmalloc_array over kmalloc

PATCH In review Bhaktipriya Shridhar 2 participants 3 messages Feb 26, 2016 – Mar 2, 2016 Staging

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: lustre: libcfs: Prefer kmalloc_array over kmalloc

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

---
Changes in v2:
-Fixed type lusture to lustre in subject.

 drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c +3 -2
index 91c2ae8..b78f6e5 100644
@@ -61,8 +61,9 @@ int cfs_tracefile_init_arch(void)
 	memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
 	for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
 		cfs_trace_data[i] =
-			kmalloc(sizeof(union cfs_trace_data_union) *
-				num_possible_cpus(), GFP_KERNEL);
+			kmalloc_array(num_possible_cpus(),
+				      sizeof(union cfs_trace_data_union),
+				      GFP_KERNEL);
 		if (!cfs_trace_data[i])
 			goto out;
 	}

--
2.1.4

maintainer
#1 lore raw
Quoted 56 lines · Bhaktipriya Shridhar

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

---
Changes in v2:
-Fixed type lusture to lustre in subject.

 drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c +0 -0
index 91c2ae8..b78f6e5 100644

drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c int cfs_tracefile_init_arch(void) · line 65

Quoted 5 lines
drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c +0 -2
@@ -61,8 +61,9 @@ int cfs_tracefile_init_arch(void)
 	memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
 	for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
 		cfs_trace_data[i] =
-			kmalloc(sizeof(union cfs_trace_data_union) *
-				num_possible_cpus(), GFP_KERNEL);

We know this can't overflow, it's a static number at build time, so this change isn't needed.

maintainer
#2 lore raw
In reply to Bhaktipriya Shridhar
Quoted 56 lines · Bhaktipriya Shridhar

Use kmalloc_array instead of kmalloc for arrays to prevent integer overflows.
This was done using Coccinelle:

@@
expression e1, e2;
constant C;
type t;
@@
(
- kmalloc(
+ kmalloc_array(
e2
- *
+ ,
sizeof(e1), C)
|
- kmalloc(
+ kmalloc_array(
e1
- *
+ ,
sizeof(t), C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(e1)
- *
e2
+ ,
+ sizeof(e1)
, C)
|
- kmalloc(
+ kmalloc_array(
- sizeof(t)
- *
e1
+ ,
+ sizeof(t)
, C)
)

---
Changes in v2:
-Fixed type lusture to lustre in subject.

 drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c +0 -0
index 91c2ae8..b78f6e5 100644

drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c int cfs_tracefile_init_arch(void) · line 65

Quoted 5 lines
drivers/staging/lustre/lustre/libcfs/linux/linux-tracefile.c +0 -2
@@ -61,8 +61,9 @@ int cfs_tracefile_init_arch(void)
 	memset(cfs_trace_data, 0, sizeof(cfs_trace_data));
 	for (i = 0; i < CFS_TCD_TYPE_MAX; i++) {
 		cfs_trace_data[i] =
-			kmalloc(sizeof(union cfs_trace_data_union) *
-				num_possible_cpus(), GFP_KERNEL);

We know this can't overflow, it's a static number at build time, so this change isn't needed.