Skip to contentSkip to conversation

xf86drm: Bound strstr() to the allocated data

PATCH In review Damien Lespiau 3 participants 4 messages Jan 22, 2016 – Jan 25, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xf86drm: Bound strstr() to the allocated data

We are reading at most sizeof(data) bytes, but then data may not contain a terminating '\0', at least in theory, so strstr() may overflow the stack allocated array.

Make sure that data always contains at least one '\0'.

---
 xf86drm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xf86drm.c +2 -1
index 7e28b4f..5f587d9 100644
@@ -2863,7 +2863,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
 {
 #ifdef __linux__
     char path[PATH_MAX + 1];
-    char data[128];
+    char data[128 + 1];
     char *str;
     int domain, bus, dev, func;
     int fd, ret;
@@ -2874,6 +2874,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
         return -errno;
 
     ret = read(fd, data, sizeof(data));
+    data[128] = '\0';
     close(fd);
     if (ret < 0)
         return -errno;
Signature · 6 lines
2.4.3

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Damien Lespiau

We are reading at most sizeof(data) bytes, but then data may not contain a terminating '\0', at least in theory, so strstr() may overflow the stack allocated array.

Make sure that data always contains at least one '\0'.

---
 xf86drm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xf86drm.c +1 -1
index 7e28b4f..5f587d9 100644
@@ -2863,7 +2863,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
 {
 #ifdef __linux__
     char path[PATH_MAX + 1];
-    char data[128];
+    char data[128 + 1];
     char *str;
     int domain, bus, dev, func;
     int fd, ret;

xf86drm.c static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) · line 2877

Quoted 4 lines
xf86drm.c +1 -0
@@ -2874,6 +2874,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
         return -errno;
 
     ret = read(fd, data, sizeof(data));
+    data[128] = '\0';

Slightly more paranoid would be something along the lines of if (ret >= 0)

	data[ret] = '\0';

But this should be good enough I think so

The other thing I spotted while looking at the code is the fact that it doesn't check the snprint() return value. But I guess PATH_MAX is big enough that even if you somehow make maj and min INT_MIN it'll still fit.

Quoted 10 lines
xf86drm.c +0 -0
     close(fd);
     if (ret < 0)
         return -errno;
Signature · 6 lines
2.4.3

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 33 lines · Ville Syrjälä
Quoted 25 lines · Damien Lespiau

We are reading at most sizeof(data) bytes, but then data may not contain a terminating '\0', at least in theory, so strstr() may overflow the stack allocated array.

Make sure that data always contains at least one '\0'.

---
 xf86drm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xf86drm.c +1 -1
index 7e28b4f..5f587d9 100644
@@ -2863,7 +2863,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
 {
 #ifdef __linux__
     char path[PATH_MAX + 1];
-    char data[128];
+    char data[128 + 1];
     char *str;
     int domain, bus, dev, func;
     int fd, ret;

xf86drm.c static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) · line 2877

Quoted 4 lines
xf86drm.c +1 -0
@@ -2874,6 +2874,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
         return -errno;
 
     ret = read(fd, data, sizeof(data));
+    data[128] = '\0';

Slightly more paranoid would be something along the lines of if (ret >= 0)

Quoted 4 lines
	data[ret] = '\0';

But this should be good enough I think so

Thanks for the review, pushed!

Quoted 4 lines

The other thing I spotted while looking at the code is the fact that it doesn't check the snprint() return value. But I guess PATH_MAX is big enough that even if you somehow make maj and min INT_MIN it'll still fit.

Right, doesn't seem we can overflow path[].

Signature · 5 lines
Damien
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw
In reply to Ville Syrjälä
Quoted 28 lines · Ville Syrjälä
Quoted 27 lines · Damien Lespiau

We are reading at most sizeof(data) bytes, but then data may not contain a terminating '\0', at least in theory, so strstr() may overflow the stack allocated array.

Make sure that data always contains at least one '\0'.

---
  xf86drm.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
xf86drm.c +1 -1
index 7e28b4f..5f587d9 100644
@@ -2863,7 +2863,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
  {
  #ifdef __linux__
      char path[PATH_MAX + 1];
-    char data[128];
+    char data[128 + 1];
      char *str;
      int domain, bus, dev, func;
      int fd, ret;
@@ -2874,6 +2874,7 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
          return -errno;
 
Quoted 6 lines

xf86drm.c static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) · line 2877

Quoted 2 lines
xf86drm.c +1 -0
      ret = read(fd, data, sizeof(data));
+    data[128] = '\0';

Slightly more paranoid would be something along the lines of if (ret >= 0)

	data[ret] = '\0';

Except that this could now be out-of-bounds :(
I think the read() should be changed to not fill the newly-enlarged array completely:

	char data[N+1]
	ret = read(fd, data, N);
	if (ret >= 0)
		data[ret] = '\0';

so that in the last line, ret <= N and therefore can't overflow.

But writing the NUL at the constant offset ("data[N] = '\0';") was OK too, it just means that if the data is short and not NUL-terminated it will be treated as having random bytes appended, up to the guaranteed NUL. Since the input could actually have contained those random bytes, putting the NUL at the very end of the buffer doesn't make things worse.

.Dave.

Quoted 19 lines

But this should be good enough I think so

The other thing I spotted while looking at the code is the fact that it doesn't check the snprint() return value. But I guess PATH_MAX is big enough that even if you somehow make maj and min INT_MIN it'll still fit.

Quoted 10 lines
xf86drm.c +0 -1
      close(fd);
      if (ret < 0)
          return -errno;
--

2.4.3

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx