Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2088830pxb; Sat, 27 Feb 2021 09:59:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSk+Ttgdv3Rag94DetPll4ScblwfLbuLsWtRJWwEJJuJaCD/xdXfKe1+L8t/LbAoxIjMPL X-Received: by 2002:a17:907:77cb:: with SMTP id kz11mr8563610ejc.12.1614448784969; Sat, 27 Feb 2021 09:59:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614448784; cv=none; d=google.com; s=arc-20160816; b=YN3dqDub+yl4vWNrlr9a0HixQsvqHKGtFx/vwSO/MjWU6QWu7i54X7qz33IixqDUNh 96EtvibN1dXeFRs4N+NsNKyE7GSvcUfiU81NmgShFTFMLkzn3GVgrRQNxRoQYuyF2QNH qR40saJMxQuQknebLZuLwpK5trUJ1Apm8eUlA+hIN7edb3iA2jEeatDXfr9mhZXSAA9P +5pMwk7UnLTF584tyaZy01KN4NI7sehq+mc/xUplULwKHZh5rVR3FYaggWt4k2Is33Sw n9V17uFNzcTwKTz03vJaaueh1JWZ5RqK2/E4Of/S0m55hPw6geUn1iutXlcBgXjdLMZv jnGQ== 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=9fGYQacyC4tGk74BydRo9O5n3N3z4C9kSQOymMBt9Pk=; b=bUqrzmvx5nLpaAbyrkSpr7p+XqvOtie8QovMT5n8lRqLkeppY4q903VUlzRAvhZcn7 z0JhUK3Gm9r3JcRPjBMJakGHxIkUx6cKNuGRwUjsY8WpMySWipdg88cmW3ySWxfVCidH 1ja5H6VYpRWcWjaXejIF8OthEtobDr4PcXsV1oqiynEJURN583v+SU1xGJdU5wBb5m4+ 5WovAl4Tn+fjduqmZAe5QPKke0GYfdn0Q/JbLC1jZj27aZKZtMIWJN6qFR+MeAA+LFjl R1dBHvsH79kuiKFizyulAUAlG7x5pFrxjMrX3yeBdv4zZ4s5JvxC+NTZYGCTu8WrDXqk Os7w== 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 fx8si5494677ejb.481.2021.02.27.09.59.22; Sat, 27 Feb 2021 09:59:44 -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 S230207AbhB0Ry2 (ORCPT + 99 others); Sat, 27 Feb 2021 12:54:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:56960 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230261AbhB0Rui (ORCPT ); Sat, 27 Feb 2021 12:50:38 -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 2FF8F64E46; Sat, 27 Feb 2021 17:49:55 +0000 (UTC) Date: Sat, 27 Feb 2021 17:49:52 +0000 From: Jonathan Cameron To: Alexandru Ardelean Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandru Ardelean Subject: Re: [PATCH v2] iio: core: use kstrdup_const/kfree_const for buffer attributes Message-ID: <20210227174952.06f45503@archlinux> In-Reply-To: <20210223072926.79590-1-alexandru.ardelean@analog.com> References: <20210219085826.46622-2-alexandru.ardelean@analog.com> <20210223072926.79590-1-alexandru.ardelean@analog.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 Tue, 23 Feb 2021 09:29:26 +0200 Alexandru Ardelean wrote: > When the buffer attributes were wrapped in iio_dev_attr types, I forgot to > duplicate the names, so that when iio_free_chan_devattr_list() gets called > on cleanup, these get free'd. > I stumbled over this while accidentally breaking a driver doing > iio_device_register(), and then the issue appeared. > > Some ways to fix this are: > 1. Just use kstrdup() during iio_buffer_wrap_attr() > 2. Just use kfree_const() during iio_free_chan_devattr_list > 3. Use both kstrdup_const() & kfree_const() (in the places mentioned above) > > This implements the third option, as it allows some users/drivers to > specify some attributes allocated on the heap. > > Fixes: a1a11142f66c ("iio: buffer: wrap all buffer attributes into iio_dev_attr") > Signed-off-by: Alexandru Ardelean > --- > drivers/iio/industrialio-buffer.c | 1 + > drivers/iio/industrialio-core.c | 2 +- > 2 files changed, 2 insertions(+), 1 deletion(-) > > diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c > index 5d641f8adfbd..ac882e60c419 100644 > --- a/drivers/iio/industrialio-buffer.c > +++ b/drivers/iio/industrialio-buffer.c > @@ -1306,6 +1306,7 @@ static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer, > return NULL; > > iio_attr->buffer = buffer; > + iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL); > memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr)); Doesn't this wipe out the duplicated string? I swapped the two lines above which I think should avoid that. Merged into original patch Jonathan > > list_add(&iio_attr->l, &buffer->buffer_attr_list); > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > index 0d8c6e88d993..cb2735d2ae4b 100644 > --- a/drivers/iio/industrialio-core.c > +++ b/drivers/iio/industrialio-core.c > @@ -1358,7 +1358,7 @@ void iio_free_chan_devattr_list(struct list_head *attr_list) > struct iio_dev_attr *p, *n; > > list_for_each_entry_safe(p, n, attr_list, l) { > - kfree(p->dev_attr.attr.name); > + kfree_const(p->dev_attr.attr.name); > list_del(&p->l); > kfree(p); > }