linux-stable/Documentation
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
..
ABI x86/speculation: Add Gather Data Sampling mitigation 2023-08-08 19:57:38 +02:00
accounting psi: Fix uaf issue when psi trigger is destroyed while being polled 2022-02-05 12:37:55 +01:00
admin-guide Documentation: sysctl: align cells in second content column 2023-10-25 11:54:18 +02:00
arm ARM: 9012/1: move device tree mapping out of linear region 2021-05-19 10:13:18 +02:00
arm64 perf/smmuv3: Enable HiSilicon Erratum 162001900 quirk for HIP08/09 2023-09-23 11:01:05 +02:00
block block-5.10-2020-10-24 2020-10-24 12:46:42 -07:00
bpf bpf: Migrate from patchwork.ozlabs.org to patchwork.kernel.org. 2020-10-11 22:05:47 +02:00
cdrom
core-api kernel.h: split out kstrtox() and simple_strtox() to a separate header 2023-06-21 15:45:35 +02:00
cpu-freq
crypto crypto: af_alg - add extra parameters for DRBG interface 2020-09-25 17:48:52 +10:00
dev-tools docs/scripts/gdb: add necessary make scripts_gdb step 2023-03-11 16:39:52 +01:00
devicetree dt-bindings: clock: xlnx,versal-clk: drop select:false 2023-09-19 12:20:24 +02:00
doc-guide docs: kerneldoc.py: add support for kerneldoc -nosymbol 2020-10-15 07:49:38 +02:00
driver-api spi: Update reference to struct spi_controller 2023-01-14 10:15:26 +01:00
fault-injection debugfs: fix error when writing negative value to atomic_t debugfs file 2023-01-14 10:15:19 +01:00
fb drm fixes (round two) for 5.10-rc1 2020-10-23 13:56:34 -07:00
features s390 updates for the 5.10 merge window 2020-10-16 12:36:38 -07:00
filesystems fs: Lock moved directories 2023-07-27 08:44:13 +02:00
firmware_class
firmware-guide Documentation: ACPI: EINJ: Fix obsolete example 2022-08-25 11:37:53 +02:00
fpga
gpu Revert "fbcon: Disable accelerated scrolling" 2022-02-08 18:30:40 +01:00
hid
hwmon hwmon: (lm90) Add basic support for TI TMP461 2021-12-29 12:25:59 +01:00
i2c Documentation: i2c: add testunit docs to index 2020-10-05 22:57:45 +02:00
ia64
ide
iio
infiniband
input Input: iforce - add support for Boeder Force Feedback Wheel 2022-09-20 12:38:32 +02:00
isdn
kbuild Documentation/Kbuild: Remove references to gcc-plugin.sh 2021-12-14 11:32:46 +01:00
kernel-hacking docs: futex: Fix kernel-doc references after code split-up preparation 2023-04-26 11:27:40 +02:00
leds docs: leds: index.rst: add a missing file 2020-11-02 13:45:37 +01:00
litmus-tests
livepatch
locking Documentation/locking/locktypes: Update migrate_disable() bits. 2021-12-14 11:32:42 +01:00
m68k
maintainer
mhi
mips dt: Remove booting-without-of.rst 2020-10-13 13:33:16 -05:00
misc-devices Documentation: remove mic/index from misc-devices/index.rst 2020-11-04 11:38:32 +01:00
netlabel
networking net: change accept_ra_min_rtr_lft to affect all RA lifetimes 2023-10-25 11:54:14 +02:00
nios2
nvdimm
openrisc
parisc
PCI Documentation: better locations for sysfs-pci, sysfs-tagging 2020-10-09 09:33:23 -06:00
pcmcia
power PCI/PM: Rename pci_dev.d3_delay to d3hot_delay 2020-09-29 14:21:50 -05:00
powerpc powerpc/doc: Fix htmldocs errors 2023-04-26 11:27:37 +02:00
process overflow: Implement size_t saturating arithmetic helpers 2023-11-20 11:06:44 +01:00
RCU Merge branch 'for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu into core/rcu 2020-10-09 08:21:56 +02:00
riscv
s390
scheduler
scsi scsi: core: Fix the scsi_set_resid() documentation 2023-09-19 12:20:21 +02:00
security watch_queue: Drop references to /dev/watch_queue 2021-03-04 11:37:59 +01:00
sh dt: Remove booting-without-of.rst 2020-10-13 13:33:16 -05:00
sound ALSA: hda/sigmatel: add pin overrides for Intel DP45SG motherboard 2023-04-20 12:10:24 +02:00
sparc
sphinx docs: Fix the docs build with Sphinx 6.0 2023-01-18 11:44:53 +01:00
sphinx-static
spi
staging
target tweewide: Fix most Shebang lines 2021-05-22 11:40:55 +02:00
timers
trace attr: use consistent sgid stripping checks 2023-03-22 13:30:08 +01:00
translations docs: futex: Fix kernel-doc references after code split-up preparation 2023-04-26 11:27:40 +02:00
usb
userspace-api Remove DECnet support from kernel 2023-06-21 15:45:38 +02:00
virt KVM: s390: disable migration mode when dirty tracking is disabled 2023-03-11 16:39:58 +01:00
vm arm: remove CONFIG_ARCH_HAS_HOLES_MEMORYMODEL 2022-05-15 20:00:09 +02:00
w1 docs: w1: w1_therm: Fix broken xref, mistakes, clarify text 2020-10-08 09:47:15 +02:00
watchdog
x86 x86/CPU/AMD: Save AMD NodeId as cpu_die_id 2020-12-30 11:54:29 +01:00
xtensa xtensa: fix TLBTEMP area placement 2020-11-16 02:13:15 -08:00
.gitignore
asm-annotations.rst x86/entry: Emit a symbol for register restoring thunk 2021-02-03 23:28:40 +01:00
atomic_bitops.txt locking/atomic: Make test_and_*_bit() ordered on failure 2022-08-25 11:38:10 +02:00
atomic_t.txt
Changes
CodingStyle
conf.py docs/conf.py: Cope with removal of language=None in Sphinx 5.0.0 2022-06-09 10:21:28 +02:00
COPYING-logo
docutils.conf
dontdiff kbuild: generate Module.symvers only when vmlinux exists 2021-05-19 10:12:59 +02:00
index.rst
Kconfig docs: Kconfig/Makefile: add a check for broken ABI files 2020-10-30 13:08:07 +01:00
logo.gif
Makefile A small number of fixes, plus a build tweak to respect the desire for 2020-11-03 09:57:30 -08:00
memory-barriers.txt
SubmittingPatches
watch_queue.rst