Implement a robust overflows_type() macro to test if a variable or constant value would overflow another variable or type. This can be used as a constant expression for static_assert() (which requires a constant expression[1][2]) when used on constant values. This must be constructed manually, since __builtin_add_overflow() does not produce a constant expression[3]. Additionally adds castable_to_type(), similar to __same_type(), but for checking if a constant value would overflow if cast to a given type. Add unit tests for overflows_type(), __same_type(), and castable_to_type() to the existing KUnit "overflow" test: [16:03:33] ================== overflow (21 subtests) ================== ... [16:03:33] [PASSED] overflows_type_test [16:03:33] [PASSED] same_type_test [16:03:33] [PASSED] castable_to_type_test [16:03:33] ==================== [PASSED] overflow ===================== [16:03:33] ============================================================ [16:03:33] Testing complete. Ran 21 tests: passed: 21 [16:03:33] Elapsed time: 24.022s total, 0.002s configuring, 22.598s building, 0.767s running [1] https://en.cppreference.com/w/c/language/_Static_assert [2] C11 standard (ISO/IEC 9899:2011): 6.7.10 Static assertions [3] https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html 6.56 Built-in Functions to Perform Arithmetic with Overflow Checking Built-in Function: bool __builtin_add_overflow (type1 a, type2 b, Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Tom Rix <trix@redhat.com> Cc: Daniel Latypov <dlatypov@google.com> Cc: Vitor Massaru Iha <vitor@massaru.org> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org> Cc: Jani Nikula <jani.nikula@intel.com> Cc: Mauro Carvalho Chehab <mchehab@kernel.org> Cc: linux-hardening@vger.kernel.org Cc: llvm@lists.linux.dev Co-developed-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com> Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com> Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20221024201125.1416422-1-gwan-gyeong.mun@intel.com
62 lines
1.1 KiB
C
62 lines
1.1 KiB
C
/*
|
|
* SPDX-License-Identifier: MIT
|
|
*
|
|
* Copyright © 2018 Intel Corporation
|
|
*/
|
|
|
|
#include <linux/nospec.h>
|
|
#include <linux/sched/signal.h>
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <uapi/drm/i915_drm.h>
|
|
|
|
#include "i915_user_extensions.h"
|
|
#include "i915_utils.h"
|
|
|
|
int i915_user_extensions(struct i915_user_extension __user *ext,
|
|
const i915_user_extension_fn *tbl,
|
|
unsigned int count,
|
|
void *data)
|
|
{
|
|
unsigned int stackdepth = 512;
|
|
|
|
while (ext) {
|
|
int i, err;
|
|
u32 name;
|
|
u64 next;
|
|
|
|
if (!stackdepth--) /* recursion vs useful flexibility */
|
|
return -E2BIG;
|
|
|
|
err = check_user_mbz(&ext->flags);
|
|
if (err)
|
|
return err;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ext->rsvd); i++) {
|
|
err = check_user_mbz(&ext->rsvd[i]);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
if (get_user(name, &ext->name))
|
|
return -EFAULT;
|
|
|
|
err = -EINVAL;
|
|
if (name < count) {
|
|
name = array_index_nospec(name, count);
|
|
if (tbl[name])
|
|
err = tbl[name](ext, data);
|
|
}
|
|
if (err)
|
|
return err;
|
|
|
|
if (get_user(next, &ext->next_extension) ||
|
|
overflows_type(next, uintptr_t))
|
|
return -EFAULT;
|
|
|
|
ext = u64_to_user_ptr(next);
|
|
}
|
|
|
|
return 0;
|
|
}
|