drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
Subsystems (2)
Joonas Lahtinen
maintainerPATCH 1/1 drm/i915/gem: Don't use VMA from wrong VM in EXECBUF
Do not pick 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).
Also avoids returning a VMA without increasing the refcount, which may potentially lead to UAF.
References: https://lore.kernel.org/all/[email protected]/
--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +2 -0
@@ -897,6 +897,8 @@ static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle) vma =radix_tree_lookup (&eb->gem_context->handles_vma, handle);if (likely (vma && vma->vm == vm)) vma =i915_vma_tryget (vma); +else + vma =NULL ;rcu_read_unlock ();if (likely (vma))return vma;
Signature · 1 line
2.53.0
Joonas Lahtinen
maintainerThere will be no CI results as Linus already has a temporary fix in his tree which got to drm-tip via the various -fixes trees. But its the same change suggested by Ville so effectively it's been tested already in every CI run after incorporating -rc7 to -fixes trees this week.
Regards, Joonas
Quoted 21 lines · Joonas Lahtinen
Do not pick 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).
Also avoids returning a VMA without increasing the refcount, which may potentially lead to UAF.
References: https://lore.kernel.org/all/[email protected]/
Reported-by Ville Syrjälä <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Joonas Lahtinen <[email protected]>--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 ++ 1 file changed, 2 insertions(+)drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +0
-0
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle) · line 898
Quoted 2 lines
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +0
-0@@ -897,6 +897,8 @@ static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle) vma =radix_tree_lookup (&eb->gem_context->handles_vma, handle);if (likely (vma && vma->vm == vm))
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.
With the WARN_ON_ONCE added and the above analysis included this is
Cheers, Sima
PS: Caught a cold, brain's not entirely working, this might be all very wrong so please check my history digging.
Quoted 9 lines
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +2
-0vma =i915_vma_tryget (vma); + else + 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