linux/Documentation/process
Kees Cook 30f20ceb87 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: e001e60869 ("fs/ntfs3: Harden against integer overflows")
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-12-31 13:14:33 +01:00
..
1.Intro.rst doc:it_IT: add some process/* translations 2018-11-20 09:11:12 -07:00
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 doc: use KCFLAGS instead of EXTRA_CFLAGS to pass flags from command line 2021-02-22 13:59:10 -07:00
5.Posting.rst docs: Clarify the usage and sign-off requirements for Co-developed-by 2019-03-25 10:30:32 -06:00
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 docs: Make syscalls' helpers naming consistent 2021-02-04 14:47:24 -07:00
applying-patches.rst Documentation/process/applying-patches: Activate linux-next man hyperlink 2021-08-20 11:10:12 -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: raise minimum supported version of GCC to 5.1 2021-09-13 10:18:28 -07:00
clang-format.rst Documentation: fix typos found in process, dev-tools, and doc-guide subdirectories 2020-12-03 15:55:04 -07:00
code-of-conduct-interpretation.rst docs: update mediator contact information in CoC doc 2022-11-26 09:24:48 +01:00
code-of-conduct.rst
coding-style.rst docs: Remove the Microsoft rhetoric 2021-02-11 09:42:17 -07:00
deprecated.rst overflow: Implement size_t saturating arithmetic helpers 2022-12-31 13:14:33 +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: fix typos found in process, dev-tools, and doc-guide subdirectories 2020-12-03 15:55:04 -07:00
howto.rst docs: process/howto.rst: make sections on bug reporting match practice 2021-01-18 12:29:38 -07:00
index.rst docs: process/index.rst: Fix reference to nonexistent document 2020-07-23 14:26:12 -06:00
kernel-docs.rst Documentation: Update details of The Linux Kernel Module Programming Guide 2021-08-20 17:05:45 -06:00
kernel-driver-statement.rst doc:it_IT: add some process/* translations 2018-11-20 09:11:12 -07:00
kernel-enforcement-statement.rst Documentation: drop optional BOMs 2021-05-10 15:17:34 -06:00
license-rules.rst LICENSES: Rename other to deprecated 2019-05-03 06:34:32 -06:00
magic-number.rst tty: remove TTY_LDISC_MAGIC 2021-03-10 09:34:06 +01:00
maintainer-pgp-guide.rst Documentation/process/maintainer-pgp-guide: Replace broken link to PGP path finder 2021-08-24 13:23:21 -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 doc: process: GPL -> GPL-compatible 2019-02-01 16:19:11 -07:00
stable-kernel-rules.rst Documentation: update stable tree link 2022-04-08 14:22:51 +02:00
submit-checklist.rst doc: use KCFLAGS instead of EXTRA_CFLAGS to pass flags from command line 2021-02-22 13:59:10 -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:43:41 +02:00
volatile-considered-harmful.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00