Skip to content

Commit dc537a7

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 <nuno.sa@analog.com> Signed-off-by: Julien Stephan <jstephan@baylibre.com> Link: https://lore.kernel.org/r/20240530-iio-core-fix-segfault-v3-1-8b7cd2a03773@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent af84813 commit dc537a7

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

drivers/iio/industrialio-core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,11 @@ static ssize_t iio_read_channel_info(struct device *dev,
758758
INDIO_MAX_RAW_ELEMENTS,
759759
vals, &val_len,
760760
this_attr->address);
761-
else
761+
else if (indio_dev->info->read_raw)
762762
ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
763763
&vals[0], &vals[1], this_attr->address);
764+
else
765+
return -EINVAL;
764766

765767
if (ret < 0)
766768
return ret;
@@ -842,6 +844,9 @@ static ssize_t iio_read_channel_info_avail(struct device *dev,
842844
int length;
843845
int type;
844846

847+
if (!indio_dev->info->read_avail)
848+
return -EINVAL;
849+
845850
ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
846851
&vals, &type, &length,
847852
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
@@ -543,6 +543,7 @@ EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
543543
static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
544544
enum iio_chan_info_enum info)
545545
{
546+
const struct iio_info *iio_info = chan->indio_dev->info;
546547
int unused;
547548
int vals[INDIO_MAX_RAW_ELEMENTS];
548549
int ret;
@@ -554,15 +555,18 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
554555
if (!iio_channel_has_info(chan->channel, info))
555556
return -EINVAL;
556557

557-
if (chan->indio_dev->info->read_raw_multi) {
558-
ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
559-
chan->channel, INDIO_MAX_RAW_ELEMENTS,
560-
vals, &val_len, info);
558+
if (iio_info->read_raw_multi) {
559+
ret = iio_info->read_raw_multi(chan->indio_dev,
560+
chan->channel,
561+
INDIO_MAX_RAW_ELEMENTS,
562+
vals, &val_len, info);
561563
*val = vals[0];
562564
*val2 = vals[1];
565+
} else if (iio_info->read_raw) {
566+
ret = iio_info->read_raw(chan->indio_dev,
567+
chan->channel, val, val2, info);
563568
} else {
564-
ret = chan->indio_dev->info->read_raw(chan->indio_dev,
565-
chan->channel, val, val2, info);
569+
return -EINVAL;
566570
}
567571

568572
return ret;
@@ -750,11 +754,15 @@ static int iio_channel_read_avail(struct iio_channel *chan,
750754
const int **vals, int *type, int *length,
751755
enum iio_chan_info_enum info)
752756
{
757+
const struct iio_info *iio_info = chan->indio_dev->info;
758+
753759
if (!iio_channel_has_available(chan->channel, info))
754760
return -EINVAL;
755761

756-
return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
757-
vals, type, length, info);
762+
if (iio_info->read_avail)
763+
return iio_info->read_avail(chan->indio_dev, chan->channel,
764+
vals, type, length, info);
765+
return -EINVAL;
758766
}
759767

760768
int iio_read_avail_channel_attribute(struct iio_channel *chan,
@@ -917,8 +925,12 @@ EXPORT_SYMBOL_GPL(iio_get_channel_type);
917925
static int iio_channel_write(struct iio_channel *chan, int val, int val2,
918926
enum iio_chan_info_enum info)
919927
{
920-
return chan->indio_dev->info->write_raw(chan->indio_dev,
921-
chan->channel, val, val2, info);
928+
const struct iio_info *iio_info = chan->indio_dev->info;
929+
930+
if (iio_info->write_raw)
931+
return iio_info->write_raw(chan->indio_dev,
932+
chan->channel, val, val2, info);
933+
return -EINVAL;
922934
}
923935

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

0 commit comments

Comments
 (0)