Skip to contentSkip to conversation

iio-utils: fix memory overflow fordynamically allocateded memory to hold filename

PATCH Superseded Barry Song 2 participants 2 messages May 24, 2010

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH v2 1/1 iio-utils: fix memory overflow fordynamically allocateded memory to hold filename

---
 drivers/staging/iio/Documentation/iio_utils.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +2 -1
index a4555e6..6411bf9 100644
@@ -64,7 +64,8 @@ inline int find_type_by_name(const char *name, const char *type)
 						+ strlen(type)
 						+ 1
 						+ numstrlen
-						+ 1);
+						+ 1
+						+ IIO_MAX_NAME_LENGTH);
 				if (filename == NULL)
 					return -ENOMEM;
 				sprintf(filename, "%s%s%d/name",
Signature · 1 line
1.5.6.3
#1 lore raw
Quoted 1 line · Barry Song

Nack - see below.

Quoted 9 lines
---
 drivers/staging/iio/Documentation/iio_utils.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +0 -0
index a4555e6..6411bf9 100644

drivers/staging/iio/Documentation/iio_utils.h inline int find_type_by_name(const char *name, const char *type) · line 68

Quoted 6 lines
drivers/staging/iio/Documentation/iio_utils.h +2 -1
@@ -64,7 +64,8 @@ inline int find_type_by_name(const char *name, const char *type)
 						+ strlen(type)
 						+ 1
 						+ numstrlen
-						+ 1);
+						+ 1
+						+ IIO_MAX_NAME_LENGTH);

The filename in question will be something like
/sys/bus/iio/device0/name

IIO_MAX_NAME_LENGTH refers to the contents of that file, not the name of the file.

So, I agree there is a bug here, the right fix is to make that buffer the length to take the string we write into it in:

sprintf(filename, "%s%s%d/name",

                  iio_dir,
                  type,
                  number);

So, strlen(iio_dir)+strlen(type)+numstrlen + 6;

(the 6 is from 5 for the /name and 1 for the trailing null character).

We could make life easiser and use asprintf to do the allocation at time of usage, though that would make our usespace example non standard c (those functions are a gnu extension according to the man page).

Good spot on the bug. Thanks!

Jonathan

Quoted 3 lines
drivers/staging/iio/Documentation/iio_utils.h +0 -0
 				if (filename == NULL)
 					return -ENOMEM;
 				sprintf(filename, "%s%s%d/name",