Skip to contentSkip to conversation

file-posix: clean up max_segments buffer termination

PATCH Applied Stefan Hajnoczi 3 participants 3 messages Mar 14, 2017 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 file-posix: clean up max_segments buffer termination

The following pattern is unsafe:

  char buf[32];
  ret = read(fd, buf, sizeof(buf));
  ...
  buf[ret] = 0;

If read(2) returns 32 then a byte beyond the end of the buffer is zeroed.

In practice this buffer overflow does not occur because the sysfs max_segments file only contains an unsigned short + '\n'. The string is always shorter than 32 bytes.

Regardless, avoid this pattern because static analysis tools might complain and it could lead to real buffer overflows if copy-pasted elsewhere in the codebase.

---
 block/file-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/file-posix.c +1 -1
index c4c0663..ac6bd9f 100644
@@ -686,7 +686,7 @@ static int hdev_get_max_segments(const struct stat *st)
         goto out;
     }
     do {
-        ret = read(fd, buf, sizeof(buf));
+        ret = read(fd, buf, sizeof(buf) - 1);
     } while (ret == -1 && errno == EINTR);
     if (ret < 0) {
         ret = -errno;
Signature · 1 line
2.9.3
Applied
#1 lore raw

Am 14.03.2017 um 10:09 hat Stefan Hajnoczi geschrieben:

Quoted 14 lines

The following pattern is unsafe:

  char buf[32];
  ret = read(fd, buf, sizeof(buf));
  ...
  buf[ret] = 0;

If read(2) returns 32 then a byte beyond the end of the buffer is zeroed.

In practice this buffer overflow does not occur because the sysfs max_segments file only contains an unsigned short + '\n'. The string is always shorter than 32 bytes.

Quoted 5 lines

Regardless, avoid this pattern because static analysis tools might complain and it could lead to real buffer overflows if copy-pasted elsewhere in the codebase.

Oops. I should have found this during review. Thanks for catching it.

Thanks, applied to the block branch.

Kevin

#2 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

The following pattern is unsafe:

  char buf[32];
  ret = read(fd, buf, sizeof(buf));
  ...
  buf[ret] = 0;

If read(2) returns 32 then a byte beyond the end of the buffer is zeroed.

Quoted 7 lines

In practice this buffer overflow does not occur because the sysfs max_segments file only contains an unsigned short + '\n'. The string is always shorter than 32 bytes.

Regardless, avoid this pattern because static analysis tools might complain and it could lead to real buffer overflows if copy-pasted elsewhere in the codebase.

Yes that's a good point, thanks!

Fam

Quoted 22 lines
---
 block/file-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/file-posix.c +1 -1
index c4c0663..ac6bd9f 100644
@@ -686,7 +686,7 @@ static int hdev_get_max_segments(const struct stat *st)
         goto out;
     }
     do {
-        ret = read(fd, buf, sizeof(buf));
+        ret = read(fd, buf, sizeof(buf) - 1);
     } while (ret == -1 && errno == EINTR);
     if (ret < 0) {
         ret = -errno;
Signature · 1 line
2.9.3