diff mbox

libmultipath: Don't chop const strings

Message ID 1392222110-13324-1-git-send-email-bmarzins@redhat.com (mailing list archive)
State Accepted, archived
Delegated to: christophe varoqui
Headers show

Commit Message

Benjamin Marzinski Feb. 12, 2014, 4:21 p.m. UTC
multipath was chopping sysfs attr strings in order to make sure that
ending whitespace didn't cause them to overflow their buffer. However
those strings were const strings. This patch makes multipath check how
much of the string length is ending whitespace, so that it can tell if
it will really overflow the buffer without chopping the const string.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 libmultipath/discovery.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Hannes Reinecke Feb. 13, 2014, 7:43 a.m. UTC | #1
On 02/12/2014 05:21 PM, Benjamin Marzinski wrote:
> multipath was chopping sysfs attr strings in order to make sure that
> ending whitespace didn't cause them to overflow their buffer. However
> those strings were const strings. This patch makes multipath check how
> much of the string length is ending whitespace, so that it can tell if
> it will really overflow the buffer without chopping the const string.
> 
> Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
Yeah, you are correct.
I actually have a similar patch, but didn't manage to send it upstream.

So:

Acked-by: Hannes Reinecke <hare@suse.de>

Cheers,

Hannes
Christophe Varoqui Feb. 13, 2014, 9:22 p.m. UTC | #2
Applied,
thanks.

On Wed, Feb 12, 2014 at 5:21 PM, Benjamin Marzinski <bmarzins@redhat.com> wrote:
> multipath was chopping sysfs attr strings in order to make sure that
> ending whitespace didn't cause them to overflow their buffer. However
> those strings were const strings. This patch makes multipath check how
> much of the string length is ending whitespace, so that it can tell if
> it will really overflow the buffer without chopping the const string.
>
> Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
> ---
>  libmultipath/discovery.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/libmultipath/discovery.c b/libmultipath/discovery.c
> index 228ffd3..a6ff658 100644
> --- a/libmultipath/discovery.c
> +++ b/libmultipath/discovery.c
> @@ -4,6 +4,7 @@
>   * Copyright (c) 2005 Mike Anderson
>   */
>  #include <stdio.h>
> +#include <ctype.h>
>  #include <unistd.h>
>  #include <fcntl.h>
>  #include <sys/ioctl.h>
> @@ -143,6 +144,7 @@ path_discovery (vector pathvec, struct config * conf, int flag)
>  extern ssize_t                                                         \
>  sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len) \
>  {                                                                      \
> +       int l;                                                  \
>         const char * attr;                                              \
>         const char * devname;                                           \
>                                                                         \
> @@ -157,12 +159,14 @@ sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)    \
>                         devname, #fname);                               \
>                 return -ENXIO;                                          \
>         }                                                               \
> -       if (strchop(attr) > len) {                                      \
> +       for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--);      \
> +       if (l > len) {                                                  \
>                 condlog(3, "%s: overflow in attribute %s",              \
>                         devname, #fname);                               \
>                 return -EINVAL;                                         \
>         }                                                               \
> -       return strlcpy(buff, attr, len);                                \
> +       strlcpy(buff, attr, len);                                       \
> +       return strchop(buff);                                           \
>  }
>
>  declare_sysfs_get_str(devtype);
> --
> 1.8.4.2
>
> --
> dm-devel mailing list
> dm-devel@redhat.com
> https://www.redhat.com/mailman/listinfo/dm-devel

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel
diff mbox

Patch

diff --git a/libmultipath/discovery.c b/libmultipath/discovery.c
index 228ffd3..a6ff658 100644
--- a/libmultipath/discovery.c
+++ b/libmultipath/discovery.c
@@ -4,6 +4,7 @@ 
  * Copyright (c) 2005 Mike Anderson
  */
 #include <stdio.h>
+#include <ctype.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -143,6 +144,7 @@  path_discovery (vector pathvec, struct config * conf, int flag)
 extern ssize_t								\
 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)	\
 {									\
+	int l;							\
 	const char * attr;						\
 	const char * devname;						\
 									\
@@ -157,12 +159,14 @@  sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)	\
 			devname, #fname);				\
 		return -ENXIO;						\
 	}								\
-	if (strchop(attr) > len) {					\
+	for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--);	\
+	if (l > len) {							\
 		condlog(3, "%s: overflow in attribute %s",		\
 			devname, #fname);				\
 		return -EINVAL;						\
 	}								\
-	return strlcpy(buff, attr, len);				\
+	strlcpy(buff, attr, len);					\
+	return strchop(buff);						\
 }
 
 declare_sysfs_get_str(devtype);