Skip to content

Commit 72f022e

Browse files
justephgregkh
authored andcommitted
driver: iio: add missing checks on iio_info's callback access
[ Upstream commit c4ec8de ] Some callbacks from iio_info structure are accessed without any check, so if a driver doesn't implement them trying to access the corresponding sysfs entries produce a kernel oops such as: [ 2203.527791] Unable to handle kernel NULL pointer dereference at virtual address 00000000 when execute [...] [ 2203.783416] Call trace: [ 2203.783429] iio_read_channel_info_avail from dev_attr_show+0x18/0x48 [ 2203.789807] dev_attr_show from sysfs_kf_seq_show+0x90/0x120 [ 2203.794181] sysfs_kf_seq_show from seq_read_iter+0xd0/0x4e4 [ 2203.798555] seq_read_iter from vfs_read+0x238/0x2a0 [ 2203.802236] vfs_read from ksys_read+0xa4/0xd4 [ 2203.805385] ksys_read from ret_fast_syscall+0x0/0x54 [ 2203.809135] Exception stack(0xe0badfa8 to 0xe0badff0) [ 2203.812880] dfa0: 00000003 b6f10f80 00000003 b6eab000 00020000 00000000 [ 2203.819746] dfc0: 00000003 b6f10f80 7ff00000 00000003 00000003 00000000 00020000 00000000 [ 2203.826619] dfe0: b6e1bc88 bed80958 b6e1bc94 b6e1bcb0 [ 2203.830363] Code: bad PC value [ 2203.832695] ---[ end trace 0000000000000000 ]--- Reviewed-by: Nuno Sa <[email protected]> Signed-off-by: Julien Stephan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 6290d3f commit 72f022e

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

drivers/iio/industrialio-core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,11 @@ static ssize_t iio_read_channel_info(struct device *dev,
752752
INDIO_MAX_RAW_ELEMENTS,
753753
vals, &val_len,
754754
this_attr->address);
755-
else
755+
else if (indio_dev->info->read_raw)
756756
ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
757757
&vals[0], &vals[1], this_attr->address);
758+
else
759+
return -EINVAL;
758760

759761
if (ret < 0)
760762
return ret;
@@ -836,6 +838,9 @@ static ssize_t iio_read_channel_info_avail(struct device *dev,
836838
int length;
837839
int type;
838840

841+
if (!indio_dev->info->read_avail)
842+
return -EINVAL;
843+
839844
ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
840845
&vals, &type, &length,
841846
this_attr->address);

drivers/iio/industrialio-event.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ static ssize_t iio_ev_state_store(struct device *dev,
285285
if (ret < 0)
286286
return ret;
287287

288+
if (!indio_dev->info->write_event_config)
289+
return -EINVAL;
290+
288291
ret = indio_dev->info->write_event_config(indio_dev,
289292
this_attr->c, iio_ev_attr_type(this_attr),
290293
iio_ev_attr_dir(this_attr), val);
@@ -300,6 +303,9 @@ static ssize_t iio_ev_state_show(struct device *dev,
300303
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
301304
int val;
302305

306+
if (!indio_dev->info->read_event_config)
307+
return -EINVAL;
308+
303309
val = indio_dev->info->read_event_config(indio_dev,
304310
this_attr->c, iio_ev_attr_type(this_attr),
305311
iio_ev_attr_dir(this_attr));
@@ -318,6 +324,9 @@ static ssize_t iio_ev_value_show(struct device *dev,
318324
int val, val2, val_arr[2];
319325
int ret;
320326

327+
if (!indio_dev->info->read_event_value)
328+
return -EINVAL;
329+
321330
ret = indio_dev->info->read_event_value(indio_dev,
322331
this_attr->c, iio_ev_attr_type(this_attr),
323332
iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),

drivers/iio/inkern.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
562562
static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
563563
enum iio_chan_info_enum info)
564564
{
565+
const struct iio_info *iio_info = chan->indio_dev->info;
565566
int unused;
566567
int vals[INDIO_MAX_RAW_ELEMENTS];
567568
int ret;
@@ -573,15 +574,18 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
573574
if (!iio_channel_has_info(chan->channel, info))
574575
return -EINVAL;
575576

576-
if (chan->indio_dev->info->read_raw_multi) {
577-
ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
578-
chan->channel, INDIO_MAX_RAW_ELEMENTS,
579-
vals, &val_len, info);
577+
if (iio_info->read_raw_multi) {
578+
ret = iio_info->read_raw_multi(chan->indio_dev,
579+
chan->channel,
580+
INDIO_MAX_RAW_ELEMENTS,
581+
vals, &val_len, info);
580582
*val = vals[0];
581583
*val2 = vals[1];
584+
} else if (iio_info->read_raw) {
585+
ret = iio_info->read_raw(chan->indio_dev,
586+
chan->channel, val, val2, info);
582587
} else {
583-
ret = chan->indio_dev->info->read_raw(chan->indio_dev,
584-
chan->channel, val, val2, info);
588+
return -EINVAL;
585589
}
586590

587591
return ret;
@@ -801,11 +805,15 @@ static int iio_channel_read_avail(struct iio_channel *chan,
801805
const int **vals, int *type, int *length,
802806
enum iio_chan_info_enum info)
803807
{
808+
const struct iio_info *iio_info = chan->indio_dev->info;
809+
804810
if (!iio_channel_has_available(chan->channel, info))
805811
return -EINVAL;
806812

807-
return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
808-
vals, type, length, info);
813+
if (iio_info->read_avail)
814+
return iio_info->read_avail(chan->indio_dev, chan->channel,
815+
vals, type, length, info);
816+
return -EINVAL;
809817
}
810818

811819
int iio_read_avail_channel_attribute(struct iio_channel *chan,
@@ -995,8 +1003,12 @@ EXPORT_SYMBOL_GPL(iio_get_channel_type);
9951003
static int iio_channel_write(struct iio_channel *chan, int val, int val2,
9961004
enum iio_chan_info_enum info)
9971005
{
998-
return chan->indio_dev->info->write_raw(chan->indio_dev,
999-
chan->channel, val, val2, info);
1006+
const struct iio_info *iio_info = chan->indio_dev->info;
1007+
1008+
if (iio_info->write_raw)
1009+
return iio_info->write_raw(chan->indio_dev,
1010+
chan->channel, val, val2, info);
1011+
return -EINVAL;
10001012
}
10011013

10021014
int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,

0 commit comments

Comments
 (0)