All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] misc: mic: use kstrdup() in mic_sysfs
@ 2015-10-02  2:43 Geliang Tang
  2015-10-04 11:56 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Geliang Tang @ 2015-10-02  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Sudeep Dutt, Nikhil Rao, Ashutosh Dixit
  Cc: Geliang Tang, linux-kernel

Use kstrdup instead of kmalloc and strncpy.

Signed-off-by: Geliang Tang <geliangtang@163.com>
---
 drivers/misc/mic/host/mic_sysfs.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)

diff --git a/drivers/misc/mic/host/mic_sysfs.c b/drivers/misc/mic/host/mic_sysfs.c
index 6dd864e..7b67aea 100644
--- a/drivers/misc/mic/host/mic_sysfs.c
+++ b/drivers/misc/mic/host/mic_sysfs.c
@@ -210,18 +210,14 @@ cmdline_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&mdev->mic_mutex);
 	kfree(mdev->cmdline);
 
-	mdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
+	mdev->cmdline = kstrdup(buf, GFP_KERNEL);
 	if (!mdev->cmdline) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(mdev->cmdline, buf, count);
-
 	if (mdev->cmdline[count - 1] == '\n')
 		mdev->cmdline[count - 1] = '\0';
-	else
-		mdev->cmdline[count] = '\0';
 unlock:
 	mutex_unlock(&mdev->mic_mutex);
 	return count;
@@ -256,17 +252,14 @@ firmware_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&mdev->mic_mutex);
 	kfree(mdev->firmware);
 
-	mdev->firmware = kmalloc(count + 1, GFP_KERNEL);
+	mdev->firmware = kstrdup(buf, GFP_KERNEL);
 	if (!mdev->firmware) {
 		count = -ENOMEM;
 		goto unlock;
 	}
-	strncpy(mdev->firmware, buf, count);
 
 	if (mdev->firmware[count - 1] == '\n')
 		mdev->firmware[count - 1] = '\0';
-	else
-		mdev->firmware[count] = '\0';
 unlock:
 	mutex_unlock(&mdev->mic_mutex);
 	return count;
@@ -301,18 +294,14 @@ ramdisk_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&mdev->mic_mutex);
 	kfree(mdev->ramdisk);
 
-	mdev->ramdisk = kmalloc(count + 1, GFP_KERNEL);
+	mdev->ramdisk = kstrdup(buf, GFP_KERNEL);
 	if (!mdev->ramdisk) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(mdev->ramdisk, buf, count);
-
 	if (mdev->ramdisk[count - 1] == '\n')
 		mdev->ramdisk[count - 1] = '\0';
-	else
-		mdev->ramdisk[count] = '\0';
 unlock:
 	mutex_unlock(&mdev->mic_mutex);
 	return count;
@@ -350,18 +339,14 @@ bootmode_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&mdev->mic_mutex);
 	kfree(mdev->bootmode);
 
-	mdev->bootmode = kmalloc(count + 1, GFP_KERNEL);
+	mdev->bootmode = kstrdup(buf, GFP_KERNEL);
 	if (!mdev->bootmode) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(mdev->bootmode, buf, count);
-
 	if (mdev->bootmode[count - 1] == '\n')
 		mdev->bootmode[count - 1] = '\0';
-	else
-		mdev->bootmode[count] = '\0';
 unlock:
 	mutex_unlock(&mdev->mic_mutex);
 	return count;
-- 
2.5.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] misc: mic: use kstrdup() in mic_sysfs
  2015-10-02  2:43 [PATCH] misc: mic: use kstrdup() in mic_sysfs Geliang Tang
@ 2015-10-04 11:56 ` Greg Kroah-Hartman
  2015-10-10 11:46   ` [PATCH] misc: mic: use kstrdup() in cosm_sysfs Geliang Tang
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2015-10-04 11:56 UTC (permalink / raw)
  To: Geliang Tang; +Cc: Sudeep Dutt, Nikhil Rao, Ashutosh Dixit, linux-kernel

On Fri, Oct 02, 2015 at 10:43:34AM +0800, Geliang Tang wrote:
> Use kstrdup instead of kmalloc and strncpy.
> 
> Signed-off-by: Geliang Tang <geliangtang@163.com>
> ---
>  drivers/misc/mic/host/mic_sysfs.c | 23 ++++-------------------
>  1 file changed, 4 insertions(+), 19 deletions(-)

This file isn't even in the tree anymore :(

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] misc: mic: use kstrdup() in cosm_sysfs
  2015-10-04 11:56 ` Greg Kroah-Hartman
@ 2015-10-10 11:46   ` Geliang Tang
  2015-10-10 19:14     ` Joe Perches
  0 siblings, 1 reply; 5+ messages in thread
From: Geliang Tang @ 2015-10-10 11:46 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Geliang Tang, Sudeep Dutt, Nikhil Rao, Ashutosh Dixit,
	linux-kernel

Use kstrdup instead of kmalloc and strncpy.

Signed-off-by: Geliang Tang <geliangtang@163.com>
---
 drivers/misc/mic/cosm/cosm_sysfs.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)

diff --git a/drivers/misc/mic/cosm/cosm_sysfs.c b/drivers/misc/mic/cosm/cosm_sysfs.c
index 29d6863..c3db466 100644
--- a/drivers/misc/mic/cosm/cosm_sysfs.c
+++ b/drivers/misc/mic/cosm/cosm_sysfs.c
@@ -211,18 +211,14 @@ cmdline_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&cdev->cosm_mutex);
 	kfree(cdev->cmdline);
 
-	cdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
+	cdev->cmdline = kstrdup(buf, GFP_KERNEL);
 	if (!cdev->cmdline) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(cdev->cmdline, buf, count);
-
 	if (cdev->cmdline[count - 1] == '\n')
 		cdev->cmdline[count - 1] = '\0';
-	else
-		cdev->cmdline[count] = '\0';
 unlock:
 	mutex_unlock(&cdev->cosm_mutex);
 	return count;
@@ -257,17 +253,14 @@ firmware_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&cdev->cosm_mutex);
 	kfree(cdev->firmware);
 
-	cdev->firmware = kmalloc(count + 1, GFP_KERNEL);
+	cdev->firmware = kstrdup(buf, GFP_KERNEL);
 	if (!cdev->firmware) {
 		count = -ENOMEM;
 		goto unlock;
 	}
-	strncpy(cdev->firmware, buf, count);
 
 	if (cdev->firmware[count - 1] == '\n')
 		cdev->firmware[count - 1] = '\0';
-	else
-		cdev->firmware[count] = '\0';
 unlock:
 	mutex_unlock(&cdev->cosm_mutex);
 	return count;
@@ -302,18 +295,14 @@ ramdisk_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&cdev->cosm_mutex);
 	kfree(cdev->ramdisk);
 
-	cdev->ramdisk = kmalloc(count + 1, GFP_KERNEL);
+	cdev->ramdisk = kstrdup(buf, GFP_KERNEL);
 	if (!cdev->ramdisk) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(cdev->ramdisk, buf, count);
-
 	if (cdev->ramdisk[count - 1] == '\n')
 		cdev->ramdisk[count - 1] = '\0';
-	else
-		cdev->ramdisk[count] = '\0';
 unlock:
 	mutex_unlock(&cdev->cosm_mutex);
 	return count;
@@ -351,18 +340,14 @@ bootmode_store(struct device *dev, struct device_attribute *attr,
 	mutex_lock(&cdev->cosm_mutex);
 	kfree(cdev->bootmode);
 
-	cdev->bootmode = kmalloc(count + 1, GFP_KERNEL);
+	cdev->bootmode = kstrdup(buf, GFP_KERNEL);
 	if (!cdev->bootmode) {
 		count = -ENOMEM;
 		goto unlock;
 	}
 
-	strncpy(cdev->bootmode, buf, count);
-
 	if (cdev->bootmode[count - 1] == '\n')
 		cdev->bootmode[count - 1] = '\0';
-	else
-		cdev->bootmode[count] = '\0';
 unlock:
 	mutex_unlock(&cdev->cosm_mutex);
 	return count;
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] misc: mic: use kstrdup() in cosm_sysfs
  2015-10-10 11:46   ` [PATCH] misc: mic: use kstrdup() in cosm_sysfs Geliang Tang
@ 2015-10-10 19:14     ` Joe Perches
  2015-10-11  1:01       ` Ashutosh Dixit
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2015-10-10 19:14 UTC (permalink / raw)
  To: Geliang Tang
  Cc: Greg Kroah-Hartman, Sudeep Dutt, Nikhil Rao, Ashutosh Dixit,
	linux-kernel

On Sat, 2015-10-10 at 04:46 -0700, Geliang Tang wrote:
> Use kstrdup instead of kmalloc and strncpy.

As count is a function argument, this isn't the same code.

You should state why it's correct in the commit log.

> diff --git a/drivers/misc/mic/cosm/cosm_sysfs.c b/drivers/misc/mic/cosm/cosm_sysfs.c
[]
> @@ -211,18 +211,14 @@ cmdline_store(struct device *dev, struct device_attribute *attr,
>  	mutex_lock(&cdev->cosm_mutex);
>  	kfree(cdev->cmdline);
>  
> -	cdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
> +	cdev->cmdline = kstrdup(buf, GFP_KERNEL);



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] misc: mic: use kstrdup() in cosm_sysfs
  2015-10-10 19:14     ` Joe Perches
@ 2015-10-11  1:01       ` Ashutosh Dixit
  0 siblings, 0 replies; 5+ messages in thread
From: Ashutosh Dixit @ 2015-10-11  1:01 UTC (permalink / raw)
  To: Geliang Tang
  Cc: Joe Perches, Greg Kroah-Hartman, Dutt, Sudeep, Rao, Nikhil,
	linux-kernel@vger.kernel.org

On Sat, Oct 10 2015 at 12:14:30 PM, Joe Perches <joe@perches.com> wrote:
> On Sat, 2015-10-10 at 04:46 -0700, Geliang Tang wrote:
>> Use kstrdup instead of kmalloc and strncpy.
>
>> diff --git a/drivers/misc/mic/cosm/cosm_sysfs.c b/drivers/misc/mic/cosm/cosm_sysfs.c
> []
>> @@ -211,18 +211,14 @@ cmdline_store(struct device *dev, struct device_attribute *attr,
>>  	mutex_lock(&cdev->cosm_mutex);
>>  	kfree(cdev->cmdline);
>>
>> -	cdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
>> +	cdev->cmdline = kstrdup(buf, GFP_KERNEL);
>>
> As count is a function argument, this isn't the same code.
>
Correct, is this code safe for unterminated strings from user land?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-10-11  1:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-02  2:43 [PATCH] misc: mic: use kstrdup() in mic_sysfs Geliang Tang
2015-10-04 11:56 ` Greg Kroah-Hartman
2015-10-10 11:46   ` [PATCH] misc: mic: use kstrdup() in cosm_sysfs Geliang Tang
2015-10-10 19:14     ` Joe Perches
2015-10-11  1:01       ` Ashutosh Dixit

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.