]> git.proxmox.com Git - mirror_qemu.git/commitdiff
file-posix: clean up max_segments buffer termination
authorStefan Hajnoczi <stefanha@redhat.com>
Tue, 14 Mar 2017 09:09:22 +0000 (17:09 +0800)
committerKevin Wolf <kwolf@redhat.com>
Fri, 17 Mar 2017 11:54:06 +0000 (12:54 +0100)
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.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
block/file-posix.c

index c4c06637ef7db72f23f0e5da78d9c2bbf977ac46..ac6bd9fae8e08f4d9429f5db0d1cfc2d49f6b26d 100644 (file)
@@ -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;