* [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
@ 2026-09-22 22:35 Bill Wendling
2026-09-23 5:46 ` Gustavo A. R. Silva
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Bill Wendling @ 2026-09-22 22:35 UTC (permalink / raw)
To: Christophe Leroy (CS GROUP)
Cc: Kees Cook, Gustavo A. R. Silva, Nathan Chancellor,
Nick Desaulniers, Justin Stitt, linuxppc-dev, linux-arm-kernel,
linux-kernel, linux-hardening, llvm, Bill Wendling,
codemender-patching+linux
The GCC and Clang compilers provide the __counted_by_ptr attribute,
which is used by KASAN and compiler bounds-checking to detect
out-of-bounds accesses to pointer fields.
In "struct fsl_mc_io", the "portal_virt_addr" pointer points to the MC
command portal virtual address. The size of this allocated portal in
bytes is tracked by the "portal_size" field within the same structure.
Annotate the "portal_virt_addr" pointer field with
"__counted_by_ptr(portal_size)" to enable compiler bounds-checking and
harden against potential out-of-bounds accesses.
Cc: codemender-patching+linux@google.com
Assisted-by: LLM
Signed-off-by: Bill Wendling <morbo@google.com>
---
include/linux/fsl/mc.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/fsl/mc.h b/include/linux/fsl/mc.h
index c25f0f7e6dd4..0b2c0eb9b605 100644
--- a/include/linux/fsl/mc.h
+++ b/include/linux/fsl/mc.h
@@ -336,7 +336,7 @@ struct fsl_mc_io {
u16 flags;
u32 portal_size;
phys_addr_t portal_phys_addr;
- void __iomem *portal_virt_addr;
+ void __iomem *portal_virt_addr __counted_by_ptr(portal_size);
struct fsl_mc_device *dpmcp_dev;
union {
/*
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
2026-09-22 22:35 [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr Bill Wendling
@ 2026-09-23 5:46 ` Gustavo A. R. Silva
2026-09-23 8:12 ` Kees Cook
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2026-09-23 5:46 UTC (permalink / raw)
To: Bill Wendling, Christophe Leroy (CS GROUP)
Cc: Kees Cook, Gustavo A. R. Silva, Nathan Chancellor,
Nick Desaulniers, Justin Stitt, linuxppc-dev, linux-arm-kernel,
linux-kernel, linux-hardening, llvm, codemender-patching+linux
On 9/23/26 07:35, Bill Wendling wrote:
> The GCC and Clang compilers provide the __counted_by_ptr attribute,
> which is used by KASAN and compiler bounds-checking to detect
> out-of-bounds accesses to pointer fields.
>
> In "struct fsl_mc_io", the "portal_virt_addr" pointer points to the MC
> command portal virtual address. The size of this allocated portal in
> bytes is tracked by the "portal_size" field within the same structure.
>
> Annotate the "portal_virt_addr" pointer field with
> "__counted_by_ptr(portal_size)" to enable compiler bounds-checking and
> harden against potential out-of-bounds accesses.
>
> Cc: codemender-patching+linux@google.com
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@google.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Thanks
-Gustavo
> ---
> include/linux/fsl/mc.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/fsl/mc.h b/include/linux/fsl/mc.h
> index c25f0f7e6dd4..0b2c0eb9b605 100644
> --- a/include/linux/fsl/mc.h
> +++ b/include/linux/fsl/mc.h
> @@ -336,7 +336,7 @@ struct fsl_mc_io {
> u16 flags;
> u32 portal_size;
> phys_addr_t portal_phys_addr;
> - void __iomem *portal_virt_addr;
> + void __iomem *portal_virt_addr __counted_by_ptr(portal_size);
> struct fsl_mc_device *dpmcp_dev;
> union {
> /*
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
2026-09-22 22:35 [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr Bill Wendling
2026-09-23 5:46 ` Gustavo A. R. Silva
@ 2026-09-23 8:12 ` Kees Cook
2026-10-02 12:55 ` Ioana Ciornei
2026-10-02 13:35 ` Christophe Leroy (CS GROUP)
3 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2026-09-23 8:12 UTC (permalink / raw)
To: Bill Wendling
Cc: Christophe Leroy (CS GROUP), Gustavo A. R. Silva,
Nathan Chancellor, Nick Desaulniers, Justin Stitt, linuxppc-dev,
linux-arm-kernel, linux-kernel, linux-hardening, llvm,
codemender-patching+linux
On Tue, Sep 22, 2026 at 10:35:38PM +0000, Bill Wendling wrote:
> The GCC and Clang compilers provide the __counted_by_ptr attribute,
> which is used by KASAN and compiler bounds-checking to detect
> out-of-bounds accesses to pointer fields.
>
> In "struct fsl_mc_io", the "portal_virt_addr" pointer points to the MC
> command portal virtual address. The size of this allocated portal in
> bytes is tracked by the "portal_size" field within the same structure.
>
> Annotate the "portal_virt_addr" pointer field with
> "__counted_by_ptr(portal_size)" to enable compiler bounds-checking and
> harden against potential out-of-bounds accesses.
Another one where I hope things agree. :)
mc_portal_size = resource_size(dpmcp_dev->regions);
error = fsl_create_mc_io(&mc_bus_dev->dev,
mc_portal_phys_addr,
mc_portal_size, dpmcp_dev,
mc_io_flags, &mc_io);
...
mc_io->portal_size = mc_portal_size;
...
mc_portal_virt_addr = devm_ioremap(dev,
mc_portal_phys_addr,
mc_portal_size);
...
mc_io->portal_virt_addr = mc_portal_virt_addr;
But it actually reminds me that I still want a warning for having
compile-time warning about pointers being stripped from their counter:
status = mc_read_response(mc_io->portal_virt_addr, cmd);
...
static inline enum mc_cmd_status mc_read_response(struct fsl_mc_command __iomem
*portal,
struct fsl_mc_command *resp)
{
int i;
enum mc_cmd_status status;
/* Copy command response header from MC portal: */
resp->header = cpu_to_le64(readq_relaxed(&portal->header));
Not only is mc_io->portal_virt_addr separated from mc_io->portal_size
via getting passed to mc_read_response(), but it then immediately gets
cast to struct fsl_mc_command.
We should get the __singleton attribute so we can mark function arg
pointers as "not an array", and then these kinds of casts could generate
a run-time check at function call time to check
sizeof(struct fsl_mc_command) against mc_io->portal_size when
__singleton was there, or kick up a warning that the counter got
stripped.
Because as-is, this patch is a no-op: nothing dereferences
mc_io->portal_virt_addr with the counter in context.
-Kees
--
Kees Cook
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
2026-09-22 22:35 [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr Bill Wendling
2026-09-23 5:46 ` Gustavo A. R. Silva
2026-09-23 8:12 ` Kees Cook
@ 2026-10-02 12:55 ` Ioana Ciornei
2026-10-02 13:35 ` Christophe Leroy (CS GROUP)
3 siblings, 0 replies; 5+ messages in thread
From: Ioana Ciornei @ 2026-10-02 12:55 UTC (permalink / raw)
To: Bill Wendling
Cc: Christophe Leroy (CS GROUP), Kees Cook, Gustavo A. R. Silva,
Nathan Chancellor, Nick Desaulniers, Justin Stitt, linuxppc-dev,
linux-arm-kernel, linux-kernel, linux-hardening, llvm,
codemender-patching+linux
On Tue, Sep 22, 2026 at 10:35:38PM +0000, Bill Wendling wrote:
> The GCC and Clang compilers provide the __counted_by_ptr attribute,
> which is used by KASAN and compiler bounds-checking to detect
> out-of-bounds accesses to pointer fields.
>
> In "struct fsl_mc_io", the "portal_virt_addr" pointer points to the MC
> command portal virtual address. The size of this allocated portal in
> bytes is tracked by the "portal_size" field within the same structure.
>
> Annotate the "portal_virt_addr" pointer field with
> "__counted_by_ptr(portal_size)" to enable compiler bounds-checking and
> harden against potential out-of-bounds accesses.
>
> Cc: codemender-patching+linux@google.com
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@google.com>
Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
2026-09-22 22:35 [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr Bill Wendling
` (2 preceding siblings ...)
2026-10-02 12:55 ` Ioana Ciornei
@ 2026-10-02 13:35 ` Christophe Leroy (CS GROUP)
3 siblings, 0 replies; 5+ messages in thread
From: Christophe Leroy (CS GROUP) @ 2026-10-02 13:35 UTC (permalink / raw)
To: Bill Wendling
Cc: Christophe Leroy, Kees Cook, Gustavo A. R. Silva,
Nathan Chancellor, Nick Desaulniers, Justin Stitt, linuxppc-dev,
linux-arm-kernel, linux-kernel, linux-hardening, llvm,
codemender-patching+linux
On Tue, 22 Sep 2026 22:35:38 +0000, Bill Wendling wrote:
> The GCC and Clang compilers provide the __counted_by_ptr attribute,
> which is used by KASAN and compiler bounds-checking to detect
> out-of-bounds accesses to pointer fields.
>
> In "struct fsl_mc_io", the "portal_virt_addr" pointer points to the MC
> command portal virtual address. The size of this allocated portal in
> bytes is tracked by the "portal_size" field within the same structure.
>
> [...]
Applied, thanks!
[1/1] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr
commit: df0fd0f5af4ddd84a76e21da8e6b489928c5e300
Best regards,
--
Christophe Leroy (CS GROUP) <chleroy@kernel.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-02 13:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-22 22:35 [PATCH] bus: fsl-mc: Annotate fsl_mc_io.portal_virt_addr with __counted_by_ptr Bill Wendling
2026-09-23 5:46 ` Gustavo A. R. Silva
2026-09-23 8:12 ` Kees Cook
2026-10-02 12:55 ` Ioana Ciornei
2026-10-02 13:35 ` Christophe Leroy (CS GROUP)
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.