Re: [PATCH v2] scsi: ibmvscsi_tgt: replace deprecated strncpy with strscpy

2024-01-29 Thread Martin K. Petersen
On Tue, 12 Dec 2023 01:20:20 +, Justin Stitt wrote: > strncpy() is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > We don't need the NUL-padding behavior that strncpy() provides as vscsi > is N

Re: [RFC] string: Allow 2-argument strscpy()

2024-01-29 Thread Andy Shevchenko
On Tue, Jan 30, 2024 at 1:27 AM Andy Shevchenko wrote: > On Tue, Jan 30, 2024 at 12:03 AM Kees Cook wrote: > > On Mon, Jan 29, 2024 at 09:55:25PM +, Justin Stitt wrote: > > > On Mon, Jan 29, 2024 at 12:29:04PM -0800, Kees Cook wrote: ... > > > BTW, this hack for function overloading is insa

Re: [RFC] string: Allow 2-argument strscpy()

2024-01-29 Thread Andy Shevchenko
On Tue, Jan 30, 2024 at 12:03 AM Kees Cook wrote: > On Mon, Jan 29, 2024 at 09:55:25PM +, Justin Stitt wrote: > > On Mon, Jan 29, 2024 at 12:29:04PM -0800, Kees Cook wrote: ... > > BTW, this hack for function overloading is insane. Never really looked into > > it before. > > It very much is.

Re: [RFC] string: Allow 2-argument strscpy()

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 09:55:25PM +, Justin Stitt wrote: > Hi, > > On Mon, Jan 29, 2024 at 12:29:04PM -0800, Kees Cook wrote: > > Using sizeof(dst) is the overwhelmingly common case for strscpy(). > > Instead of requiring this everywhere, allow a 2-argument version to be > > used that will us

Re: [PATCH 5/5] overflow: Introduce inc_wrap() and dec_wrap()

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 09:16:36PM +0100, Rasmus Villemoes wrote: > On 29/01/2024 19.34, Kees Cook wrote: > > This allows replacements of the idioms "var += offset" and "var -= offset" > > with the inc_wrap() and dec_wrap() helpers respectively. They will avoid > > wrap-around sanitizer instrumenta

Re: [RFC] string: Allow 2-argument strscpy()

2024-01-29 Thread Justin Stitt
Hi, On Mon, Jan 29, 2024 at 12:29:04PM -0800, Kees Cook wrote: > Using sizeof(dst) is the overwhelmingly common case for strscpy(). > Instead of requiring this everywhere, allow a 2-argument version to be > used that will use the sizeof() internally. Yeah, this is definitely the case. I have a to

[RFC] string: Allow 2-argument strscpy()

2024-01-29 Thread Kees Cook
Using sizeof(dst) is the overwhelmingly common case for strscpy(). Instead of requiring this everywhere, allow a 2-argument version to be used that will use the sizeof() internally. Cc: Rasmus Villemoes Cc: Andy Shevchenko Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- What do

Re: [PATCH 4/5] overflow: Introduce add_wrap(), sub_wrap(), and mul_wrap()

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 09:08:43PM +0100, Rasmus Villemoes wrote: > On 29/01/2024 19.34, Kees Cook wrote: > > Provide helpers that will perform wrapping addition, subtraction, or > > multiplication without tripping the arithmetic wrap-around sanitizers. The > > first argument is the type under whic

Re: [PATCH 2/6] ubsan: Reintroduce signed and unsigned overflow sanitizers

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 07:54:18PM +, Justin Stitt wrote: > Hi, > > On Mon, Jan 29, 2024 at 10:00:39AM -0800, Kees Cook wrote: > > Effectively revert commit 6aaa31aeb9cf ("ubsan: remove overflow > > checks"), to allow the kernel to be built with the "overflow" > > sanitizers again. This gives

Re: [PATCH 5/5] overflow: Introduce inc_wrap() and dec_wrap()

2024-01-29 Thread Rasmus Villemoes
On 29/01/2024 19.34, Kees Cook wrote: > This allows replacements of the idioms "var += offset" and "var -= offset" > with the inc_wrap() and dec_wrap() helpers respectively. They will avoid > wrap-around sanitizer instrumentation. > > Cc: Rasmus Villemoes > Cc: Mark Rutland > Cc: "Gustavo A. R.

Re: [PATCH 4/5] overflow: Introduce add_wrap(), sub_wrap(), and mul_wrap()

2024-01-29 Thread Rasmus Villemoes
On 29/01/2024 19.34, Kees Cook wrote: > Provide helpers that will perform wrapping addition, subtraction, or > multiplication without tripping the arithmetic wrap-around sanitizers. The > first argument is the type under which the wrap-around should happen > with. In other words, these two calls wi

Re: [PATCH 2/6] ubsan: Reintroduce signed and unsigned overflow sanitizers

2024-01-29 Thread Justin Stitt
Hi, On Mon, Jan 29, 2024 at 10:00:39AM -0800, Kees Cook wrote: > Effectively revert commit 6aaa31aeb9cf ("ubsan: remove overflow > checks"), to allow the kernel to be built with the "overflow" > sanitizers again. This gives developers a chance to experiment[1][2][3] > with the instrumentation agai

Re: rcar-dmac.c: race condition regarding cookie handling?

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 08:08:28PM +0100, Geert Uytterhoeven wrote: > Hi Kees, > > On Mon, Jan 29, 2024 at 6:38 PM Kees Cook wrote> > > On Mon, Jan 29, 2024 at 10:57:40AM +0100, Geert Uytterhoeven wrote: > > > CC Kees (for the wrap-around in dma_cookie_assign() not handled in [A]) > > > [...] > >

Re: rcar-dmac.c: race condition regarding cookie handling?

2024-01-29 Thread Geert Uytterhoeven
Hi Kees, On Mon, Jan 29, 2024 at 6:38 PM Kees Cook wrote> > On Mon, Jan 29, 2024 at 10:57:40AM +0100, Geert Uytterhoeven wrote: > > CC Kees (for the wrap-around in dma_cookie_assign() not handled in [A]) > > [...] > > Was the system running for a very long time? > > dma_cookie_assign() relies on

Re: [PATCH 1/6] ubsan: Use Clang's -fsanitize-trap=undefined option

2024-01-29 Thread Fangrui Song
On Mon, Jan 29, 2024 at 10:00 AM Kees Cook wrote: > > Clang changed the way it enables UBSan trapping mode. Update the Makefile > logic to discover it. > > Suggested-by: Fangrui Song > Link: > https://lore.kernel.org/lkml/cafp8o3jivzh+aav7n90nk7u2bhrnst6mrp0zhtfq-vj0m4+...@mail.gmail.com/ > Cc:

[PATCH] select: Avoid wrap-around instrumentation in do_sys_poll()

2024-01-29 Thread Kees Cook
The mix of int, unsigned int, and unsigned long used by struct poll_list::len, todo, len, and j meant that the signed overflow sanitizer got worried it needed to instrument several places where arithmetic happens between these variables. Since all of the variables are always positive and bounded by

[PATCH] iov_iter: Avoid wrap-around instrumentation in copy_compat_iovec_from_user()

2024-01-29 Thread Kees Cook
The loop counter "i" in copy_compat_iovec_from_user() is an int, but because the nr_segs argument is unsigned long, the signed overflow sanitizer got worried "i" could wrap around. Instead of making "i" an unsigned long (which may enlarge the type size), switch both nr_segs and i to u32. There is n

[PATCH 5/5] overflow: Introduce inc_wrap() and dec_wrap()

2024-01-29 Thread Kees Cook
This allows replacements of the idioms "var += offset" and "var -= offset" with the inc_wrap() and dec_wrap() helpers respectively. They will avoid wrap-around sanitizer instrumentation. Cc: Rasmus Villemoes Cc: Mark Rutland Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-o

[PATCH 4/5] overflow: Introduce add_wrap(), sub_wrap(), and mul_wrap()

2024-01-29 Thread Kees Cook
Provide helpers that will perform wrapping addition, subtraction, or multiplication without tripping the arithmetic wrap-around sanitizers. The first argument is the type under which the wrap-around should happen with. In other words, these two calls will get very different results: add_wr

[PATCH 0/5] overflow: Introduce wrapping helpers

2024-01-29 Thread Kees Cook
Hi, In preparation for gaining instrumentation for signed[1], unsigned[2], and pointer[3] wrap-around, expand the overflow header to include wrap-around helpers that can be used to annotate arithmetic where wrapped calculations are expected (e.g. atomics). After spending time getting the unsigned

[PATCH 2/5] overflow: Expand check_add_overflow() for pointer addition

2024-01-29 Thread Kees Cook
The check_add_overflow() helper is mostly a wrapper around __builtin_add_overflow(), but GCC and Clang refuse to operate on pointer arguments that would normally be allowed if the addition were open-coded. For example, we have many places where pointer overflow is tested: struct foo *ptr;

[PATCH 1/5] overflow: Adjust check_*_overflow() kern-doc to reflect results

2024-01-29 Thread Kees Cook
The check_*_overflow() helpers will return results with potentially wrapped-around values. These values have always been checked by the selftests, so avoid the confusing language in the kern-doc. The idea of "safe for use" was relative to the expectation of whether or not the caller wants a wrapped

[PATCH 3/5] overflow: Introduce add_would_overflow()

2024-01-29 Thread Kees Cook
For instances where only the overflow needs to be checked (and the sum isn't used), provide the new helper add_would_overflow(), which is a wrapper for check_add_overflow(). Cc: Rasmus Villemoes Cc: "Gustavo A. R. Silva" Cc: linux-hardening@vger.kernel.org Signed-off-by: Kees Cook --- include/

[PATCH 5/6] ubsan: Split wrapping sanitizer Makefile rules

2024-01-29 Thread Kees Cook
To allow for fine-grained control of where the wrapping sanitizers can be disabled, split them from the main UBSAN CFLAGS into their own set of rules. Cc: Masahiro Yamada Cc: Nathan Chancellor Cc: Nicolas Schier Cc: linux-kbu...@vger.kernel.org Signed-off-by: Kees Cook --- scripts/Makefile.li

[PATCH 6/6] ubsan: Get x86_64 booting with unsigned wrap-around sanitizer

2024-01-29 Thread Kees Cook
In order to get x86_64 booting with the unsigned wrap-around sanitizer, several kernel areas that depend heavily on unsigned wrap-around need to be disabled entirely (with "UBSAN_UNSIGNED_WRAP := n"). As we fine-tune the sanitizer, we can revisit these and perform finer grain annotations. Signed-o

[PATCH 3/6] ubsan: Introduce CONFIG_UBSAN_POINTER_WRAP

2024-01-29 Thread Kees Cook
Gain coverage for pointer wrap-around checking. Adds support for -fsanitize=pointer-overflow, and introduces the __pointer_wrap function attribute to match the signed and unsigned attributes. Also like the others, it is currently disabled under CONFIG_COMPILE_TEST. Cc: Andrew Morton Cc: Masahiro

[PATCH 2/6] ubsan: Reintroduce signed and unsigned overflow sanitizers

2024-01-29 Thread Kees Cook
Effectively revert commit 6aaa31aeb9cf ("ubsan: remove overflow checks"), to allow the kernel to be built with the "overflow" sanitizers again. This gives developers a chance to experiment[1][2][3] with the instrumentation again, while compilers adjust their sanitizers to deal with the impact of -f

[PATCH 0/6] ubsan: Introduce wrap-around sanitizers

2024-01-29 Thread Kees Cook
Hi, Lay the ground work for gaining instrumentation for signed[1], unsigned[2], and pointer[3] wrap-around by making all 3 sanitizers available for testing. Additionally gets x86_64 bootable under the unsigned sanitizer for the first time. The compilers will need work before this can be generally

[PATCH 1/6] ubsan: Use Clang's -fsanitize-trap=undefined option

2024-01-29 Thread Kees Cook
Clang changed the way it enables UBSan trapping mode. Update the Makefile logic to discover it. Suggested-by: Fangrui Song Link: https://lore.kernel.org/lkml/cafp8o3jivzh+aav7n90nk7u2bhrnst6mrp0zhtfq-vj0m4+...@mail.gmail.com/ Cc: Nathan Chancellor Cc: Masahiro Yamada Cc: Nicolas Schier Cc: Ni

Re: rcar-dmac.c: race condition regarding cookie handling?

2024-01-29 Thread Kees Cook
On Mon, Jan 29, 2024 at 10:57:40AM +0100, Geert Uytterhoeven wrote: > Hi Dirk, > > CC Kees (for the wrap-around in dma_cookie_assign() not handled in [A]) > [...] > Was the system running for a very long time? > dma_cookie_assign() relies on 2-complement signed wrap-around: > > cookie = c

Re: [PATCH 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread Lee Jones
On Mon, 29 Jan 2024, David Laight wrote: > ... > > > I'm sure that the safest return for 'truncated' is the buffer length. > > > The a series of statements like: > > > buf += xxx(buf, buf_end - buf, .); > > > can all be called with a single overflow check at the end. > > > > > > Forget the c

RE: [PATCH 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread David Laight
... > > I'm sure that the safest return for 'truncated' is the buffer length. > > The a series of statements like: > > buf += xxx(buf, buf_end - buf, .); > > can all be called with a single overflow check at the end. > > > > Forget the check, and the length just contains a trailing '\0' > >

Re: [PATCH 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread Lee Jones
Please discard - missing version identifier in the subject line. New version here: https://lore.kernel.org/r/20240129092952.1980246-1-...@kernel.org -- Lee Jones [李琼斯]

[PATCH v2 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread Lee Jones
There is an ongoing effort to replace the use of {v}snprintf() variants with safer alternatives - for a more in depth view, see Jon's write-up on LWN [0] and/or Alex's on the Kernel Self Protection Project [1]. Whist executing the task, it quickly became apparent that the initial thought of simply

[PATCH 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread Lee Jones
There is an ongoing effort to replace the use of {v}snprintf() variants with safer alternatives - for a more in depth view, see Jon's write-up on LWN [0] and/or Alex's on the Kernel Self Protection Project [1]. Whist executing the task, it quickly became apparent that the initial thought of simply

Re: [PATCH 1/1] lib/vsprintf: Implement ssprintf() to catch truncated strings

2024-01-29 Thread Lee Jones
NB: I was _just_ about to send out v2 with Rasmus's suggestions before I saw your reply. I'm going to submit it anyway and Cc both you and Rasmus. If you still disagree with my suggested approach, we can either continue discussion here or on the new version. More below: > From: Lee Jones > > Se