Skip to content

Commit 5f3cec2

Browse files
committed
overflow: add range_overflows() and range_end_overflows()
Move the range_overflows() and range_end_overflows() along with the _t variants over from drm/i915 and drm/buddy to overflow.h. Cc: Kees Cook <kees@kernel.org> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org> Cc: linux-hardening@vger.kernel.org Reviewed-by: Kees Cook <kees@kernel.org> Reviewed-by: Jouni Högander <jouni.hogander@intel.com> Acked-by: Thomas Zimmermann <tzimmermann@suse.de> Link: https://lore.kernel.org/r/20250829174601.2163064-3-jani.nikula@intel.com Signed-off-by: Jani Nikula <jani.nikula@intel.com>
1 parent 59bf37a commit 5f3cec2

3 files changed

Lines changed: 70 additions & 79 deletions

File tree

drivers/gpu/drm/i915/i915_utils.h

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -67,76 +67,6 @@ bool i915_error_injected(void);
6767
drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \
6868
})
6969

70-
/**
71-
* range_overflows() - Check if a range is out of bounds
72-
* @start: Start of the range.
73-
* @size: Size of the range.
74-
* @max: Exclusive upper boundary.
75-
*
76-
* A strict check to determine if the range [@start, @start + @size) is
77-
* invalid with respect to the allowable range [0, @max). Any range
78-
* starting at or beyond @max is considered an overflow, even if @size is 0.
79-
*
80-
* Returns: true if the range is out of bounds.
81-
*/
82-
#define range_overflows(start, size, max) ({ \
83-
typeof(start) start__ = (start); \
84-
typeof(size) size__ = (size); \
85-
typeof(max) max__ = (max); \
86-
(void)(&start__ == &size__); \
87-
(void)(&start__ == &max__); \
88-
start__ >= max__ || size__ > max__ - start__; \
89-
})
90-
91-
/**
92-
* range_overflows_t() - Check if a range is out of bounds
93-
* @type: Data type to use.
94-
* @start: Start of the range.
95-
* @size: Size of the range.
96-
* @max: Exclusive upper boundary.
97-
*
98-
* Same as range_overflows() but forcing the parameters to @type.
99-
*
100-
* Returns: true if the range is out of bounds.
101-
*/
102-
#define range_overflows_t(type, start, size, max) \
103-
range_overflows((type)(start), (type)(size), (type)(max))
104-
105-
/**
106-
* range_end_overflows() - Check if a range's endpoint is out of bounds
107-
* @start: Start of the range.
108-
* @size: Size of the range.
109-
* @max: Exclusive upper boundary.
110-
*
111-
* Checks only if the endpoint of a range (@start + @size) exceeds @max.
112-
* Unlike range_overflows(), a zero-sized range at the boundary (@start == @max)
113-
* is not considered an overflow. Useful for iterator-style checks.
114-
*
115-
* Returns: true if the endpoint exceeds the boundary.
116-
*/
117-
#define range_end_overflows(start, size, max) ({ \
118-
typeof(start) start__ = (start); \
119-
typeof(size) size__ = (size); \
120-
typeof(max) max__ = (max); \
121-
(void)(&start__ == &size__); \
122-
(void)(&start__ == &max__); \
123-
start__ > max__ || size__ > max__ - start__; \
124-
})
125-
126-
/**
127-
* range_end_overflows_t() - Check if a range's endpoint is out of bounds
128-
* @type: Data type to use.
129-
* @start: Start of the range.
130-
* @size: Size of the range.
131-
* @max: Exclusive upper boundary.
132-
*
133-
* Same as range_end_overflows() but forcing the parameters to @type.
134-
*
135-
* Returns: true if the endpoint exceeds the boundary.
136-
*/
137-
#define range_end_overflows_t(type, start, size, max) \
138-
range_end_overflows((type)(start), (type)(size), (type)(max))
139-
14070
#define ptr_mask_bits(ptr, n) ({ \
14171
unsigned long __v = (unsigned long)(ptr); \
14272
(typeof(ptr))(__v & -BIT(n)); \

include/drm/drm_buddy.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313

1414
#include <drm/drm_print.h>
1515

16-
#define range_overflows(start, size, max) ({ \
17-
typeof(start) start__ = (start); \
18-
typeof(size) size__ = (size); \
19-
typeof(max) max__ = (max); \
20-
(void)(&start__ == &size__); \
21-
(void)(&start__ == &max__); \
22-
start__ >= max__ || size__ > max__ - start__; \
23-
})
24-
2516
#define DRM_BUDDY_RANGE_ALLOCATION BIT(0)
2617
#define DRM_BUDDY_TOPDOWN_ALLOCATION BIT(1)
2718
#define DRM_BUDDY_CONTIGUOUS_ALLOCATION BIT(2)

include/linux/overflow.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,76 @@ static inline bool __must_check __must_check_overflow(bool overflow)
238238
__overflows_type_constexpr(n, T), \
239239
__overflows_type(n, T))
240240

241+
/**
242+
* range_overflows() - Check if a range is out of bounds
243+
* @start: Start of the range.
244+
* @size: Size of the range.
245+
* @max: Exclusive upper boundary.
246+
*
247+
* A strict check to determine if the range [@start, @start + @size) is
248+
* invalid with respect to the allowable range [0, @max). Any range
249+
* starting at or beyond @max is considered an overflow, even if @size is 0.
250+
*
251+
* Returns: true if the range is out of bounds.
252+
*/
253+
#define range_overflows(start, size, max) ({ \
254+
typeof(start) start__ = (start); \
255+
typeof(size) size__ = (size); \
256+
typeof(max) max__ = (max); \
257+
(void)(&start__ == &size__); \
258+
(void)(&start__ == &max__); \
259+
start__ >= max__ || size__ > max__ - start__; \
260+
})
261+
262+
/**
263+
* range_overflows_t() - Check if a range is out of bounds
264+
* @type: Data type to use.
265+
* @start: Start of the range.
266+
* @size: Size of the range.
267+
* @max: Exclusive upper boundary.
268+
*
269+
* Same as range_overflows() but forcing the parameters to @type.
270+
*
271+
* Returns: true if the range is out of bounds.
272+
*/
273+
#define range_overflows_t(type, start, size, max) \
274+
range_overflows((type)(start), (type)(size), (type)(max))
275+
276+
/**
277+
* range_end_overflows() - Check if a range's endpoint is out of bounds
278+
* @start: Start of the range.
279+
* @size: Size of the range.
280+
* @max: Exclusive upper boundary.
281+
*
282+
* Checks only if the endpoint of a range (@start + @size) exceeds @max.
283+
* Unlike range_overflows(), a zero-sized range at the boundary (@start == @max)
284+
* is not considered an overflow. Useful for iterator-style checks.
285+
*
286+
* Returns: true if the endpoint exceeds the boundary.
287+
*/
288+
#define range_end_overflows(start, size, max) ({ \
289+
typeof(start) start__ = (start); \
290+
typeof(size) size__ = (size); \
291+
typeof(max) max__ = (max); \
292+
(void)(&start__ == &size__); \
293+
(void)(&start__ == &max__); \
294+
start__ > max__ || size__ > max__ - start__; \
295+
})
296+
297+
/**
298+
* range_end_overflows_t() - Check if a range's endpoint is out of bounds
299+
* @type: Data type to use.
300+
* @start: Start of the range.
301+
* @size: Size of the range.
302+
* @max: Exclusive upper boundary.
303+
*
304+
* Same as range_end_overflows() but forcing the parameters to @type.
305+
*
306+
* Returns: true if the endpoint exceeds the boundary.
307+
*/
308+
#define range_end_overflows_t(type, start, size, max) \
309+
range_end_overflows((type)(start), (type)(size), (type)(max))
310+
241311
/**
242312
* castable_to_type - like __same_type(), but also allows for casted literals
243313
*

0 commit comments

Comments
 (0)