cifsd: fix potential read overflow in ksmbd_vfs_stream_read()
authorNamjae Jeon <namjae.jeon@samsung.com>
Mon, 31 May 2021 08:26:43 +0000 (17:26 +0900)
committerNamjae Jeon <namjae.jeon@samsung.com>
Tue, 1 Jun 2021 00:26:22 +0000 (09:26 +0900)
If *pos or *pos + count is greater than v_len, It will read beyond
the stream_buf buffer. This patch add the check and cut down count with
size of the buffer.

Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/cifsd/vfs.c

index 56b1091473b97ede450a63ad503f684cf3bf6eef..9111b485d6115b4dd67d3ac13f8151e97ffa8b65 100644 (file)
@@ -285,9 +285,19 @@ static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
        if ((int)v_len <= 0)
                return (int)v_len;
 
+       if (v_len <= *pos) {
+               count = -EINVAL;
+               goto free_buf;
+       }
+
+       if (v_len - *pos < count)
+               count = v_len - *pos;
+
        memcpy(buf, &stream_buf[*pos], count);
+
+free_buf:
        kvfree(stream_buf);
-       return v_len > count ? count : v_len;
+       return count;
 }
 
 /**