All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] opp: fix a missing check on list iterator
@ 2022-03-27  5:39 Xiaomeng Tong
  2022-03-28  3:17 ` Viresh Kumar
  0 siblings, 1 reply; 7+ messages in thread
From: Xiaomeng Tong @ 2022-03-27  5:39 UTC (permalink / raw)
  To: vireshk
  Cc: nm, sboyd, rafael.j.wysocki, linux-pm, linux-kernel,
	Xiaomeng Tong, stable

The bug is here:
    dev = new_dev->dev;

The list iterator 'new_dev' will point to a bogus position containing
HEAD if the list is empty or no element is found. This case must
be checked before any use of the iterator, otherwise it will lead
to a invalid memory access.

To fix this bug, add an check. Use a new variable 'iter' as the
list iterator, while use the old variable 'new_dev' as a dedicated
pointer to point to the found element.

Cc: stable@vger.kernel.org
Fixes: deaa51465105a ("PM / OPP: Add debugfs support")
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
---
 drivers/opp/debugfs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index 596c185b5dda..a4476985e4ce 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -187,14 +187,19 @@ void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
 static void opp_migrate_dentry(struct opp_device *opp_dev,
 			       struct opp_table *opp_table)
 {
-	struct opp_device *new_dev;
+	struct opp_device *new_dev = NULL, *iter;
 	const struct device *dev;
 	struct dentry *dentry;
 
 	/* Look for next opp-dev */
-	list_for_each_entry(new_dev, &opp_table->dev_list, node)
-		if (new_dev != opp_dev)
+	list_for_each_entry(iter, &opp_table->dev_list, node)
+		if (iter != opp_dev) {
+			new_dev = iter;
 			break;
+		}
+
+	if (!new_dev)
+		return;
 
 	/* new_dev is guaranteed to be valid here */
 	dev = new_dev->dev;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-31  2:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-27  5:39 [PATCH] opp: fix a missing check on list iterator Xiaomeng Tong
2022-03-28  3:17 ` Viresh Kumar
2022-03-28  7:43   ` Xiaomeng Tong
2022-03-28  8:50     ` Viresh Kumar
2022-03-28  9:13       ` Xiaomeng Tong
2022-03-28  9:39         ` Viresh Kumar
2022-03-31  2:10           ` Xiaomeng Tong

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.