From patchwork Mon Oct 3 08:10:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 12997141 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 72B5AC433FE for ; Mon, 3 Oct 2022 08:38:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231500AbiJCIiB (ORCPT ); Mon, 3 Oct 2022 04:38:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229770AbiJCIhi (ORCPT ); Mon, 3 Oct 2022 04:37:38 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC6C42BB2F; Mon, 3 Oct 2022 01:10:58 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id d26so2642529ljl.2; Mon, 03 Oct 2022 01:10:58 -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=OsCc64eJJaHmezFY3+fQiaWmA0ACCwHkFNgq4FVgBCc=; b=DAre7PP/V/+fUXyIU1cVixP9l/aP21f7MmWrs5BlDuQHOjVYs5zQNQju1G0BKejp17 3yOuaZg3eNGlqkQi5hzyrR2pNWVTcY5LCMz9t67FNlEfhU1Ntx7MEghFUjdFUCML2KZN cqei7DtadFE2fJXq8jk3A7Qcs2wqM3qIxIGs9GjUfOAQNyid660KdXHnR581z9/Q0wXi 1YFn3sS/TUqc0wBXgMCwzWQZkv3hgCmAC+XkXJtb1qD2yVrqFSOL9SVa35EamZcVaQNv z2ba6f21zKcBE3cpXB82msGQ3M5N3quKJQFZCqwye5ZCNGckwMD6efVu1q8kMe6ZcS9S B9sA== 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=OsCc64eJJaHmezFY3+fQiaWmA0ACCwHkFNgq4FVgBCc=; b=6rn/pwXoyidO3tS38H7jd6/ha4+0nQqmfabOj5mJgDQGLzmJYEWgCRTOtwtGvNRwoN Vgewt/53ixc5I03hKw61sEVgVB5VQJCe/v79t+H7nTjTIRM8pNxXvpzI+QgemtfBQN9n 312EG0SsORZxISNw5KWo9h07Fmo6E+2fHsWjylK4BsxG0mgRKB315t6iM3RRwoVSa99q OSrzCreKljLuPvzH5d+UEAdYvRatnCBEa/+QVcAji2mMTNRjD1EOB9qFC0HoxZuMkSL2 ztt6+/+DPC9fDnsCB8HcPuOlUWDOSQdEiDHsbH4000UNwkfpc6iViP1Qzef6ah/PJX2t M56A== X-Gm-Message-State: ACrzQf3Te2kBe9PeLa9F+eT0UVVak7hDyPGLgedi2ptNafUBNa1eQtw8 Te+5NMJSmNA9P++GzrBq+Ag= X-Google-Smtp-Source: AMsMyM6uc0eNJtN8QqJCwTQjeVHMxQDOI6LjcF/gvHiW8I234MpMvkfANbKpUwDFPnadktT5W++a2A== X-Received: by 2002:a2e:585:0:b0:26d:d39b:19d3 with SMTP id 127-20020a2e0585000000b0026dd39b19d3mr1549181ljf.345.1664784656981; Mon, 03 Oct 2022 01:10:56 -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 br32-20020a056512402000b0049465afdd38sm1363358lfb.108.2022.10.03.01.10.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 01:10:56 -0700 (PDT) Date: Mon, 3 Oct 2022 11:10:51 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Alexandru Ardelean , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 02/10] iio: adxl372: Fix unsafe buffer attributes Message-ID: <19158499623cdf7f9c5efae1f13c9f1a918ff75f.1664782676.git.mazziesaccount@gmail.com> 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/adxl372.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c index e3ecbaee61f7..bc53af809d5d 100644 --- a/drivers/iio/accel/adxl372.c +++ b/drivers/iio/accel/adxl372.c @@ -998,17 +998,30 @@ static ssize_t adxl372_get_fifo_watermark(struct device *dev, return sprintf(buf, "%d\n", st->watermark); } -static IIO_CONST_ATTR(hwfifo_watermark_min, "1"); -static IIO_CONST_ATTR(hwfifo_watermark_max, - __stringify(ADXL372_FIFO_SIZE)); +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(ADXL372_FIFO_SIZE)); +} + +static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0); +static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0); static IIO_DEVICE_ATTR(hwfifo_watermark, 0444, adxl372_get_fifo_watermark, NULL, 0); static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, adxl372_get_fifo_enabled, NULL, 0); static const struct attribute *adxl372_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,