Skip to contentSkip to conversation

drm: fix division-by-zero on dumb_create()

PATCH Superseded David Herrmann 2 participants 2 messages Aug 24, 2014

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: fix division-by-zero on dumb_create()

Kinda unexpected, but DIV_ROUND_UP() can overflow if passed an argument bigger than UINT_MAX - DIVISOR. Fix this by testing for "!cpp" before using it in the following division.

Note that DIV_ROUND_UP() is defined as:

        #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

..this will obviously overflow if (n + d - 1) is bigger than UINT_MAX.

---
 drivers/gpu/drm/drm_crtc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_crtc.c +1 -1
index fe94cc1..55057f7 100644
@@ -4397,7 +4397,7 @@ int drm_mode_create_dumb_ioctl(struct drm_device *dev,
 
 	/* overflow checks for 32bit size calculations */
 	cpp = DIV_ROUND_UP(args->bpp, 8);
-	if (cpp > 0xffffffffU / args->width)
+	if (!cpp || cpp > 0xffffffffU / args->width)
 		return -EINVAL;
 	stride = cpp * args->width;
 	if (args->height > 0xffffffffU / stride)
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · David Herrmann

Kinda unexpected, but DIV_ROUND_UP() can overflow if passed an argument bigger than UINT_MAX - DIVISOR. Fix this by testing for "!cpp" before using it in the following division.

Note that DIV_ROUND_UP() is defined as:

        #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

..this will obviously overflow if (n + d - 1) is bigger than UINT_MAX.

---
 drivers/gpu/drm/drm_crtc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_crtc.c +0 -0
index fe94cc1..55057f7 100644

drivers/gpu/drm/drm_crtc.c int drm_mode_create_dumb_ioctl(struct drm_device *dev, · line 4400

Quoted 4 lines
drivers/gpu/drm/drm_crtc.c +0 -1
@@ -4397,7 +4397,7 @@ int drm_mode_create_dumb_ioctl(struct drm_device *dev,
 
        /* overflow checks for 32bit size calculations */
        cpp = DIV_ROUND_UP(args->bpp, 8);
-       if (cpp > 0xffffffffU / args->width)

might be an idea to add:

+ /* NOTE: DIV_ROUND_UP() can overflow */

or something along those lines.. I also assumed that DIV_ROUND_UP() would not overflow, and would otherwise find that code funny looking.

(yeah, technically someone could figure that out via git-blame.. but still..)

other than that,

Quoted 7 lines
drivers/gpu/drm/drm_crtc.c +1 -1
+       if (!cpp || cpp > 0xffffffffU / args->width)
                return -EINVAL;
        stride = cpp * args->width;
        if (args->height > 0xffffffffU / stride)
--

2.1.0