Skip to content

Commit 4f312c3

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 ce905b6 commit 4f312c3

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);
@@ -356,6 +376,9 @@ static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
356376
size = 0;
357377
}
358378

379+
vma->vm_ops = &dlfb_vm_ops;
380+
vma->vm_private_data = dlfb;
381+
atomic_inc(&dlfb->mmap_count);
359382
return 0;
360383
}
361384

@@ -1219,7 +1242,6 @@ static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
12191242

12201243
/*
12211244
* Assumes &info->lock held by caller
1222-
* Assumes no active clients have framebuffer open
12231245
*/
12241246
static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
12251247
{
@@ -1231,6 +1253,13 @@ static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info
12311253
new_len = PAGE_ALIGN(new_len);
12321254

12331255
if (new_len > old_len) {
1256+
if (atomic_read(&dlfb->mmap_count) > 0) {
1257+
dev_warn(info->dev,
1258+
"refusing realloc: %d active mmaps\n",
1259+
atomic_read(&dlfb->mmap_count));
1260+
return -EBUSY;
1261+
}
1262+
12341263
/*
12351264
* Alloc system memory for virtual framebuffer
12361265
*/

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)