linux-stable/Documentation/process
Kees Cook 2b46db3bb7 overflow: Implement size_t saturating arithmetic helpers
[ Upstream commit e1be43d9b5 ]

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: d692873cbe ("gve: Use size_add() in call to struct_size()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-11-20 11:06:44 +01:00
..
1.Intro.rst
2.Process.rst Documentation: process: step 2: Link to email list fixed. 2020-09-03 10:31:03 -06:00
3.Early-stage.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00
4.Coding.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
5.Posting.rst
6.Followthrough.rst
7.AdvancedTopics.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00
8.Conclusion.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00
adding-syscalls.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00
applying-patches.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00
botching-up-ioctls.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
changes.rst Documentation: add minimum clang/llvm version 2020-08-31 16:30:20 -06:00
clang-format.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
code-of-conduct-interpretation.rst docs: update mediator contact information in CoC doc 2022-11-25 17:45:53 +01:00
code-of-conduct.rst
coding-style.rst It's been a busy cycle for documentation - hopefully the busiest for a 2020-08-04 22:47:54 -07:00
deprecated.rst overflow: Implement size_t saturating arithmetic helpers 2023-11-20 11:06:44 +01:00
development-process.rst
email-clients.rst Documentation/process: expand plain-text advice 2020-09-03 15:39:01 -06:00
embargoed-hardware-issues.rst Documentation: provide IBM contacts for embargoed hardware 2020-03-26 15:10:44 +01:00
howto.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
index.rst docs: process/index.rst: Fix reference to nonexistent document 2020-07-23 14:26:12 -06:00
kernel-docs.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
kernel-driver-statement.rst
kernel-enforcement-statement.rst
license-rules.rst
magic-number.rst Remove every trace of SERIAL_MAGIC 2019-11-13 19:01:14 +08:00
maintainer-pgp-guide.rst Replace HTTP links with HTTPS ones: Documentation/process 2020-06-26 11:19:43 -06:00
maintainers.rst doc-rst: Programmatically render MAINTAINERS into ReST 2019-10-02 10:03:17 -06:00
management-style.rst Documentation: management-style: Fix formatting of emphsized word 2020-03-10 11:32:34 -06:00
programming-language.rst docs: programming-languages: refresh blurb on clang support 2020-10-05 09:44:11 -06:00
stable-api-nonsense.rst
stable-kernel-rules.rst Documentation: update stable tree link 2022-04-08 14:39:50 +02:00
submit-checklist.rst Kbuild updates for v5.10 2020-10-22 13:13:57 -07:00
submitting-drivers.rst Documentation: clarify driver licensing rules 2020-08-31 16:36:11 -06:00
submitting-patches.rst docs: submitting-patches: Fix crossref to 'The canonical patch format' 2022-06-06 08:42:44 +02:00
volatile-considered-harmful.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00