Skip to content

Commit 4bfbc26

Browse files
tobluxkees
authored andcommitted
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: KSPP/linux#83 Signed-off-by: Thorsten Blum <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent e04c78d commit 4bfbc26

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

drivers/mux/core.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ struct mux_chip *mux_chip_alloc(struct device *dev,
9898
if (WARN_ON(!dev || !controllers))
9999
return ERR_PTR(-EINVAL);
100100

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);
104104
if (!mux_chip)
105105
return ERR_PTR(-ENOMEM);
106106

107-
mux_chip->mux = (struct mux_control *)(mux_chip + 1);
108107
mux_chip->dev.class = &mux_class;
109108
mux_chip->dev.type = &mux_type;
110109
mux_chip->dev.parent = dev;

include/linux/mux/driver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ struct mux_control {
5656
/**
5757
* struct mux_chip - Represents a chip holding mux controllers.
5858
* @controllers: Number of mux controllers handled by the chip.
59-
* @mux: Array of mux controllers that are handled.
6059
* @dev: Device structure.
6160
* @id: Used to identify the device internally.
6261
* @ops: Mux controller operations.
62+
* @mux: Array of mux controllers that are handled.
6363
*/
6464
struct mux_chip {
6565
unsigned int controllers;
66-
struct mux_control *mux;
6766
struct device dev;
6867
int id;
6968

7069
const struct mux_control_ops *ops;
70+
struct mux_control mux[] __counted_by(controllers);
7171
};
7272

7373
#define to_mux_chip(x) container_of((x), struct mux_chip, dev)

0 commit comments

Comments
 (0)