Skip to contentSkip to conversation

fsdev: Fix overrun after readlink() fills buffer completely

PATCH Applied Markus Armbruster 2 participants 2 messages Feb 21, 2014 – Feb 26, 2014

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsdev: Fix overrun after readlink() fills buffer completely

readlink() returns the number of bytes written to the buffer, and it doesn't write a terminating null byte. do_readlink() writes it itself. Overruns the buffer when readlink() filled it completely.

Fix by reserving space for the null byte when calling readlink(), like we do elsewhere.

---
 fsdev/virtfs-proxy-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fsdev/virtfs-proxy-helper.c +1 -1
index 713a7b2..bfecb87 100644
@@ -595,7 +595,7 @@ static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
     }
     buffer = g_malloc(size);
     v9fs_string_init(&target);
-    retval = readlink(path.data, buffer, size);
+    retval = readlink(path.data, buffer, size - 1);
     if (retval > 0) {
         buffer[retval] = '\0';
         v9fs_string_sprintf(&target, "%s", buffer);
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 8 lines · Markus Armbruster

readlink() returns the number of bytes written to the buffer, and it doesn't write a terminating null byte. do_readlink() writes it itself. Overruns the buffer when readlink() filled it completely.

Fix by reserving space for the null byte when calling readlink(), like we do elsewhere.

applied.

Quoted 19 lines
---
 fsdev/virtfs-proxy-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fsdev/virtfs-proxy-helper.c +1 -1
index 713a7b2..bfecb87 100644
@@ -595,7 +595,7 @@ static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
     }
     buffer = g_malloc(size);
     v9fs_string_init(&target);
-    retval = readlink(path.data, buffer, size);
+    retval = readlink(path.data, buffer, size - 1);
     if (retval > 0) {
         buffer[retval] = '\0';
         v9fs_string_sprintf(&target, "%s", buffer);
Signature · 1 line
1.8.1.4