Skip to contentSkip to conversation

arm: sys_oabi-compat: Use kmalloc_array() in two functions

PATCH In review SF Markus Elfring 3 participants 6 messages Aug 25, 2016 – Aug 26, 2016

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

PATCH 1/1 arm: sys_oabi-compat: Use kmalloc_array() in two functions

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 17:45:23 +0200

Multiplications for the size determination of memory allocations indicated that array data structures should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/arm/kernel/sys_oabi-compat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/kernel/sys_oabi-compat.c +2 -2
index 5f221ac..e624db9 100644
@@ -285,7 +285,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
 		return -EINVAL;
 	if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
 		return -EFAULT;
-	kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
+	kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
 	fs = get_fs();
@@ -323,7 +323,7 @@ asmlinkage long sys_oabi_semtimedop(int semid,
 		return -EINVAL;
 	if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops))
 		return -EFAULT;
-	sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
+	sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
 	if (!sops)
 		return -ENOMEM;
 	err = 0;
Signature · 1 line
2.9.3
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 17:45:23 +0200

Multiplications for the size determination of memory allocations indicated that array data structures should be processed. Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

The use of kmalloc_array() introduces a duplicate of the size capping check that already exists in the code. However it seems that gcc is smart enough to figure that out and doesn't emit it twice.

Note that I'm not implying that the existing check should be removed if this patch is applied though. Having it there makes the code clearer. But if this patch makes a Coccinelle script happier then ...

Quoted 30 lines
---
 arch/arm/kernel/sys_oabi-compat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/kernel/sys_oabi-compat.c +2 -2
index 5f221ac..e624db9 100644
@@ -285,7 +285,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
 		return -EINVAL;
 	if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
 		return -EFAULT;
-	kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
+	kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
 	if (!kbuf)
 		return -ENOMEM;
 	fs = get_fs();
@@ -323,7 +323,7 @@ asmlinkage long sys_oabi_semtimedop(int semid,
 		return -EINVAL;
 	if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops))
 		return -EFAULT;
-	sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
+	sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
 	if (!sops)
 		return -ENOMEM;
 	err = 0;
Signature · 1 line
2.9.3
#2 lore raw
In reply to SF Markus Elfring
Quoted 5 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 25 Aug 2016 17:45:23 +0200

Multiplications for the size determination of memory allocations indicated that array data structures should be processed.

I'm afraid the above comment doesn't mean much to me, can you rephrase? Maybe:

"Multiplications for kmalloc size arguments are liable to overflow, potentially causing a potential security issue. Using kmalloc_array() allows the overflow to be caught and the allocation failed. Switch these callsites to kmalloc_array()."

Quoted 14 lines

Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

---
 arch/arm/kernel/sys_oabi-compat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/kernel/sys_oabi-compat.c +0 -0
index 5f221ac..e624db9 100644

arch/arm/kernel/sys_oabi-compat.c asmlinkage long sys_oabi_epoll_wait(int epfd, · line 288

Quoted 5 lines
arch/arm/kernel/sys_oabi-compat.c +1 -1
@@ -285,7 +285,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
 		return -EINVAL;
 	if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
 		return -EFAULT;
-	kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
+	kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);

kmalloc_array() here actually buys us no additional safety at either of the callsites in your patch - we need to have carefully checked the values to ensure they don't overflow prior to the kmalloc for other reasons. That's probably something that should be noted in the commit message too, so reviewers have the confidence that you're not blindly changing everything...

Thanks.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#3 lore raw
Quoted 4 lines
Quoted 2 lines

Multiplications for the size determination of memory allocations indicated that array data structures should be processed.

I'm afraid the above comment doesn't mean much to me, can you rephrase?

Yes, of course.

How verbose should the explanation for this update suggestion become?

Quoted 6 lines

Maybe:

"Multiplications for kmalloc size arguments are liable to overflow, potentially causing a potential security issue. Using kmalloc_array() allows the overflow to be caught and the allocation failed. Switch these callsites to kmalloc_array()."

Thanks for your wording variant.

Quoted 9 lines

arch/arm/kernel/sys_oabi-compat.c asmlinkage long sys_oabi_epoll_wait(int epfd, · line 288

Quoted 6 lines
arch/arm/kernel/sys_oabi-compat.c +1 -1
@@ -285,7 +285,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
 		return -EINVAL;
 	if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
 		return -EFAULT;
-	kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
+	kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);

kmalloc_array() here actually buys us no additional safety at either of the callsites in your patch

Can this inline function apply a few sanity checks in a consistent way?
http://lxr.free-electrons.com/source/include/linux/slab.h#L564

Quoted 2 lines

- we need to have carefully checked the values to ensure they don't overflow prior to the kmalloc for other reasons.

Are there any more constraints to consider?

Quoted 2 lines

That's probably something that should be noted in the commit message too, so reviewers have the confidence that you're not blindly changing everything...

I imagine that a few contributors can get mixed feelings from a bunch of my recent patches. There is a significant patch number in the works for various Linux software modules.

Regards,
Markus

#4 lore raw
Quoted 8 lines · SF Markus Elfring
Quoted 4 lines
Quoted 2 lines

Multiplications for the size determination of memory allocations indicated that array data structures should be processed.

I'm afraid the above comment doesn't mean much to me, can you rephrase?

Yes, of course.

How verbose should the explanation for this update suggestion become?

It should at least make basic English sense. I'm afraid the above doesn't convey any meaning what so ever, at least to me. It doesn't matter how many times I read it, I still end up wondering "wtf is this trying to tell me?"

A commit message which doesn't convey any meaning is totally useless. The commit message is there to describe the change not just for the present, but for years into the future. It _has_ to be meaningful and it _has_ to make sense.

Quoted 10 lines
Quoted 9 lines

arch/arm/kernel/sys_oabi-compat.c asmlinkage long sys_oabi_epoll_wait(int epfd, · line 288

Quoted 6 lines
arch/arm/kernel/sys_oabi-compat.c +1 -1
@@ -285,7 +285,7 @@ asmlinkage long sys_oabi_epoll_wait(int epfd,
 		return -EINVAL;
 	if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
 		return -EFAULT;
-	kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
+	kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);

kmalloc_array() here actually buys us no additional safety at either of the callsites in your patch

Quoted 2 lines

Can this inline function apply a few sanity checks in a consistent way?
http://lxr.free-electrons.com/source/include/linux/slab.h#L564

What do you mean "a consistent way" ? This function is already checking in this way:

asmlinkage long sys_oabi_epoll_wait(int epfd,
struct oabi_epoll_event __user *events, int maxevents, int timeout)

if (maxevents <= 0 ||
maxevents > (INT_MAX/sizeof(*kbuf)) || maxevents > (INT_MAX/sizeof(*events)))
return -EINVAL;

Do you mean doing something like:

static inline bool array_size_valid(int num, int size)

{
	return num >= 0 && size && num > SIZE_MAX / size;
}

with the above becoming:

	if (!max_events ||
	    !array_size_valid(max_events, sizeof(*kbuf)) ||
	    !array_size_valid(max_events, sizeof(*events)))
		return -EINVAL;

then maybe - but notice that this is using "int" arguments (which can be negative) as opposed to kmalloc_array() which takes size_t arguments. So we can't have one helper that fits both kmalloc_array() and this case - you're talking about creating a specific helper for these cases.

Quoted 4 lines
Quoted 2 lines

- we need to have carefully checked the values to ensure they don't overflow prior to the kmalloc for other reasons.

Are there any more constraints to consider?

For these functions, I don't think so.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#5 lore raw
Quoted 4 lines

It should at least make basic English sense. I'm afraid the above doesn't convey any meaning what so ever, at least to me. It doesn't matter how many times I read it, I still end up wondering "wtf is this trying to tell me?"

I have started to look a bit more on the usage of the following function pairs in some Linux source files.

kmalloc() ⇔ kmalloc_array()
kzalloc() ⇔ kcalloc()

Can such combinations be extended anyhow?

Some update candidates can be found for this use case in several software areas.

Regards,
Markus