Skip to contentSkip to conversation

drm/managed: Fix off-by-one in warning

PATCH In review Daniel Vetter 3 participants 6 messages Mar 28, 2020 – Mar 30, 2020

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

PATCH 1/1 drm/managed: Fix off-by-one in warning

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/drm_managed.c +1 -2
index 4955241ceb4c..9cebfe370a65 100644
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
 	WARN_ON(dev->managed.final_kfree);
 	WARN_ON(dev < (struct drm_device *) container);
-	WARN_ON(dev + 1 >=
-		(struct drm_device *) (container + ksize(container)));
+	WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
 	dev->managed.final_kfree = container;
 }
 EXPORT_SYMBOL(drmm_add_final_kfree);
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Hi Daniel.

Quoted 28 lines · Daniel Vetter

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/drm_managed.c +0 -0
index 4955241ceb4c..9cebfe370a65 100644

drivers/gpu/drm/drm_managed.c void drmm_add_final_kfree(struct drm_device *dev, void *container) · line 142

Quoted 6 lines
drivers/gpu/drm/drm_managed.c +1 -2
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
 	WARN_ON(dev->managed.final_kfree);
 	WARN_ON(dev < (struct drm_device *) container);
-	WARN_ON(dev + 1 >=
-		(struct drm_device *) (container + ksize(container)));
+	WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));

I do not think this is the right fix...
The original code would trigger if
1) the container only had a drm_device - and nothing else
2) and the allocated size was the same

And the modification will now allow for a container with the exact size of drm_device.

I checked all users in my tree - no-one only had a drm_device. The minimum was one extra pointer.

Another thing that could trigger the warning was if any users did not specify a pointer to memory allocated by k(z)alloc() But I could not find any.

tiny/st7735r.c looked suspisius, but I think it is also OK, because struct st7735r_priv is allocated, but the poitner specified in st7735r_priv.dbidev. But dbidev is the first field - so OK.

So no better clue...

Sam
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 27 lines · Sam Ravnborg

Hi Daniel.

Quoted 23 lines · Daniel Vetter

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 16 lines

drivers/gpu/drm/drm_managed.c void drmm_add_final_kfree(struct drm_device *dev, void *container) · line 142

Quoted 11 lines
drivers/gpu/drm/drm_managed.c +1 -2
index 4955241ceb4c..9cebfe370a65 100644
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
      WARN_ON(dev->managed.final_kfree);
      WARN_ON(dev < (struct drm_device *) container);
-     WARN_ON(dev + 1 >=
-             (struct drm_device *) (container + ksize(container)));
+     WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));

I do not think this is the right fix...
The original code would trigger if
1) the container only had a drm_device - and nothing else
2) and the allocated size was the same

Yup, which apparently happens for all the drivers calling drm_dev_alloc(). At least on the unlucky architecture that 0day tested on (or build settings, or whatever). The issue was hit with drm/bochs, which is still using drm_dev_alloc (like most older-ish drivers).

Quoted 10 lines

And the modification will now allow for a container with the exact size of drm_device.

I checked all users in my tree - no-one only had a drm_device. The minimum was one extra pointer.

Another thing that could trigger the warning was if any users did not specify a pointer to memory allocated by k(z)alloc() But I could not find any.

Quoted 5 lines

tiny/st7735r.c looked suspisius, but I think it is also OK, because struct st7735r_priv is allocated, but the poitner specified in st7735r_priv.dbidev. But dbidev is the first field - so OK.

So no better clue...

Yeah all the drivers using drm_dev_init with embedded drm_device wont hit this. -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by
#3 lore raw
Quoted 45 lines · Daniel Vetter
Quoted 27 lines · Sam Ravnborg

Hi Daniel.

Quoted 23 lines · Daniel Vetter

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 16 lines

drivers/gpu/drm/drm_managed.c void drmm_add_final_kfree(struct drm_device *dev, void *container) · line 142

Quoted 11 lines
drivers/gpu/drm/drm_managed.c +1 -2
index 4955241ceb4c..9cebfe370a65 100644
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
      WARN_ON(dev->managed.final_kfree);
      WARN_ON(dev < (struct drm_device *) container);
-     WARN_ON(dev + 1 >=
-             (struct drm_device *) (container + ksize(container)));
+     WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));

I do not think this is the right fix...
The original code would trigger if
1) the container only had a drm_device - and nothing else
2) and the allocated size was the same

Quoted 4 lines

Yup, which apparently happens for all the drivers calling drm_dev_alloc(). At least on the unlucky architecture that 0day tested on (or build settings, or whatever). The issue was hit with drm/bochs, which is still using drm_dev_alloc (like most older-ish drivers).

That explains it and then the checks makes sense.

Sam
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 46 lines · Sam Ravnborg
Quoted 45 lines · Daniel Vetter
Quoted 43 lines · Sam Ravnborg

Hi Daniel.

Quoted 28 lines · Daniel Vetter

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/drm_managed.c +0 -0
index 4955241ceb4c..9cebfe370a65 100644

drivers/gpu/drm/drm_managed.c void drmm_add_final_kfree(struct drm_device *dev, void *container) · line 142

Quoted 6 lines
drivers/gpu/drm/drm_managed.c +1 -2
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
      WARN_ON(dev->managed.final_kfree);
      WARN_ON(dev < (struct drm_device *) container);
-     WARN_ON(dev + 1 >=
-             (struct drm_device *) (container + ksize(container)));
+     WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));

I do not think this is the right fix...
The original code would trigger if
1) the container only had a drm_device - and nothing else
2) and the allocated size was the same

Quoted 8 lines
Quoted 4 lines

Yup, which apparently happens for all the drivers calling drm_dev_alloc(). At least on the unlucky architecture that 0day tested on (or build settings, or whatever). The issue was hit with drm/bochs, which is still using drm_dev_alloc (like most older-ish drivers).

That explains it and then the checks makes sense.

Thanks for your review, patch applied and a note to the commit message added that 0day confirmed that it's indeed this WARN_ON that they've hit. -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw
In reply to Daniel Vetter

Ok 0day people uploaded the tree they tested (from patches) now, I confirmed it's indeed this line that's blowing up. -Daniel

Quoted 41 lines · Daniel Vetter

I'm thinking this is the warning that fired in the 0day report, but I can't double-check yet since 0day didn't upload its source tree anywhere I can check. And all the drivers I can easily test don't use drm_dev_alloc anymore ...

Also if I'm correct supreme amounts of bad luck because usually kslap (for bigger structures) gives us something quite a bit bigger than what we asked for.

Fixes c6603c740e0e ("drm: add managed resources tied to drm_device")
Cc Sam Ravnborg <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Laurent Pinchart <[email protected]>
Cc Neil Armstrong <[email protected]
Cc Greg Kroah-Hartman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 drivers/gpu/drm/drm_managed.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/drm_managed.c +1 -3
index 4955241ceb4c..9cebfe370a65 100644
@@ -139,8 +139,7 @@ void drmm_add_final_kfree(struct drm_device *dev, void *container)
 {
        WARN_ON(dev->managed.final_kfree);
        WARN_ON(dev < (struct drm_device *) container);
-       WARN_ON(dev + 1 >=
-               (struct drm_device *) (container + ksize(container)));
+       WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
        dev->managed.final_kfree = container;
 }
 EXPORT_SYMBOL(drmm_add_final_kfree);
--

2.25.1

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel