[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 1/8] file-posix: clean up max_segments buffer termina
From: |
Kevin Wolf |
Subject: |
[Qemu-block] [PULL 1/8] file-posix: clean up max_segments buffer termination |
Date: |
Fri, 17 Mar 2017 14:15:39 +0100 |
From: Stefan Hajnoczi <address@hidden>
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 <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/file-posix.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index c4c0663..ac6bd9f 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -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;
--
1.8.3.1
- [Qemu-block] [PULL 0/8] Block layer fixes for 2.9.0-rc1, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 1/8] file-posix: clean up max_segments buffer termination,
Kevin Wolf <=
- [Qemu-block] [PULL 2/8] replication: clarify permissions, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 5/8] blockdev: fix bitmap clear undo, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 6/8] block: Propagate error in bdrv_open_backing_file, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 3/8] file-posix: Don't leak fd in hdev_get_max_segments, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 7/8] thread-pool: add missing qemu_bh_cancel in completion function, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 4/8] block: Always call bdrv_child_check_perm first, Kevin Wolf, 2017/03/17
- [Qemu-block] [PULL 8/8] block: quiesce AioContext when detaching from it, Kevin Wolf, 2017/03/17
- Re: [Qemu-block] [Qemu-devel] [PULL 0/8] Block layer fixes for 2.9.0-rc1, Peter Maydell, 2017/03/17