Skip to contentSkip to conversation

Patch to fix null termination of string buffer

PATCH In review [email protected] 2 participants 4 messages Dec 13, 2016 – Dec 14, 2016

lore
Share & tools
lore
#0 lore raw

This is my first time to sending a patch to the mailing list. So, I'm sorry if I did something wrong.

The function drmParsePciBusInfo() in xf86drm.c reads the contents of the file "/sys/dev/char/x:y/device/uevent" into the buffer.
The string written to the buffer by read() is not null-terminated, but this function is writing null only at the end of the buffer.
As a result, the string passed to sscanf() contains an uninitialized value and sscanf uses it.

For example,
The string that should be passed to sscanf().
"The contents of the file\0"

The string actually passed to sscanf().
"The contents of the file and uninitialized value until the end of the buffer\0"
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw

PATCH 1/1 xf86drm: fix null termination of string buffer

From: Taro Yamada <[email protected]>

The string written to the buffer by read() is not null-terminated,
but currently drmParsePciBusInfo() places null character only at the end of the buffer, not at the end of the string.
As a result, the string passed to sscanf() contains an uninitialized value.

This patch changes to places null character at the end of the string.

---
 xf86drm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drm.c +2 -2
index b5eeeb0..a59cfd0 100644
@@ -2925,11 +2925,11 @@ static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
     if (fd < 0)
         return -errno;
 
-    ret = read(fd, data, sizeof(data));
-    data[sizeof(data)-1] = '\0';
+    ret = read(fd, data, sizeof(data)-1);
     close(fd);
     if (ret < 0)
         return -errno;
+    data[ret] = '\0';
 
 #define TAG "PCI_SLOT_NAME="
     str = strstr(data, TAG);
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH libdrm] xf86drm: fix null termination of string buffer

Hi Taro Yamada,

Quoted 9 lines · On 13 December 2016 at 11:18, <[email protected]> wrote:

From: Taro Yamada <[email protected]>

The string written to the buffer by read() is not null-terminated,
but currently drmParsePciBusInfo() places null character only at the end of the buffer, not at the end of the string.
As a result, the string passed to sscanf() contains an uninitialized value.

This patch changes to places null character at the end of the string.

Did you see this causing issues in practise ?

We use a combination of strstr to strip any unwanted starting data, and sscanf which [should] trim any trailing garbage. That aside, your patch provides an extra bit of robustness which is always nice ^_^

Barring any objections I'll commit this in a few days.

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Re: [PATCH libdrm] xf86drm: fix null termination of string buffer

Quoted 6 lines · Emil Velikov

Did you see this causing issues in practise ?

We use a combination of strstr to strip any unwanted starting data, and sscanf which [should] trim any trailing garbage. That aside, your patch provides an extra bit of robustness which is always nice ^_^

No, I've not encountering any problem in practice. I completely agree with you.

Quoted 1 line

Barring any objections I'll commit this in a few days.

Thank you for your prompt reply!

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel