Skip to content

Commit 66665c8

Browse files
committed
udmabuf: fix racy memfd sealing check
JIRA: https://issues.redhat.com/browse/RHEL-89519 Conflicts: A merge conflict due to missing upstream commit c87a126 ("udmabuf: reuse folio array when pin folios") commit 9cb189a Author: Jann Horn <jannh@google.com> Date: Wed, 4 Dec 2024 17:26:19 +0100 udmabuf: fix racy memfd sealing check The current check_memfd_seals() is racy: Since we first do check_memfd_seals() and then udmabuf_pin_folios() without holding any relevant lock across both, F_SEAL_WRITE can be set in between. This is problematic because we can end up holding pins to pages in a write-sealed memfd. Fix it using the inode lock, that's probably the easiest way. In the future, we might want to consider moving this logic into memfd, especially if anyone else wants to use memfd_pin_folios(). Reported-by: Julian Orth <ju.orth@gmail.com> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219106 Closes: https://lore.kernel.org/r/CAG48ez0w8HrFEZtJkfmkVKFDhE5aP7nz=obrimeTgpD+StkV9w@mail.gmail.com Fixes: fbb0de7 ("Add udmabuf misc device") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn <jannh@google.com> Acked-by: Joel Fernandes (Google) <joel@joelfernandes.org> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20241204-udmabuf-fixes-v2-1-23887289de1c@google.com Signed-off-by: Waiman Long <longman@redhat.com>
1 parent 3c90d8b commit 66665c8

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

drivers/dma-buf/udmabuf.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,19 @@ static long udmabuf_create(struct miscdevice *device,
423423
goto err;
424424
}
425425

426+
/*
427+
* Take the inode lock to protect against concurrent
428+
* memfd_add_seals(), which takes this lock in write mode.
429+
*/
430+
inode_lock_shared(file_inode(memfd));
426431
ret = check_memfd_seals(memfd);
427-
if (ret < 0) {
428-
fput(memfd);
429-
goto err;
430-
}
432+
if (ret)
433+
goto out_unlock;
431434

432435
ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
433436
list[i].size);
437+
out_unlock:
438+
inode_unlock_shared(file_inode(memfd));
434439
fput(memfd);
435440
if (ret)
436441
goto err;

0 commit comments

Comments
 (0)