Skip to contentSkip to conversation

iio: light: veml6030: add support for triggered buffer

PATCH Superseded Javier Carrasco 2 participants 6 messages Nov 10, 2024 – Nov 24, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Javier Carrasco

author maintainer
#0 lore raw

PATCH v2 1/1 iio: light: veml6030: add support for triggered buffer

All devices supported by this driver (currently veml6030, veml6035 and veml7700) have two 16-bit channels, and can profit for the same configuration to support data access via triggered buffers.

The measurements are stored in two 16-bit consecutive registers (addresses 0x04 and 0x05) as little endian, unsigned data.

---
Changes in v2:
- Use u16 instead of __le16 for the channels.
- Move scan struct from data to trigger_handler.
- Drop avail_scan_masks.
- Fix scan_type (IIO_CPU instead of IIO_LE, drop shift = 0).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/iio/light/Kconfig    |  2 ++
 drivers/iio/light/veml6030.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
drivers/iio/light/Kconfig +2 -0
index 29ffa8491927..0e2566ff5f7b 100644
@@ -683,6 +683,8 @@ config VEML3235
 config VEML6030
 	tristate "VEML6030 and VEML6035 ambient light sensors"
 	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
 	depends on I2C
 	help
 	  Say Y here if you want to build a driver for the Vishay VEML6030
drivers/iio/light/veml6030.c +74 -0

130 lines not on this page · raw message

---
base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
maintainer
#1 lore raw
Quoted 8 lines · On Sun, 10 Nov 2024 18:49:05 +0100 Javier Carrasco…

All devices supported by this driver (currently veml6030, veml6035 and veml7700) have two 16-bit channels, and can profit for the same configuration to support data access via triggered buffers.

The measurements are stored in two 16-bit consecutive registers (addresses 0x04 and 0x05) as little endian, unsigned data.

Hi Javier,

We have to be a little careful with pushing data from the stack. Need to makes sure holes are zero filled.

Jonathan

Quoted 10 lines
drivers/iio/light/veml6030.c +0 -0
index ccb43dfd5cf7..ce9af9a0e933 100644
 
 
 static const struct regmap_config veml6030_regmap_config = {
@@ -889,6 +928,35 @@ static irqreturn_t veml6030_event_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 939

Quoted 10 lines
drivers/iio/light/veml6030.c +9 -0
 
+static irqreturn_t veml6030_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6030_data *data = iio_priv(iio);
+	unsigned int reg;
+	int ch, ret, i = 0;
+	struct {
+		u16 chans[2];

There is a hole here...

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 949

Quoted 10 lines
drivers/iio/light/veml6030.c +10 -0
+		aligned_s64 timestamp;
+	} scan;
+
+	iio_for_each_active_channel(iio, ch) {
+		ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch),
+				  &reg);
+		if (ret)
+			goto done;
+
+		scan.chans[i++] = reg;

This fills in at least 1 channel, but maybe not the second.

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 951

Quoted 2 lines
drivers/iio/light/veml6030.c +2 -0
+	}
+

So this leaks random stack data I think.

Upshot, when holes are involved or not all the channels are set, need memset(&scan, 0, sizeof(scan));
for the structure on the stack which will zero the holes as well as both channels.

Ancient article on this: https://lwn.net/Articles/417989/

We get away with it when they are in the iio_priv space because they are kzalloc + if we do leak data due to changes in configured channels it's just old sensor data which is (I think) never a security problem!

Quoted 30 lines
drivers/iio/light/veml6030.c +14 -1
+	iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp);
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6030_set_info(struct iio_dev *indio_dev)
 {
 	struct veml6030_data *data = iio_priv(indio_dev);
@@ -1077,6 +1145,12 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      veml6030_trigger_handler, NULL);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to register triggered buffer");
+
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
 
---

base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

author maintainer
#2 lore raw
Quoted 90 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 10 Nov 2024 18:49:05 +0100 Javier Carrasco…

All devices supported by this driver (currently veml6030, veml6035 and veml7700) have two 16-bit channels, and can profit for the same configuration to support data access via triggered buffers.

The measurements are stored in two 16-bit consecutive registers (addresses 0x04 and 0x05) as little endian, unsigned data.

Hi Javier,

We have to be a little careful with pushing data from the stack. Need to makes sure holes are zero filled.

Jonathan

Quoted 10 lines
drivers/iio/light/veml6030.c +0 -0
index ccb43dfd5cf7..ce9af9a0e933 100644
 
 
 static const struct regmap_config veml6030_regmap_config = {
@@ -889,6 +928,35 @@ static irqreturn_t veml6030_event_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 939

Quoted 10 lines
drivers/iio/light/veml6030.c +9 -0
 
+static irqreturn_t veml6030_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6030_data *data = iio_priv(iio);
+	unsigned int reg;
+	int ch, ret, i = 0;
+	struct {
+		u16 chans[2];

There is a hole here...

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 949

Quoted 10 lines
drivers/iio/light/veml6030.c +10 -0
+		aligned_s64 timestamp;
+	} scan;
+
+	iio_for_each_active_channel(iio, ch) {
+		ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch),
+				  &reg);
+		if (ret)
+			goto done;
+
+		scan.chans[i++] = reg;

This fills in at least 1 channel, but maybe not the second.

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 951

Quoted 2 lines
drivers/iio/light/veml6030.c +2 -0
+	}
+

So this leaks random stack data I think.

Upshot, when holes are involved or not all the channels are set, need memset(&scan, 0, sizeof(scan));
for the structure on the stack which will zero the holes as well as both channels.

Ancient article on this: https://lwn.net/Articles/417989/

We get away with it when they are in the iio_priv space because they are kzalloc + if we do leak data due to changes in configured channels it's just old sensor data which is (I think) never a security problem!

Quoted 25 lines
drivers/iio/light/veml6030.c +14 -0
+	iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp);
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6030_set_info(struct iio_dev *indio_dev)
 {
 	struct veml6030_data *data = iio_priv(indio_dev);
@@ -1077,6 +1145,12 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      veml6030_trigger_handler, NULL);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to register triggered buffer");
+
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
Quoted 6 lines
Quoted 5 lines

---
base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

Hi Jonathan,

thanks a lot for your explanation and the link, it makes perfect sense. By the way, when I moved this struct from the iio_priv to the function, I took a look at some existing code, and a couple of them might have the same issue:

- temperature/tmp006.c: it also has a hole between the two 16-bit channels and the timestamp (aligned(8)), but it is not set to zero.

- adc/ti-ads1119.c: the scan consists of an unsigned int and the timestamp (aligned(8)). I believe there is a hole there as well.

I did not go over all drivers (most of them store the scan struct in the iio_priv space anyway), but at least those two look suspicious.

Should I fix (e.g. memset) those two I mentioned?

Best regards,
Javier Carrasco

maintainer
#3 lore raw
Quoted 109 lines · On Sat, 23 Nov 2024 22:15:11 +0100 Javier Carrasco…
Quoted 90 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 10 Nov 2024 18:49:05 +0100 Javier Carrasco…

All devices supported by this driver (currently veml6030, veml6035 and veml7700) have two 16-bit channels, and can profit for the same configuration to support data access via triggered buffers.

The measurements are stored in two 16-bit consecutive registers (addresses 0x04 and 0x05) as little endian, unsigned data.

Hi Javier,

We have to be a little careful with pushing data from the stack. Need to makes sure holes are zero filled.

Jonathan

Quoted 10 lines
drivers/iio/light/veml6030.c +0 -0
index ccb43dfd5cf7..ce9af9a0e933 100644
 
 
 static const struct regmap_config veml6030_regmap_config = {
@@ -889,6 +928,35 @@ static irqreturn_t veml6030_event_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 939

Quoted 10 lines
drivers/iio/light/veml6030.c +9 -0
 
+static irqreturn_t veml6030_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6030_data *data = iio_priv(iio);
+	unsigned int reg;
+	int ch, ret, i = 0;
+	struct {
+		u16 chans[2];  

There is a hole here...

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 949

Quoted 10 lines
drivers/iio/light/veml6030.c +10 -0
+		aligned_s64 timestamp;
+	} scan;
+
+	iio_for_each_active_channel(iio, ch) {
+		ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch),
+				  &reg);
+		if (ret)
+			goto done;
+
+		scan.chans[i++] = reg;  

This fills in at least 1 channel, but maybe not the second.

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 951

Quoted 2 lines
drivers/iio/light/veml6030.c +2 -0
+	}
+  

So this leaks random stack data I think.

Upshot, when holes are involved or not all the channels are set, need memset(&scan, 0, sizeof(scan));
for the structure on the stack which will zero the holes as well as both channels.

Ancient article on this: https://lwn.net/Articles/417989/

We get away with it when they are in the iio_priv space because they are kzalloc + if we do leak data due to changes in configured channels it's just old sensor data which is (I think) never a security problem!

Quoted 25 lines
drivers/iio/light/veml6030.c +14 -0
+	iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp);
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6030_set_info(struct iio_dev *indio_dev)
 {
 	struct veml6030_data *data = iio_priv(indio_dev);
@@ -1077,6 +1145,12 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      veml6030_trigger_handler, NULL);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to register triggered buffer");
+
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
Quoted 6 lines
Quoted 5 lines

---
base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

Hi Jonathan,

thanks a lot for your explanation and the link, it makes perfect sense. By the way, when I moved this struct from the iio_priv to the function, I took a look at some existing code, and a couple of them might have the same issue:

- temperature/tmp006.c: it also has a hole between the two 16-bit channels and the timestamp (aligned(8)), but it is not set to zero.

Quoted 7 lines

- adc/ti-ads1119.c: the scan consists of an unsigned int and the timestamp (aligned(8)). I believe there is a hole there as well.

I did not go over all drivers (most of them store the scan struct in the iio_priv space anyway), but at least those two look suspicious.

Should I fix (e.g. memset) those two I mentioned?

Please do. Thanks!

Jonathan

Quoted 4 lines

Best regards,
Javier Carrasco

author maintainer
#4 lore raw
Quoted 82 lines · Jonathan Cameron
Quoted 82 lines
Quoted 61 lines

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 939

Quoted 9 lines
drivers/iio/light/veml6030.c +9 -0
+static irqreturn_t veml6030_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6030_data *data = iio_priv(iio);
+	unsigned int reg;
+	int ch, ret, i = 0;
+	struct {
+		u16 chans[2];  

There is a hole here...

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 949

Quoted 10 lines
drivers/iio/light/veml6030.c +10 -0
+		aligned_s64 timestamp;
+	} scan;
+
+	iio_for_each_active_channel(iio, ch) {
+		ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch),
+				  &reg);
+		if (ret)
+			goto done;
+
+		scan.chans[i++] = reg;  

This fills in at least 1 channel, but maybe not the second.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+  

So this leaks random stack data I think.

Upshot, when holes are involved or not all the channels are set, need memset(&scan, 0, sizeof(scan));
for the structure on the stack which will zero the holes as well as both channels.

Ancient article on this: https://lwn.net/Articles/417989/

We get away with it when they are in the iio_priv space because they are kzalloc + if we do leak data due to changes in configured channels it's just old sensor data which is (I think) never a security problem!

Quoted 25 lines
drivers/iio/light/veml6030.c +14 -0
+	iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp);
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6030_set_info(struct iio_dev *indio_dev)
 {
 	struct veml6030_data *data = iio_priv(indio_dev);
@@ -1077,6 +1145,12 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      veml6030_trigger_handler, NULL);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to register triggered buffer");
+
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
Quoted 6 lines
Quoted 5 lines

---
base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

Hi Jonathan,

thanks a lot for your explanation and the link, it makes perfect sense. By the way, when I moved this struct from the iio_priv to the function, I took a look at some existing code, and a couple of them might have the same issue:

- temperature/tmp006.c: it also has a hole between the two 16-bit channels and the timestamp (aligned(8)), but it is not set to zero.

- adc/ti-ads1119.c: the scan consists of an unsigned int and the timestamp (aligned(8)). I believe there is a hole there as well.

Quoted 9 lines
Quoted 4 lines

I did not go over all drivers (most of them store the scan struct in the iio_priv space anyway), but at least those two look suspicious.

Should I fix (e.g. memset) those two I mentioned?

Please do. Thanks!

Jonathan

Ok, I will take a closer look to check if there are more drivers leaking uninitialized data. By the way, would you tag the fixes for stable? This becoming an attack vector might be a bit theoretical, and I don't know the consensus about the danger of passing uninitialized data to userspace.

Thanks again,
Javier Carrasco

maintainer
#5 lore raw
Quoted 90 lines · On Sun, 24 Nov 2024 15:47:23 +0100 Javier Carrasco…
Quoted 89 lines · Jonathan Cameron
Quoted 79 lines
Quoted 61 lines

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 939

Quoted 9 lines
drivers/iio/light/veml6030.c +9 -0
+static irqreturn_t veml6030_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *iio = pf->indio_dev;
+	struct veml6030_data *data = iio_priv(iio);
+	unsigned int reg;
+	int ch, ret, i = 0;
+	struct {
+		u16 chans[2];    

There is a hole here...

drivers/iio/light/veml6030.c static irqreturn_t veml6030_event_handler(int irq, void *private) · line 949

Quoted 10 lines
drivers/iio/light/veml6030.c +10 -0
+		aligned_s64 timestamp;
+	} scan;
+
+	iio_for_each_active_channel(iio, ch) {
+		ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch),
+				  &reg);
+		if (ret)
+			goto done;
+
+		scan.chans[i++] = reg;    

This fills in at least 1 channel, but maybe not the second.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+    

So this leaks random stack data I think.

Upshot, when holes are involved or not all the channels are set, need memset(&scan, 0, sizeof(scan));
for the structure on the stack which will zero the holes as well as both channels.

Ancient article on this: https://lwn.net/Articles/417989/

We get away with it when they are in the iio_priv space because they are kzalloc + if we do leak data due to changes in configured channels it's just old sensor data which is (I think) never a security problem!

Quoted 25 lines
drivers/iio/light/veml6030.c +14 -0
+	iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp);
+
+done:
+	iio_trigger_notify_done(iio->trig);
+
+	return IRQ_HANDLED;
+}
+
 static int veml6030_set_info(struct iio_dev *indio_dev)
 {
 	struct veml6030_data *data = iio_priv(indio_dev);
@@ -1077,6 +1145,12 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
+					      veml6030_trigger_handler, NULL);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to register triggered buffer");
+
 	return devm_iio_device_register(&client->dev, indio_dev);
 }
 
Quoted 6 lines
Quoted 5 lines

---
base-commit: 9dd2270ca0b38ee16094817f4a53e7ba78e31567
change-id: 20241106-veml6030_triggered_buffer-a38886ca4cce

Best regards,

Hi Jonathan,

thanks a lot for your explanation and the link, it makes perfect sense. By the way, when I moved this struct from the iio_priv to the function, I took a look at some existing code, and a couple of them might have the same issue:

- temperature/tmp006.c: it also has a hole between the two 16-bit channels and the timestamp (aligned(8)), but it is not set to zero.

Quoted 7 lines

- adc/ti-ads1119.c: the scan consists of an unsigned int and the timestamp (aligned(8)). I believe there is a hole there as well.

I did not go over all drivers (most of them store the scan struct in the iio_priv space anyway), but at least those two look suspicious.

Should I fix (e.g. memset) those two I mentioned?

Please do. Thanks!

Quoted 7 lines
Quoted 2 lines

Jonathan

Ok, I will take a closer look to check if there are more drivers leaking uninitialized data. By the way, would you tag the fixes for stable? This becoming an attack vector might be a bit theoretical, and I don't know the consensus about the danger of passing uninitialized data to userspace.

Yes. Stable would be appropriate for these. Thanks for looking into it. Your patch had me adding checking this to my todo list but I'm very happy to have you do it instead! :)

Jonathan

Quoted 3 lines

Thanks again,
Javier Carrasco