From patchwork Mon Oct 3 08:11:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 12997142 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3A2AC433FE for ; Mon, 3 Oct 2022 08:38:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbiJCIiV (ORCPT ); Mon, 3 Oct 2022 04:38:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231276AbiJCIh6 (ORCPT ); Mon, 3 Oct 2022 04:37:58 -0400 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AD3450187; Mon, 3 Oct 2022 01:11:19 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id q17so10881811lji.11; Mon, 03 Oct 2022 01:11:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=9BUwF+XZJCR4cJYLZDpcdXvk0SAKfXGR6yS3B9zUzGY=; b=DGZMtTQdqPrFHrw9jAkijix7FwZQgmOF1AN40Glh9zxOZlqBMgNK+YgtQCBxDF+VB7 fZb3zZsXVswJFoTTtt2AxCWOgK//IK9sb42PE/BGlK71o+aATJZ1seYyS3gF1DoU1NK+ jtKY0RV6eyL70quAivpzR06I3xCDaFZhbWKh7+YE1XbArSpNTqYirJ8rfQKP3CQcqZM3 tdeazwe3EILBZSLXnAH3+GKMZKs6jSPXS/eL5ZYPqaIcj7iOKsIeV4CflQVySItz1S2v 5Js6tPJgeqOrWZfaHO/LnRo4h6+yWaV1Es2N7puJOlgAn9iI7vGLCr2I8vPhOQIN5Te0 ClUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=9BUwF+XZJCR4cJYLZDpcdXvk0SAKfXGR6yS3B9zUzGY=; b=RyMUEXn8JLWN0NHJC77BkSyB5oQPcpklMIVqjTR0S42IPal4z0JRFdXJN9eF5X3jSE 3BAKG68Tzi13UW09GP2p/hn5ThtyPNuFymrCOPbQP75Fu9vhRmKc2G2JG9HgUfCqOBK8 o4MFWAj7fCXJgC1MJSJxlrpnv7+HvUlp/AJM8SbSBPoy6bfVYHUz5YBWkegJVHK0bIAu Bn+1m6+iP+0QvYj5ejz70u7RdaLc2rE8gZKj8XTYH/JZYttnO+atHKT6Iil7WQv9lKUE ieIFYYgFa6xtCqDqerNjweEEObuNFj/d1GHbPLsGA5ZkOf1+j6+ar08lzMCe8+hb+Bij Gajg== X-Gm-Message-State: ACrzQf2dVOQCboeb0GvJENcAZwuqvEjZhSnJuflZt/t97OUQzV9nCogq Ji4gIPUbZ456QSnlo1WELcM= X-Google-Smtp-Source: AMsMyM4Dbl6Y8NoSUpoi8ffWtCGje77ttIWdGPAl7KRlKgK5gIPUiH2uu9hXHSHhwfz3jNlnyPs2rg== X-Received: by 2002:a2e:bcc3:0:b0:26c:4c59:75ed with SMTP id z3-20020a2ebcc3000000b0026c4c5975edmr5810140ljp.505.1664784677371; Mon, 03 Oct 2022 01:11:17 -0700 (PDT) Received: from dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi (dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi. [2001:14ba:16f3:4a00::1]) by smtp.gmail.com with ESMTPSA id u11-20020ac243cb000000b0048af9576d30sm1363878lfl.83.2022.10.03.01.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 01:11:16 -0700 (PDT) Date: Mon, 3 Oct 2022 11:11:12 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Matti Vaittinen , Alexandru Ardelean , Miquel Raynal , Srinivas Pandruvada , Miaoqian Lin , Uwe =?iso-8859-1?q?Kleine-K=F6nig?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 03/10] iio: bmc150-accel-core: Fix unsafe buffer attributes Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The iio_triggered_buffer_setup_ext() was changed by commit 15097c7a1adc ("iio: buffer: wrap all buffer attributes into iio_dev_attr") to silently expect that all attributes given in buffer_attrs array are device-attributes. This expectation was not forced by the API - and some drivers did register attributes created by IIO_CONST_ATTR(). The added attribute "wrapping" does not copy the pointer to stored string constant and when the sysfs file is read the kernel will access to invalid location. Change the IIO_CONST_ATTRs from the driver to IIO_DEVICE_ATTR in order to prevent the invalid memory access. Signed-off-by: Matti Vaittinen Fixes: 15097c7a1adc ("iio: buffer: wrap all buffer attributes into iio_dev_attr") --- v2 => v3: Split change to own patch for simpler fix backporting. --- drivers/iio/accel/bmc150-accel-core.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c index 57e8a8350cd1..92f8b139acce 100644 --- a/drivers/iio/accel/bmc150-accel-core.c +++ b/drivers/iio/accel/bmc150-accel-core.c @@ -925,17 +925,30 @@ static const struct iio_chan_spec_ext_info bmc150_accel_ext_info[] = { { } }; -static IIO_CONST_ATTR(hwfifo_watermark_min, "1"); -static IIO_CONST_ATTR(hwfifo_watermark_max, - __stringify(BMC150_ACCEL_FIFO_LENGTH)); +static ssize_t hwfifo_watermark_min_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%s\n", "1"); +} + +static ssize_t hwfifo_watermark_max_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%s\n", __stringify(BMC150_ACCEL_FIFO_LENGTH)); +} + +static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0); +static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0); static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO, bmc150_accel_get_fifo_state, NULL, 0); static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO, bmc150_accel_get_fifo_watermark, NULL, 0); static const struct attribute *bmc150_accel_fifo_attributes[] = { - &iio_const_attr_hwfifo_watermark_min.dev_attr.attr, - &iio_const_attr_hwfifo_watermark_max.dev_attr.attr, + &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, + &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, &iio_dev_attr_hwfifo_watermark.dev_attr.attr, &iio_dev_attr_hwfifo_enabled.dev_attr.attr, NULL,