linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Zijun Hu <zijun_hu@icloud.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Tejun Heo <tj@kernel.org>, Josef Bacik <josef@toxicpanda.com>,
	Jens Axboe <axboe@kernel.dk>, Boris Burkov <boris@bur.io>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Dave Jiang <dave.jiang@intel.com>,
	Alison Schofield <alison.schofield@intel.com>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Ira Weiny <ira.weiny@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	<linux-kernel@vger.kernel.org>, <cgroups@vger.kernel.org>,
	<linux-block@vger.kernel.org>, <linux-cxl@vger.kernel.org>,
	Zijun Hu <quic_zijuhu@quicinc.com>
Subject: Re: [PATCH v3 1/9] driver core: class: Fix wild pointer dereferences in API class_dev_iter_next()
Date: Mon, 16 Dec 2024 15:36:11 +0000	[thread overview]
Message-ID: <20241216153611.00007f26@huawei.com> (raw)
In-Reply-To: <20241212-class_fix-v3-1-04e20c4f0971@quicinc.com>

On Thu, 12 Dec 2024 21:38:37 +0800
Zijun Hu <zijun_hu@icloud.com> wrote:

> From: Zijun Hu <quic_zijuhu@quicinc.com>
> 
> There are a potential wild pointer dereferences issue regarding APIs
> class_dev_iter_(init|next|exit)(), as explained by below typical usage:
> 
> // All members of @iter are wild pointers.
> struct class_dev_iter iter;
> 
> // class_dev_iter_init(@iter, @class, ...) checks parameter @class for
> // potential class_to_subsys() error, and it returns void type and does
> // not initialize its output parameter @iter, so caller can not detect
> // the error and continues to invoke class_dev_iter_next(@iter) even if
> // @iter still contains wild pointers.
> class_dev_iter_init(&iter, ...);
> 
> // Dereference these wild pointers in @iter here once suffer the error.
> while (dev = class_dev_iter_next(&iter)) { ... };
> 
> // Also dereference these wild pointers here.
> class_dev_iter_exit(&iter);
> 
> Actually, all callers of these APIs have such usage pattern in kernel tree.
> Fix by:
> - Initialize output parameter @iter by memset() in class_dev_iter_init()
>   and give callers prompt by pr_crit() for the error.
> - Check if @iter is valid in class_dev_iter_next().
> 
> Fixes: 7b884b7f24b4 ("driver core: class.c: convert to only use class_to_subsys")
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>

This looks fine in general, but over to the core device model folk for which
element they think should be used as the sentinel and whether zeroing the
whole thing makes sense or just the one being used as a flag, or even setting
it to an error pointer.

> 
> ---
> Alternative fix solutions ever thought about:
> 
> 1) Use BUG_ON(!sp) instead of error return in class_dev_iter_init().
> 2) Change class_dev_iter_init()'s type to int, lots of jobs to do.
> 
> This issue is APIs themself issues, and regardless of how various API
> users use them, and silent wild pointer dereferences are not what API
> users expect for the error absolutely.
> ---
>  drivers/base/class.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/base/class.c b/drivers/base/class.c
> index 582b5a02a5c410113326601fe00eb6d7231f988f..d57f277978dc9033fba3484b4620bcf884a4029f 100644
> --- a/drivers/base/class.c
> +++ b/drivers/base/class.c
> @@ -323,8 +323,12 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
>  	struct subsys_private *sp = class_to_subsys(class);
>  	struct klist_node *start_knode = NULL;
>  
> -	if (!sp)
> +	memset(iter, 0, sizeof(*iter));
> +	if (!sp) {
> +		pr_crit("%s: class %p was not registered yet\n",
> +			__func__, class);
>  		return;
> +	}
>  
>  	if (start)
>  		start_knode = &start->p->knode_class;
> @@ -351,6 +355,9 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)
>  	struct klist_node *knode;
>  	struct device *dev;
>  
> +	if (!iter->sp)
> +		return NULL;
> +
>  	while (1) {
>  		knode = klist_next(&iter->ki);
>  		if (!knode)
> 


  reply	other threads:[~2024-12-16 15:36 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-12 13:38 [PATCH v3 0/9] driver core: class: Fix bug and code improvements for class APIs Zijun Hu
2024-12-12 13:38 ` [PATCH v3 1/9] driver core: class: Fix wild pointer dereferences in API class_dev_iter_next() Zijun Hu
2024-12-16 15:36   ` Jonathan Cameron [this message]
2024-12-17 14:09     ` Zijun Hu
2024-12-12 13:38 ` [PATCH v3 2/9] blk-cgroup: Fix class @block_class's subsystem refcount leakage Zijun Hu
2024-12-17 10:53   ` Michal Koutný
2024-12-12 13:38 ` [PATCH v3 3/9] driver core: bus: Move true expression out of if condition in API bus_find_device() Zijun Hu
2024-12-16 15:14   ` Jonathan Cameron
2024-12-12 13:38 ` [PATCH v3 4/9] driver core: Move true expression out of if condition in API driver_find_device() Zijun Hu
2024-12-16 15:18   ` Jonathan Cameron
2024-12-16 18:01   ` Fan Ni
2024-12-17 14:13     ` Zijun Hu
2024-12-12 13:38 ` [PATCH v3 5/9] driver core: Move true expression out of if condition in API device_find_child() Zijun Hu
2024-12-16 15:19   ` Jonathan Cameron
2024-12-16 18:02   ` Fan Ni
2024-12-12 13:38 ` [PATCH v3 6/9] driver core: Rename declaration parameter name for API device_find_child() cluster Zijun Hu
2024-12-16 15:21   ` Jonathan Cameron
2024-12-16 18:02   ` Fan Ni
2024-12-12 13:38 ` [PATCH v3 7/9] driver core: Correct parameter check for API device_for_each_child_reverse_from() Zijun Hu
2024-12-16 15:23   ` Jonathan Cameron
2024-12-17 14:18     ` Zijun Hu
2024-12-12 13:38 ` [PATCH v3 8/9] driver core: Correct API device_for_each_child_reverse_from() prototype Zijun Hu
2024-12-16 15:30   ` Jonathan Cameron
2024-12-12 13:38 ` [PATCH v3 9/9] driver core: Introduce device_iter_t for device iterating APIs Zijun Hu
2024-12-16 15:33   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241216153611.00007f26@huawei.com \
    --to=jonathan.cameron@huawei.com \
    --cc=alison.schofield@intel.com \
    --cc=axboe@kernel.dk \
    --cc=boris@bur.io \
    --cc=cgroups@vger.kernel.org \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=ira.weiny@intel.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quic_zijuhu@quicinc.com \
    --cc=rafael@kernel.org \
    --cc=tj@kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=zijun_hu@icloud.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).