Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp623089lqb; Wed, 29 May 2024 06:12:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX9zcTSYTz7nItsNvuCmMgCgs/GIzPu989UJwGc3IqCpvfm7LSFAat/FY10R9PTH/vJSLQpgJpWqqFKKmawrq7nnnBHXCJp7RwFOXpD1Q== X-Google-Smtp-Source: AGHT+IEhbagzaINRStEQM1+/kmCJtUBghUgYoFuFa9DxGjsD9H0PSPn1NT3fArxLSAyc5CD34nkq X-Received: by 2002:a05:6a20:561b:b0:1af:ab23:82ce with SMTP id adf61e73a8af0-1b259a090d6mr2835252637.3.1716988371767; Wed, 29 May 2024 06:12:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716988371; cv=pass; d=google.com; s=arc-20160816; b=NpQu7oKJdNHV1lo6P9dLrfLUXzR0exKWbffU263CaIqM2cEBxEqk4h5s00i1I0PylG CHMtzBZU9fRP9ZY4esOy+sxUZauab/iT3+VUw3S9DKaxt9GUW+yzLm6OlwGa+3Hl9t6T d/kovIq2hC6p1ChbDmbWS+Iutfa36p+qOmNe1UiRvYkSOSlHglQj9F3qYhKHBVO4vqLi Rd5IeDYO4rDl94V3UsUW52U6ZSktd7aFbZ2UkjLxoxFSe2V3qKhh1JvbfZGNCvwxCpn6 3ebv8Dt0JLxA1/4ZrIcWrAHRQ5jIxxoCVB70+y9nP3I0pPKxoHFl2bdPRkno7XYORhMw pVfg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=azRDT6gpTMZiud8t7xJbr3kGPCFGLs0Glt2PXkWkLkE=; fh=7FSg2BqgMr0QN7Ldd26xulMuOUYsirMZAYj+cHIaiR4=; b=y5/JpqbCf4m7ZQLYkBJS7ny5xXBX8sLdKvxSg1CTUr2GCFm3eskYjTFN1zvShpX4fT 8wG5LXunT28iEp3fWv4e1Uu2uAdB6xCt4MRJa2JnXkE6dr6tGZnqdodrsdilwDbRUIBN xno2piLJ/nrXR/w9LHsl7VGR1JjNU/2EcuB2fNATn5wbqIkmLHDKVu3cnSdbYKKpbJMU N/JMwwOyojTvHSlctF+Y70x2NbZEOP73TEnmFrfHWAiLnqCdWnkSs7+kysK5c3FLcpBI gJ+G3gCU1FWCkkUf4zNUmfII4Tpxevgku/p1SfZlvC8U0ANXvas5I0e5FOBizKF5D1xO dtsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=wrNHQVYu; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-194134-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194134-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fc0511a8si9961839b3a.11.2024.05.29.06.12.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 06:12:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-194134-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=wrNHQVYu; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-194134-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194134-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E4667287A77 for ; Wed, 29 May 2024 13:11:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AACFF134D1; Wed, 29 May 2024 13:11:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="wrNHQVYu" Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DC65E576 for ; Wed, 29 May 2024 13:10:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988260; cv=none; b=XdU0iO2LGnw7qtA22ToAi113ISse/z4Z/INCswNuis8WXO1pR952r3TxDgmqhUEQzjOs6uympdCBE5YVu9wNwn8zCYiGwAlAKBE3pXpVYMvqfu25iTkQ5lkIuha1FlKj7nGrj9VkeNAE1n5aHzDpazOA9VlqXjkhX3QZOin7dSo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716988260; c=relaxed/simple; bh=27tuP5wl143oePuzHxzCglDI6RFchxnmMb1lpfU7CoE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=gbKJeCFPOOC5OKlrPIBmLKwmdsj4mSKKUL8P9G4mfPwAYIViXaRAGswFWUgMG0kdwgX9/9nA1LQiD8yNo3+5VMfV2oj/Hyr5GVG17CTXCO6tLkq4vqk46NEKPJwvhlj48GdRms0h/Dtb3iqF5dcla7PXZVkq3c+XSJH207xpAng= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=wrNHQVYu; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a62ef52e837so224313166b.3 for ; Wed, 29 May 2024 06:10:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1716988254; x=1717593054; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=azRDT6gpTMZiud8t7xJbr3kGPCFGLs0Glt2PXkWkLkE=; b=wrNHQVYu4LB4eygkJBVjb90ryHlDJ3O8BJs6maUhaA4/kgeZEL9zqvCKrFaFvbZgK1 yDhB8shkMDCdRWN3Jqhd4KLoYE9TvrFTOCl8k3ZKaTPpu1QHMiLM83ekEs/QqEz25NKh 0thSDh+h82RYblJAwoCKKtdS28Kc8iT5wSJS63+8x8hoQ0R1qBDZJ27noc2Qu3ptZ6T+ I+YLs2opESyRECfUbMBCvTKGTjp7PoQE78UA3ctDMIwKXaKxGVfCFlFajlAkMzWORjYr uxYIZFkO3TbvUVsWR6ecYc2npPODoqyTjsi7Wna9w0/IqHBmauo0f9Jzwapnc8RSMYoC ER3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716988254; x=1717593054; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=azRDT6gpTMZiud8t7xJbr3kGPCFGLs0Glt2PXkWkLkE=; b=KuOM4z5a6rs7OuaBY+wbI4qcKZ6MaZi6NfWTXiB7ZsPIYNiOZZ8Gf3GTx2Qrz8CiKl 7kY1aTGSDsi3DfsPtarU1Rw44tLDXXS11UQrggRniXe6Nnok4Houu5LXgx8YKoR2fPTQ oXCMsxr/HWSEgVUCnMKGHsq7hgK0u4Z+E2Kv05yovRQs5JdyyymONfCubhGlsxoLrg4D yWIOprpxDVr1Dj4iBckOPwLQsSgcJapMaOoNIqdcgbVfSQV9mheTG+G/TavaOnJFGDA3 bgGKcXDeKYiSUlcX5tDWoWwgvOqC2OVzYJDBaoyS5OrgUivt9vcXODXXW+IM1z2PV0Y4 04XA== X-Forwarded-Encrypted: i=1; AJvYcCVqa3eoUesTEru6a7xE0022rgdA6QL+Y6XIAj5nlqpwGJS+0o1Myb8pxXfmadbohKC33AjqKB+9Lkj8tRQvB5ER4p/K506Qjsi4lAMw X-Gm-Message-State: AOJu0Yxa39AJhoUzioSrLZbb6vqsqCLq0lVK1FNHuqEek6I7W6T30SZQ tf/QDB0OzsS5clKMdAxYiwzq5XyxNXhfHLivp6qoRsZYbTN3JhkmVy2W71K9azgXiznZhVWQmn6 9q8GAJP7SCYvWNQGR2YuaekyNBx4Jp7vjyS0qGw== X-Received: by 2002:a17:906:f0d1:b0:a62:80f4:9ef2 with SMTP id a640c23a62f3a-a6280f4a19emr916098366b.71.1716988254571; Wed, 29 May 2024 06:10:54 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240529-iio-core-fix-segfault-v2-1-7b5a5fa6853f@baylibre.com> <20240529130458.000049e6@Huawei.com> In-Reply-To: <20240529130458.000049e6@Huawei.com> From: Julien Stephan Date: Wed, 29 May 2024 15:10:42 +0200 Message-ID: Subject: Re: [PATCH v2] driver: iio: add missing checks on iio_info's callback access To: Jonathan Cameron Cc: Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Nuno Sa Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Le mer. 29 mai 2024 =C3=A0 14:05, Jonathan Cameron a =C3=A9crit : > > On Wed, 29 May 2024 13:55:52 +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 virt= ual address 00000000 when execute > > [...] > > [ 2203.783416] Call trace: > > [ 2203.783429] iio_read_channel_info_avail from dev_attr_show+0x18/0x4= 8 > > [ 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 b6eab= 000 00020000 00000000 > > [ 2203.819746] dfc0: 00000003 b6f10f80 7ff00000 00000003 00000003 00000= 000 00020000 00000000 > > [ 2203.826619] dfe0: b6e1bc88 bed80958 b6e1bc94 b6e1bcb0 > > [ 2203.830363] Code: bad PC value > > [ 2203.832695] ---[ end trace 0000000000000000 ]--- > > > > Reviewed-by: Nuno Sa > > Signed-off-by: Julien Stephan > > How bad would a registration time check look? > I'd rather catch this early than have drivers with missing hooks > that we don't notice because no one pokes the file. Hi Jonathan, Do you mean something like that (as it is done for ext_info for example) : ret =3D __iio_add_chan_devattr(iio_chan_info_postfix[i], chan, - &iio_read_channel_info, - &iio_write_channel_info, + indio_dev->info->read_raw ? + &iio_read_channel_info : NULL, + indio_dev->info->write_raw ? + &iio_write_channel_info : NULL, i, shared_by, &indio_dev->dev, NULL, &iio_dev_opaque->channel_attr_list); Or do you want to check even before and do not create the sysfs entry if there is no callback registered by the driver? Julien > > The inkern ones are good though. > > Jonathan > > > --- > > Changes in v2: > > - crop dmesg log to show only pertinent info and reduce commit message > > - Link to v1: https://lore.kernel.org/r/20240529-iio-core-fix-segfault-= v1-1-7ff1ba881d38@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(-) > > > > 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_ELE= MENTS, > > vals, &val_len, > > this_attr->addres= s); > > - else > > + else if (indio_dev->info->read_raw) > > ret =3D indio_dev->info->read_raw(indio_dev, this_attr->c= , > > &vals[0], &vals[1], this_attr->addres= s); > > + else > > + return -EINVAL; > > > > if (ret < 0) > > return ret; > > @@ -842,6 +844,9 @@ static ssize_t iio_read_channel_info_avail(struct d= evice *dev, > > int length; > > int type; > > > > + if (!indio_dev->info->read_avail) > > + return -EINVAL; > > + > > ret =3D 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/industriali= o-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 *de= v, > > if (ret < 0) > > return ret; > > > > + if (!indio_dev->info->write_event_config) > > + return -EINVAL; > > + > > ret =3D 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 =3D to_iio_dev_attr(attr); > > int val; > > > > + if (!indio_dev->info->read_event_config) > > + return -EINVAL; > > + > > val =3D 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 =3D 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 *ch= an, int *val, int *val2, > > vals, &val_len, info); > > *val =3D vals[0]; > > *val2 =3D vals[1]; > > - } else { > > + } else if (chan->indio_dev->info->read_raw) { > > ret =3D 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_chann= el *chan, > > if (!iio_channel_has_available(chan->channel, info)) > > return -EINVAL; > > > > - return chan->indio_dev->info->read_avail(chan->indio_dev, chan->c= hannel, > > - 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, leng= th, 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 va= l2, > > 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, va= l, val2, info); > > + return -EINVAL; > > } > > > > int iio_write_channel_attribute(struct iio_channel *chan, int val, int= val2, > > > > --- > > base-commit: 409b6d632f5078f3ae1018b6e43c32f2e12f6736 > > change-id: 20240528-iio-core-fix-segfault-aa74be7eee4a > > > > Best regards, >