linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] mwifiex: fix mwifiex_rdeeprom_read()
@ 2015-08-20 10:30 Dan Carpenter
  2015-08-25  6:56 ` Amitkumar Karwar
  2015-08-25 12:29 ` Kalle Valo
  0 siblings, 2 replies; 6+ messages in thread
From: Dan Carpenter @ 2015-08-20 10:30 UTC (permalink / raw)
  To: Amitkumar Karwar, Bing Zhao
  Cc: Nishant Sarmukadam, Kalle Valo, linux-wireless, kernel-janitors

There were several bugs here.

1)  The done label was in the wrong place so we didn't copy any
    information out when there was no command given.

2)  We were using PAGE_SIZE as the size of the buffer instead of
    "PAGE_SIZE - pos".

3)  snprintf() returns the number of characters that would have been
    printed if there were enough space.  If there was not enough space
    (and we had fixed the memory corruption bug #2) then it would result
    in an information leak when we do simple_read_from_buffer().  I've
    changed it to use scnprintf() instead.

I also removed the initialization at the start of the function, because
I thought it made the code a little more clear.

Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell mwifiex driver')
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/net/wireless/mwifiex/debugfs.c b/drivers/net/wireless/mwifiex/debugfs.c
index 5a0636d4..bd2f5d2 100644
--- a/drivers/net/wireless/mwifiex/debugfs.c
+++ b/drivers/net/wireless/mwifiex/debugfs.c
@@ -731,7 +731,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 		(struct mwifiex_private *) file->private_data;
 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
 	char *buf = (char *) addr;
-	int pos = 0, ret = 0, i;
+	int pos, ret, i;
 	u8 value[MAX_EEPROM_DATA];
 
 	if (!buf)
@@ -739,7 +739,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 
 	if (saved_offset == -1) {
 		/* No command has been given */
-		pos += snprintf(buf, PAGE_SIZE, "0");
+		pos = snprintf(buf, PAGE_SIZE, "0");
 		goto done;
 	}
 
@@ -751,14 +751,14 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 		goto done;
 	}
 
-	pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
+	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
 
 	for (i = 0; i < saved_bytes; i++)
-		pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ", value[i]);
+		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ", value[i]);
 
+done:
 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
 
-done:
 	free_page(addr);
 	return ret;
 }

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

* RE: [patch] mwifiex: fix mwifiex_rdeeprom_read()
  2015-08-20 10:30 [patch] mwifiex: fix mwifiex_rdeeprom_read() Dan Carpenter
@ 2015-08-25  6:56 ` Amitkumar Karwar
  2015-08-25 12:29 ` Kalle Valo
  1 sibling, 0 replies; 6+ messages in thread
From: Amitkumar Karwar @ 2015-08-25  6:56 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Nishant Sarmukadam, Kalle Valo, linux-wireless@vger.kernel.org,
	kernel-janitors@vger.kernel.org

Hi Dan,

> From: Dan Carpenter [mailto:dan.carpenter@oracle.com]
> Sent: Thursday, August 20, 2015 4:01 PM
> To: Amitkumar Karwar; Bing Zhao
> Cc: Nishant Sarmukadam; Kalle Valo; linux-wireless@vger.kernel.org;
> kernel-janitors@vger.kernel.org
> Subject: [patch] mwifiex: fix mwifiex_rdeeprom_read()
> 
> There were several bugs here.
> 
> 1)  The done label was in the wrong place so we didn't copy any
>     information out when there was no command given.
> 
> 2)  We were using PAGE_SIZE as the size of the buffer instead of
>     "PAGE_SIZE - pos".
> 
> 3)  snprintf() returns the number of characters that would have been
>     printed if there were enough space.  If there was not enough space
>     (and we had fixed the memory corruption bug #2) then it would result
>     in an information leak when we do simple_read_from_buffer().  I've
>     changed it to use scnprintf() instead.
> 
> I also removed the initialization at the start of the function, because
> I thought it made the code a little more clear.
> 
> Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell
> mwifiex driver')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> diff --git a/drivers/net/wireless/mwifiex/debugfs.c
> b/drivers/net/wireless/mwifiex/debugfs.c
> index 5a0636d4..bd2f5d2 100644
> --- a/drivers/net/wireless/mwifiex/debugfs.c
> +++ b/drivers/net/wireless/mwifiex/debugfs.c
> @@ -731,7 +731,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user
> *ubuf,
>  		(struct mwifiex_private *) file->private_data;
>  	unsigned long addr = get_zeroed_page(GFP_KERNEL);
>  	char *buf = (char *) addr;
> -	int pos = 0, ret = 0, i;
> +	int pos, ret, i;
>  	u8 value[MAX_EEPROM_DATA];
> 
>  	if (!buf)
> @@ -739,7 +739,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user
> *ubuf,
> 
>  	if (saved_offset == -1) {
>  		/* No command has been given */
> -		pos += snprintf(buf, PAGE_SIZE, "0");
> +		pos = snprintf(buf, PAGE_SIZE, "0");
>  		goto done;
>  	}
> 
> @@ -751,14 +751,14 @@ mwifiex_rdeeprom_read(struct file *file, char
> __user *ubuf,
>  		goto done;
>  	}
> 
> -	pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset,
> saved_bytes);
> +	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset,
> saved_bytes);
> 
>  	for (i = 0; i < saved_bytes; i++)
> -		pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ",
> value[i]);
> +		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ",
> value[i]);
> 
> +done:
>  	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
> 
> -done:
>  	free_page(addr);
>  	return ret;
>  }

Looks good.

Acked-by: Amitkumar Karwar <akarwar@marvell.com>

Regards,
Amitkumar

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

* Re: [patch] mwifiex: fix mwifiex_rdeeprom_read()
  2015-08-20 10:30 [patch] mwifiex: fix mwifiex_rdeeprom_read() Dan Carpenter
  2015-08-25  6:56 ` Amitkumar Karwar
@ 2015-08-25 12:29 ` Kalle Valo
  2015-09-21 16:19   ` [patch v2] " Dan Carpenter
  1 sibling, 1 reply; 6+ messages in thread
From: Kalle Valo @ 2015-08-25 12:29 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Amitkumar Karwar, Bing Zhao, Nishant Sarmukadam, linux-wireless,
	kernel-janitors

Dan Carpenter <dan.carpenter@oracle.com> writes:

> There were several bugs here.
>
> 1)  The done label was in the wrong place so we didn't copy any
>     information out when there was no command given.
>
> 2)  We were using PAGE_SIZE as the size of the buffer instead of
>     "PAGE_SIZE - pos".
>
> 3)  snprintf() returns the number of characters that would have been
>     printed if there were enough space.  If there was not enough space
>     (and we had fixed the memory corruption bug #2) then it would result
>     in an information leak when we do simple_read_from_buffer().  I've
>     changed it to use scnprintf() instead.
>
> I also removed the initialization at the start of the function, because
> I thought it made the code a little more clear.
>
> Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell mwifiex driver')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

I see a new warning:

drivers/net/wireless/mwifiex/debugfs.c:760:31: warning: pos may be used uninitialized in this function [-Wuninitialized]

Is it a valid warning or should I apply the patch anyway?

-- 
Kalle Valo

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

* [patch v2] mwifiex: fix mwifiex_rdeeprom_read()
  2015-08-25 12:29 ` Kalle Valo
@ 2015-09-21 16:19   ` Dan Carpenter
  2015-09-22 10:58     ` Amitkumar Karwar
  2015-09-29  8:09     ` [v2] " Kalle Valo
  0 siblings, 2 replies; 6+ messages in thread
From: Dan Carpenter @ 2015-09-21 16:19 UTC (permalink / raw)
  To: Amitkumar Karwar
  Cc: Nishant Sarmukadam, Kalle Valo, linux-wireless, kernel-janitors

There were several bugs here.

1)  The done label was in the wrong place so we didn't copy any
    information out when there was no command given.

2)  We were using PAGE_SIZE as the size of the buffer instead of
    "PAGE_SIZE - pos".

3)  snprintf() returns the number of characters that would have been
    printed if there were enough space.  If there was not enough space
    (and we had fixed the memory corruption bug #2) then it would result
    in an information leak when we do simple_read_from_buffer().  I've
    changed it to use scnprintf() instead.

I also removed the initialization at the start of the function, because
I thought it made the code a little more clear.

Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell mwifiex driver')
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
v2:  My first part introduced a bug.  :(

Sorry for the delay on this.

diff --git a/drivers/net/wireless/mwifiex/debugfs.c b/drivers/net/wireless/mwifiex/debugfs.c
index 5a0636d4..5583856 100644
--- a/drivers/net/wireless/mwifiex/debugfs.c
+++ b/drivers/net/wireless/mwifiex/debugfs.c
@@ -731,7 +731,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 		(struct mwifiex_private *) file->private_data;
 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
 	char *buf = (char *) addr;
-	int pos = 0, ret = 0, i;
+	int pos, ret, i;
 	u8 value[MAX_EEPROM_DATA];
 
 	if (!buf)
@@ -739,7 +739,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 
 	if (saved_offset == -1) {
 		/* No command has been given */
-		pos += snprintf(buf, PAGE_SIZE, "0");
+		pos = snprintf(buf, PAGE_SIZE, "0");
 		goto done;
 	}
 
@@ -748,17 +748,17 @@ mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
 				  (u16) saved_bytes, value);
 	if (ret) {
 		ret = -EINVAL;
-		goto done;
+		goto out_free;
 	}
 
-	pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
+	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
 
 	for (i = 0; i < saved_bytes; i++)
-		pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ", value[i]);
-
-	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
+		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ", value[i]);
 
 done:
+	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
+out_free:
 	free_page(addr);
 	return ret;
 }

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

* RE: [patch v2] mwifiex: fix mwifiex_rdeeprom_read()
  2015-09-21 16:19   ` [patch v2] " Dan Carpenter
@ 2015-09-22 10:58     ` Amitkumar Karwar
  2015-09-29  8:09     ` [v2] " Kalle Valo
  1 sibling, 0 replies; 6+ messages in thread
From: Amitkumar Karwar @ 2015-09-22 10:58 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Kalle Valo, linux-wireless@vger.kernel.org,
	kernel-janitors@vger.kernel.org

Hi Dan,

> From: Dan Carpenter [mailto:dan.carpenter@oracle.com]
> Sent: Monday, September 21, 2015 9:50 PM
> To: Amitkumar Karwar
> Cc: Nishant Sarmukadam; Kalle Valo; linux-wireless@vger.kernel.org;
> kernel-janitors@vger.kernel.org
> Subject: [patch v2] mwifiex: fix mwifiex_rdeeprom_read()
> 
> There were several bugs here.
> 
> 1)  The done label was in the wrong place so we didn't copy any
>     information out when there was no command given.
> 
> 2)  We were using PAGE_SIZE as the size of the buffer instead of
>     "PAGE_SIZE - pos".
> 
> 3)  snprintf() returns the number of characters that would have been
>     printed if there were enough space.  If there was not enough space
>     (and we had fixed the memory corruption bug #2) then it would result
>     in an information leak when we do simple_read_from_buffer().  I've
>     changed it to use scnprintf() instead.
> 
> I also removed the initialization at the start of the function, because
> I thought it made the code a little more clear.
> 
> Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell
> mwifiex driver')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> v2:  My first part introduced a bug.  :(
> 
> Sorry for the delay on this.
> 
> diff --git a/drivers/net/wireless/mwifiex/debugfs.c
> b/drivers/net/wireless/mwifiex/debugfs.c
> index 5a0636d4..5583856 100644
> --- a/drivers/net/wireless/mwifiex/debugfs.c
> +++ b/drivers/net/wireless/mwifiex/debugfs.c
> @@ -731,7 +731,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user
> *ubuf,
>  		(struct mwifiex_private *) file->private_data;
>  	unsigned long addr = get_zeroed_page(GFP_KERNEL);
>  	char *buf = (char *) addr;
> -	int pos = 0, ret = 0, i;
> +	int pos, ret, i;
>  	u8 value[MAX_EEPROM_DATA];
> 
>  	if (!buf)
> @@ -739,7 +739,7 @@ mwifiex_rdeeprom_read(struct file *file, char __user
> *ubuf,
> 
>  	if (saved_offset == -1) {
>  		/* No command has been given */
> -		pos += snprintf(buf, PAGE_SIZE, "0");
> +		pos = snprintf(buf, PAGE_SIZE, "0");
>  		goto done;
>  	}
> 
> @@ -748,17 +748,17 @@ mwifiex_rdeeprom_read(struct file *file, char
> __user *ubuf,
>  				  (u16) saved_bytes, value);
>  	if (ret) {
>  		ret = -EINVAL;
> -		goto done;
> +		goto out_free;
>  	}
> 
> -	pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset,
> saved_bytes);
> +	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset,
> saved_bytes);
> 
>  	for (i = 0; i < saved_bytes; i++)
> -		pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ",
> value[i]);
> -
> -	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
> +		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ",
> value[i]);
> 
>  done:
> +	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
> +out_free:
>  	free_page(addr);
>  	return ret;
>  }

Acked-by: Amitkumar Karwar <akarwar@marvell.com>

Regards,
Amitkumar

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

* Re: [v2] mwifiex: fix mwifiex_rdeeprom_read()
  2015-09-21 16:19   ` [patch v2] " Dan Carpenter
  2015-09-22 10:58     ` Amitkumar Karwar
@ 2015-09-29  8:09     ` Kalle Valo
  1 sibling, 0 replies; 6+ messages in thread
From: Kalle Valo @ 2015-09-29  8:09 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Amitkumar Karwar, Nishant Sarmukadam, linux-wireless,
	kernel-janitors


> There were several bugs here.
> 
> 1)  The done label was in the wrong place so we didn't copy any
>     information out when there was no command given.
> 
> 2)  We were using PAGE_SIZE as the size of the buffer instead of
>     "PAGE_SIZE - pos".
> 
> 3)  snprintf() returns the number of characters that would have been
>     printed if there were enough space.  If there was not enough space
>     (and we had fixed the memory corruption bug #2) then it would result
>     in an information leak when we do simple_read_from_buffer().  I've
>     changed it to use scnprintf() instead.
> 
> I also removed the initialization at the start of the function, because
> I thought it made the code a little more clear.
> 
> Fixes: 5e6e3a92b9a4 ('wireless: mwifiex: initial commit for Marvell mwifiex driver')
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> Acked-by: Amitkumar Karwar <akarwar@marvell.com>

Thanks, applied to wireless-drivers-next.git.

Kalle Valo

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

end of thread, other threads:[~2015-09-29  8:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-20 10:30 [patch] mwifiex: fix mwifiex_rdeeprom_read() Dan Carpenter
2015-08-25  6:56 ` Amitkumar Karwar
2015-08-25 12:29 ` Kalle Valo
2015-09-21 16:19   ` [patch v2] " Dan Carpenter
2015-09-22 10:58     ` Amitkumar Karwar
2015-09-29  8:09     ` [v2] " Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).