[PATCH 0/2] Adjust brk randomness

2024-02-16 Thread Kees Cook
Hi, It was recently pointed out[1] that x86_64 brk entropy was not great, and that on all architectures the brk can (when the random offset is 0) be immediately adjacent to .bss, leaving no gap that could stop linear overflows from the .bss. Address both issues. -Kees Link:

[PATCH 1/2] x86: Increase brk randomness entropy on x86_64

2024-02-16 Thread Kees Cook
In commit c1d171a00294 ("x86: randomize brk"), arch_randomize_brk() was defined to use a 32MB range (13 bits of entropy), but was never increased when moving to 64-bit. The default arch_randomize_brk() uses 32MB for 32-bit tasks, and 1GB (18 bits of entropy) for 64-bit tasks. Update x86_64 to

[PATCH 2/2] binfmt_elf: Leave a gap between .bss and brk

2024-02-16 Thread Kees Cook
Currently the brk starts its randomization immediately after .bss, which means there is a chance that when the random offset is 0, linear overflows from .bss can reach into the brk area. Leave at least a single page gap between .bss and brk (when it has not already been explicitly relocated into

Re: [PATCH] x86/vdso: Move vDSO to mmap region

2024-02-16 Thread Kees Cook
On Sat, Feb 10, 2024 at 01:18:35AM -0800, Kees Cook wrote: > The vDSO (and its initial randomization) was introduced in commit > 2aae950b21e4 ("x86_64: Add vDSO for x86-64 with > gettimeofday/clock_gettime/getcpu"), > but had very low entropy. The entropy was improved in commit > 394f56fe4801

[PATCH v3 4/5] fortify: Add KUnit tests for runtime overflows

2024-02-16 Thread Kees Cook
With fortify overflows able to be redirected, we can use KUnit to exercise the overflow conditions. Add tests for every API covered by CONFIG_FORTIFY_SOURCE, except for memset() and memcpy(), which are special-cased for now. Note that this makes the LKDTM FORTIFY_STR* tests obsolete, but those

[PATCH v3 5/5] fortify: Improve buffer overflow reporting

2024-02-16 Thread Kees Cook
Improve the reporting of buffer overflows under CONFIG_FORTIFY_SOURCE to help accelerate debugging efforts. The calculations are all just sitting in registers anyway, so pass them along to the function to be reported. For example, before: detected buffer overflow in memcpy and after:

[PATCH v3 3/5] fortify: Provide KUnit counters for failure testing

2024-02-16 Thread Kees Cook
The standard C string APIs were not designed to have a failure mode; they were expected to always succeed without memory safety issues. Normally, CONFIG_FORTIFY_SOURCE will use fortify_panic() to stop processing, as truncating a read or write may provide an even worse system state. However, this

[PATCH v3 1/5] fortify: Split reporting and avoid passing string pointer

2024-02-16 Thread Kees Cook
In preparation for KUnit testing and further improvements in fortify failure reporting, split out the report and encode the function and access failure (read or write overflow) into a single u8 argument. This mainly ends up saving a tiny bit of space in the data segment. For a defconfig with

[PATCH v3 0/5] fortify: Add KUnit tests for runtime overflows

2024-02-16 Thread Kees Cook
Hi, This series is the rest of the v2 series that was half landed last year, and finally introduces KUnit runtime testing of the CONFIG_FORTIFY_SOURCE APIs. Additionally FORTIFY failure messages are improved to give more context about read/write and sizes. -Kees v3 - rebase (goodbye strlcpy)

[PATCH v3 2/5] fortify: Allow KUnit test to build without FORTIFY

2024-02-16 Thread Kees Cook
In order for CI systems to notice all the skipped tests related to CONFIG_FORTIFY_SOURCE, allow the FORTIFY_SOURCE KUnit tests to build with or without CONFIG_FORTIFY_SOURCE. Signed-off-by: Kees Cook --- Cc: linux-hardening@vger.kernel.org --- lib/Kconfig.debug | 2 +- lib/fortify_kunit.c |

Re: [PATCH v2] sock: Use unsafe_memcpy() for sock_copy()

2024-02-16 Thread Kuniyuki Iwashima
From: Kees Cook Date: Fri, 16 Feb 2024 15:22:23 -0800 > While testing for places where zero-sized destinations were still showing > up in the kernel, sock_copy() and inet_reqsk_clone() were found, which > are using very specific memcpy() offsets for both avoiding a portion of > struct sock, and

Re: [PATCH] net: sched: Annotate struct tc_pedit with __counted_by

2024-02-16 Thread Gustavo A. R. Silva
On 2/16/24 17:27, Kees Cook wrote: Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and

[PATCH] netfilter: x_tables: Use unsafe_memcpy() for 0-sized destination

2024-02-16 Thread Kees Cook
The struct xt_entry_target fake flexible array has not be converted to a true flexible array, which is mainly blocked by it being both UAPI and used in the middle of other structures. In order to properly check for 0-sized destinations in memcpy(), an exception must be made for the one place where

[PATCH] enic: Avoid false positive under FORTIFY_SOURCE

2024-02-16 Thread Kees Cook
FORTIFY_SOURCE has been ignoring 0-sized destinations while the kernel code base has been converted to flexible arrays. In order to enforce the 0-sized destinations (e.g. with __counted_by), the remaining 0-sized destinations need to be handled. Unfortunately, struct vic_provinfo resists full

[PATCH] ntfs: Replace struct ntfs_name 0-sized array with flexible array

2024-02-16 Thread Kees Cook
ntfs_name::name is used as a destination in memcpy(), so it cannot be a 0-sized array any more. Convert it to a flexible array and annotated with __counted_by, which matches the allocations. Signed-off-by: Kees Cook --- Cc: Anton Altaparmakov Cc: Namjae Jeon Cc: "Gustavo A. R. Silva" Cc:

[PATCH] greybus: Avoid fake flexible array for response data

2024-02-16 Thread Kees Cook
FORTIFY_SOURCE has been ignoring 0-sized destinations while the kernel code base has been converted to flexible arrays. In order to enforce the 0-sized destinations (e.g. with __counted_by), the remaining 0-sized destinations need to be handled. Instead of converting an empty struct into using a

[PATCH] net: sched: Annotate struct tc_pedit with __counted_by

2024-02-16 Thread Kees Cook
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family

[PATCH v2] sock: Use unsafe_memcpy() for sock_copy()

2024-02-16 Thread Kees Cook
While testing for places where zero-sized destinations were still showing up in the kernel, sock_copy() and inet_reqsk_clone() were found, which are using very specific memcpy() offsets for both avoiding a portion of struct sock, and copying beyond the end of it (since struct sock is really just a

Re: [PATCH] sock: Use unsafe_memcpy() for sock_copy()

2024-02-16 Thread Kuniyuki Iwashima
From: Kees Cook Date: Fri, 16 Feb 2024 12:44:24 -0800 > While testing for places where zero-sized destinations were still > showing up in the kernel, sock_copy() was found, which is using very > specific memcpy() offsets for both avoiding a portion of struct sock, > and copying beyond the end of

Re: [PATCH] fortify: Include more details when reporting overflows

2024-02-16 Thread Kees Cook
On Fri, Feb 16, 2024 at 12:39:41PM -0800, Kees Cook wrote: > When a memcpy() would exceed the length of an entire structure, no > detailed WARN would be emitted, making debugging a bit more challenging. > Similarly, other buffer overflow reports would have no size information > reported. > >

Re: [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4

2024-02-16 Thread Kees Cook
On Fri, Feb 16, 2024 at 01:27:56PM -0600, Gustavo A. R. Silva wrote: > Fix boot crash on Raspberry Pi by moving the update to `event->datalen` > before data is copied into flexible-array member `data` via `memcpy()`. > > Flexible-array member `data` was annotated with `__counted_by(datalen)` > in

[PATCH] sock: Use unsafe_memcpy() for sock_copy()

2024-02-16 Thread Kees Cook
While testing for places where zero-sized destinations were still showing up in the kernel, sock_copy() was found, which is using very specific memcpy() offsets for both avoiding a portion of struct sock, and copying beyond the end of it (since struct sock is really just a common header before the

[PATCH] fortify: Include more details when reporting overflows

2024-02-16 Thread Kees Cook
When a memcpy() would exceed the length of an entire structure, no detailed WARN would be emitted, making debugging a bit more challenging. Similarly, other buffer overflow reports would have no size information reported. Always warn for memcpy() overflows, but distinguish between the two cases

Re: [PATCH v2] module: Don't ignore errors from set_memory_XX()

2024-02-16 Thread Christophe Leroy
Le 16/02/2024 à 20:28, Kees Cook a écrit : > On Fri, Feb 16, 2024 at 09:14:27AM +0100, Christophe Leroy wrote: >> set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers >> can fail and return an error. In that case the memory might not be >> protected as expected and the module

Re: [PATCH v2] module: Don't ignore errors from set_memory_XX()

2024-02-16 Thread Kees Cook
On Fri, Feb 16, 2024 at 09:14:27AM +0100, Christophe Leroy wrote: > set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers > can fail and return an error. In that case the memory might not be > protected as expected and the module loading has to be aborted to > avoid security issues. >

[PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4

2024-02-16 Thread Gustavo A. R. Silva
Fix boot crash on Raspberry Pi by moving the update to `event->datalen` before data is copied into flexible-array member `data` via `memcpy()`. Flexible-array member `data` was annotated with `__counted_by(datalen)` in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for struct

Re: [PATCH v2] module: Don't ignore errors from set_memory_XX()

2024-02-16 Thread Luis Chamberlain
On Fri, Feb 16, 2024 at 09:14:27AM +0100, Christophe Leroy wrote: > set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers > can fail and return an error. In that case the memory might not be > protected as expected and the module loading has to be aborted to > avoid security issues. >

Re: [PATCH] iommu/mtk_iommu: Use devm_kcalloc() instead of devm_kzalloc()

2024-02-16 Thread Joerg Roedel
On Sun, Feb 11, 2024 at 07:22:50PM +0100, Erick Archer wrote: > Link: https://github.com/KSPP/linux/issues/162 [1] > Link: > https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments > [2] > Signed-off-by: Erick Archer > --- >

Re: Coverity: __do_sys_pidfd_send_signal(): UNINIT

2024-02-16 Thread Christian Brauner
On Wed, Feb 14, 2024 at 08:18:01PM +0100, Oleg Nesterov wrote: > On 02/14, Tycho Andersen wrote: > > > > On Wed, Feb 14, 2024 at 06:55:55PM +0100, Oleg Nesterov wrote: > > > > > > We want to check the "flags" argument at the start, we do not want to > > > delay the "case 0:" check until we have

Re: [cocci] [PATCH v2] cocci: Add rules to find str_plural() replacements

2024-02-16 Thread Markus Elfring
> Add rules for finding places where str_plural() can be used. … > +++ b/scripts/coccinelle/api/string_choices.cocci > @@ -0,0 +1,41 @@ … > +/// Find places to use string_choices.h's various helpers. > +// > +// Confidence: Medium > +// Options: --no-includes --include-headers > +virtual patch >

Re: [PATCH v2] module: Don't ignore errors from set_memory_XX()

2024-02-16 Thread Marek Szyprowski
On 16.02.2024 09:14, Christophe Leroy wrote: > set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers > can fail and return an error. In that case the memory might not be > protected as expected and the module loading has to be aborted to > avoid security issues. > > Check return value

[PATCH v2] module: Don't ignore errors from set_memory_XX()

2024-02-16 Thread Christophe Leroy
set_memory_ro(), set_memory_nx(), set_memory_x() and other helpers can fail and return an error. In that case the memory might not be protected as expected and the module loading has to be aborted to avoid security issues. Check return value of all calls to set_memory_XX() and handle error if