staging: vme_user: validate slave window size against buffer size
Subsystems (3)
PATCH 1/1 staging: vme_user: validate slave window size against buffer size
The VME_SET_SLAVE ioctl in drivers/staging/vme_user/vme_user.c accepts a user-controlled slave.size and forwards it to vme_slave_set() without comparing it against image[minor].size_buf. The slave-image kernel buffer is allocated at probe time with a fixed size of PCI_BUF_SIZE (0x20000 / 128 KiB), but the configured VME window size can be made much larger via the ioctl.
The subsequent read() / write() handlers (vme_user_read / vme_user_write) clamp the I/O range against vme_get_size() (the configured window size, attacker-controlled) but never consult size_buf. The slave I/O paths buffer_to_user() and buffer_from_user() then index image[minor].kern_buf with *ppos values up to image_size - 1, well beyond the actual allocation.
Result: a local user with read/write access to /dev/bus/vme/s* can trigger out-of-bounds read and write of the kernel slab adjacent to the slave-image buffer.
Fix: reject slave.size > size_buf in the VME_SET_SLAVE handler. Also add defensive bounds checks against size_buf in buffer_to_user() and buffer_from_user() so that the I/O paths cannot exceed the allocation even if a future ioctl path forgets to validate.
--- drivers/staging/vme_user/vme_user.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/staging/vme_user/vme_user.c +18 -1
@@ -156,6 +156,11 @@ static ssize_t buffer_to_user(unsigned int minor, char __user *buf, {void *image_ptr; +if (*ppos <0 || (u64)*ppos >= image[minor].size_buf || + count > image[minor].size_buf - (u64)*ppos) { +pr_warn_ratelimited ("%s: out-of-bounds access\n" , __func__); +return -EINVAL ; + } image_ptr = image[minor].kern_buf + *ppos;if (copy_to_user (buf, image_ptr, (unsigned long )count))return -EFAULT ;
@@ -168,6 +173,11 @@ static ssize_t buffer_from_user(unsigned int minor, const char __user *buf, {void *image_ptr; +if (*ppos <0 || (u64)*ppos >= image[minor].size_buf || + count > image[minor].size_buf - (u64)*ppos) { +pr_warn_ratelimited ("%s: out-of-bounds access\n" , __func__); +return -EINVAL ; + } image_ptr = image[minor].kern_buf + *ppos;if (copy_from_user (image_ptr, buf, (unsigned long )count))return -EFAULT ;
@@ -394,6 +404,14 @@ static int vme_user_ioctl(struct inode *inode, struct file *file,return -EFAULT ; } +/* +* Reject window sizes larger than the kernel buffer +* allocated at probe time, otherwise subsequent +* read/write would access memory beyond kern_buf. +*/ +if (slave.size > image[minor].size_buf) +return -EINVAL ; +/* XXX We do not want to push aspace, cycle and width * to userspace as they are */
@@ -401,7 +419,6 @@ static int vme_user_ioctl(struct inode *inode, struct file *file, slave.enable, slave.vme_addr, slave.size, image[minor].pci_buf, slave.aspace, slave.cycle); -break ; }break ;
Signature · 1 line
2.43.0