Skip to contentSkip to conversation

VMCI: Use memdup_user() as a cleanup

PATCH In review Vasyl Gomonovych 2 participants 4 messages Nov 22, 2017

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

PATCH 1/1 VMCI: Use memdup_user() as a cleanup

Fix coccicheck warning which recommends to use memdup_user():
drivers/misc/vmw_vmci/vmci_host.c:757:11-18: WARNING opportunity for memdup_user Generated by: scripts/coccinelle/memdup_user/memdup_user.cocci

---
 drivers/misc/vmw_vmci/vmci_host.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/misc/vmw_vmci/vmci_host.c +6 -13
index 8a16a26e9658..b8468b864bcd 100644
@@ -754,18 +754,12 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 	if (copy_from_user(&set_info, uptr, sizeof(set_info)))
 		return -EFAULT;
 
-	cpt_buf = kmalloc(set_info.buf_size, GFP_KERNEL);
-	if (!cpt_buf) {
-		vmci_ioctl_err(
-			"cannot allocate memory to set cpt state (type=%d)\n",
-			set_info.cpt_type);
-		return -ENOMEM;
-	}
-
-	if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
-			   set_info.buf_size)) {
-		retval = -EFAULT;
-		goto out;
+	cpt_buf = memdup_user((void __user *)(uintptr_t)set_info.cpt_buf,
+			set_info.buf_size);
+	if (IS_ERR(cpt_buf)) {
+		vmci_ioctl_err("cannot allocate memory to set cpt state (type=%d)\n",
+				set_info.cpt_type);
+		return PTR_ERR(cpt_buf);
 	}
 
 	cid = vmci_ctx_get_id(vmci_host_dev->context);
@@ -774,7 +768,6 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 
 	retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
 
-out:
 	kfree(cpt_buf);
 	return retval;
 }
Signature · 1 line
1.9.1
#1 lore raw
Quoted 3 lines · Vasyl Gomonovych

Fix coccicheck warning which recommends to use memdup_user():
drivers/misc/vmw_vmci/vmci_host.c:757:11-18: WARNING opportunity for memdup_user Generated by: scripts/coccinelle/memdup_user/memdup_user.cocci

Nice little cleanup.

drivers/misc/vmw_vmci/vmci_host.c

Quoted 1 line
drivers/misc/vmw_vmci/vmci_host.c +0 -0

[]

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,

Quoted 1 line
drivers/misc/vmw_vmci/vmci_host.c +0 -0
@@ -754,18 +754,12 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,

[]

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev, · line 762

Quoted 6 lines
drivers/misc/vmw_vmci/vmci_host.c +6 -0
+	cpt_buf = memdup_user((void __user *)(uintptr_t)set_info.cpt_buf,
+			set_info.buf_size);
+	if (IS_ERR(cpt_buf)) {
+		vmci_ioctl_err("cannot allocate memory to set cpt state (type=%d)\n",
+				set_info.cpt_type);
+		return PTR_ERR(cpt_buf);

Trivia:

The vmci_ioctl_err might not be necessary.
There is a dump_stack() on allocation failure.

and

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev, · line 772

Quoted 7 lines
drivers/misc/vmw_vmci/vmci_host.c +0 -1
@@ -774,7 +768,6 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 
 	retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
 
-out:
 	kfree(cpt_buf);
 	return retval;

Perhaps move the kfree above the copy_to_user,
remove the retval declaration and use

	return copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
#2 lore raw

I am agree, I will change it.

Quoted 38 lines · Joe Perches
Quoted 3 lines · Vasyl Gomonovych

Fix coccicheck warning which recommends to use memdup_user():
drivers/misc/vmw_vmci/vmci_host.c:757:11-18: WARNING opportunity for memdup_user Generated by: scripts/coccinelle/memdup_user/memdup_user.cocci

Nice little cleanup.

drivers/misc/vmw_vmci/vmci_host.c

Quoted 1 line
drivers/misc/vmw_vmci/vmci_host.c +0 -0

[]

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,

Quoted 1 line
drivers/misc/vmw_vmci/vmci_host.c +0 -0
@@ -754,18 +754,12 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,

[]

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev, · line 762

Quoted 6 lines
drivers/misc/vmw_vmci/vmci_host.c +6 -0
+     cpt_buf = memdup_user((void __user *)(uintptr_t)set_info.cpt_buf,
+                     set_info.buf_size);
+     if (IS_ERR(cpt_buf)) {
+             vmci_ioctl_err("cannot allocate memory to set cpt state (type=%d)\n",
+                             set_info.cpt_type);
+             return PTR_ERR(cpt_buf);

Trivia:

The vmci_ioctl_err might not be necessary.
There is a dump_stack() on allocation failure.

and

drivers/misc/vmw_vmci/vmci_host.c static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev, · line 772

Quoted 7 lines
drivers/misc/vmw_vmci/vmci_host.c +0 -1
@@ -774,7 +768,6 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 
      retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
 
-out:
      kfree(cpt_buf);
      return retval;

Perhaps move the kfree above the copy_to_user,
remove the retval declaration and use

        return copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
Signature · 1 line
Доброї вам пори дня.
#3 lore raw

PATCH v2 1/1 VMCI: Use memdup_user() as a cleanup

In reply to Vasyl Gomonovych

Fix coccicheck warning which recommends to use memdup_user()
and relative cleanup
drivers/misc/vmw_vmci/vmci_host.c:757:11-18: WARNING opportunity for memdup_user Generated by: scripts/coccinelle/memdup_user/memdup_user.cocci

---
 drivers/misc/vmw_vmci/vmci_host.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)
drivers/misc/vmw_vmci/vmci_host.c +8 -17
index 8a16a26e9658..b0416dcc590a 100644
@@ -744,7 +744,6 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 	struct vmci_ctx_chkpt_buf_info set_info;
 	u32 cid;
 	void *cpt_buf;
-	int retval;
 
 	if (vmci_host_dev->ct_type != VMCIOBJ_CONTEXT) {
 		vmci_ioctl_err("only valid for contexts\n");
@@ -754,29 +753,21 @@ static int vmci_host_do_ctx_set_cpt_state(struct vmci_host_dev *vmci_host_dev,
 	if (copy_from_user(&set_info, uptr, sizeof(set_info)))
 		return -EFAULT;
 
-	cpt_buf = kmalloc(set_info.buf_size, GFP_KERNEL);
-	if (!cpt_buf) {
-		vmci_ioctl_err(
-			"cannot allocate memory to set cpt state (type=%d)\n",
-			set_info.cpt_type);
-		return -ENOMEM;
-	}
-
-	if (copy_from_user(cpt_buf, (void __user *)(uintptr_t)set_info.cpt_buf,
-			   set_info.buf_size)) {
-		retval = -EFAULT;
-		goto out;
+	cpt_buf = memdup_user((void __user *)(uintptr_t)set_info.cpt_buf,
+			set_info.buf_size);
+	if (IS_ERR(cpt_buf)) {
+		vmci_ioctl_err("cannot allocate memory to set cpt state (type=%d)\n",
+				set_info.cpt_type);
+		return PTR_ERR(cpt_buf);
 	}
 
 	cid = vmci_ctx_get_id(vmci_host_dev->context);
 	set_info.result = vmci_ctx_set_chkpt_state(cid, set_info.cpt_type,
 						   set_info.buf_size, cpt_buf);
 
-	retval = copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
-
-out:
 	kfree(cpt_buf);
-	return retval;
+
+	return copy_to_user(uptr, &set_info, sizeof(set_info)) ? -EFAULT : 0;
 }
 
 static int vmci_host_do_get_context_id(struct vmci_host_dev *vmci_host_dev,
Signature · 1 line
1.9.1