* [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr
@ 2026-09-22 22:09 Bill Wendling
2026-09-23 5:45 ` Gustavo A. R. Silva
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Bill Wendling @ 2026-09-22 22:09 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kees Cook, Gustavo A. R. Silva, Jishnu Prakash,
Daniel Lezcano, linux-iio, linux-kernel, linux-hardening,
Bill Wendling, codemender-patching+linux
Under compiler-supported bounds checking (via KASAN or UBSAN), pointer
fields inside structures can be annotated with the '__counted_by_ptr'
attribute to specify which field in the same structure holds the element
count. This enables the compiler to perform runtime bounds checking on
the pointer.
Annotate the "base" pointer field in "struct adc5_device_data" with the
"__counted_by_ptr" attribute pointing to "num_sdams". The number of
SDAMs is determined from the device property and assigned to "num_sdams"
which is then used to initialize "base" via "devm_kcalloc".
By ensuring "num_sdams" is set to the correct element count prior to
the "base" pointer allocation, and since the size of "base" is never
reallocated or changed, this annotation is safe and will not cause any
false-positive runtime bounds check panics or KASAN issues.
Cc: codemender-patching+linux@google.com
Assisted-by: LLM
Signed-off-by: Bill Wendling <morbo@google.com>
---
include/linux/iio/adc/qcom-adc5-gen3-common.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/iio/adc/qcom-adc5-gen3-common.h b/include/linux/iio/adc/qcom-adc5-gen3-common.h
index 39cbfcbdb101..b3e2c2f50e8f 100644
--- a/include/linux/iio/adc/qcom-adc5-gen3-common.h
+++ b/include/linux/iio/adc/qcom-adc5-gen3-common.h
@@ -137,7 +137,7 @@ struct adc5_sdam_data {
*/
struct adc5_device_data {
struct regmap *regmap;
- struct adc5_sdam_data *base;
+ struct adc5_sdam_data *base __counted_by_ptr(num_sdams);
int num_sdams;
};
--
2.55.0.1082.g2b9226bbc0-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr
2026-09-22 22:09 [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr Bill Wendling
@ 2026-09-23 5:45 ` Gustavo A. R. Silva
2026-09-23 8:00 ` Daniel Lezcano
2026-09-23 8:01 ` Kees Cook
2 siblings, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2026-09-23 5:45 UTC (permalink / raw)
To: Bill Wendling, Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kees Cook, Gustavo A. R. Silva, Jishnu Prakash,
Daniel Lezcano, linux-iio, linux-kernel, linux-hardening,
codemender-patching+linux
On 9/23/26 07:09, Bill Wendling wrote:
> Under compiler-supported bounds checking (via KASAN or UBSAN), pointer
> fields inside structures can be annotated with the '__counted_by_ptr'
> attribute to specify which field in the same structure holds the element
> count. This enables the compiler to perform runtime bounds checking on
> the pointer.
>
> Annotate the "base" pointer field in "struct adc5_device_data" with the
> "__counted_by_ptr" attribute pointing to "num_sdams". The number of
> SDAMs is determined from the device property and assigned to "num_sdams"
> which is then used to initialize "base" via "devm_kcalloc".
>
> By ensuring "num_sdams" is set to the correct element count prior to
> the "base" pointer allocation, and since the size of "base" is never
> reallocated or changed, this annotation is safe and will not cause any
> false-positive runtime bounds check panics or KASAN issues.
>
> 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/iio/adc/qcom-adc5-gen3-common.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/iio/adc/qcom-adc5-gen3-common.h b/include/linux/iio/adc/qcom-adc5-gen3-common.h
> index 39cbfcbdb101..b3e2c2f50e8f 100644
> --- a/include/linux/iio/adc/qcom-adc5-gen3-common.h
> +++ b/include/linux/iio/adc/qcom-adc5-gen3-common.h
> @@ -137,7 +137,7 @@ struct adc5_sdam_data {
> */
> struct adc5_device_data {
> struct regmap *regmap;
> - struct adc5_sdam_data *base;
> + struct adc5_sdam_data *base __counted_by_ptr(num_sdams);
> int num_sdams;
> };
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr
2026-09-22 22:09 [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr Bill Wendling
2026-09-23 5:45 ` Gustavo A. R. Silva
@ 2026-09-23 8:00 ` Daniel Lezcano
2026-09-23 8:01 ` Kees Cook
2 siblings, 0 replies; 5+ messages in thread
From: Daniel Lezcano @ 2026-09-23 8:00 UTC (permalink / raw)
To: Bill Wendling, Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Kees Cook, Gustavo A. R. Silva, Jishnu Prakash,
Daniel Lezcano, linux-iio, linux-kernel, linux-hardening,
codemender-patching+linux
On 9/23/26 00:09, Bill Wendling wrote:
> Under compiler-supported bounds checking (via KASAN or UBSAN), pointer
> fields inside structures can be annotated with the '__counted_by_ptr'
> attribute to specify which field in the same structure holds the element
> count. This enables the compiler to perform runtime bounds checking on
> the pointer.
>
> Annotate the "base" pointer field in "struct adc5_device_data" with the
> "__counted_by_ptr" attribute pointing to "num_sdams". The number of
> SDAMs is determined from the device property and assigned to "num_sdams"
> which is then used to initialize "base" via "devm_kcalloc".
>
> By ensuring "num_sdams" is set to the correct element count prior to
> the "base" pointer allocation, and since the size of "base" is never
> reallocated or changed, this annotation is safe and will not cause any
> false-positive runtime bounds check panics or KASAN issues.
>
> Cc: codemender-patching+linux@google.com
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@google.com>
> ---
Acked-by: Daniel Lezcano <daniel.lezcano@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr
2026-09-22 22:09 [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr Bill Wendling
2026-09-23 5:45 ` Gustavo A. R. Silva
2026-09-23 8:00 ` Daniel Lezcano
@ 2026-09-23 8:01 ` Kees Cook
2026-09-25 2:42 ` Jonathan Cameron
2 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2026-09-23 8:01 UTC (permalink / raw)
To: Bill Wendling
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Gustavo A. R. Silva, Jishnu Prakash,
Daniel Lezcano, linux-iio, linux-kernel, linux-hardening,
codemender-patching+linux
On Tue, Sep 22, 2026 at 10:09:50PM +0000, Bill Wendling wrote:
> Under compiler-supported bounds checking (via KASAN or UBSAN), pointer
> fields inside structures can be annotated with the '__counted_by_ptr'
> attribute to specify which field in the same structure holds the element
> count. This enables the compiler to perform runtime bounds checking on
> the pointer.
>
> Annotate the "base" pointer field in "struct adc5_device_data" with the
> "__counted_by_ptr" attribute pointing to "num_sdams". The number of
> SDAMs is determined from the device property and assigned to "num_sdams"
> which is then used to initialize "base" via "devm_kcalloc".
>
> By ensuring "num_sdams" is set to the correct element count prior to
> the "base" pointer allocation, and since the size of "base" is never
> reallocated or changed, this annotation is safe and will not cause any
> false-positive runtime bounds check panics or KASAN issues.
Welp, yes, it's allocated correctly to the size. It'll be interesting to
see if this:
ret = device_property_count_u32(dev, "reg");
if (ret < 0)
return ret;
adc->dev_data.num_sdams = ret;
is never at odds with this:
ret = devm_request_threaded_irq(dev, adc->dev_data.base[ADC5_GEN3_VADC_SDAM].irq,
NULL, adc5_gen3_isr, IRQF_ONESHOT | IRQF_SHARED,
adc->dev_data.base[ADC5_GEN3_VADC_SDAM].irq_name,
adc);
Nothing checks that num_sdams >= ADC5_GEN3_VADC_SDAM (0). I feel like
that ret = device_property_count_u32 should check for < 1 :)
--
Kees Cook
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr
2026-09-23 8:01 ` Kees Cook
@ 2026-09-25 2:42 ` Jonathan Cameron
0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2026-09-25 2:42 UTC (permalink / raw)
To: Kees Cook
Cc: Bill Wendling, David Lechner, Nuno Sá,
Andy Shevchenko, Gustavo A. R. Silva, Jishnu Prakash,
Daniel Lezcano, linux-iio, linux-kernel, linux-hardening,
codemender-patching+linux
On Wed, 23 Sep 2026 01:01:42 -0700
Kees Cook <kees@kernel.org> wrote:
> On Tue, Sep 22, 2026 at 10:09:50PM +0000, Bill Wendling wrote:
> > Under compiler-supported bounds checking (via KASAN or UBSAN), pointer
> > fields inside structures can be annotated with the '__counted_by_ptr'
> > attribute to specify which field in the same structure holds the element
> > count. This enables the compiler to perform runtime bounds checking on
> > the pointer.
> >
> > Annotate the "base" pointer field in "struct adc5_device_data" with the
> > "__counted_by_ptr" attribute pointing to "num_sdams". The number of
> > SDAMs is determined from the device property and assigned to "num_sdams"
> > which is then used to initialize "base" via "devm_kcalloc".
> >
> > By ensuring "num_sdams" is set to the correct element count prior to
> > the "base" pointer allocation, and since the size of "base" is never
> > reallocated or changed, this annotation is safe and will not cause any
> > false-positive runtime bounds check panics or KASAN issues.
>
> Welp, yes, it's allocated correctly to the size. It'll be interesting to
> see if this:
>
> ret = device_property_count_u32(dev, "reg");
> if (ret < 0)
> return ret;
>
> adc->dev_data.num_sdams = ret;
>
> is never at odds with this:
>
> ret = devm_request_threaded_irq(dev, adc->dev_data.base[ADC5_GEN3_VADC_SDAM].irq,
> NULL, adc5_gen3_isr, IRQF_ONESHOT | IRQF_SHARED,
> adc->dev_data.base[ADC5_GEN3_VADC_SDAM].irq_name,
> adc);
>
> Nothing checks that num_sdams >= ADC5_GEN3_VADC_SDAM (0). I feel like
> that ret = device_property_count_u32 should check for < 1 :)
Whilst it's sensible to sanity check dt, I'm not sure we will ever
close down all the crazy ways an invalid dt can break things!
I'd certainly be fine with some additional hardening of this particular
case though.
Applied this one as any such change is a different issue anyway
Thanks,
Jonathan
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-09-25 2:43 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:09 [PATCH] iio: adc: qcom-adc5-gen3: Annotate struct adc5_device_data with __counted_by_ptr Bill Wendling
2026-09-23 5:45 ` Gustavo A. R. Silva
2026-09-23 8:00 ` Daniel Lezcano
2026-09-23 8:01 ` Kees Cook
2026-09-25 2:42 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®