Skip to contentSkip to conversation

x86-ksysfs: Use kmalloc_array() in create_setup_data_nodes()

PATCH In review SF Markus Elfring 4 participants 11 messages Sep 4, 2016 – Sep 7, 2016

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

PATCH 1/1 x86-ksysfs: Use kmalloc_array() in create_setup_data_nodes()

From: Markus Elfring <[email protected]>
Date: Sun, 4 Sep 2016 22:15:09 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/x86/kernel/ksysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/ksysfs.c +1 -1
index 4afc67f..cddf3c6 100644
@@ -283,7 +283,7 @@ static int __init create_setup_data_nodes(struct kobject *parent)
 	if (ret)
 		goto out_setup_data_kobj;
 
-	kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL);
+	kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
 	if (!kobjp) {
 		ret = -ENOMEM;
 		goto out_setup_data_kobj;
Signature · 1 line
2.9.3
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sun, 4 Sep 2016 22:15:09 +0200

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

Which rule-set was used?

Quoted 2 lines

While probably impossible to overflow in the real world, it's still better to use the right function here.

Quoted 20 lines
---
 arch/x86/kernel/ksysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/ksysfs.c +1 -2
index 4afc67f..cddf3c6 100644
@@ -283,7 +283,7 @@ static int __init create_setup_data_nodes(struct kobject *parent)
        if (ret)
                goto out_setup_data_kobj;
 
-       kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL);
+       kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
        if (!kobjp) {
                ret = -ENOMEM;
                goto out_setup_data_kobj;
--

2.9.3

Signature · 2 lines
Kees Cook
Nexus Security
#2 lore raw
Quoted 7 lines
Quoted 5 lines

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

Which rule-set was used?

Do you get an useful impression from one of my bug reports (or feature requests) like "Fix usage of white-space characters at two places for Linux coding style" which evolved together with the proposed software refactoring?
https://github.com/coccinelle/coccinelle/issues/76

I am curious if the time is ready now for the development of another script for the semantic patch language which can be executed by the make interface "coccicheck" after Julia Lawall improved also software components a bit more recently.
https://github.com/coccinelle/coccinelle/commits/

Are you looking for further possibilities to improve the involved source code search patterns?

Regards,
Markus

#3 lore raw
Quoted 14 lines · SF Markus Elfring
Quoted 7 lines
Quoted 5 lines

A multiplication for the size determination of a memory allocation indicated that an array data structure should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

Which rule-set was used?

Do you get an useful impression from one of my bug reports (or feature requests) like "Fix usage of white-space characters at two places for Linux coding style" which evolved together with the proposed software refactoring?
https://github.com/coccinelle/coccinelle/issues/76

Quoted 8 lines

I am curious if the time is ready now for the development of another script for the semantic patch language which can be executed by the make interface "coccicheck" after Julia Lawall improved also software components a bit more recently.
https://github.com/coccinelle/coccinelle/commits/

Are you looking for further possibilities to improve the involved source code search patterns?

Why are you not answering the simple question that was asked?

Paolo

#4 lore raw
Quoted 4 lines
Quoted 2 lines

Are you looking for further possibilities to improve the involved source code search patterns?

Why are you not answering the simple question that was asked?

I find that I answered it to some degree.

It can be that you do not really like the kind of answer that I chose a moment ago.
But I guess that a more pleasing (and complete) answer can become another software development challenge as you might know already.

Would the following script (for the semantic patch language) be useful enough for further development considerations?

usage_of_kmalloc_array1-excerpt2.cocci:
@replacement2@
expression count, pointer, target;
@@
target - kmalloc(sizeof(*pointer) * (count)
+ kmalloc_array(count, sizeof(*pointer)

                        , ...);

Regards,
Markus

#5 lore raw
Quoted 15 lines · SF Markus Elfring
Quoted 4 lines
Quoted 2 lines

Are you looking for further possibilities to improve the involved source code search patterns?

Why are you not answering the simple question that was asked?

I find that I answered it to some degree.

It can be that you do not really like the kind of answer that I chose a moment ago.
But I guess that a more pleasing (and complete) answer can become another software development challenge as you might know already.

Would the following script (for the semantic patch language) be useful enough for further development considerations?

Quoted 7 lines

usage_of_kmalloc_array1-excerpt2.cocci:
@replacement2@
expression count, pointer, target;
@@
target > - kmalloc(sizeof(*pointer) * (count)
+ kmalloc_array(count, sizeof(*pointer)

                        , ...);

Why don't you include the _exact_ script that you run? That's the only possible correct answer.

Paolo

#6 lore raw
Quoted 11 lines
Quoted 10 lines

Would the following script (for the semantic patch language) be useful enough for further development considerations?

usage_of_kmalloc_array1-excerpt2.cocci:
@replacement2@
expression count, pointer, target;
@@
target >> - kmalloc(sizeof(*pointer) * (count)
+ kmalloc_array(count, sizeof(*pointer)

                        , ...);
Quoted 1 line

Why don't you include the _exact_ script that you run?

I showed only the "excerpt" above because of the current situation that this single SmPL rule triggered the software change which I suggested for the referenced source file.

How do you think about to try a command out like the following also in your development (or test) environment?

elfring@Sonne:~/Projekte/Linux/next-patched> spatch.opt ~/Projekte/Coccinelle/janitor/usage_of_kmalloc_array1-excerpt2.cocci arch/x86/kernel/ksysfs.c

Regards,
Markus

#7 lore raw
Quoted 13 lines · SF Markus Elfring
Quoted 11 lines
Quoted 10 lines

Would the following script (for the semantic patch language) be useful enough for further development considerations?

usage_of_kmalloc_array1-excerpt2.cocci:
@replacement2@
expression count, pointer, target;
@@
target >>> - kmalloc(sizeof(*pointer) * (count)
+ kmalloc_array(count, sizeof(*pointer)

                        , ...);
Quoted 1 line

Why don't you include the _exact_ script that you run?

Quoted 8 lines

I showed only the "excerpt" above because of the current situation that this single SmPL rule triggered the software change which I suggested for the referenced source file.

How do you think about to try a command out like the following also in your development (or test) environment?

elfring@Sonne:~/Projekte/Linux/next-patched> spatch.opt ~/Projekte/Coccinelle/janitor/usage_of_kmalloc_array1-excerpt2.cocci arch/x86/kernel/ksysfs.c

Fixing these kmalloc calls would be a nice thing to clean up everywhere. Since it is a mistake people may continue to make, I think it would make sense to add a coccinelle script that can do this to the existing coccinelle scripts in the kernel if one to do it does not already exist. That way, it will be part of the coccinelle checking that is automatically run on the kernel regularly.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#8 lore raw
Quoted 2 lines · Kees Cook

Fixing these kmalloc calls would be a nice thing to clean up everywhere.

Dubious as gcc cannot currently optimize known small fixed size allocations with alloc_array and will always perform the multiplication.

Also the style of sizeof(*ptr) is not always as clear, obvious nor as easy to grep as sizeof(type).

#10 lore raw
In reply to Kees Cook
Quoted 1 line

Fixing these kmalloc calls would be a nice thing to clean up everywhere.

Thanks for your acknowledgement of such a software improvement opportunity.

Quoted 5 lines

Since it is a mistake people may continue to make, I think it would make sense to add a coccinelle script that can do this to the existing coccinelle scripts in the kernel if one to do it does not already exist. That way, it will be part of the coccinelle checking that is automatically run on the kernel regularly.

I am curious on how many contributors would like to help a bit more in such a software development task.

How do you think about to clarify corresponding challenges better?

Do you find any details interesting which are described in a feature request (or article) like "Improve determination of sizes with SmPL"?
https://github.com/coccinelle/coccinelle/issues/80

Regards,
Markus

#9 lore raw
In reply to Joe Perches
Quoted 7 lines · Joe Perches
Quoted 2 lines · Kees Cook

Fixing these kmalloc calls would be a nice thing to clean up everywhere.

Dubious as gcc cannot currently optimize known small fixed size allocations with alloc_array and will always perform the multiplication.

Oh, that's sad. Is it not able to tell what the types are to basic overflow validation? Could we help gcc in some way?

Quoted 2 lines

Also the style of sizeof(*ptr) is not always as clear, obvious nor as easy to grep as sizeof(type).

I've always been on the fence about this (I like being able to see destination:thing, sizeof(*thing) for easy scanning), but having the type right there can be easier for text searches.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security