Skip to contentSkip to conversation

drm/i915/ppgtt: Prevent NULL deref in reset ioctl

PATCH In review Ben Widawsky 5 participants 10 messages Dec 19, 2013 – Jan 2, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915/ppgtt: Prevent NULL deref in reset ioctl

If we look up an invalid context ID, the idr will return NULL. The ptr is unconditionally dereferenced afterwards causing a problem.

Note that if the context does not exist, we still return success. This appears to be the behavior desired by gem_reset_stats --subtest ban

Introduced in v3 of
commit 41bde5535a7d48876095926bb55b1aed5ccd6b2c

Author: Ben Widawsky <[email protected]>
Date:   Fri Dec 6 14:11:21 2013 -0800

drm/i915: Get context early in execbuf

---
 drivers/gpu/drm/i915/intel_uncore.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/intel_uncore.c +1 -1
index e52fcce..aa4c55a 100644
@@ -850,7 +850,7 @@ int i915_get_reset_stats_ioctl(struct drm_device *dev,
 		return ret;
 
 	ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
-	if (IS_ERR(ctx)) {
+	if (IS_ERR_OR_NULL(ctx)) {
 		mutex_unlock(&dev->struct_mutex);
 		return PTR_ERR(ctx);
 	}
Signature · 1 line
1.8.5.1
#1 lore raw

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#2 lore raw
Quoted 11 lines · Daniel Vetter

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

To your first point:
I think checking null is always the right thing currently, but for future proofing, IS_ERR_OR_NULL is really the right thing. After his patch, I believe only i915_gem_context_destroy_ioctl is still incorrect.

To the second:
This is only based on my memory, so feel free to change whatever you need. When I retuned -ENXIO, the test failed.

It should be:
return ctx ? PTR_ERR(ctx) : -ENOENT;

I had
return ctx ? PTR_ERR(ctx) : -ENXIO;
which made the subtest fail. However, as we've noted, this itself was not correct. Try the return above.

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#3 lore raw
Quoted 14 lines · Ben Widawsky
Quoted 11 lines · Daniel Vetter

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

Quoted 4 lines

To your first point:
I think checking null is always the right thing currently, but for future proofing, IS_ERR_OR_NULL is really the right thing. After his patch, I believe only i915_gem_context_destroy_ioctl is still incorrect.

Using IS_ERR_OR_NULL on a return value which can never contain an encoded errno value is imo confusing, more so when it's inconsitently applied.

Quoted 6 lines

To the second:
This is only based on my memory, so feel free to change whatever you need. When I retuned -ENXIO, the test failed.

It should be:
return ctx ? PTR_ERR(ctx) : -ENOENT;

On a cursory read that's been the semantics before your patch. And there seems to be a testcase in gem_reset_stat for this, have you run all subtests?

Quoted 4 lines

I had
return ctx ? PTR_ERR(ctx) : -ENXIO;
which made the subtest fail. However, as we've noted, this itself was not correct. Try the return above.

Thanks, Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
maintainer
#4 lore raw
Quoted 15 lines · Daniel Vetter
Quoted 14 lines · Ben Widawsky
Quoted 11 lines · Daniel Vetter

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

Quoted 8 lines
Quoted 4 lines

To your first point:
I think checking null is always the right thing currently, but for future proofing, IS_ERR_OR_NULL is really the right thing. After his patch, I believe only i915_gem_context_destroy_ioctl is still incorrect.

Using IS_ERR_OR_NULL on a return value which can never contain an encoded errno value is imo confusing, more so when it's inconsitently applied.

Moreover, the error path then goes on to return 0 for success when the pointer is NULL.

BR,
Jani.

Signature · 1 line
Jani Nikula, Intel Open Source Technology Center
#5 lore raw
Quoted 21 lines · Jani Nikula
Quoted 20 lines · Daniel Vetter
Quoted 18 lines · Ben Widawsky
Quoted 11 lines · Daniel Vetter

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

To your first point:
I think checking null is always the right thing currently, but for future proofing, IS_ERR_OR_NULL is really the right thing. After his patch, I believe only i915_gem_context_destroy_ioctl is still incorrect.

Quoted 7 lines
Quoted 3 lines

Using IS_ERR_OR_NULL on a return value which can never contain an encoded errno value is imo confusing, more so when it's inconsitently applied.

Moreover, the error path then goes on to return 0 for success when the pointer is NULL.

That was mentioned in the commit message.

Quoted 6 lines

BR,
Jani.

Signature · 1 line
Jani Nikula, Intel Open Source Technology Center
Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#6 lore raw
In reply to Daniel Vetter
Quoted 28 lines · Daniel Vetter
Quoted 14 lines · Ben Widawsky
Quoted 11 lines · Daniel Vetter

drivers/gpu/drm/i915/intel_uncore.c int i915_get_reset_stats_ioctl(struct drm_device *dev, · line 853

Quoted 3 lines · Ben Widawsky
        ctx = i915_gem_context_get(file->driver_priv, args->ctx_id);
drivers/gpu/drm/i915/intel_uncore.c +1 -1
-       if (IS_ERR(ctx)) {
+       if (IS_ERR_OR_NULL(ctx)) {

We now have half the callers check for IS_ERR and the others not. Afaics i915_gem_context_get can only return NULL or a real context though. Also from a quite read the expected error for a lookup failure is ENOENT (and there seems to be a testcase for this). -Daniel

Quoted 4 lines

To your first point:
I think checking null is always the right thing currently, but for future proofing, IS_ERR_OR_NULL is really the right thing. After his patch, I believe only i915_gem_context_destroy_ioctl is still incorrect.

Using IS_ERR_OR_NULL on a return value which can never contain an encoded errno value is imo confusing, more so when it's inconsitently applied.

Quoted 4 lines

To the second:
This is only based on my memory, so feel free to change whatever you need. When I retuned -ENXIO, the test failed.

Quoted 6 lines
Quoted 2 lines

It should be:
return ctx ? PTR_ERR(ctx) : -ENOENT;

On a cursory read that's been the semantics before your patch. And there seems to be a testcase in gem_reset_stat for this, have you run all subtests?

As I mentioned on IRC, but I think you missed it - a later subtest hits a really bad BUG (pre-ppgtt). It was all pass up to that subtest.

Quoted 11 lines
Quoted 5 lines

I had
return ctx ? PTR_ERR(ctx) : -ENXIO;
which made the subtest fail. However, as we've noted, this itself was not correct. Try the return above.

Thanks, Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#7 lore raw

PATCH 1/1 drm/i915/ppgtt: Never return a NULL context

In reply to Ben Widawsky

It makes all the code which calls into this function way too confusing.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72903

---
 drivers/gpu/drm/i915/i915_gem_context.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_gem_context.c +7 -1
index ebe0f67..4794c18 100644
@@ -526,10 +526,16 @@ void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
 struct i915_hw_context *
 i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
 {
+	struct i915_hw_context *ctx;
+
 	if (!HAS_HW_CONTEXTS(file_priv->dev_priv->dev))
 		return file_priv->private_default_ctx;
 
-	return (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	ctx = (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	if (!ctx)
+		return ERR_PTR(-ENOENT);
+
+	return ctx;
 }
 
 static inline int
Signature · 1 line
1.8.5.2
#8 lore raw

PATCH v2 1/1 drm/i915/ppgtt: Never return a NULL context

It makes all the code which calls into this function way too confusing.

v2: Fix destroy IOCTL as well

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72903

---
 drivers/gpu/drm/i915/i915_gem_context.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/i915_gem_context.c +9 -3
index a90a0a1..f3ac9a2 100644
@@ -524,10 +524,16 @@ void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
 struct i915_hw_context *
 i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
 {
+	struct i915_hw_context *ctx;
+
 	if (!HAS_HW_CONTEXTS(file_priv->dev_priv->dev))
 		return file_priv->private_default_ctx;
 
-	return (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	ctx = (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	if (!ctx)
+		return ERR_PTR(-ENOENT);
+
+	return ctx;
 }
 
 static inline int
@@ -774,9 +780,9 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
 		return ret;
 
 	ctx = i915_gem_context_get(file_priv, args->ctx_id);
-	if (!ctx) {
+	if (IS_ERR(ctx)) {
 		mutex_unlock(&dev->struct_mutex);
-		return -ENOENT;
+		return PTR_ERR(ctx);
 	}
 
 	idr_remove(&ctx->file_priv->context_idr, ctx->id);
Signature · 1 line
1.8.5.2
#9 lore raw

Re: [PATCH] [v2] drm/i915/ppgtt: Never return a NULL context

Quoted 4 lines · Ben Widawsky

It makes all the code which calls into this function way too confusing.

v2: Fix destroy IOCTL as well

callsite inside i915_gem_validate_context() and after it is called in i915_gem_do_execbuffer() are still confusing as they seem to expect NULL. Latter even tries to dig out error value from NULL.

-- Mika

Quoted 47 lines

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72903

---
 drivers/gpu/drm/i915/i915_gem_context.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/i915_gem_context.c +9 -3
index a90a0a1..f3ac9a2 100644
@@ -524,10 +524,16 @@ void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
 struct i915_hw_context *
 i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id)
 {
+	struct i915_hw_context *ctx;
+
 	if (!HAS_HW_CONTEXTS(file_priv->dev_priv->dev))
 		return file_priv->private_default_ctx;
 
-	return (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	ctx = (struct i915_hw_context *)idr_find(&file_priv->context_idr, id);
+	if (!ctx)
+		return ERR_PTR(-ENOENT);
+
+	return ctx;
 }
 
 static inline int
@@ -774,9 +780,9 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
 		return ret;
 
 	ctx = i915_gem_context_get(file_priv, args->ctx_id);
-	if (!ctx) {
+	if (IS_ERR(ctx)) {
 		mutex_unlock(&dev->struct_mutex);
-		return -ENOENT;
+		return PTR_ERR(ctx);
 	}
 
 	idr_remove(&ctx->file_priv->context_idr, ctx->id);
Signature · 6 lines
1.8.5.2

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx