Browse Source

drm/i915: Sanity check the computed size and base of stolen memory

Just do a quick check that the stolen memory address range doesn't
overflow our chosen integer type.

v2: Add add_overflows() to utils with the promise that gcc7 can do this
better than C and then maybe it will have a proper definition in core.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20170130134721.5159-1-chris@chris-wilson.co.uk
Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Chris Wilson 8 years ago
parent
commit
1692cd60d9
2 changed files with 12 additions and 1 deletions
  1. 1 1
      drivers/gpu/drm/i915/i915_gem_stolen.c
  2. 11 0
      drivers/gpu/drm/i915/i915_utils.h

+ 1 - 1
drivers/gpu/drm/i915/i915_gem_stolen.c

@@ -189,7 +189,7 @@ static dma_addr_t i915_stolen_to_dma(struct drm_i915_private *dev_priv)
 		base = tom - tseg_size - ggtt->stolen_size;
 		base = tom - tseg_size - ggtt->stolen_size;
 	}
 	}
 
 
-	if (base == 0)
+	if (base == 0 || add_overflows(base, ggtt->stolen_size))
 		return 0;
 		return 0;
 
 
 	/* make sure we don't clobber the GTT if it's within stolen memory */
 	/* make sure we don't clobber the GTT if it's within stolen memory */

+ 11 - 0
drivers/gpu/drm/i915/i915_utils.h

@@ -25,6 +25,17 @@
 #ifndef __I915_UTILS_H
 #ifndef __I915_UTILS_H
 #define __I915_UTILS_H
 #define __I915_UTILS_H
 
 
+#if GCC_VERSION >= 70000
+#define add_overflows(A, B) \
+	__builtin_add_overflow_p((A), (B), (typeof((A) + (B)))0)
+#else
+#define add_overflows(A, B) ({ \
+	typeof(A) a = (A); \
+	typeof(B) b = (B); \
+	a + b < a; \
+})
+#endif
+
 #define range_overflows(start, size, max) ({ \
 #define range_overflows(start, size, max) ({ \
 	typeof(start) start__ = (start); \
 	typeof(start) start__ = (start); \
 	typeof(size) size__ = (size); \
 	typeof(size) size__ = (size); \