Re: [PATCH v3] ubsan: Reintroduce signed overflow sanitizer

From: Marco Elver
Date: Mon Feb 05 2024 - 08:11:41 EST


On Mon, 5 Feb 2024 at 13:59, Kees Cook <keescook@xxxxxxxxxxxx> wrote:
>
> On Mon, Feb 05, 2024 at 01:54:24PM +0100, Andrey Ryabinin wrote:
> >
> >
> > On 2/5/24 10:37, Kees Cook wrote:
> >
> > > ---
> > > include/linux/compiler_types.h | 9 ++++-
> > > lib/Kconfig.ubsan | 14 +++++++
> > > lib/test_ubsan.c | 37 ++++++++++++++++++
> > > lib/ubsan.c | 68 ++++++++++++++++++++++++++++++++++
> > > lib/ubsan.h | 4 ++
> > > scripts/Makefile.lib | 3 ++
> > > scripts/Makefile.ubsan | 3 ++
> > > 7 files changed, 137 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> > > index 6f1ca49306d2..ee9d272008a5 100644
> > > --- a/include/linux/compiler_types.h
> > > +++ b/include/linux/compiler_types.h
> > > @@ -282,11 +282,18 @@ struct ftrace_likely_data {
> > > #define __no_sanitize_or_inline __always_inline
> > > #endif
> > >
> > > +/* Do not trap wrapping arithmetic within an annotated function. */
> > > +#ifdef CONFIG_UBSAN_SIGNED_WRAP
> > > +# define __signed_wrap __attribute__((no_sanitize("signed-integer-overflow")))
> > > +#else
> > > +# define __signed_wrap
> > > +#endif
> > > +
> > > /* Section for code which can't be instrumented at all */
> > > #define __noinstr_section(section) \
> > > noinline notrace __attribute((__section__(section))) \
> > > __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \
> > > - __no_sanitize_memory
> > > + __no_sanitize_memory __signed_wrap
> > >
> >
> > Given this disables all kinds of code instrumentations,
> > shouldn't we just add __no_sanitize_undefined here?
>
> Yeah, that's a very good point.
>
> > I suspect that ubsan's instrumentation usually doesn't cause problems
> > because it calls __ubsan_* functions with all heavy stuff (printk, locks etc)
> > only if code has an UB. So the answer to the question above depends on
> > whether we want to ignore UBs in "noinstr" code or to get some weird side effect,
> > possibly without proper UBSAN report in dmesg.
>
> I think my preference would be to fail safe (i.e. leave in the
> instrumentation), but the intent of noinstr is pretty clear. :P I wonder
> if, instead, we could adjust objtool to yell about cases where calls are
> made in noinstr functions (like it does for UACCESS)... maybe it already
> does?

It already does, see CONFIG_NOINSTR_VALIDATION (yes by default on x86).