Message ID | 20240529-iio-core-fix-segfault-v1-1-7ff1ba881d38@baylibre.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | driver: iio: add missing checks on iio_info's callback access | expand |
On Wed, 2024-05-29 at 12:07 +0200, Julien Stephan wrote: > 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.535795] [00000000] *pgd=01e70831, *pte=00000000, *ppte=00000000 > [ 2203.540822] Internal error: Oops: 80000007 [#1] PREEMPT SMP ARM > [ 2203.552078] CPU: 0 PID: 1286 Comm: cat Not tainted 6.9.0-rc5-ad7380x-mainline- > g9bf895a40063-dirty #226 > [ 2203.560079] Hardware name: Xilinx Zynq Platform > [ 2203.563300] PC is at 0x0 > [ 2203.564523] LR is at iio_read_channel_info_avail+0x48/0xd8 > [ 2203.568716] pc : [<00000000>] lr : [<c07b52e4>] psr: a0000013 > [ 2203.573672] sp : e0bade60 ip : 5e861799 fp : 00000001 > [ 2203.577586] r10: c2dcd298 r9 : 00400cc0 r8 : c2dc6000 > [ 2203.581501] r7 : c4339008 r6 : c2dc6000 r5 : 00000000 r4 : c1003fc8 > [ 2203.586718] r3 : e0bade70 r2 : e0bade68 r1 : bf00ac88 r0 : c4339000 > [ 2203.591935] Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none > [ 2203.597761] Control: 18c5387d Table: 01db804a DAC: 00000051 > [ 2203.602194] Register r0 information: slab kmalloc-1k start c4339000 pointer > offset 0 size 1024 > [ 2203.609520] Register r1 information: 2-page vmalloc region starting at > 0xbf00a000 allocated at load_module+0x704/0x1954 > [ 2203.619016] Register r2 information: 2-page vmalloc region starting at > 0xe0bac000 allocated at kernel_clone+0xb8/0x368 > [ 2203.628417] Register r3 information: 2-page vmalloc region starting at > 0xe0bac000 allocated at kernel_clone+0xb8/0x368 > [ 2203.637808] Register r4 information: non-slab/vmalloc memory > [ 2203.642166] Register r5 information: NULL pointer > [ 2203.645559] Register r6 information: slab kmalloc-4k start c2dc6000 pointer > offset 0 size 4096 > [ 2203.652886] Register r7 information: slab kmalloc-1k start c4339000 pointer > offset 8 size 1024 > [ 2203.660211] Register r8 information: slab kmalloc-4k start c2dc6000 pointer > offset 0 size 4096 > [ 2203.667537] Register r9 information: non-paged memory > [ 2203.671278] Register r10 information: slab vm_area_struct start c2dcd280 pointer > offset 24 size 80 > [ 2203.678951] Register r11 information: non-paged memory > [ 2203.682779] Register r12 information: non-paged memory > [ 2203.686607] Process cat (pid: 1286, stack limit = 0x354430a3) > [ 2203.691052] Stack: (0xe0bade60 to 0xe0bae000) > [ 2203.694105] de60: e0bade6c 00000019 c105fe40 00000001 00000002 5e861799 c4309ec0 > c07b529c > [ 2203.700979] de80: c411b200 c051f1e4 c0b48d30 c2dcd280 c411b200 c02c1fb8 c2dcd280 > 00020000 > [ 2203.707853] dea0: e0badf20 c0b12d88 7ffff000 c026c944 c1003fc8 00000000 c2dcd2a8 > e0badf08 > [ 2203.714728] dec0: b6eaa000 c0206480 00000001 e0badfb0 c2dcda00 c1003fc8 c1e46d80 > c02c0ba0 > [ 2203.721602] dee0: c0b12d88 e0badf78 b6eab000 00000000 00000000 c0240114 00000a55 > c1dbadb8 > [ 2203.728476] df00: c1dbadb8 00000000 00000000 00000000 b6eab000 00020000 00000001 > 00000000 > [ 2203.735342] df20: c1e46d80 00000000 00000000 00000000 00000000 00000000 00000000 > 00000000 > [ 2203.742208] df40: 00000000 00000000 00000000 5e861799 c1003fc8 c1003fc8 c1e46d80 > 00000000 > [ 2203.749074] df60: 00000000 c1e46d80 b6eab000 00020000 00000000 c0240a80 00000000 > 00000000 > [ 2203.755949] df80: 00028008 5e861799 00000003 b6f10f80 7ff00000 00000003 c01002c4 > c6716e80 > [ 2203.762814] dfa0: 00000003 c0100060 00000003 b6f10f80 00000003 b6eab000 00020000 > 00000000 > [ 2203.769680] dfc0: 00000003 b6f10f80 7ff00000 00000003 00000003 00000000 00020000 > 00000000 > [ 2203.776546] dfe0: b6e1bc88 bed80958 b6e1bc94 b6e1bcb0 60000010 00000003 00000000 > 00000000 > [ 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 ]--- > > Signed-off-by: Julien Stephan <jstephan@baylibre.com> > --- I think you should crop the splat on the commit message only with the pertinent info. With that, Reviewed-by: Nuno Sa <nuno.sa@analog.com>
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index fa7cc051b4c4..2f185b386949 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -758,9 +758,11 @@ static ssize_t iio_read_channel_info(struct device *dev, INDIO_MAX_RAW_ELEMENTS, vals, &val_len, this_attr->address); - else + else if (indio_dev->info->read_raw) ret = indio_dev->info->read_raw(indio_dev, this_attr->c, &vals[0], &vals[1], this_attr->address); + else + return -EINVAL; if (ret < 0) return ret; @@ -842,6 +844,9 @@ static ssize_t iio_read_channel_info_avail(struct device *dev, int length; int type; + if (!indio_dev->info->read_avail) + return -EINVAL; + ret = indio_dev->info->read_avail(indio_dev, this_attr->c, &vals, &type, &length, this_attr->address); diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c index 910c1f14abd5..a64f8fbac597 100644 --- a/drivers/iio/industrialio-event.c +++ b/drivers/iio/industrialio-event.c @@ -285,6 +285,9 @@ static ssize_t iio_ev_state_store(struct device *dev, if (ret < 0) return ret; + if (!indio_dev->info->write_event_config) + return -EINVAL; + ret = indio_dev->info->write_event_config(indio_dev, this_attr->c, iio_ev_attr_type(this_attr), iio_ev_attr_dir(this_attr), val); @@ -300,6 +303,9 @@ static ssize_t iio_ev_state_show(struct device *dev, struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); int val; + if (!indio_dev->info->read_event_config) + return -EINVAL; + val = indio_dev->info->read_event_config(indio_dev, this_attr->c, iio_ev_attr_type(this_attr), iio_ev_attr_dir(this_attr)); @@ -318,6 +324,9 @@ static ssize_t iio_ev_value_show(struct device *dev, int val, val2, val_arr[2]; int ret; + if (!indio_dev->info->read_event_value) + return -EINVAL; + ret = indio_dev->info->read_event_value(indio_dev, this_attr->c, iio_ev_attr_type(this_attr), iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 52d773261828..74f87f6ac390 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -560,9 +560,11 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, vals, &val_len, info); *val = vals[0]; *val2 = vals[1]; - } else { + } else if (chan->indio_dev->info->read_raw) { ret = chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel, val, val2, info); + } else { + return -EINVAL; } return ret; @@ -753,8 +755,10 @@ static int iio_channel_read_avail(struct iio_channel *chan, if (!iio_channel_has_available(chan->channel, info)) return -EINVAL; - return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel, - vals, type, length, info); + if (chan->indio_dev->info->read_avail) + return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel, + vals, type, length, info); + return -EINVAL; } int iio_read_avail_channel_attribute(struct iio_channel *chan, @@ -917,8 +921,10 @@ EXPORT_SYMBOL_GPL(iio_get_channel_type); static int iio_channel_write(struct iio_channel *chan, int val, int val2, enum iio_chan_info_enum info) { - return chan->indio_dev->info->write_raw(chan->indio_dev, - chan->channel, val, val2, info); + if (chan->indio_dev->info->write_raw) + return chan->indio_dev->info->write_raw(chan->indio_dev, + chan->channel, val, val2, info); + return -EINVAL; } int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
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.535795] [00000000] *pgd=01e70831, *pte=00000000, *ppte=00000000 [ 2203.540822] Internal error: Oops: 80000007 [#1] PREEMPT SMP ARM [ 2203.552078] CPU: 0 PID: 1286 Comm: cat Not tainted 6.9.0-rc5-ad7380x-mainline-g9bf895a40063-dirty #226 [ 2203.560079] Hardware name: Xilinx Zynq Platform [ 2203.563300] PC is at 0x0 [ 2203.564523] LR is at iio_read_channel_info_avail+0x48/0xd8 [ 2203.568716] pc : [<00000000>] lr : [<c07b52e4>] psr: a0000013 [ 2203.573672] sp : e0bade60 ip : 5e861799 fp : 00000001 [ 2203.577586] r10: c2dcd298 r9 : 00400cc0 r8 : c2dc6000 [ 2203.581501] r7 : c4339008 r6 : c2dc6000 r5 : 00000000 r4 : c1003fc8 [ 2203.586718] r3 : e0bade70 r2 : e0bade68 r1 : bf00ac88 r0 : c4339000 [ 2203.591935] Flags: NzCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none [ 2203.597761] Control: 18c5387d Table: 01db804a DAC: 00000051 [ 2203.602194] Register r0 information: slab kmalloc-1k start c4339000 pointer offset 0 size 1024 [ 2203.609520] Register r1 information: 2-page vmalloc region starting at 0xbf00a000 allocated at load_module+0x704/0x1954 [ 2203.619016] Register r2 information: 2-page vmalloc region starting at 0xe0bac000 allocated at kernel_clone+0xb8/0x368 [ 2203.628417] Register r3 information: 2-page vmalloc region starting at 0xe0bac000 allocated at kernel_clone+0xb8/0x368 [ 2203.637808] Register r4 information: non-slab/vmalloc memory [ 2203.642166] Register r5 information: NULL pointer [ 2203.645559] Register r6 information: slab kmalloc-4k start c2dc6000 pointer offset 0 size 4096 [ 2203.652886] Register r7 information: slab kmalloc-1k start c4339000 pointer offset 8 size 1024 [ 2203.660211] Register r8 information: slab kmalloc-4k start c2dc6000 pointer offset 0 size 4096 [ 2203.667537] Register r9 information: non-paged memory [ 2203.671278] Register r10 information: slab vm_area_struct start c2dcd280 pointer offset 24 size 80 [ 2203.678951] Register r11 information: non-paged memory [ 2203.682779] Register r12 information: non-paged memory [ 2203.686607] Process cat (pid: 1286, stack limit = 0x354430a3) [ 2203.691052] Stack: (0xe0bade60 to 0xe0bae000) [ 2203.694105] de60: e0bade6c 00000019 c105fe40 00000001 00000002 5e861799 c4309ec0 c07b529c [ 2203.700979] de80: c411b200 c051f1e4 c0b48d30 c2dcd280 c411b200 c02c1fb8 c2dcd280 00020000 [ 2203.707853] dea0: e0badf20 c0b12d88 7ffff000 c026c944 c1003fc8 00000000 c2dcd2a8 e0badf08 [ 2203.714728] dec0: b6eaa000 c0206480 00000001 e0badfb0 c2dcda00 c1003fc8 c1e46d80 c02c0ba0 [ 2203.721602] dee0: c0b12d88 e0badf78 b6eab000 00000000 00000000 c0240114 00000a55 c1dbadb8 [ 2203.728476] df00: c1dbadb8 00000000 00000000 00000000 b6eab000 00020000 00000001 00000000 [ 2203.735342] df20: c1e46d80 00000000 00000000 00000000 00000000 00000000 00000000 00000000 [ 2203.742208] df40: 00000000 00000000 00000000 5e861799 c1003fc8 c1003fc8 c1e46d80 00000000 [ 2203.749074] df60: 00000000 c1e46d80 b6eab000 00020000 00000000 c0240a80 00000000 00000000 [ 2203.755949] df80: 00028008 5e861799 00000003 b6f10f80 7ff00000 00000003 c01002c4 c6716e80 [ 2203.762814] dfa0: 00000003 c0100060 00000003 b6f10f80 00000003 b6eab000 00020000 00000000 [ 2203.769680] dfc0: 00000003 b6f10f80 7ff00000 00000003 00000003 00000000 00020000 00000000 [ 2203.776546] dfe0: b6e1bc88 bed80958 b6e1bc94 b6e1bcb0 60000010 00000003 00000000 00000000 [ 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 ]--- Signed-off-by: Julien Stephan <jstephan@baylibre.com> --- drivers/iio/industrialio-core.c | 7 ++++++- drivers/iio/industrialio-event.c | 9 +++++++++ drivers/iio/inkern.c | 16 +++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) --- base-commit: 409b6d632f5078f3ae1018b6e43c32f2e12f6736 change-id: 20240528-iio-core-fix-segfault-aa74be7eee4a Best regards,