Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp2184971pxf; Sat, 13 Mar 2021 10:30:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzC/iiprw9pvMuVK7y4pcykpvvCmvq/l0GNLRtqEwuuZs6RVDvUfWUq3eXB2dqXlRgHal84 X-Received: by 2002:aa7:c6d2:: with SMTP id b18mr21146536eds.183.1615660257374; Sat, 13 Mar 2021 10:30:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615660257; cv=none; d=google.com; s=arc-20160816; b=McenMyRu4b+EvjHpysfM9PuqIima5Hw6l/JbiOJI6rRLpc8pmPvDt447oFrW6C3/KD QzYPiQqLe+tB+5pWhAGf5IntmsfJxHqLvPPSTDpGefwYvmNT59agN7c4s4phRbV59DLx yaKczCNwURzRoq1/n3G0+I0Jemt5fZ8yWMpaLgwFzEe4lVy28ynRIJUM/zbvT3wXrdJT 8JikatMsyxJv6+9jmbVTFQDvmmv0xCCxagSSoJ9F8ps2rm9IKXT+ZeM4IB+PcQ2yh/8K vlRrvVPKzTUXXQoRuo+EucHbWLTzqgAFcErbiShcvadW9VPp9qab9REWLerPuomaMTgM YuLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=SdqblFvOrkoSahLVaDA9gOaRqkRUTCvgsfz0w3xeBmo=; b=UK2EemIMrR0SYsxsWMXGZmWpqVckED5axmVRA6zNs8xo2wiE2DNRp6Ov7JFzOC6vG4 m3CW6QGcgH34LodogwM0+o42ep9ojrevTqBFLq6fQbU33kR2zSW8JSDnbenZbzAOxc5M vKm8/9+CPb0MmVkZNmaylegv9+wNVSEnq05oiGxYqhx26OptLeaAWIDROOz1QlaUWZmN Gu/jFEhceJ3IVpBXJD2oYUU/UXfa8jfbrXmjoc7lklA9qOtcXUCs69sHaPadONap0zk9 BQwofA7QJwHasYN/GvGyutzihHfNJm2wRs1k6J6qCT2MtL2uhvFFjTOlR3wsa+EdjRir JTaw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hb4si7159310ejb.642.2021.03.13.10.30.23; Sat, 13 Mar 2021 10:30:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234165AbhCMS2r (ORCPT + 99 others); Sat, 13 Mar 2021 13:28:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:37824 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234010AbhCMS2j (ORCPT ); Sat, 13 Mar 2021 13:28:39 -0500 Received: from archlinux (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4DCE864E58; Sat, 13 Mar 2021 18:28:38 +0000 (UTC) Date: Sat, 13 Mar 2021 18:28:32 +0000 From: Jonathan Cameron To: Alexandru Ardelean Cc: linux-iio , LKML , Lars-Peter Clausen Subject: Re: [PATCH v2] iio: buffer: fix use-after-free for attached_buffers array Message-ID: <20210313182832.588ed3d5@archlinux> In-Reply-To: References: <20210306164710.9944-1-ardeleanalex@gmail.com> <20210307185444.32924-1-ardeleanalex@gmail.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 8 Mar 2021 09:29:39 +0200 Alexandru Ardelean wrote: > On Sun, Mar 7, 2021 at 8:55 PM Alexandru Ardelean > wrote: > > > > Thanks to Lars for finding this. > > The free of the 'attached_buffers' array should be done as late as > > possible. This change moves it to iio_buffers_put(), which looks like > > the best place for it, since it takes place right before the IIO device > > data is free'd. > > The free of this array will be handled by calling iio_device_free(). > > The iio_buffers_put() function is renamed to iio_device_detach_buffers() > > since the role of this function changes a bit. > > > > It looks like this issue was ocurring on the error path of > > iio_buffers_alloc_sysfs_and_mask() and in > > iio_buffers_free_sysfs_and_mask() > > > > Added a comment in the doc-header of iio_device_attach_buffer() to > > mention how this will be free'd in case anyone is reading the code > > and becoming confused about it. > > > > Fixes: 36f3118c414d ("iio: buffer: introduce support for attaching more IIO buffers") > > Reported-by: Lars-Peter Clausen > > Signed-off-by: Alexandru Ardelean Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to poke at it. Thanks, Jonathan > > --- > > Forgot the changelog. > > Changelog v1 -> v2: > * rename iio_buffers_put() -> iio_device_detach_buffers() > > > drivers/iio/iio_core.h | 4 ++-- > > drivers/iio/industrialio-buffer.c | 9 +++++---- > > drivers/iio/industrialio-core.c | 2 +- > > 3 files changed, 8 insertions(+), 7 deletions(-) > > > > diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h > > index 062fe16c6c49..8f4a9b264962 100644 > > --- a/drivers/iio/iio_core.h > > +++ b/drivers/iio/iio_core.h > > @@ -77,7 +77,7 @@ void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev); > > > > void iio_disable_all_buffers(struct iio_dev *indio_dev); > > void iio_buffer_wakeup_poll(struct iio_dev *indio_dev); > > -void iio_buffers_put(struct iio_dev *indio_dev); > > +void iio_device_detach_buffers(struct iio_dev *indio_dev); > > > > #else > > > > @@ -93,7 +93,7 @@ static inline void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev) {} > > > > static inline void iio_disable_all_buffers(struct iio_dev *indio_dev) {} > > static inline void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) {} > > -static inline void iio_buffers_put(struct iio_dev *indio_dev) {} > > +static inline void iio_device_detach_buffers(struct iio_dev *indio_dev) {} > > > > #endif > > > > diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c > > index 1a415e97174e..1ff7f731b044 100644 > > --- a/drivers/iio/industrialio-buffer.c > > +++ b/drivers/iio/industrialio-buffer.c > > @@ -326,7 +326,7 @@ void iio_buffer_init(struct iio_buffer *buffer) > > } > > EXPORT_SYMBOL(iio_buffer_init); > > > > -void iio_buffers_put(struct iio_dev *indio_dev) > > +void iio_device_detach_buffers(struct iio_dev *indio_dev) > > { > > struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); > > struct iio_buffer *buffer; > > @@ -336,6 +336,8 @@ void iio_buffers_put(struct iio_dev *indio_dev) > > buffer = iio_dev_opaque->attached_buffers[i]; > > iio_buffer_put(buffer); > > } > > + > > + kfree(iio_dev_opaque->attached_buffers); > > } > > > > static ssize_t iio_show_scan_index(struct device *dev, > > @@ -1764,7 +1766,6 @@ int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) > > buffer = iio_dev_opaque->attached_buffers[unwind_idx]; > > __iio_buffer_free_sysfs_and_mask(buffer); > > } > > - kfree(iio_dev_opaque->attached_buffers); > > return ret; > > } > > > > @@ -1786,8 +1787,6 @@ void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev) > > buffer = iio_dev_opaque->attached_buffers[i]; > > __iio_buffer_free_sysfs_and_mask(buffer); > > } > > - > > - kfree(iio_dev_opaque->attached_buffers); > > } > > > > /** > > @@ -2062,6 +2061,8 @@ static int iio_buffer_mmap(struct file *filep, struct vm_area_struct *vma) > > * This function attaches a buffer to a IIO device. The buffer stays attached to > > * the device until the device is freed. For legacy reasons, the first attached > > * buffer will also be assigned to 'indio_dev->buffer'. > > + * The array allocated here, will be free'd via the iio_device_detach_buffers() > > + * call which is handled by the iio_device_free(). > > */ > > int iio_device_attach_buffer(struct iio_dev *indio_dev, > > struct iio_buffer *buffer) > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > > index d74fbac908df..3be5f75c2846 100644 > > --- a/drivers/iio/industrialio-core.c > > +++ b/drivers/iio/industrialio-core.c > > @@ -1587,7 +1587,7 @@ static void iio_dev_release(struct device *device) > > iio_device_unregister_eventset(indio_dev); > > iio_device_unregister_sysfs(indio_dev); > > > > - iio_buffers_put(indio_dev); > > + iio_device_detach_buffers(indio_dev); > > > > ida_simple_remove(&iio_ida, indio_dev->id); > > kfree(iio_dev_opaque); > > -- > > 2.25.1 > >