All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] enclosure: fix oops while iterating enclosure_status array
@ 2009-11-20  0:23 Jeff Mahoney
  2009-11-20  3:33 ` James Bottomley
  2009-11-20 21:43 ` James Bottomley
  0 siblings, 2 replies; 4+ messages in thread
From: Jeff Mahoney @ 2009-11-20  0:23 UTC (permalink / raw)
  To: James Bottomley, Linux Kernel Mailing List, Linus Torvalds

 enclosure_status is expected to be a NULL terminated array of strings
 but isn't actually NULL terminated. When writing an invalid value to
 /sys/class/enclosure/.../.../status, it goes off the end of the array
 and Oopses.

 This patch uses the array size instead.

Reported-by: Artur Wojcik <artur.wojcik@intel.com>
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
---
 drivers/misc/enclosure.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

--- a/drivers/misc/enclosure.c
+++ b/drivers/misc/enclosure.c
@@ -412,8 +412,9 @@ static ssize_t set_component_status(stru
 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
 	int i;
 
-	for (i = 0; enclosure_status[i]; i++) {
-		if (strncmp(buf, enclosure_status[i],
+	for (i = 0; i < ARRAY_SIZE(enclosure_status); i++) {
+		if (enclosure_status[i] &&
+		    strncmp(buf, enclosure_status[i],
 			    strlen(enclosure_status[i])) == 0 &&
 		    (buf[strlen(enclosure_status[i])] == '\n' ||
 		     buf[strlen(enclosure_status[i])] == '\0'))
-- 
Jeff Mahoney

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

* Re: [PATCH] enclosure: fix oops while iterating enclosure_status array
  2009-11-20  0:23 [PATCH] enclosure: fix oops while iterating enclosure_status array Jeff Mahoney
@ 2009-11-20  3:33 ` James Bottomley
  2009-11-20 21:43 ` James Bottomley
  1 sibling, 0 replies; 4+ messages in thread
From: James Bottomley @ 2009-11-20  3:33 UTC (permalink / raw)
  To: Jeff Mahoney; +Cc: Linux Kernel Mailing List, Linus Torvalds

Adding linux-scsi to the cc list.

Patch looks fine to me, though, thanks.

James

On Thu, 2009-11-19 at 19:23 -0500, Jeff Mahoney wrote:
> enclosure_status is expected to be a NULL terminated array of strings
>  but isn't actually NULL terminated. When writing an invalid value to
>  /sys/class/enclosure/.../.../status, it goes off the end of the array
>  and Oopses.
> 
>  This patch uses the array size instead.
> 
> Reported-by: Artur Wojcik <artur.wojcik@intel.com>
> Signed-off-by: Jeff Mahoney <jeffm@suse.com>
> ---
>  drivers/misc/enclosure.c |    5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> --- a/drivers/misc/enclosure.c
> +++ b/drivers/misc/enclosure.c
> @@ -412,8 +412,9 @@ static ssize_t set_component_status(stru
>  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
>  	int i;
>  
> -	for (i = 0; enclosure_status[i]; i++) {
> -		if (strncmp(buf, enclosure_status[i],
> +	for (i = 0; i < ARRAY_SIZE(enclosure_status); i++) {
> +		if (enclosure_status[i] &&
> +		    strncmp(buf, enclosure_status[i],
>  			    strlen(enclosure_status[i])) == 0 &&
>  		    (buf[strlen(enclosure_status[i])] == '\n' ||
>  		     buf[strlen(enclosure_status[i])] == '\0'))



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

* Re: [PATCH] enclosure: fix oops while iterating enclosure_status array
  2009-11-20  0:23 [PATCH] enclosure: fix oops while iterating enclosure_status array Jeff Mahoney
  2009-11-20  3:33 ` James Bottomley
@ 2009-11-20 21:43 ` James Bottomley
  2009-11-26 15:50   ` James Bottomley
  1 sibling, 1 reply; 4+ messages in thread
From: James Bottomley @ 2009-11-20 21:43 UTC (permalink / raw)
  To: Jeff Mahoney; +Cc: Linux Kernel Mailing List, Linus Torvalds, linux-scsi

*really* (promise) adding linux-scsi to the cc list this time.

Patch looks fine to me, though, thanks.

James

On Thu, 2009-11-19 at 19:23 -0500, Jeff Mahoney wrote:
> enclosure_status is expected to be a NULL terminated array of strings
>  but isn't actually NULL terminated. When writing an invalid value to
>  /sys/class/enclosure/.../.../status, it goes off the end of the array
>  and Oopses.
> 
>  This patch uses the array size instead.
> 
> Reported-by: Artur Wojcik <artur.wojcik@intel.com>
> Signed-off-by: Jeff Mahoney <jeffm@suse.com>
> ---
>  drivers/misc/enclosure.c |    5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> --- a/drivers/misc/enclosure.c
> +++ b/drivers/misc/enclosure.c
> @@ -412,8 +412,9 @@ static ssize_t set_component_status(stru
>  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
>  	int i;
>  
> -	for (i = 0; enclosure_status[i]; i++) {
> -		if (strncmp(buf, enclosure_status[i],
> +	for (i = 0; i < ARRAY_SIZE(enclosure_status); i++) {
> +		if (enclosure_status[i] &&
> +		    strncmp(buf, enclosure_status[i],
>  			    strlen(enclosure_status[i])) == 0 &&
>  		    (buf[strlen(enclosure_status[i])] == '\n' ||
>  		     buf[strlen(enclosure_status[i])] == '\0'))




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

* Re: [PATCH] enclosure: fix oops while iterating enclosure_status array
  2009-11-20 21:43 ` James Bottomley
@ 2009-11-26 15:50   ` James Bottomley
  0 siblings, 0 replies; 4+ messages in thread
From: James Bottomley @ 2009-11-26 15:50 UTC (permalink / raw)
  To: Jeff Mahoney; +Cc: Linux Kernel Mailing List, Linus Torvalds, linux-scsi

On Fri, 2009-11-20 at 16:43 -0500, James Bottomley wrote:
> *really* (promise) adding linux-scsi to the cc list this time.
> 
> Patch looks fine to me, though, thanks.
> 
> James
> 
> On Thu, 2009-11-19 at 19:23 -0500, Jeff Mahoney wrote:
> > enclosure_status is expected to be a NULL terminated array of strings
> >  but isn't actually NULL terminated. When writing an invalid value to
> >  /sys/class/enclosure/.../.../status, it goes off the end of the array
> >  and Oopses.
> > 
> >  This patch uses the array size instead.
> > 
> > Reported-by: Artur Wojcik <artur.wojcik@intel.com>
> > Signed-off-by: Jeff Mahoney <jeffm@suse.com>
> > ---
> >  drivers/misc/enclosure.c |    5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > --- a/drivers/misc/enclosure.c
> > +++ b/drivers/misc/enclosure.c
> > @@ -412,8 +412,9 @@ static ssize_t set_component_status(stru
> >  	struct enclosure_component *ecomp = to_enclosure_component(cdev);
> >  	int i;
> >  
> > -	for (i = 0; enclosure_status[i]; i++) {
> > -		if (strncmp(buf, enclosure_status[i],
> > +	for (i = 0; i < ARRAY_SIZE(enclosure_status); i++) {
> > +		if (enclosure_status[i] &&
> > +		    strncmp(buf, enclosure_status[i],
> >  			    strlen(enclosure_status[i])) == 0 &&
> >  		    (buf[strlen(enclosure_status[i])] == '\n' ||
> >  		     buf[strlen(enclosure_status[i])] == '\0'))

Actually, it's not fine ... it's giving this error:

drivers/misc/enclosure.c: In function 'set_component_status':
drivers/misc/enclosure.c:453: warning: array subscript is above array bounds

It's another place where there's an assumption that the array is zero
terminated.

I think the actual best fix is simply to make the assumption true, like
the patch below.

James

---

diff --git a/drivers/misc/enclosure.c b/drivers/misc/enclosure.c
index e9eae4a..1eac626 100644
--- a/drivers/misc/enclosure.c
+++ b/drivers/misc/enclosure.c
@@ -391,6 +391,7 @@ static const char *const enclosure_status [] = {
 	[ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
 	[ENCLOSURE_STATUS_UNKNOWN] = "unknown",
 	[ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
+	[ENCLOSURE_STATUS_MAX] = NULL,
 };
 
 static const char *const enclosure_type [] = {
diff --git a/include/linux/enclosure.h b/include/linux/enclosure.h
index 90d1c21..9a33c5f 100644
--- a/include/linux/enclosure.h
+++ b/include/linux/enclosure.h
@@ -42,6 +42,8 @@ enum enclosure_status {
 	ENCLOSURE_STATUS_NOT_INSTALLED,
 	ENCLOSURE_STATUS_UNKNOWN,
 	ENCLOSURE_STATUS_UNAVAILABLE,
+	/* last element for counting purposes */
+	ENCLOSURE_STATUS_MAX
 };
 
 /* SFF-8485 activity light settings */



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

end of thread, other threads:[~2009-11-26 15:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-20  0:23 [PATCH] enclosure: fix oops while iterating enclosure_status array Jeff Mahoney
2009-11-20  3:33 ` James Bottomley
2009-11-20 21:43 ` James Bottomley
2009-11-26 15:50   ` 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.