Ubuntu-focal-kernel/Documentation
Kees Cook 7eef7457c7 overflow: Implement size_t saturating arithmetic helpers
BugLink: https://bugs.launchpad.net/bugs/2080595

commit e1be43d9b5d0d1310dbd90185a8e5c7145dde40f upstream.

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
2024-09-27 10:50:22 +02:00
..
ABI net: sysfs: Fix /sys/class/net/<iface> path 2024-03-28 15:18:39 +01:00
EDID
PCI
RCU
accounting psi: Fix uaf issue when psi trigger is destroyed while being polled 2022-03-29 09:13:53 +02:00
admin-guide x86/cpu: Enable STIBP on AMD if Automatic IBRS is enabled 2024-06-07 15:01:38 +02:00
arm ARM: 9012/1: move device tree mapping out of linear region 2021-06-18 15:02:21 +02:00
arm64 arm64: cpufeature: Fix the visibility of compat hwcaps 2024-09-27 10:50:21 +02:00
block
bpf
cdrom
cgroups
core-api XArray: add xas_split 2021-07-15 19:27:10 +02:00
cpu-freq
crypto
dev-tools docs/scripts/gdb: add necessary make scripts_gdb step 2023-05-12 17:15:07 +02:00
devicetree ASoC: dt-bindings: rt5645: add cbj sleeve gpio property 2024-07-05 10:51:55 +02:00
doc-guide
driver-api spi: Update reference to struct spi_controller 2023-02-01 15:22:23 +01:00
fault-injection debugfs: fix error when writing negative value to atomic_t debugfs file 2023-02-01 15:22:16 +01:00
fb
features
filesystems rename(): fix the locking of subdirectories 2024-03-28 15:18:31 +01:00
firmware-guide Documentation: ACPI: EINJ: Fix obsolete example 2022-10-17 15:01:38 +02:00
firmware_class
fpga
gpu
hid
hwmon hwmon: (lm90) Add basic support for TI TMP461 2022-03-07 16:34:27 +01:00
i2c
ia64
ide
iio
infiniband
input Input: iforce - add support for Boeder Force Feedback Wheel 2022-11-24 10:28:20 +01:00
ioctl Remove DECnet support from kernel 2023-09-01 14:48:44 +02:00
isdn
kbuild kbuild: support LLVM=1 to switch the default tools to Clang/LLVM 2020-09-16 05:13:26 -04:00
kernel-hacking
kmsg
leds
livepatch
locking
m68k
maintainer
media media: rename VFL_TYPE_GRABBER to _VIDEO 2024-06-07 15:01:29 +02:00
mic
mips
misc-devices
netlabel
networking xsk: Honor SO_BINDTODEVICE on bind 2023-10-02 12:12:08 +02:00
nios2
openrisc
parisc
pcmcia
power PM: runtime: Add pm_runtime_get_if_active() 2023-10-30 11:41:56 +01:00
powerpc
process overflow: Implement size_t saturating arithmetic helpers 2024-09-27 10:50:22 +02:00
riscv
s390
scheduler
scsi
security
sh
sound ASoC: doc: Fix undefined SND_SOC_DAPM_NOPM argument 2024-03-28 15:18:36 +01:00
sparc
sphinx docs: kernel_include.py: Cope with docutils 0.21 2024-07-05 10:51:55 +02:00
sphinx-static
spi
target tweewide: Fix most Shebang lines 2021-06-18 15:02:27 +02:00
timers
trace tracing/histogram: Update document for KEYS_MAX size 2023-01-06 08:44:44 -08:00
translations media: rename VFL_TYPE_GRABBER to _VIDEO 2024-06-07 15:01:29 +02:00
usb
userspace-api Documentation: seccomp: Fix user notification documentation 2021-06-18 15:02:42 +02:00
virt KVM: s390: disable migration mode when dirty tracking is disabled 2023-05-12 17:15:09 +02:00
virtual
vm arm: remove CONFIG_ARCH_HAS_HOLES_MEMORYMODEL 2022-07-11 16:39:53 +02:00
w1
watchdog
x86 x86/CPU/AMD: Save AMD NodeId as cpu_die_id 2021-01-20 14:26:39 +01:00
xtensa xtensa: fix TLBTEMP area placement 2021-01-20 14:24:28 +01:00
.gitignore
COPYING-logo
Changes
CodingStyle
DMA-API-HOWTO.txt
DMA-API.txt
DMA-ISA-LPC.txt
DMA-attributes.txt Reinstate some of "swiotlb: rework "fix info leak with DMA_FROM_DEVICE"" 2022-08-26 11:06:53 +02:00
IPMI.txt
IRQ-affinity.txt
IRQ-domain.txt
IRQ.txt
Kconfig
Makefile
SubmittingPatches
asm-annotations.rst linkage: Introduce new macros for assembler symbols 2020-12-10 12:05:43 +01:00
atomic_bitops.txt locking/atomic: Make test_and_*_bit() ordered on failure 2022-10-17 15:01:52 +02:00
atomic_t.txt
bus-virt-phys-mapping.txt
conf.py docs/conf.py: Cope with removal of language=None in Sphinx 5.0.0 2022-08-26 11:08:41 +02:00
crc32.txt
debugging-modules.txt
debugging-via-ohci1394.txt
digsig.txt
docutils.conf
dontdiff
futex-requeue-pi.txt
hwspinlock.txt
index.rst linkage: Introduce new macros for assembler symbols 2020-12-10 12:05:43 +01:00
io-mapping.txt
io_ordering.txt
irqflags-tracing.txt
kobject.txt
kprobes.txt
kref.txt
logo.gif
lzo.txt
mailbox.txt
memory-barriers.txt
nommu-mmap.txt
padata.txt
percpu-rw-semaphore.txt
pi-futex.txt
preempt-locking.txt
rbtree.txt
remoteproc.txt
robust-futex-ABI.txt
robust-futexes.txt
rpmsg.txt
speculation.txt
static-keys.txt
tee.txt
this_cpu_ops.txt
unaligned-memory-access.txt
xz.txt