clk: visconti: Add support common clock driver and reset driver
Hello Kernel Hardenning developers,
Commit b4cbe606dc36 ("clk: visconti: Add support common clock driver and reset driver") from Oct 25, 2021 (linux-next), leads to the question:
drivers/clk/visconti/clkc.c
187 struct visconti_clk_provider *visconti_init_clk(struct device *dev,
188 struct regmap *regmap,
189 unsigned long nr_clks)
190 {
191 struct visconti_clk_provider *ctx;
192 int i;
193
194 ctx = devm_kzalloc(dev, struct_size(ctx, clk_data.hws, nr_clks), GFP_KERNEL);
195 if (!ctx)
196 return ERR_PTR(-ENOMEM);
197
198 for (i = 0; i < nr_clks; ++i)
--> 199 ctx->clk_data.hws[i] = ERR_PTR(-ENOENT);
200 ctx->clk_data.num = nr_clks;ctx->clk_data.hws[] is __counted_by() ctx->clk_data.num. Don't we have to set the .num before we fill initialize the array? Or does the checker code allow us to access the array when the counted by variable is zero? I seem to remember this used to be a common false positive with the counted by checking.
201
202 ctx->dev = dev;
203 ctx->regmap = regmap;
204
205 return ctx;
206 }regards,
dan carpenter
Hey Dan,
Quoted 22 lines · Dan Carpenter
Hello Kernel Hardenning developers,
Commit
b4cbe606dc36("clk: visconti: Add support common clock driver and reset driver") from Oct 25, 2021 (linux-next), leads to the question:
drivers/clk/visconti/clkc.c187 struct visconti_clk_provider *visconti_init_clk(struct device *dev, 188 struct regmap *regmap, 189 unsigned long nr_clks) 190 { 191 struct visconti_clk_provider *ctx; 192 int i; 193 194 ctx = devm_kzalloc(dev, struct_size(ctx, clk_data.hws, nr_clks), GFP_KERNEL); 195 if (!ctx) 196 return ERR_PTR(-ENOMEM); 197 198 for (i = 0; i < nr_clks; ++i) --> 199 ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); 200 ctx->clk_data.num = nr_clks;
Quoted 2 lines
ctx->clk_data.hws[] is
__counted_by()ctx->clk_data.num. Don't we have to set the .num before we fill initialize the array? Or does the checker
Yes, it looks like line 200 needs to be moved above line 198.
Quoted 1 line
code allow us to access the array when the counted by variable is zero?
Nope, I just had to fix an instance where num is one and hws[0] is being accessed:
https://lore.kernel.org/20251124-exynos-clkout-fix-ubsan-bounds-error-v1-1-224a5282514b@kernel.org/
I suspect there will eventually be a report on real hardware when CONFIG_UBSAN_BOUNDS is enabled and GCC 15 is used to build the running kernel, which is the first version that supports __counted_by(). How did you find this? A new smatch check? It would be nice if the compilers could flag this but I guess it is probably harder to do this in a generic way.
Cheers,
Nathan
Quoted 35 lines · Nathan Chancellor
Hey Dan,
Quoted 22 lines · Dan Carpenter
Hello Kernel Hardenning developers,
Commit
b4cbe606dc36("clk: visconti: Add support common clock driver and reset driver") from Oct 25, 2021 (linux-next), leads to the question:
drivers/clk/visconti/clkc.c187 struct visconti_clk_provider *visconti_init_clk(struct device *dev, 188 struct regmap *regmap, 189 unsigned long nr_clks) 190 { 191 struct visconti_clk_provider *ctx; 192 int i; 193 194 ctx = devm_kzalloc(dev, struct_size(ctx, clk_data.hws, nr_clks), GFP_KERNEL); 195 if (!ctx) 196 return ERR_PTR(-ENOMEM); 197 198 for (i = 0; i < nr_clks; ++i) --> 199 ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); 200 ctx->clk_data.num = nr_clks;Quoted 2 lines
ctx->clk_data.hws[] is
__counted_by()ctx->clk_data.num. Don't we have to set the .num before we fill initialize the array? Or does the checkerYes, it looks like line 200 needs to be moved above line 198.
Quoted 1 line
code allow us to access the array when the counted by variable is zero?
Nope, I just had to fix an instance where num is one and hws[0] is being accessed:
Quoted 6 lines
https://lore.kernel.org/20251124-exynos-clkout-fix-ubsan-bounds-error-v1-1-224a5282514b@kernel.org/
I suspect there will eventually be a report on real hardware when
CONFIG_UBSAN_BOUNDSis enabled and GCC 15 is used to build the running kernel, which is the first version that supports__counted_by(). How did you find this? A new smatch check?
Yeah, but it was a bug in my check. I need to add __counted_by() support to Sparse. If I had that, it would easy enough to add a warning for places which access the array before setting the counted by variable.
regards,
carpenter