Skip to contentSkip to conversation

efi_test: fix Coccinelle warning and CoverityScan issues

PATCH Applied Ivan Hu 2 participants 10 messages Sep 26, 2016 – Oct 5, 2016 7 messages not in the archive

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Ivan Hu

author
#0 lore raw

cover efi_test: fix Coccinelle warning and CoverityScan issues

Fix the memdup_user warning found by Cocinelle, and some minor uninitialized scalar variable issues found by CoverityScan.

Ivan Hu (3):
efi/efi_test: use memdup_user() as a cleanup efi/efi_test: fix the uninitialized value datasize efi/efi_test: fix the uninitialized value rv

 drivers/firmware/efi/test/efi_test.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1474859691-8574-1-git-send-email-ivan.hu-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org

author
#2 lore raw

PATCH 1/3 efi/efi_test: use memdup_user() as a cleanup

Cleanup the warning, drivers/firmware/efi/test/efi_test.c:269:8-15: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation This is a little bit restricted to reduce false positives

Generated by: coccinelle/api/memdup_user.coc

---
 drivers/firmware/efi/test/efi_test.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/firmware/efi/test/efi_test.c +3 -7
index f61bb52..5602c46 100644
@@ -266,14 +266,10 @@ static long efi_runtime_set_variable(unsigned long arg)
 			return rv;
 	}
 
-	data = kmalloc(setvariable.data_size, GFP_KERNEL);
-	if (!data) {
+	data = memdup_user(setvariable.data, setvariable.data_size);
+	if (IS_ERR(data)) {
 		kfree(name);
-		return -ENOMEM;
-	}
-	if (copy_from_user(data, setvariable.data, setvariable.data_size)) {
-		rv = -EFAULT;
-		goto out;
+		return PTR_ERR(data);
 	}
 
 	status = efi.set_variable(name, &vendor_guid,
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1474859691-8574-2-git-send-email-ivan.hu-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org

#4 lore raw

Re: [PATCH 1/3] efi/efi_test: use memdup_user() as a cleanup

Quoted 6 lines · Ivan Hu

Cleanup the warning, drivers/firmware/efi/test/efi_test.c:269:8-15: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation This is a little bit restricted to reduce false positives

I don't understand this sentence. What restriction are you talking about?

Quoted 32 lines

Generated by: coccinelle/api/memdup_user.coc

---
 drivers/firmware/efi/test/efi_test.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/firmware/efi/test/efi_test.c +3 -7
index f61bb52..5602c46 100644
@@ -266,14 +266,10 @@ static long efi_runtime_set_variable(unsigned long arg)
 			return rv;
 	}
 
-	data = kmalloc(setvariable.data_size, GFP_KERNEL);
-	if (!data) {
+	data = memdup_user(setvariable.data, setvariable.data_size);
+	if (IS_ERR(data)) {
 		kfree(name);
-		return -ENOMEM;
-	}
-	if (copy_from_user(data, setvariable.data, setvariable.data_size)) {
-		rv = -EFAULT;
-		goto out;
+		return PTR_ERR(data);
 	}
 
 	status = efi.set_variable(name, &vendor_guid,
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 20161003201718.GL16071-mF/unelCI9GS6iBeEJttW/[email protected]

author
#6 lore raw

Re: [PATCH 1/3] efi/efi_test: use memdup_user() as a cleanup

Hi Matt,

The warning message is from Kbuild test robot,

https://chromium.googlesource.com/linux-fpga-chameleon/+/fpga-chameleon-4.2/scripts/coccinelle/api/memdup_user.cocci,

and I believe it tries to persuade us to use memdup_user instead of kmalloc and copy_form_user,

With this new function, we don't have to write 'len' twice, which can lead to typos/mistakes. It also produces smaller code and kernel text.

http://lkml.iu.edu/hypermail/linux/kernel/0903.0/02349.html

Cheers,

Ivan

Quoted 43 lines · Matt Fleming
Quoted 6 lines · Ivan Hu

Cleanup the warning, drivers/firmware/efi/test/efi_test.c:269:8-15: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation This is a little bit restricted to reduce false positives

I don't understand this sentence. What restriction are you talking about?

Quoted 32 lines

Generated by: coccinelle/api/memdup_user.coc

---
  drivers/firmware/efi/test/efi_test.c | 10 +++-------
  1 file changed, 3 insertions(+), 7 deletions(-)
drivers/firmware/efi/test/efi_test.c +3 -7
index f61bb52..5602c46 100644
@@ -266,14 +266,10 @@ static long efi_runtime_set_variable(unsigned long arg)
  			return rv;
  	}
 
-	data = kmalloc(setvariable.data_size, GFP_KERNEL);
-	if (!data) {
+	data = memdup_user(setvariable.data, setvariable.data_size);
+	if (IS_ERR(data)) {
  		kfree(name);
-		return -ENOMEM;
-	}
-	if (copy_from_user(data, setvariable.data, setvariable.data_size)) {
-		rv = -EFAULT;
-		goto out;
+		return PTR_ERR(data);
  	}
 
  	status = efi.set_variable(name, &vendor_guid,
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 3fb6d9fa-b554-509b-fc59-d75340017589-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org

#8 lore raw

Re: [PATCH 1/3] efi/efi_test: use memdup_user() as a cleanup

Quoted 7 lines · ivanhu

Hi Matt,

The warning message is from Kbuild test robot,

https://chromium.googlesource.com/linux-fpga-chameleon/+/fpga-chameleon-4.2/scripts/coccinelle/api/memdup_user.cocci,

Quoted 8 lines

and I believe it tries to persuade us to use memdup_user instead of kmalloc and copy_form_user,

With this new function, we don't have to write 'len' twice, which can lead to typos/mistakes.
It also produces smaller code and kernel text.

http://lkml.iu.edu/hypermail/linux/kernel/0903.0/02349.html

Right, I understand all that. My question was specifically about the following sentence from your patch,

Quoted 1 line

This is a little bit restricted to reduce false positives

What restriction that reduces false positives are you referring to?

A message that is not in the archive (sent off-list or not yet received): 20161004213743.GV16071-mF/unelCI9GS6iBeEJttW/[email protected]

author
#10 lore raw

Re: [PATCH 1/3] efi/efi_test: use memdup_user() as a cleanup

Quoted 15 lines · Matt Fleming
Quoted 14 lines · ivanhu

Hi Matt,

The warning message is from Kbuild test robot,

https://chromium.googlesource.com/linux-fpga-chameleon/+/fpga-chameleon-4.2/scripts/coccinelle/api/memdup_user.cocci,

and I believe it tries to persuade us to use memdup_user instead of kmalloc and copy_form_user,

With this new function, we don't have to write 'len' twice, which can lead to typos/mistakes.
It also produces smaller code and kernel text.

Quoted 8 lines
Quoted 1 line

http://lkml.iu.edu/hypermail/linux/kernel/0903.0/02349.html

Right, I understand all that. My question was specifically about the following sentence from your patch,

Quoted 1 line

This is a little bit restricted to reduce false positives

What restriction that reduces false positives are you referring to?

I believe that author means that, with this new function, we don't have to write 'len' twice, which can lead to typos/mistakes ....

Let me remove the ambiguous comments and send out the patch again.

Ivan

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-efi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#11 lore raw

PATCH 2/3 efi/efi_test: fix the uninitialized value datasize

In reply to a message not in the archive

Fix the minor issue found by CoverityScan
CID 1358931 (#1 of 1): Uninitialized scalar variable (UNINIT)9.
uninit_use: Using uninitialized value datasize.
199 prev_datasize = datasize;
200 status = efi.get_variable(name, vd, at, dz, data);

---
 drivers/firmware/efi/test/efi_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/test/efi_test.c +1 -1
index 5602c46..87394f0 100644
@@ -156,7 +156,7 @@ static long efi_runtime_get_variable(unsigned long arg)
 {
 	struct efi_getvariable __user *getvariable_user;
 	struct efi_getvariable getvariable;
-	unsigned long datasize, prev_datasize, *dz;
+	unsigned long datasize = 0, prev_datasize, *dz;
 	efi_guid_t vendor_guid, *vd = NULL;
 	efi_status_t status;
 	efi_char16_t *name = NULL;
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1474859691-8574-3-git-send-email-ivan.hu-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org

Applied
#13 lore raw

Re: [PATCH 2/3] efi/efi_test: fix the uninitialized value datasize

Quoted 16 lines · Ivan Hu

Fix the minor issue found by CoverityScan
CID 1358931 (#1 of 1): Uninitialized scalar variable (UNINIT)9.
uninit_use: Using uninitialized value datasize.
199 prev_datasize = datasize;
200 status = efi.get_variable(name, vd, at, dz, data);

---
 drivers/firmware/efi/test/efi_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/test/efi_test.c +0 -0
index 5602c46..87394f0 100644

drivers/firmware/efi/test/efi_test.c static long efi_runtime_get_variable(unsigned long arg) · line 162

Quoted 8 lines
drivers/firmware/efi/test/efi_test.c +1 -1
@@ -156,7 +156,7 @@ static long efi_runtime_get_variable(unsigned long arg)
 {
 	struct efi_getvariable __user *getvariable_user;
 	struct efi_getvariable getvariable;
-	unsigned long datasize, prev_datasize, *dz;
+	unsigned long datasize = 0, prev_datasize, *dz;
 	efi_guid_t vendor_guid, *vd = NULL;
 	efi_status_t status;
 	efi_char16_t *name = NULL;

Thanks, applied to 'next'.

author
#14 lore raw

PATCH 3/3 efi/efi_test: fix the uninitialized value rv

In reply to a message not in the archive

Fix the minor issue found by CoverityScan
520 kfree(name);
CID 1358932 (#1 of 1): Uninitialized scalar variable (UNINIT)17.
uninit_use: Using uninitialized value rv.
521 return rv;
522}

---
 drivers/firmware/efi/test/efi_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/test/efi_test.c +1 -1
index 87394f0..39302e6 100644
@@ -425,7 +425,7 @@ static long efi_runtime_get_nextvariablename(unsigned long arg)
 	efi_guid_t *vd = NULL;
 	efi_guid_t vendor_guid;
 	efi_char16_t *name = NULL;
-	int rv;
+	int rv = 0;
 
 	getnextvariablename_user = (struct efi_getnextvariablename __user *)arg;
 
Signature · 1 line
1.9.1

A message that is not in the archive (sent off-list or not yet received): 1474859691-8574-4-git-send-email-ivan.hu-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org

Applied
#16 lore raw

Re: [PATCH 3/3] efi/efi_test: fix the uninitialized value rv

Quoted 17 lines · Ivan Hu

Fix the minor issue found by CoverityScan
520 kfree(name);
CID 1358932 (#1 of 1): Uninitialized scalar variable (UNINIT)17.
uninit_use: Using uninitialized value rv.
521 return rv;
522}

---
 drivers/firmware/efi/test/efi_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/test/efi_test.c +0 -0
index 87394f0..39302e6 100644

drivers/firmware/efi/test/efi_test.c static long efi_runtime_get_nextvariablename(unsigned long arg) · line 431

Quoted 8 lines
drivers/firmware/efi/test/efi_test.c +1 -1
@@ -425,7 +425,7 @@ static long efi_runtime_get_nextvariablename(unsigned long arg)
 	efi_guid_t *vd = NULL;
 	efi_guid_t vendor_guid;
 	efi_char16_t *name = NULL;
-	int rv;
+	int rv = 0;
 
 	getnextvariablename_user = (struct efi_getnextvariablename __user *)arg;
 

Thanks, applied to 'next'.