linux-stable/drivers/clk
Gustavo A. R. Silva 1f026218f8 clk: visconti: Fix undefined behavior bug in struct visconti_pll_provider
[ Upstream commit 5ad1e217a2 ]

`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 visconti_pll_provider`, in this case
`struct device_node *node;`, at run-time:

drivers/clk/visconti/pll.h:
 16 struct visconti_pll_provider {
 17         void __iomem *reg_base;
 18         struct clk_hw_onecell_data clk_data;
 19         struct device_node *node;
 20 };

Notice that a total of 56 bytes are allocated for flexible-array `hws`
at line 328. See below:

include/dt-bindings/clock/toshiba,tmpv770x.h:
 14 #define TMPV770X_NR_PLL		7

drivers/clk/visconti/pll-tmpv770x.c:
 69 ctx = visconti_init_pll(np, reg_base, TMPV770X_NR_PLL);

drivers/clk/visconti/pll.c:
321 struct visconti_pll_provider * __init visconti_init_pll(struct device_node *np,
322                                                         void __iomem *base,
323                                                         unsigned long nr_plls)
324 {
325         struct visconti_pll_provider *ctx;
...
328         ctx = kzalloc(struct_size(ctx, clk_data.hws, nr_plls), GFP_KERNEL);

`struct_size(ctx, clk_data.hws, nr_plls)` above translates to
sizeof(struct visconti_pll_provider) + sizeof(struct clk_hw *) * 7 ==
24 + 8 * 7 == 24 + 56
		  ^^^^
		   |
	allocated bytes for flex array `hws`

$ pahole -C visconti_pll_provider drivers/clk/visconti/pll.o
struct visconti_pll_provider {
	void *                     reg_base;             /*     0     8 */
	struct clk_hw_onecell_data clk_data;             /*     8     8 */
	struct device_node *       node;                 /*    16     8 */

	/* size: 24, cachelines: 1, members: 3 */
	/* last cacheline: 24 bytes */
};

And then, after the allocation, some data is written into all members
of `struct visconti_pll_provider`:

332         for (i = 0; i < nr_plls; ++i)
333                 ctx->clk_data.hws[i] = ERR_PTR(-ENOENT);
334
335         ctx->node = np;
336         ctx->reg_base = base;
337         ctx->clk_data.num = nr_plls;

Fix all these by placing the declaration of object `clk_data` at the
end of `struct visconti_pll_provider`. Also, add a comment to make it
clear that this object must always be last in the structure, and
prevent this bug from being introduced again in the future.

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

Fixes: b4cbe606dc ("clk: visconti: Add support common clock driver and reset driver")
Cc: stable@vger.kernel.org
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/57a831d94ee2b3889b11525d4ad500356f89576f.1697492890.git.gustavoars@kernel.org
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-11-28 17:07:16 +00:00
..
actions clk: actions: remove redundant assignment after a mask operation 2022-04-22 19:01:20 -07:00
analogbits
at91 clk: at91: clk-sam9x60-pll: fix return value check 2023-05-11 23:03:32 +09:00
axis clk: cleanup comments 2022-03-11 18:22:15 -08:00
axs10x treewide: Replace GPLv2 boilerplate/reference with SPDX - gpl-2.0_56.RULE (part 2) 2022-06-10 14:51:35 +02:00
baikal-t1 clk: baikal-t1: Convert to platform device driver 2022-09-30 14:20:56 -07:00
bcm clk: bcm: rpi: Fix off by one in raspberrypi_discover_clocks() 2023-07-19 16:21:30 +02:00
berlin clk: berlin: Add of_node_put() for of_get_parent() 2022-08-19 14:50:24 -07:00
davinci Merge branches 'clk-fixed-rate', 'clk-spreadtrum', 'clk-pxa' and 'clk-ti' into clk-next 2022-10-04 10:54:14 -07:00
hisilicon Merge branches 'clk-starfive', 'clk-ti', 'clk-terminate' and 'clk-cleanup' into clk-next 2022-03-29 10:19:10 -07:00
imgtec
imx clk: imx: imx8qxp: Fix elcdif_pll clock 2023-11-20 11:51:56 +01:00
ingenic clk: ingenic: jz4760: Update M/N/OD calculation algorithm 2023-02-14 19:11:52 +01:00
keystone clk: keystone: pll: fix a couple NULL vs IS_ERR() checks 2023-11-20 11:51:56 +01:00
loongson1 clk: loongson1: Terminate clk_div_table with sentinel element 2022-03-11 18:13:24 -08:00
mediatek clk: mediatek: clk-mt2701: Add check for mtk_alloc_clk_data 2023-11-20 11:51:57 +01:00
meson clk: meson: Hold reference returned by of_get_parent() 2022-08-19 14:29:00 -07:00
microchip clk: microchip: fix potential UAF in auxdev release callback 2023-05-11 23:03:40 +09:00
mmp clk: mmp: pxa168: control shared SDH bits with separate clock 2022-09-30 13:34:07 -07:00
mstar
mvebu clk: mvebu: armada-37xx-tbg: Remove the unneeded result variable 2022-09-30 17:46:30 -07:00
mxs
nxp clk: nxp: fix typo in comment 2022-10-03 12:34:32 -07:00
pistachio clk: pistachio: Fix initconst confusion 2022-09-28 18:35:45 -07:00
pxa clk: pxa: fix NULL pointer dereference in pxa3xx_clk_update_accr 2023-06-21 16:00:57 +02:00
qcom clk: qcom: ipq6018: drop the CLK_SET_RATE_PARENT flag from PLL clocks 2023-11-28 17:07:10 +00:00
ralink
renesas clk: renesas: rzg2l: Fix computation formula 2023-11-20 11:51:56 +01:00
rockchip clk: rockchip: rk3568: Fix PLL rate setting for 78.75MHz 2023-09-13 09:42:45 +02:00
samsung clk: samsung: Fix memory leak in _samsung_clk_register_pll() 2022-12-31 13:32:25 +01:00
sifive clk: sifive: select by default if SOC_SIFIVE 2022-10-17 14:07:11 -07:00
socfpga clk: socfpga: Fix undefined behavior bug in struct stratix10_clock_data 2023-11-28 17:07:10 +00:00
spear clk: spear: Move prototype to accessible header 2022-10-10 10:37:58 +02:00
sprd clk: sprd: Fix thm_parents incorrect configuration 2023-10-06 14:56:47 +02:00
st clk: st: Fix memory leak in st_of_quadfs_setup() 2022-12-31 13:33:04 +01:00
starfive clk: starfive: Add JH7100 audio clock driver 2022-03-10 18:17:33 -08:00
stm32 clk: stm32: rcc_reset: Fix missing spin_lock_init() 2022-06-09 15:34:08 -07:00
sunxi clk: sunxi: Do not select the PRCM MFD 2022-07-04 21:59:30 +02:00
sunxi-ng clk: sunxi-ng: Modify mismatched function name 2023-09-13 09:42:45 +02:00
tegra clk: tegra: fix error return case for recalc_rate 2023-10-06 14:56:47 +02:00
ti clk: ti: fix double free in of_ti_divider_clk_setup() 2023-11-20 11:51:57 +01:00
uniphier clk: uniphier: Fix fixed-rate initialization 2022-03-11 19:29:02 -08:00
ux500 clk: ux500: fix a possible off-by-one in u8500_prcc_reset_base() 2022-05-18 13:34:03 -07:00
versatile
visconti clk: visconti: Fix undefined behavior bug in struct visconti_pll_provider 2023-11-28 17:07:16 +00:00
x86 clk: mxl: syscon_node_to_regmap() returns error pointers 2023-02-25 11:25:39 +01:00
xilinx clk: clocking-wizard: Fix Oops in clk_wzrd_register_divider() 2023-07-19 16:21:30 +02:00
zynq clk: zynq: Update the parameters to zynq_clk_register_periph_clk 2022-03-29 10:17:49 -07:00
zynqmp clk: zynqmp: pll: rectify rate rounding in zynqmp_pll_round_rate 2022-08-30 15:05:59 -07:00
.kunitconfig clk: explicitly disable CONFIG_UML_PCI_OVER_VIRTIO in .kunitconfig 2022-07-14 10:35:44 -06:00
clk-apple-nco.c clk: clk-apple-nco: Allow and fix module building 2022-03-15 09:56:12 -07:00
clk-asm9260.c clk: asm9260: use parent index to link the reference clock 2022-09-29 17:55:26 -07:00
clk-aspeed.c
clk-aspeed.h
clk-ast2600.c clk: ast2600: BCLK comes from EPLL 2022-10-03 14:12:15 -07:00
clk-axi-clkgen.c
clk-axm5516.c
clk-bd718x7.c
clk-bm1880.c
clk-bulk.c
clk-cdce706.c i2c: Make remove callback return void 2022-08-16 12:46:26 +02:00
clk-cdce925.c clk: cdce925: check return value of kasprintf() 2023-07-19 16:21:32 +02:00
clk-clps711x.c clk: clps711x: Terminate clk_div_table with sentinel element 2022-03-11 18:13:24 -08:00
clk-composite.c clk: Stop forwarding clk_rate_requests to the parent 2022-09-15 09:32:11 -07:00
clk-conf.c clk: add missing of_node_put() in "assigned-clocks" property parsing 2023-05-11 23:03:33 +09:00
clk-cs2000-cp.c i2c: Make remove callback return void 2022-08-16 12:46:26 +02:00
clk-devres.c clk: Fix slab-out-of-bounds error in devm_clk_release() 2023-08-30 16:11:06 +02:00
clk-divider.c clk: Introduce clk_hw_init_rate_request() 2022-09-15 09:31:40 -07:00
clk-en7523.c clk: en7523: fix wrong pointer check in en7523_clk_probe() 2022-04-26 11:17:47 -07:00
clk-fixed-factor.c clk: fixed-factor: Introduce *clk_hw_register_fixed_factor_parent_hw() 2022-07-29 16:44:08 -07:00
clk-fixed-mmio.c
clk-fixed-rate.c clk: fixed-rate: add devm_clk_hw_register_fixed_rate 2022-09-29 17:55:29 -07:00
clk-fractional-divider.c clk: cleanup comments 2022-03-11 18:22:15 -08:00
clk-fractional-divider.h
clk-fsl-flexspi.c
clk-fsl-sai.c
clk-gate.c
clk-gate_test.c
clk-gemini.c
clk-gpio.c
clk-hi655x.c
clk-highbank.c
clk-hsdk-pll.c treewide: Replace GPLv2 boilerplate/reference with SPDX - gpl-2.0_56.RULE (part 2) 2022-06-10 14:51:35 +02:00
clk-k210.c
clk-lan966x.c clk: allow building lan966x as a module 2022-10-03 14:14:02 -07:00
clk-lmk04832.c
clk-lochnagar.c dt-bindings: clock: Move lochnagar.h to dt-bindings/clock 2022-08-22 16:04:00 -07:00
clk-max9485.c clk: max9485: use simple i2c probe function 2022-04-22 19:28:08 -07:00
clk-max77686.c
clk-milbeaut.c
clk-moxart.c treewide: Replace GPLv2 boilerplate/reference with SPDX - gpl-2.0_56.RULE (part 2) 2022-06-10 14:51:35 +02:00
clk-multiplier.c
clk-mux.c clk: mux: remove redundant initialization of variable width 2022-04-25 15:49:20 -07:00
clk-nomadik.c clk: nomadik: Add missing of_node_put() 2022-08-19 14:24:27 -07:00
clk-npcm7xx.c clk: npcm7xx: Fix incorrect kfree 2023-11-20 11:51:57 +01:00
clk-nspire.c
clk-oxnas.c clk: oxnas: Hold reference returned by of_get_parent() 2022-08-19 14:35:43 -07:00
clk-palmas.c
clk-plldig.c
clk-pwm.c
clk-qoriq.c clk: qoriq: Hold reference returned by of_get_parent() 2022-08-19 14:41:47 -07:00
clk-renesas-pcie.c clk: rs9: Fix .driver_data content in i2c_device_id 2023-07-19 16:21:17 +02:00
clk-rk808.c
clk-s2mps11.c
clk-scmi.c clk: scmi: Free scmi_clk allocated when the clocks with invalid info are skipped 2023-11-20 11:52:03 +01:00
clk-scpi.c
clk-si514.c i2c: Make remove callback return void 2022-08-16 12:46:26 +02:00
clk-si544.c clk: si544: use i2c_match_id and simple i2c probe 2022-04-22 19:28:08 -07:00
clk-si570.c i2c: Make remove callback return void 2022-08-16 12:46:26 +02:00
clk-si5341.c clk: si5341: free unused memory on probe failure 2023-07-19 16:21:33 +02:00
clk-si5351.c i2c: Make remove callback return void 2022-08-16 12:46:26 +02:00
clk-si5351.h
clk-sparx5.c
clk-stm32f4.c
clk-stm32h7.c
clk-stm32mp1.c
clk-tps68470.c platform/x86: int3472: Support multiple clock consumers 2022-09-24 19:12:32 +02:00
clk-twl6040.c
clk-versaclock5.c clk: vc5: check memory returned by kasprintf() 2023-07-19 16:21:32 +02:00
clk-versaclock7.c clk: vc7: Fix .driver_data content in i2c_device_id 2023-07-19 16:21:17 +02:00
clk-vt8500.c
clk-wm831x.c
clk-xgene.c clk: clk-xgene: simplify if-if to if-else 2022-10-03 14:13:29 -07:00
clk.c clk: Sanitize possible_parent_show to Handle Return Value of of_clk_get_parent_name 2023-11-02 09:35:32 +01:00
clk.h
clk_test.c clk: tests: Add tests for notifiers 2022-10-10 20:37:50 -07:00
clkdev.c clkdev: Simplify devm_clk_hw_register_clkdev() function 2022-09-30 14:50:11 -07:00
Kconfig clk: fixed-mmio: make COMMON_CLK_FIXED_MMIO depend on HAS_IOMEM 2023-09-13 09:42:23 +02:00
Makefile clk: Renesas versaclock7 ccf device driver 2022-09-30 17:34:35 -07:00