All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: prevent stack buffer overflow in host_reset
@ 2012-11-15 20:51 Sasha Levin
  2012-11-16  9:35 ` Rolf Eike Beer
  2012-11-30  9:07 ` James Bottomley
  0 siblings, 2 replies; 3+ messages in thread
From: Sasha Levin @ 2012-11-15 20:51 UTC (permalink / raw)
  To: JBottomley; +Cc: linux-scsi, linux-kernel, Sasha Levin

store_host_reset() has tried to re-invent the wheel to compare sysfs strings.
Unfortunately it did so poorly and never bothered to check the input from
userspace before overwriting stack with it, so something simple as:

echo "WoopsieWoopsie" >
/sys/devices/pseudo_0/adapter0/host0/scsi_host/host0/host_reset

would result in:

[  316.310101] Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: ffffffff81f5bac7
[  316.310101]
[  316.320051] Pid: 6655, comm: sh Tainted: G        W    3.7.0-rc5-next-20121114-sasha-00016-g5c9d68d-dirty #129
[  316.320051] Call Trace:
[  316.340058] pps pps0: PPS event at 1352918752.620355751
[  316.340062] pps pps0: capture assert seq #303
[  316.320051]  [<ffffffff83b3856b>] panic+0xcd/0x1f4
[  316.320051]  [<ffffffff81f5bac7>] ? store_host_reset+0xd7/0x100
[  316.320051]  [<ffffffff8110b996>] __stack_chk_fail+0x16/0x20
[  316.320051]  [<ffffffff81f5bac7>] store_host_reset+0xd7/0x100
[  316.320051]  [<ffffffff81e55bb3>] dev_attr_store+0x13/0x30
[  316.320051]  [<ffffffff812f7db1>] sysfs_write_file+0x101/0x170
[  316.320051]  [<ffffffff8127acc8>] vfs_write+0xb8/0x180
[  316.320051]  [<ffffffff8127ae80>] sys_write+0x50/0xa0
[  316.320051]  [<ffffffff83c03418>] tracesys+0xe1/0xe6

Fix this by uninventing whatever was going on there and just use sysfs_streq.

Bug introduced by 29443691 ("[SCSI] scsi: Added support for adapter and
firmware reset").

Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
---
 drivers/scsi/scsi_sysfs.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index ce5224c..77ba946 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -249,9 +249,9 @@ static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL)
 
 static int check_reset_type(char *str)
 {
-	if (strncmp(str, "adapter", 10) == 0)
+	if (sysfs_streq(str, "adapter"))
 		return SCSI_ADAPTER_RESET;
-	else if (strncmp(str, "firmware", 10) == 0)
+	else if (sysfs_streq(str, "firmware"))
 		return SCSI_FIRMWARE_RESET;
 	else
 		return 0;
@@ -264,12 +264,9 @@ store_host_reset(struct device *dev, struct device_attribute *attr,
 	struct Scsi_Host *shost = class_to_shost(dev);
 	struct scsi_host_template *sht = shost->hostt;
 	int ret = -EINVAL;
-	char str[10];
 	int type;
 
-	sscanf(buf, "%s", str);
-	type = check_reset_type(str);
-
+	type = check_reset_type(buf);
 	if (!type)
 		goto exit_store_host_reset;
 
-- 
1.8.0


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

* Re: [PATCH] scsi: prevent stack buffer overflow in host_reset
  2012-11-15 20:51 [PATCH] scsi: prevent stack buffer overflow in host_reset Sasha Levin
@ 2012-11-16  9:35 ` Rolf Eike Beer
  2012-11-30  9:07 ` James Bottomley
  1 sibling, 0 replies; 3+ messages in thread
From: Rolf Eike Beer @ 2012-11-16  9:35 UTC (permalink / raw)
  To: Sasha Levin; +Cc: JBottomley, linux-scsi, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]

Am Donnerstag 15 November 2012, 15:51:46 schrieb Sasha Levin:
> store_host_reset() has tried to re-invent the wheel to compare sysfs
> strings. Unfortunately it did so poorly and never bothered to check the
> input from userspace before overwriting stack with it, so something simple
> as:
> 
> echo "WoopsieWoopsie" >
> /sys/devices/pseudo_0/adapter0/host0/scsi_host/host0/host_reset
> 
> would result in:
> 
> [  316.310101] Kernel panic - not syncing: stack-protector: Kernel stack is
> corrupted in: ffffffff81f5bac7 [  316.310101]
> [  316.320051] Pid: 6655, comm: sh Tainted: G        W   
> 3.7.0-rc5-next-20121114-sasha-00016-g5c9d68d-dirty #129 [  316.320051] Call
> Trace:
> [  316.340058] pps pps0: PPS event at 1352918752.620355751
> [  316.340062] pps pps0: capture assert seq #303
> [  316.320051]  [<ffffffff83b3856b>] panic+0xcd/0x1f4
> [  316.320051]  [<ffffffff81f5bac7>] ? store_host_reset+0xd7/0x100
> [  316.320051]  [<ffffffff8110b996>] __stack_chk_fail+0x16/0x20
> [  316.320051]  [<ffffffff81f5bac7>] store_host_reset+0xd7/0x100
> [  316.320051]  [<ffffffff81e55bb3>] dev_attr_store+0x13/0x30
> [  316.320051]  [<ffffffff812f7db1>] sysfs_write_file+0x101/0x170
> [  316.320051]  [<ffffffff8127acc8>] vfs_write+0xb8/0x180
> [  316.320051]  [<ffffffff8127ae80>] sys_write+0x50/0xa0
> [  316.320051]  [<ffffffff83c03418>] tracesys+0xe1/0xe6
> 
> Fix this by uninventing whatever was going on there and just use
> sysfs_streq.
> 
> Bug introduced by 29443691 ("[SCSI] scsi: Added support for adapter and
> firmware reset").
> 
> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>

That revision is in 3.2 and all following, so I think this needs to go into 
stable, too.

Eike

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: [PATCH] scsi: prevent stack buffer overflow in host_reset
  2012-11-15 20:51 [PATCH] scsi: prevent stack buffer overflow in host_reset Sasha Levin
  2012-11-16  9:35 ` Rolf Eike Beer
@ 2012-11-30  9:07 ` James Bottomley
  1 sibling, 0 replies; 3+ messages in thread
From: James Bottomley @ 2012-11-30  9:07 UTC (permalink / raw)
  To: Sasha Levin; +Cc: linux-scsi, linux-kernel

On Thu, 2012-11-15 at 15:51 -0500, Sasha Levin wrote:
> diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
> index ce5224c..77ba946 100644
> --- a/drivers/scsi/scsi_sysfs.c
> +++ b/drivers/scsi/scsi_sysfs.c
> @@ -249,9 +249,9 @@ static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL)
>  
>  static int check_reset_type(char *str)
>  {
> -	if (strncmp(str, "adapter", 10) == 0)
> +	if (sysfs_streq(str, "adapter"))
>  		return SCSI_ADAPTER_RESET;
> -	else if (strncmp(str, "firmware", 10) == 0)
> +	else if (sysfs_streq(str, "firmware"))
>  		return SCSI_FIRMWARE_RESET;
>  	else
>  		return 0;
> @@ -264,12 +264,9 @@ store_host_reset(struct device *dev, struct device_attribute *attr,
>  	struct Scsi_Host *shost = class_to_shost(dev);
>  	struct scsi_host_template *sht = shost->hostt;
>  	int ret = -EINVAL;
> -	char str[10];
>  	int type;
>  
> -	sscanf(buf, "%s", str);
> -	type = check_reset_type(str);
> -
> +	type = check_reset_type(buf);
>  	if (!type)
>  		goto exit_store_host_reset;

You didn't compile test this:

  CC [M]  drivers/scsi/scsi_sysfs.o
drivers/scsi/scsi_sysfs.c: In function ‘store_host_reset’:
drivers/scsi/scsi_sysfs.c:269:2: warning: passing argument 1 of ‘check_reset_type’ discards ‘const’ qualifier from pointer target type [enabled by default]
drivers/scsi/scsi_sysfs.c:250:12: note: expected ‘char *’ but argument is of type ‘const char *’

It needs a const in check_reset_type().

I've added it.

James




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

end of thread, other threads:[~2012-11-30  9:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-15 20:51 [PATCH] scsi: prevent stack buffer overflow in host_reset Sasha Levin
2012-11-16  9:35 ` Rolf Eike Beer
2012-11-30  9:07 ` James Bottomley

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.