Skip to content

Commit 98d278c

Browse files
Gabriel Davidpavelmachek
authored andcommitted
leds: lm3697: Fix out-of-bound access
If both LED banks aren't used in device tree, an out-of-bounds condition in lm3697_init occurs because of the for loop assuming that all the banks are used. Fix it by adding a variable that contains the number of used banks. Signed-off-by: Gabriel David <[email protected]> [removed extra rename, minor tweaks] Signed-off-by: Pavel Machek <[email protected]> Cc: [email protected]
1 parent 8fd8f94 commit 98d278c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

drivers/leds/leds-lm3697.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct lm3697 {
7878
struct mutex lock;
7979

8080
int bank_cfg;
81+
int num_banks;
8182

8283
struct lm3697_led leds[];
8384
};
@@ -180,7 +181,7 @@ static int lm3697_init(struct lm3697 *priv)
180181
if (ret)
181182
dev_err(dev, "Cannot write OUTPUT config\n");
182183

183-
for (i = 0; i < LM3697_MAX_CONTROL_BANKS; i++) {
184+
for (i = 0; i < priv->num_banks; i++) {
184185
led = &priv->leds[i];
185186
ret = ti_lmu_common_set_ramp(&led->lmu_data);
186187
if (ret)
@@ -301,8 +302,8 @@ static int lm3697_probe(struct i2c_client *client,
301302
int ret;
302303

303304
count = device_get_child_node_count(dev);
304-
if (!count) {
305-
dev_err(dev, "LEDs are not defined in device tree!");
305+
if (!count || count > LM3697_MAX_CONTROL_BANKS) {
306+
dev_err(dev, "Strange device tree!");
306307
return -ENODEV;
307308
}
308309

@@ -315,6 +316,7 @@ static int lm3697_probe(struct i2c_client *client,
315316

316317
led->client = client;
317318
led->dev = dev;
319+
led->num_banks = count;
318320
led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config);
319321
if (IS_ERR(led->regmap)) {
320322
ret = PTR_ERR(led->regmap);

0 commit comments

Comments
 (0)