diff mbox series

[SRU,N,2/6] compiler_types: add Endianness-dependent __counted_by_{le, be}

Message ID 20241002094457.1777904-3-juerg.haefliger@canonical.com
State New
Headers show
Series UBSAN array-index-out-of-bounds reported with N-6.8 on P9 node baltar (LP: #2078038) | expand

Commit Message

Juerg Haefliger Oct. 2, 2024, 9:44 a.m. UTC
From: Alexander Lobakin <aleksander.lobakin@intel.com>

BugLink: https://bugs.launchpad.net/bugs/2078038

Some structures contain flexible arrays at the end and the counter for
them, but the counter has explicit Endianness and thus __counted_by()
can't be used directly.

To increase test coverage for potential problems without breaking
anything, introduce __counted_by_{le,be}() defined depending on
platform's Endianness to either __counted_by() when applicable or noop
otherwise.
Maybe it would be a good idea to introduce such attributes on compiler
level if possible, but for now let's stop on what we have.

Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Link: https://lore.kernel.org/r/20240327142241.1745989-2-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
(backported from commit ca7e324e8ad385a2da15049953c04ea7310687f7)
[juergh: Adjusted context in include/linux/compiler_types.h.]
Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
---
 Documentation/conf.py          |  2 ++
 include/linux/compiler_types.h | 11 +++++++++++
 scripts/kernel-doc             |  1 +
 3 files changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/Documentation/conf.py b/Documentation/conf.py
index d148f3e8dd57..0c2205d536b3 100644
--- a/Documentation/conf.py
+++ b/Documentation/conf.py
@@ -75,6 +75,8 @@  if major >= 3:
             "__rcu",
             "__user",
             "__force",
+            "__counted_by_le",
+            "__counted_by_be",
 
             # include/linux/compiler_attributes.h:
             "__alias",
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index a6a28952836c..454d942384a4 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -293,6 +293,17 @@  struct ftrace_likely_data {
 #define __no_sanitize_or_inline __always_inline
 #endif
 
+/*
+ * Apply __counted_by() when the Endianness matches to increase test coverage.
+ */
+#ifdef __LITTLE_ENDIAN
+#define __counted_by_le(member)	__counted_by(member)
+#define __counted_by_be(member)
+#else
+#define __counted_by_le(member)
+#define __counted_by_be(member)	__counted_by(member)
+#endif
+
 /* Section for code which can't be instrumented at all */
 #define __noinstr_section(section)					\
 	noinline notrace __attribute((__section__(section)))		\
diff --git a/scripts/kernel-doc b/scripts/kernel-doc
index 08df66070252..b2d2f5849ed0 100755
--- a/scripts/kernel-doc
+++ b/scripts/kernel-doc
@@ -1145,6 +1145,7 @@  sub dump_struct($$) {
         $members =~ s/\s*$attribute/ /gi;
         $members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
         $members =~ s/\s*__counted_by\s*\([^;]*\)/ /gos;
+        $members =~ s/\s*__counted_by_(le|be)\s*\([^;]*\)/ /gos;
         $members =~ s/\s*__packed\s*/ /gos;
         $members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
         $members =~ s/\s*____cacheline_aligned_in_smp/ /gos;