linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] ubsan: introducing CONFIG_UBSAN_LOCAL_BOUNDS for Clang
@ 2020-09-22  7:43 George-Aurelian Popescu
  2020-10-02 14:43 ` George Popescu
  0 siblings, 1 reply; 3+ messages in thread
From: George-Aurelian Popescu @ 2020-09-22  7:43 UTC (permalink / raw)
  To: masahiroy, michal.lkml, natechancellor, ndesaulniers, keescook,
	akpm, elver, dvyukov
  Cc: peterz, arnd, linux-kernel, clang-built-linux, dbrazdil, George Popescu

From: George Popescu <georgepope@android.com>

When the kernel is compiled with Clang, -fsanitize=bounds expands to
-fsanitize=array-bounds and -fsanitize=local-bounds.

Enabling -fsanitize=local-bounds with Clang has the unfortunate
side-effect of inserting traps; this goes back to its original intent,
which was as a hardening and not a debugging feature [1]. The same feature
made its way into -fsanitize=bounds, but the traps remained. For that
reason, -fsanitize=bounds was split into 'array-bounds' and
'local-bounds' [2].

Since 'local-bounds' doesn't behave like a normal sanitizer, enable
it with Clang only if trapping behaviour was requested by
CONFIG_UBSAN_TRAP=y.

Add the UBSAN_BOUNDS_LOCAL config to Kconfig.ubsan to enable the
'local-bounds' option by default when UBSAN_TRAP is enabled.

[1] http://lists.llvm.org/pipermail/llvm-dev/2012-May/049972.html
[2] http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20131021/091536.html

Suggested-by: Marco Elver <elver@google.com>
Reviewed-by: David Brazdil <dbrazdil@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Signed-off-by: George Popescu <georgepope@android.com>

---
v2: changed the name of the config, in Kconfig, to UBSAN_LOCAL_BOUNDS
---
v3: added Reviewed-by tag
---
 lib/Kconfig.ubsan      | 14 ++++++++++++++
 scripts/Makefile.ubsan | 10 +++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
index 774315de555a..58f8d03d037b 100644
--- a/lib/Kconfig.ubsan
+++ b/lib/Kconfig.ubsan
@@ -47,6 +47,20 @@ config UBSAN_BOUNDS
 	  to the {str,mem}*cpy() family of functions (that is addressed
 	  by CONFIG_FORTIFY_SOURCE).
 
+config UBSAN_LOCAL_BOUNDS
+	bool "Perform array local bounds checking"
+	depends on UBSAN_TRAP
+	depends on CC_IS_CLANG
+	depends on !UBSAN_KCOV_BROKEN
+	help
+	  This option enables -fsanitize=local-bounds which traps when an
+	  exception/error is detected. Therefore, it should be enabled only
+	  if trapping is expected.
+	  Enabling this option detects errors due to accesses through a
+	  pointer that is derived from an object of a statically-known size,
+	  where an added offset (which may not be known statically) is
+	  out-of-bounds.
+
 config UBSAN_MISC
 	bool "Enable all other Undefined Behavior sanity checks"
 	default UBSAN
diff --git a/scripts/Makefile.ubsan b/scripts/Makefile.ubsan
index 27348029b2b8..4e3fff0745e8 100644
--- a/scripts/Makefile.ubsan
+++ b/scripts/Makefile.ubsan
@@ -4,7 +4,15 @@ ifdef CONFIG_UBSAN_ALIGNMENT
 endif
 
 ifdef CONFIG_UBSAN_BOUNDS
-      CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
+      ifdef CONFIG_CC_IS_CLANG
+            CFLAGS_UBSAN += -fsanitize=array-bounds
+      else
+            CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
+      endif
+endif
+
+ifdef CONFIG_UBSAN_LOCAL_BOUNDS
+      CFLAGS_UBSAN += -fsanitize=local-bounds
 endif
 
 ifdef CONFIG_UBSAN_MISC
-- 
2.28.0.681.g6f77f65b4e-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] ubsan: introducing CONFIG_UBSAN_LOCAL_BOUNDS for Clang
  2020-09-22  7:43 [PATCH v3] ubsan: introducing CONFIG_UBSAN_LOCAL_BOUNDS for Clang George-Aurelian Popescu
@ 2020-10-02 14:43 ` George Popescu
  2020-10-02 15:03   ` Marco Elver
  0 siblings, 1 reply; 3+ messages in thread
From: George Popescu @ 2020-10-02 14:43 UTC (permalink / raw)
  To: Masahiro Yamada, Michal Marek, Nathan Chancellor,
	Nick Desaulniers, Kees Cook, Andrew Morton, Marco Elver,
	Dmitry Vyukov
  Cc: peterz, Arnd Bergmann, LKML, clang-built-linux, David Brazdil,
	George Popescu

Is this patch ready to be merged?

Best regards,
George


On Tue, Sep 22, 2020 at 10:43 AM George-Aurelian Popescu
<georgepope@google.com> wrote:
>
> From: George Popescu <georgepope@android.com>
>
> When the kernel is compiled with Clang, -fsanitize=bounds expands to
> -fsanitize=array-bounds and -fsanitize=local-bounds.
>
> Enabling -fsanitize=local-bounds with Clang has the unfortunate
> side-effect of inserting traps; this goes back to its original intent,
> which was as a hardening and not a debugging feature [1]. The same feature
> made its way into -fsanitize=bounds, but the traps remained. For that
> reason, -fsanitize=bounds was split into 'array-bounds' and
> 'local-bounds' [2].
>
> Since 'local-bounds' doesn't behave like a normal sanitizer, enable
> it with Clang only if trapping behaviour was requested by
> CONFIG_UBSAN_TRAP=y.
>
> Add the UBSAN_BOUNDS_LOCAL config to Kconfig.ubsan to enable the
> 'local-bounds' option by default when UBSAN_TRAP is enabled.
>
> [1] http://lists.llvm.org/pipermail/llvm-dev/2012-May/049972.html
> [2] http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20131021/091536.html
>
> Suggested-by: Marco Elver <elver@google.com>
> Reviewed-by: David Brazdil <dbrazdil@google.com>
> Reviewed-by: Marco Elver <elver@google.com>
> Signed-off-by: George Popescu <georgepope@android.com>
>
> ---
> v2: changed the name of the config, in Kconfig, to UBSAN_LOCAL_BOUNDS
> ---
> v3: added Reviewed-by tag
> ---
>  lib/Kconfig.ubsan      | 14 ++++++++++++++
>  scripts/Makefile.ubsan | 10 +++++++++-
>  2 files changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
> index 774315de555a..58f8d03d037b 100644
> --- a/lib/Kconfig.ubsan
> +++ b/lib/Kconfig.ubsan
> @@ -47,6 +47,20 @@ config UBSAN_BOUNDS
>           to the {str,mem}*cpy() family of functions (that is addressed
>           by CONFIG_FORTIFY_SOURCE).
>
> +config UBSAN_LOCAL_BOUNDS
> +       bool "Perform array local bounds checking"
> +       depends on UBSAN_TRAP
> +       depends on CC_IS_CLANG
> +       depends on !UBSAN_KCOV_BROKEN
> +       help
> +         This option enables -fsanitize=local-bounds which traps when an
> +         exception/error is detected. Therefore, it should be enabled only
> +         if trapping is expected.
> +         Enabling this option detects errors due to accesses through a
> +         pointer that is derived from an object of a statically-known size,
> +         where an added offset (which may not be known statically) is
> +         out-of-bounds.
> +
>  config UBSAN_MISC
>         bool "Enable all other Undefined Behavior sanity checks"
>         default UBSAN
> diff --git a/scripts/Makefile.ubsan b/scripts/Makefile.ubsan
> index 27348029b2b8..4e3fff0745e8 100644
> --- a/scripts/Makefile.ubsan
> +++ b/scripts/Makefile.ubsan
> @@ -4,7 +4,15 @@ ifdef CONFIG_UBSAN_ALIGNMENT
>  endif
>
>  ifdef CONFIG_UBSAN_BOUNDS
> -      CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
> +      ifdef CONFIG_CC_IS_CLANG
> +            CFLAGS_UBSAN += -fsanitize=array-bounds
> +      else
> +            CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
> +      endif
> +endif
> +
> +ifdef CONFIG_UBSAN_LOCAL_BOUNDS
> +      CFLAGS_UBSAN += -fsanitize=local-bounds
>  endif
>
>  ifdef CONFIG_UBSAN_MISC
> --
> 2.28.0.681.g6f77f65b4e-goog
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] ubsan: introducing CONFIG_UBSAN_LOCAL_BOUNDS for Clang
  2020-10-02 14:43 ` George Popescu
@ 2020-10-02 15:03   ` Marco Elver
  0 siblings, 0 replies; 3+ messages in thread
From: Marco Elver @ 2020-10-02 15:03 UTC (permalink / raw)
  To: George Popescu
  Cc: Masahiro Yamada, Michal Marek, Nathan Chancellor,
	Nick Desaulniers, Kees Cook, Andrew Morton, Dmitry Vyukov,
	Peter Zijlstra, Arnd Bergmann, LKML, clang-built-linux,
	David Brazdil, George Popescu

On Fri, 2 Oct 2020 at 16:43, George Popescu <georgepope@google.com> wrote:
>
> Is this patch ready to be merged?

Andrew already picked this up and it's in the -mm tree:
https://lore.kernel.org/mm-commits/20200922170717.qhs0j%25akpm@linux-foundation.org/

You have to wait for the next merge window, which will likely start in
1.5 weeks (or, on Monday, but unlikely:
https://lwn.net/Articles/832733/).

Thanks,
-- Marco

> Best regards,
> George
>
>
> On Tue, Sep 22, 2020 at 10:43 AM George-Aurelian Popescu
> <georgepope@google.com> wrote:
> >
> > From: George Popescu <georgepope@android.com>
> >
> > When the kernel is compiled with Clang, -fsanitize=bounds expands to
> > -fsanitize=array-bounds and -fsanitize=local-bounds.
> >
> > Enabling -fsanitize=local-bounds with Clang has the unfortunate
> > side-effect of inserting traps; this goes back to its original intent,
> > which was as a hardening and not a debugging feature [1]. The same feature
> > made its way into -fsanitize=bounds, but the traps remained. For that
> > reason, -fsanitize=bounds was split into 'array-bounds' and
> > 'local-bounds' [2].
> >
> > Since 'local-bounds' doesn't behave like a normal sanitizer, enable
> > it with Clang only if trapping behaviour was requested by
> > CONFIG_UBSAN_TRAP=y.
> >
> > Add the UBSAN_BOUNDS_LOCAL config to Kconfig.ubsan to enable the
> > 'local-bounds' option by default when UBSAN_TRAP is enabled.
> >
> > [1] http://lists.llvm.org/pipermail/llvm-dev/2012-May/049972.html
> > [2] http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20131021/091536.html
> >
> > Suggested-by: Marco Elver <elver@google.com>
> > Reviewed-by: David Brazdil <dbrazdil@google.com>
> > Reviewed-by: Marco Elver <elver@google.com>
> > Signed-off-by: George Popescu <georgepope@android.com>
> >
> > ---
> > v2: changed the name of the config, in Kconfig, to UBSAN_LOCAL_BOUNDS
> > ---
> > v3: added Reviewed-by tag
> > ---
> >  lib/Kconfig.ubsan      | 14 ++++++++++++++
> >  scripts/Makefile.ubsan | 10 +++++++++-
> >  2 files changed, 23 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
> > index 774315de555a..58f8d03d037b 100644
> > --- a/lib/Kconfig.ubsan
> > +++ b/lib/Kconfig.ubsan
> > @@ -47,6 +47,20 @@ config UBSAN_BOUNDS
> >           to the {str,mem}*cpy() family of functions (that is addressed
> >           by CONFIG_FORTIFY_SOURCE).
> >
> > +config UBSAN_LOCAL_BOUNDS
> > +       bool "Perform array local bounds checking"
> > +       depends on UBSAN_TRAP
> > +       depends on CC_IS_CLANG
> > +       depends on !UBSAN_KCOV_BROKEN
> > +       help
> > +         This option enables -fsanitize=local-bounds which traps when an
> > +         exception/error is detected. Therefore, it should be enabled only
> > +         if trapping is expected.
> > +         Enabling this option detects errors due to accesses through a
> > +         pointer that is derived from an object of a statically-known size,
> > +         where an added offset (which may not be known statically) is
> > +         out-of-bounds.
> > +
> >  config UBSAN_MISC
> >         bool "Enable all other Undefined Behavior sanity checks"
> >         default UBSAN
> > diff --git a/scripts/Makefile.ubsan b/scripts/Makefile.ubsan
> > index 27348029b2b8..4e3fff0745e8 100644
> > --- a/scripts/Makefile.ubsan
> > +++ b/scripts/Makefile.ubsan
> > @@ -4,7 +4,15 @@ ifdef CONFIG_UBSAN_ALIGNMENT
> >  endif
> >
> >  ifdef CONFIG_UBSAN_BOUNDS
> > -      CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
> > +      ifdef CONFIG_CC_IS_CLANG
> > +            CFLAGS_UBSAN += -fsanitize=array-bounds
> > +      else
> > +            CFLAGS_UBSAN += $(call cc-option, -fsanitize=bounds)
> > +      endif
> > +endif
> > +
> > +ifdef CONFIG_UBSAN_LOCAL_BOUNDS
> > +      CFLAGS_UBSAN += -fsanitize=local-bounds
> >  endif
> >
> >  ifdef CONFIG_UBSAN_MISC
> > --
> > 2.28.0.681.g6f77f65b4e-goog
> >

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-02 15:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-22  7:43 [PATCH v3] ubsan: introducing CONFIG_UBSAN_LOCAL_BOUNDS for Clang George-Aurelian Popescu
2020-10-02 14:43 ` George Popescu
2020-10-02 15:03   ` Marco Elver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).