drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
Subsystems (2)
Joonas Lahtinen
maintainerPATCH v3 1/1 drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
Do not pick a VMA with non-matching VM (ppGTT) on quick path of BO handle lookup for a given EXECBUF call. VMA from wrong VM could be picked if same BO is repeatedly used in EXECBUF calls on same context with alternating VMs (ppGTTs). However due to the introduction of proto-ctx that should not be possible since d4433c7600f7 ("drm/i915/gem: Use the proto-context to handle
create parameters (v5)").
Also avoids returning a VMA without increasing the refcount, which may potentially lead to UAF since f7ce8639f6ff ("drm/i915/gem:
Split the context's obj:vma lut into its own mutex") and until d4433c7600f7 ("drm/i915/gem: Use the proto-context to handle
create parameters (v5)").
Sima's analysis:
This check was added in f7ce8639f6ff ("drm/i915/gem: Split the context's obj:vma lut into its own mutex") but without any hint in the commit message as to why. In another hunk of that commit there's a hint though in __eb_add_lut:
/* user racing with ctx set-vm */
This would mean that this bug was introduced in e0695db7298e ("drm/i915:
Create/destroy VM (ppGTT) for use with contexts"), which allowed to change the gem_ctx->vm at runtime, opening up the race that was partially fixed in the earlier referenced commit about a year later.
But it cannot be exploited anymore in anything remotely recent because with the introduction of proto-contexts we've made gem_ctx->vm invariant again, exactly to preemptively close all these potential issues. Specifically d4433c7600f7 ("drm/i915/gem: Use the proto-context to handle create parameters (v5)") is the vm specific part of the proto-context work.
Despite that this is impossible to exploit I think it's still good to fix, but I think for paranoia's sake we should put a WARN_ON_ONCE(vma->vm != vm) in there, since this really should be impossible.
I don't think there's a harm in backporting this though, since there's a 2 year window between the introduction of the ctx->vm change and it's complete fix with the proto-ctx work between 2019 and 2021. It's not realistic to backport the latter and this here is trivial in case anyone is foolish enough to run such an old kernel.
v3:
- Include Sima's analysis and WARN_ON_ONCE
References: https://lore.kernel.org/all/[email protected]/
--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +5 -1
@@ -895,8 +895,12 @@ static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle)rcu_read_lock (); vma =radix_tree_lookup (&eb->gem_context->handles_vma, handle); -if (likely (vma && vma->vm == vm)) +if (likely (vma && vma->vm == vm)) { vma =i915_vma_tryget (vma); + }else { +WARN_ON_ONCE (vma && vma->vm != vm); + vma =NULL ; + }rcu_read_unlock ();if (likely (vma))return vma;
Signature · 1 line
2.53.0
Quoted 8 lines · Joonas Lahtinen
Do not pick a VMA with non-matching VM (ppGTT) on quick path of BO handle lookup for a given EXECBUF call. VMA from wrong VM could be picked if same BO is repeatedly used in EXECBUF calls on same context with alternating VMs (ppGTTs). However due to the introduction of proto-ctx that should not be possible since
d4433c7600f7("drm/i915/gem: Use the proto-context to handle
create parameters (v5)").
Quoted 5 lines
Also avoids returning a VMA without increasing the refcount, which may potentially lead to UAF since
f7ce8639f6ff("drm/i915/gem:
Split the context's obj:vma lut into its own mutex") and untild4433c7600f7("drm/i915/gem: Use the proto-context to handle
create parameters (v5)").
Also, much more succinct summary above than my babbling below :-)
Cheers, Sima
Quoted 69 lines
Sima's analysis:
This check was added in
f7ce8639f6ff("drm/i915/gem: Split the context's obj:vma lut into its own mutex") but without any hint in the commit message as to why. In another hunk of that commit there's a hint though in__eb_add_lut:/* user racing with ctx set-vm */This would mean that this bug was introduced in
e0695db7298e("drm/i915:
Create/destroy VM (ppGTT) for use with contexts"), which allowed to change thegem_ctx->vmat runtime, opening up the race that was partially fixed in the earlier referenced commit about a year later.But it cannot be exploited anymore in anything remotely recent because with the introduction of proto-contexts we've made
gem_ctx->vminvariant again, exactly to preemptively close all these potential issues. Specificallyd4433c7600f7("drm/i915/gem: Use the proto-context to handle create parameters (v5)") is the vm specific part of the proto-context work.Despite that this is impossible to exploit I think it's still good to fix, but I think for paranoia's sake we should put a
WARN_ON_ONCE(vma->vm != vm)in there, since this really should be impossible.I don't think there's a harm in backporting this though, since there's a 2 year window between the introduction of the
ctx->vmchange and it's complete fix with the proto-ctx work between 2019 and 2021. It's not realistic to backport the latter and this here is trivial in case anyone is foolish enough to run such an old kernel.v3:
- Include Sima's analysis andWARN_ON_ONCEFixes f7ce8639f6ff ("drm/i915/gem: Split the context's obj:vma lut into its own mutex")References: https://lore.kernel.org/all/[email protected]/
Reported-by Ville Syrjälä <[email protected]>Cc Linus Torvalds <[email protected]>Cc Simona Vetter <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Andi Shyti <[email protected]>Cc Chris Wilson <[email protected]>Signed-off-by Joonas Lahtinen <[email protected]>--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +5
-1@@ -895,8 +895,12 @@ static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle)rcu_read_lock (); vma =radix_tree_lookup (&eb->gem_context->handles_vma, handle); -if (likely (vma && vma->vm == vm)) +if (likely (vma && vma->vm == vm)) { vma =i915_vma_tryget (vma); + }else { +WARN_ON_ONCE (vma && vma->vm != vm); + vma =NULL ; + }rcu_read_unlock ();if (likely (vma))return vma;Signature · 1 line
2.53.0
Signature · 3 lines
Simona Vetter Software Engineer http://blog.ffwll.ch
Patchwork
CI✗ Fi.CI.BUILD: failure for drm/i915/gem: Don't use VMA from wrong VM in EXECBUF (rev3)
In reply to Joonas Lahtinen== Series Details ==
== Series Details ==
Series: drm/i915/gem: Don't use VMA from wrong VM in EXECBUF (rev3)
URL : https://patchwork.freedesktop.org/series/164504/
State : failure
== Summary ==
Error: patch https://patchwork.freedesktop.org/api/1.0/series/164504/revisions/3/mbox/ not applied
Applying: drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
Using index info to reconstruct a base tree...
M drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
Falling back to patching base and 3-way merge...
Auto-merging drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Build failed, no error log produced