Skip to content

Commit a2c53a3

Browse files
Rajat Guptagregkh
authored andcommitted
fbdev: udlfb: add vm_ops to dlfb_ops_mmap to prevent use-after-free
commit 8de779d upstream. dlfb_ops_mmap() uses remap_pfn_range() to map vmalloc framebuffer pages to userspace but sets no vm_ops on the VMA. This means the kernel cannot track active mmaps. When dlfb_realloc_framebuffer() replaces the backing buffer via FBIOPUT_VSCREENINFO, existing mmap PTEs are not invalidated. On USB disconnect, dlfb_ops_destroy() calls vfree() on the old pages while userspace PTEs still reference them, resulting in a use-after-free: the process retains read/write access to freed kernel pages. Add vm_operations_struct with open/close callbacks that maintain an atomic mmap_count on struct dlfb_data. In dlfb_realloc_framebuffer(), check mmap_count and return -EBUSY if the buffer is currently mapped, preventing buffer replacement while userspace holds stale PTEs. Tested with PoC using dummy_hcd + raw_gadget USB device emulation. Signed-off-by: Rajat Gupta <rajgupt@qti.qualcomm.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: stable@vger.kernel.org Signed-off-by: Helge Deller <deller@gmx.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ba60140 commit a2c53a3

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

drivers/video/fbdev/udlfb.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,32 @@ static int dlfb_set_video_mode(struct dlfb_data *dlfb,
321321
return retval;
322322
}
323323

324+
static void dlfb_vm_open(struct vm_area_struct *vma)
325+
{
326+
struct dlfb_data *dlfb = vma->vm_private_data;
327+
328+
atomic_inc(&dlfb->mmap_count);
329+
}
330+
331+
static void dlfb_vm_close(struct vm_area_struct *vma)
332+
{
333+
struct dlfb_data *dlfb = vma->vm_private_data;
334+
335+
atomic_dec(&dlfb->mmap_count);
336+
}
337+
338+
static const struct vm_operations_struct dlfb_vm_ops = {
339+
.open = dlfb_vm_open,
340+
.close = dlfb_vm_close,
341+
};
342+
324343
static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
325344
{
326345
unsigned long start = vma->vm_start;
327346
unsigned long size = vma->vm_end - vma->vm_start;
328347
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
329348
unsigned long page, pos;
349+
struct dlfb_data *dlfb = info->par;
330350

331351
if (info->fbdefio)
332352
return fb_deferred_io_mmap(info, vma);
@@ -358,6 +378,9 @@ static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
358378
size = 0;
359379
}
360380

381+
vma->vm_ops = &dlfb_vm_ops;
382+
vma->vm_private_data = dlfb;
383+
atomic_inc(&dlfb->mmap_count);
361384
return 0;
362385
}
363386

@@ -1176,7 +1199,6 @@ static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
11761199

11771200
/*
11781201
* Assumes &info->lock held by caller
1179-
* Assumes no active clients have framebuffer open
11801202
*/
11811203
static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
11821204
{
@@ -1188,6 +1210,13 @@ static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info
11881210
new_len = PAGE_ALIGN(new_len);
11891211

11901212
if (new_len > old_len) {
1213+
if (atomic_read(&dlfb->mmap_count) > 0) {
1214+
dev_warn(info->dev,
1215+
"refusing realloc: %d active mmaps\n",
1216+
atomic_read(&dlfb->mmap_count));
1217+
return -EBUSY;
1218+
}
1219+
11911220
/*
11921221
* Alloc system memory for virtual framebuffer
11931222
*/

include/video/udlfb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct dlfb_data {
5656
spinlock_t damage_lock;
5757
struct work_struct damage_work;
5858
struct fb_ops ops;
59+
atomic_t mmap_count;
5960
/* blit-only rendering path metrics, exposed through sysfs */
6061
atomic_t bytes_rendered; /* raw pixel-bytes driver asked to render */
6162
atomic_t bytes_identical; /* saved effort with backbuffer comparison */

0 commit comments

Comments
 (0)