kernel_samsung_a53x/Documentation/process
Kees Cook 399f7a8c04 overflow: Implement size_t saturating arithmetic helpers
[ Upstream commit e1be43d9b5d0d1310dbd90185a8e5c7145dde40f ]

In order to perform more open-coded replacements of common allocation
size arithmetic, the kernel needs saturating (SIZE_MAX) helpers for
multiplication, addition, and subtraction. For example, it is common in
allocators, especially on realloc, to add to an existing size:

    p = krealloc(map->patch,
                 sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
                 GFP_KERNEL);

There is no existing saturating replacement for this calculation, and
just leaving the addition open coded inside array_size() could
potentially overflow as well. For example, an overflow in an expression
for a size_t argument might wrap to zero:

    array_size(anything, something_at_size_max + 1) == 0

Introduce size_mul(), size_add(), and size_sub() helpers that
implicitly promote arguments to size_t and saturated calculations for
use in allocations. With these helpers it is also possible to redefine
array_size(), array3_size(), flex_array_size(), and struct_size() in
terms of the new helpers.

As with the check_*_overflow() helpers, the new helpers use __must_check,
though what is really desired is a way to make sure that assignment is
only to a size_t lvalue. Without this, it's still possible to introduce
overflow/underflow via type conversion (i.e. from size_t to int).
Enforcing this will currently need to be left to static analysis or
future use of -Wconversion.

Additionally update the overflow unit tests to force runtime evaluation
for the pathological cases.

Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Len Baker <len.baker@gmx.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Stable-dep-of: d692873cbe86 ("gve: Use size_add() in call to struct_size()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-11-18 11:42:47 +01:00
..
1.Intro.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
2.Process.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
3.Early-stage.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
4.Coding.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
5.Posting.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
6.Followthrough.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
7.AdvancedTopics.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
8.Conclusion.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
adding-syscalls.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
applying-patches.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
botching-up-ioctls.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
changes.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
clang-format.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
code-of-conduct-interpretation.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
code-of-conduct.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
coding-style.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
deprecated.rst overflow: Implement size_t saturating arithmetic helpers 2024-11-18 11:42:47 +01:00
development-process.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
email-clients.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
embargoed-hardware-issues.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
howto.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
index.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
kernel-docs.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
kernel-driver-statement.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
kernel-enforcement-statement.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
license-rules.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
magic-number.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
maintainer-pgp-guide.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
maintainers.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
management-style.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
programming-language.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
stable-api-nonsense.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
stable-kernel-rules.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
submit-checklist.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
submitting-drivers.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
submitting-patches.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00
volatile-considered-harmful.rst Import A536BXXU9EXDC 2024-06-15 16:02:09 -03:00