From: Kees Cook <kees@kernel.org>
To: Miguel Ojeda <ojeda@kernel.org>
Cc: "Kees Cook" <kees@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Marco Elver" <elver@google.com>,
"Przemek Kitszel" <przemyslaw.kitszel@intel.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Masahiro Yamada" <masahiroy@kernel.org>,
"Christophe Leroy" <christophe.leroy@csgroup.eu>,
"Johannes Weiner" <hannes@cmpxchg.org>,
llvm@lists.linux.dev, "Al Viro" <viro@zeniv.linux.org.uk>,
"Arnd Bergmann" <arnd@arndb.de>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>,
"Nicolas Schier" <nicolas.schier@linux.dev>,
"Shuah Khan" <shuah@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"Tamir Duberstein" <tamird@gmail.com>,
"Michael Kelley" <mhklinux@outlook.com>,
"kernel test robot" <lkp@intel.com>,
"Heiko Carstens" <hca@linux.ibm.com>,
"Uros Bizjak" <ubizjak@gmail.com>,
"Jan Hendrik Farr" <kernel@jfarr.cc>,
"Yafang Shao" <laoar.shao@gmail.com>,
"Marc Herbert" <Marc.Herbert@linux.intel.com>,
"Christopher Ferris" <cferris@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Alexander Lobakin" <aleksander.lobakin@intel.com>,
"Paolo Abeni" <pabeni@redhat.com>, "Tejun Heo" <tj@kernel.org>,
"Jeff Xu" <jeffxu@chromium.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Shakeel Butt" <shakeel.butt@linux.dev>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Brian Gerst" <brgerst@gmail.com>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-kbuild@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH 1/3] compiler_types: Introduce __counted_by_ptr()
Date: Mon, 20 Oct 2025 15:01:15 -0700 [thread overview]
Message-ID: <20251020220118.1226740-1-kees@kernel.org> (raw)
In-Reply-To: <20251020220005.work.095-kees@kernel.org>
Introduce __counted_by_ptr(), which works like __counted_by(), but for
pointer struct members:
struct foo {
int a, b, c;
char *buffer __counted_by_ptr(bytes);
short nr_bars;
struct bar *bars __counted_by_ptr(nr_bars);
size_t bytes;
};
Since "counted_by" can only be applied to pointer members in very recent
compiler versions, its application ends up needing to be distinct from
flexible array "counted_by" annotations, hence a separate macro.
Unfortunately, this annotation cannot be used for "void *" members
(since such a member is considered a pointer to an incomplete type,
and neither Clang nor GCC developers could be convinced otherwise[1],
even in the face of the GNU extension that "void *" has size "1 byte"
for pointer arithmetic). For "void *" members, we must use the coming
"sized_by" attribute.
Link: https://gcc.gnu.org/pipermail/gcc-patches/2025-May/683136.html [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Marco Elver <elver@google.com>
Cc: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: <llvm@lists.linux.dev>
---
init/Kconfig | 11 +++++++++++
Makefile | 4 ++++
include/linux/compiler_types.h | 21 ++++++++++++++++++++-
include/uapi/linux/stddef.h | 4 ++++
4 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/init/Kconfig b/init/Kconfig
index cab3ad28ca49..54691b086bc6 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -139,6 +139,17 @@ config CC_HAS_COUNTED_BY
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
default y if CC_IS_GCC && GCC_VERSION >= 150100
+config CC_HAS_COUNTED_BY_PTR_BARE
+ def_bool $(success,echo 'struct foo { int *ptr __attribute__((__counted_by__(count))); int count; };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror)
+
+config CC_HAS_COUNTED_BY_PTR_EXP
+ def_bool $(success,echo 'struct foo { int *ptr __attribute__((__counted_by__(count))); int count; };' | $(CC) $(CLANG_FLAGS) -fexperimental-late-parse-attributes -x c - -c -o /dev/null -Werror)
+ depends on !CC_HAS_COUNTED_BY_PTR_BARE
+
+config CC_HAS_COUNTED_BY_PTR
+ def_bool y
+ depends on CC_HAS_COUNTED_BY_PTR_BARE || CC_HAS_COUNTED_BY_PTR_EXP
+
config CC_HAS_MULTIDIMENSIONAL_NONSTRING
def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror)
diff --git a/Makefile b/Makefile
index d14824792227..1b297dcbb0df 100644
--- a/Makefile
+++ b/Makefile
@@ -933,6 +933,10 @@ KBUILD_CFLAGS += $(CC_AUTO_VAR_INIT_ZERO_ENABLER)
endif
endif
+ifdef CONFIG_CC_HAS_COUNTED_BY_PTR_EXP
+KBUILD_CFLAGS += -fexperimental-late-parse-attributes
+endif
+
# Explicitly clear padding bits during variable initialization
KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 59288a2c1ad2..f197ea03b593 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -353,11 +353,14 @@ struct ftrace_likely_data {
#endif
/*
+ * Runtime track number of flexible array member elements for use by
+ * CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS.
+ *
* Optional: only supported since gcc >= 15
* Optional: only supported since clang >= 18
*
* gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
- * clang: https://github.com/llvm/llvm-project/pull/76348
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
*
* __bdos on clang < 19.1.2 can erroneously return 0:
* https://github.com/llvm/llvm-project/pull/110497
@@ -371,6 +374,22 @@ struct ftrace_likely_data {
# define __counted_by(member)
#endif
+/*
+ * Runtime track number of objects pointed to by a pointer member for
+ * use by CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS.
+ *
+ * Optional: only supported since gcc >= 16
+ * Optional: only supported since clang >= 20
+ *
+ * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
+ * clang: ...
+ */
+#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
+# define __counted_by_ptr(member) __attribute__((__counted_by__(member)))
+#else
+# define __counted_by_ptr(member)
+#endif
+
/*
* Optional: only supported since gcc >= 15
* Optional: not supported by Clang
diff --git a/include/uapi/linux/stddef.h b/include/uapi/linux/stddef.h
index 9a28f7d9a334..111b097ec00b 100644
--- a/include/uapi/linux/stddef.h
+++ b/include/uapi/linux/stddef.h
@@ -72,6 +72,10 @@
#define __counted_by_be(m)
#endif
+#ifndef __counted_by_ptr
+#define __counted_by_ptr(m)
+#endif
+
#ifdef __KERNEL__
#define __kernel_nonstring __nonstring
#else
--
2.34.1
next prev parent reply other threads:[~2025-10-20 22:01 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-20 22:01 [PATCH 0/3] compiler_types: Introduce __counted_by_ptr() Kees Cook
2025-10-20 22:01 ` Kees Cook [this message]
2025-10-20 22:34 ` [PATCH 1/3] " Marco Elver
2025-10-20 22:53 ` Bill Wendling
2025-10-21 9:59 ` Peter Zijlstra
2025-10-21 9:54 ` Peter Zijlstra
2025-10-21 19:24 ` Kees Cook
2025-10-22 8:20 ` Peter Zijlstra
2025-10-23 0:47 ` Kees Cook
2025-10-23 8:01 ` Peter Zijlstra
2025-10-23 13:45 ` Kees Cook
2025-10-20 22:01 ` [PATCH 2/3] lkdtm/bugs: Add __counted_by_ptr() test PTR_BOUNDS Kees Cook
2025-10-20 22:01 ` [PATCH 3/3] coredump: Use __counted_by_ptr for struct core_name::corename Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251020220118.1226740-1-kees@kernel.org \
--to=kees@kernel.org \
--cc=Marc.Herbert@linux.intel.com \
--cc=akpm@linux-foundation.org \
--cc=aleksander.lobakin@intel.com \
--cc=arnd@arndb.de \
--cc=brauner@kernel.org \
--cc=brgerst@gmail.com \
--cc=cferris@google.com \
--cc=christophe.leroy@csgroup.eu \
--cc=elver@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=gustavoars@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hca@linux.ibm.com \
--cc=jack@suse.cz \
--cc=jeffxu@chromium.org \
--cc=justinstitt@google.com \
--cc=kernel@jfarr.cc \
--cc=kuba@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lkp@intel.com \
--cc=llvm@lists.linux.dev \
--cc=masahiroy@kernel.org \
--cc=mhklinux@outlook.com \
--cc=mkoutny@suse.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=nicolas.schier@linux.dev \
--cc=ojeda@kernel.org \
--cc=pabeni@redhat.com \
--cc=peterz@infradead.org \
--cc=przemyslaw.kitszel@intel.com \
--cc=rdunlap@infradead.org \
--cc=shakeel.butt@linux.dev \
--cc=shuah@kernel.org \
--cc=tamird@gmail.com \
--cc=thomas.weissschuh@linutronix.de \
--cc=tj@kernel.org \
--cc=ubizjak@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).