Skip to content

Commit b7fd68a

Browse files
Matthew Wilcox (Oracle)mripard
authored andcommitted
drm: Do not overrun array in drm_gem_get_pages()
If the shared memory object is larger than the DRM object that it backs, we can overrun the page array. Limit the number of pages we install from each folio to prevent this. Signed-off-by: "Matthew Wilcox (Oracle)" <[email protected]> Reported-by: Oleksandr Natalenko <[email protected]> Tested-by: Oleksandr Natalenko <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Fixes: 3291e09 ("drm: convert drm_gem_put_pages() to use a folio_batch") Cc: [email protected] # 6.5.x Signed-off-by: Maxime Ripard <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 2b7947b commit b7fd68a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/gpu/drm/drm_gem.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
540540
struct page **pages;
541541
struct folio *folio;
542542
struct folio_batch fbatch;
543-
int i, j, npages;
543+
long i, j, npages;
544544

545545
if (WARN_ON(!obj->filp))
546546
return ERR_PTR(-EINVAL);
@@ -564,11 +564,13 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj)
564564

565565
i = 0;
566566
while (i < npages) {
567+
long nr;
567568
folio = shmem_read_folio_gfp(mapping, i,
568569
mapping_gfp_mask(mapping));
569570
if (IS_ERR(folio))
570571
goto fail;
571-
for (j = 0; j < folio_nr_pages(folio); j++, i++)
572+
nr = min(npages - i, folio_nr_pages(folio));
573+
for (j = 0; j < nr; j++, i++)
572574
pages[i] = folio_file_page(folio, i);
573575

574576
/* Make sure shmem keeps __GFP_DMA32 allocated pages in the

0 commit comments

Comments
 (0)