media: mc: fix potential use-after-free in media_request_alloc()
Subsystems (3)
PATCH 1/1 media: mc: fix potential use-after-free in media_request_alloc()
Commit 6f504cbf108a ("media: convert media_request_alloc() to FD_PREPARE()") moved the call to fd_install() (now hidden in fd_publish()) before the snprintf(), making the later write to potentially already freed memory, as userland is free to call close() concurrently right after the call to fd_install() which may end up in the request_fops.release() handler freeing 'req'.
--- drivers/media/mc/mc-request.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/mc/mc-request.c +3 -3
@@ -315,12 +315,12 @@ int media_request_alloc(struct media_device *mdev, int *alloc_fd)fd_prepare_file (fdf)->private_data = req; - *alloc_fd =fd_publish (fdf); -snprintf (req->debug_str,sizeof (req->debug_str),"%u:%d" , -atomic_inc_return (&mdev->request_id), *alloc_fd); +atomic_inc_return (&mdev->request_id),fd_prepare_fd (fdf));dev_dbg (mdev->dev,"request: allocated %s\n" , req->debug_str); + *alloc_fd =fd_publish (fdf); +return 0 ; err_free_req:
Signature · 1 line
2.47.3
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 23 lines · Patchwork Integration
Dear Mathias Krause:
Thanks for your patches! Unfortunately the Media CI robot has not been able to test them.
Make sure that the whole series [email protected] is available at lore. And that it can be cherry-picked on top the "next" branch of "https://gitlab.freedesktop.org/linux-media/media-committers.git".
You can try something like this:
git fetch https://gitlab.freedesktop.org/linux-media/media-committers.git next git checkoutFETCH_HEAD
b4 shazam [email protected]Error message:
Trying branch nextf7231cff1f3ff8259bef02dc4999bc132abf29cf...
Running in OFFLINE mode
Analyzing 1 messages in the thread
Checking attestation on all messages, may take a moment...
---
[PATCH] media: mc: fix potential use-after-free inmedia_request_alloc()
+ Link: https://lore.kernel.org/r/[email protected]
---
Quoted 8 lines
Total patches: 1
---
Applying: media: mc: fix potential use-after-free inmedia_request_alloc()Patch failed at 0001 media: mc: fix potential use-after-free inmedia_request_alloc()[...]
error: patch failed:drivers/media/mc/mc-request.c:315
error:drivers/media/mc/mc-request.c: patch does not apply
[...]
Yeah, that's because media-committers.git#next doesn't yet have commit 6f504cbf108a ("media: convert media_request_alloc() to FD_PREPARE()") which was only recently integrated upstream during the merge window.
Maybe Christian should take the fix instead, as it was his pull request[1] that introduced the bug?
Thanks,
Mathias
Quoted 8 lines · Mathias Krause
Commit
6f504cbf108a("media: convertmedia_request_alloc()toFD_PREPARE()") moved the call tofd_install()(now hidden infd_publish()) before thesnprintf(), making the later write to potentially already freed memory, as userland is free to callclose()concurrently right after the call tofd_install()which may end up in therequest_fops.release()handler freeing 'req'.[...]
Thanks for spotting this, Mathias.
---
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/1] media: mc: fix potential use-after-free in media_request_alloc()
https://git.kernel.org/vfs/vfs/c/a260bd22a355
Hi Mathias,
Quoted 9 lines · Mathias Krause
Commit
6f504cbf108a("media: convertmedia_request_alloc()toFD_PREPARE()") moved the call tofd_install()(now hidden infd_publish()) before thesnprintf(), making the later write to potentially already freed memory, as userland is free to callclose()concurrently right after the call tofd_install()which may end up in therequest_fops.release()handler freeing 'req'.Fixes 6f504cbf108a ("media: convert media_request_alloc() to FD_PREPARE()")Signed-off-by Mathias Krause <[email protected]>
Thanks!
Signature · 3 lines
Regards, Sakari Ailus