Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mux: Convert mux_control_ops to a flex array member in mux_chip

Convert mux_control_ops to a flexible array member at the end of the
mux_chip struct and add the __counted_by() compiler attribute to
improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Use struct_size() to calculate the number of bytes to allocate for a new
mux chip and to remove the following Coccinelle/coccicheck warning:

WARNING: Use struct_size

Use size_add() to safely add any extra bytes.

No functional changes intended.

Link: https://github.com/KSPP/linux/issues/83
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://lore.kernel.org/r/20250610104106.1948-2-thorsten.blum@linux.dev
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

Thorsten Blum and committed by
Kees Cook
4bfbc269 e04c78d8

+5 -6
+3 -4
drivers/mux/core.c
··· 98 98 if (WARN_ON(!dev || !controllers)) 99 99 return ERR_PTR(-EINVAL); 100 100 101 - mux_chip = kzalloc(sizeof(*mux_chip) + 102 - controllers * sizeof(*mux_chip->mux) + 103 - sizeof_priv, GFP_KERNEL); 101 + mux_chip = kzalloc(size_add(struct_size(mux_chip, mux, controllers), 102 + sizeof_priv), 103 + GFP_KERNEL); 104 104 if (!mux_chip) 105 105 return ERR_PTR(-ENOMEM); 106 106 107 - mux_chip->mux = (struct mux_control *)(mux_chip + 1); 108 107 mux_chip->dev.class = &mux_class; 109 108 mux_chip->dev.type = &mux_type; 110 109 mux_chip->dev.parent = dev;
+2 -2
include/linux/mux/driver.h
··· 56 56 /** 57 57 * struct mux_chip - Represents a chip holding mux controllers. 58 58 * @controllers: Number of mux controllers handled by the chip. 59 - * @mux: Array of mux controllers that are handled. 60 59 * @dev: Device structure. 61 60 * @id: Used to identify the device internally. 62 61 * @ops: Mux controller operations. 62 + * @mux: Array of mux controllers that are handled. 63 63 */ 64 64 struct mux_chip { 65 65 unsigned int controllers; 66 - struct mux_control *mux; 67 66 struct device dev; 68 67 int id; 69 68 70 69 const struct mux_control_ops *ops; 70 + struct mux_control mux[] __counted_by(controllers); 71 71 }; 72 72 73 73 #define to_mux_chip(x) container_of((x), struct mux_chip, dev)