linux-next/Documentation/process
Kees Cook e1be43d9b5 overflow: Implement size_t saturating arithmetic helpers
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>
2022-02-16 14:29:48 -08: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: 5.Posting.rst: describe Fixes: and Link: tags 2022-01-07 09:33:13 -07:00
6.Followthrough.rst docs: rename development-process/ to process/ 2016-10-24 08:12:35 -02:00
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 Kbuild updates for v5.17 2022-01-19 11:15:19 +02: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 Code of Conduct Interpretation: Put in the proper URL for the committee 2018-10-22 07:33:36 +01:00
code-of-conduct.rst Code of Conduct: Change the contact email address 2018-10-22 07:33:36 +01:00
coding-style.rst coding-style.rst: trivial: fix location of driver model macros 2021-10-26 09:39:49 -06:00
deprecated.rst overflow: Implement size_t saturating arithmetic helpers 2022-02-16 14:29:48 -08:00
development-process.rst docs: rename development-process/ to process/ 2016-10-24 08:12:35 -02:00
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 Documentation/process: Add maintainer handbooks section 2021-09-14 14:46:49 -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-handbooks.rst Documentation/process: Add tip tree handbook 2021-09-14 14:46:49 -06: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
maintainer-tip.rst docs: use the lore redirector everywhere 2021-10-12 13:58:19 -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 docs: networking: drop special stable handling 2021-03-03 08:49:08 -08: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 docs: update file link location 2021-10-12 13:49:27 -06:00
submitting-patches.rst Remove mentions of the Trivial Patch Monkey 2021-12-16 15:43:53 -07:00
volatile-considered-harmful.rst Replace HTTP links with HTTPS ones: documentation 2020-06-08 09:30:19 -06:00