OpenCloudOS-Kernel/drivers/clk/socfpga
Gustavo A. R. Silva b0a660d46d clk: socfpga: Fix undefined behavior bug in struct stratix10_clock_data
commit d761bb01c85b22d5b44abe283eb89019693f6595 upstream.

`struct clk_hw_onecell_data` is a flexible structure, which means that
it contains flexible-array member at the bottom, in this case array
`hws`:

include/linux/clk-provider.h:
1380 struct clk_hw_onecell_data {
1381         unsigned int num;
1382         struct clk_hw *hws[] __counted_by(num);
1383 };

This could potentially lead to an overwrite of the objects following
`clk_data` in `struct stratix10_clock_data`, in this case
`void __iomem *base;` at run-time:

drivers/clk/socfpga/stratix10-clk.h:
  9 struct stratix10_clock_data {
 10         struct clk_hw_onecell_data      clk_data;
 11         void __iomem            *base;
 12 };

There are currently three different places where memory is allocated for
`struct stratix10_clock_data`, including the flex-array `hws` in
`struct clk_hw_onecell_data`:

drivers/clk/socfpga/clk-agilex.c:
469         clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
470                                 num_clks), GFP_KERNEL);

drivers/clk/socfpga/clk-agilex.c:
509         clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
510                                 num_clks), GFP_KERNEL);

drivers/clk/socfpga/clk-s10.c:
400         clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
401                                                  num_clks), GFP_KERNEL);

I'll use just one of them to describe the issue. See below.

Notice that a total of 440 bytes are allocated for flexible-array member
`hws` at line 469:

include/dt-bindings/clock/agilex-clock.h:
 70 #define AGILEX_NUM_CLKS	55

drivers/clk/socfpga/clk-agilex.c:
459         struct stratix10_clock_data *clk_data;
460         void __iomem *base;
...
466
467         num_clks = AGILEX_NUM_CLKS;
468
469         clk_data = devm_kzalloc(dev, struct_size(clk_data, clk_data.hws,
470                                 num_clks), GFP_KERNEL);

`struct_size(clk_data, clk_data.hws, num_clks)`	above translates to
sizeof(struct stratix10_clock_data) + sizeof(struct clk_hw *) * 55 ==
16 + 8 * 55 == 16 + 440
		    ^^^
		     |
	allocated bytes for flex-array `hws`

474         for (i = 0; i < num_clks; i++)
475                 clk_data->clk_data.hws[i] = ERR_PTR(-ENOENT);
476
477         clk_data->base = base;

and then some data is written into both `hws` and `base` objects.

Fix this by placing the declaration of object `clk_data` at the end of
`struct stratix10_clock_data`. Also, add a comment to make it clear
that this object must always be last in the structure.

-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.

Fixes: ba7e258425 ("clk: socfpga: Convert to s10/agilex/n5x to use clk_hw")
Cc: stable@vger.kernel.org
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/1da736106d8e0806aeafa6e471a13ced490eae22.1698117815.git.gustavoars@kernel.org
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-11-28 17:20:00 +00:00
..
Kconfig clk: socfpga: use ARCH_INTEL_SOCFPGA also for 32-bit ARM SoCs (and compile test) 2021-03-23 11:03:36 -05:00
Makefile clk: socfpga: use ARCH_INTEL_SOCFPGA also for 32-bit ARM SoCs (and compile test) 2021-03-23 11:03:36 -05:00
clk-agilex.c clk: socfpga: agilex: Convert to devm_platform_ioremap_resource() 2023-08-22 14:28:34 -07:00
clk-gate-a10.c clk: socfpga: arria10: use of_clk_add_hw_provider and improve error handling 2023-03-21 16:47:48 -07:00
clk-gate-s10.c clk: cleanup comments 2022-03-11 18:22:15 -08:00
clk-gate.c clk: socfpga: gate: Account for the divider in determine_rate 2023-10-12 17:30:54 -07:00
clk-periph-a10.c clk: socfpga: arria10: use of_clk_add_hw_provider and improve error handling 2023-03-21 16:47:48 -07:00
clk-periph-s10.c clk: socfpga: cleanup spdx tags 2022-03-11 18:21:45 -08:00
clk-periph.c clk: socfpga: use of_clk_add_hw_provider and improve error handling 2023-03-21 16:47:48 -07:00
clk-pll-a10.c clk: socfpga: arria10: use of_clk_add_hw_provider and improve error handling 2023-03-21 16:47:48 -07:00
clk-pll-s10.c clk: socfpga: cleanup spdx tags 2022-03-11 18:21:45 -08:00
clk-pll.c clk: socfpga: use of_clk_add_hw_provider and improve error handling 2023-03-21 16:47:48 -07:00
clk-s10.c clk: Explicitly include correct DT includes 2023-07-19 13:13:16 -07:00
clk.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 13 2019-05-21 11:28:45 +02:00
clk.h clk: socfpga: remove the setting of clk-phase for sdmmc_clk 2022-12-07 13:22:37 +01:00
stratix10-clk.h clk: socfpga: Fix undefined behavior bug in struct stratix10_clock_data 2023-11-28 17:20:00 +00:00