The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Sebastian Reichel <sre@kernel.org>,
	Anton Vorontsov <cbou@mail.ru>, Ian Molton <spyro@f2s.com>,
	Rhyland Klein <rklein@nvidia.com>,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 06/10] power: supply: core: Pack supplied_from densely and bound writes
Date: Thu,  3 Dec 2026 16:12:27 +0000	[thread overview]
Message-ID: <20261203161316.319090-7-lee@kernel.org> (raw)
In-Reply-To: <20261203161316.319090-1-lee@kernel.org>

__power_supply_populate_supplied_from() stores matched supplier names at
the sparse firmware reference index (i - 1) while independently
incrementing psy->num_supplies.  If an earlier referenced supply
unregisters concurrently, psy->supplied_from[0] remains NULL while
psy->num_supplies is non-zero, causing __power_supply_is_supplied_by()
to dereference NULL in strcmp().  Furthermore, multiple supplies sharing
the same fwnode can increment num_supplies past the allocated array.

Pass the allocated slot count to power_supply_populate_supplied_from(),
pack entries densely at psy->supplied_from[psy->num_supplies++], and
stop before exceeding the allocation.

Fixes: f6e0b081fb30 ("power_supply: Populate supplied_from hierarchy from the device tree")
Closes: https://sashiko.dev/#/bug/linux-a0295e92-ec18-42a6-9e66-60792792613b
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/power/supply/power_supply_core.c | 27 ++++++++++++++++++------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index de15314ef954..7488c7b94f40 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -219,10 +219,16 @@ static int power_supply_check_supplies_by_name(struct power_supply *psy)
 	return 0;
 }
 
+struct psy_populate_supplied_from_data {
+	struct power_supply *psy;
+	int max_supplies;
+};
+
 static int __power_supply_populate_supplied_from(struct power_supply *epsy,
 						 void *data)
 {
-	struct power_supply *psy = data;
+	struct psy_populate_supplied_from_data *ctx = data;
+	struct power_supply *psy = ctx->psy;
 	struct fwnode_handle *np;
 	int i = 0;
 
@@ -232,11 +238,13 @@ static int __power_supply_populate_supplied_from(struct power_supply *epsy,
 			break;
 
 		if (np == epsy->dev.fwnode) {
+			fwnode_handle_put(np);
+			if (psy->num_supplies >= ctx->max_supplies)
+				break;
 			dev_dbg(&psy->dev, "%s: Found supply : %s\n",
 				psy->desc->name, epsy->desc->name);
-			psy->supplied_from[i-1] = (char *)epsy->desc->name;
-			psy->num_supplies++;
-			fwnode_handle_put(np);
+			psy->supplied_from[psy->num_supplies++] =
+				(char *)epsy->desc->name;
 			break;
 		}
 		fwnode_handle_put(np);
@@ -245,11 +253,16 @@ static int __power_supply_populate_supplied_from(struct power_supply *epsy,
 	return 0;
 }
 
-static int power_supply_populate_supplied_from(struct power_supply *psy)
+static int power_supply_populate_supplied_from(struct power_supply *psy,
+					       int max_supplies)
 {
+	struct psy_populate_supplied_from_data data = {
+		.psy = psy,
+		.max_supplies = max_supplies,
+	};
 	int error;
 
-	error = power_supply_for_each_psy(psy, __power_supply_populate_supplied_from);
+	error = power_supply_for_each_psy(&data, __power_supply_populate_supplied_from);
 
 	dev_dbg(&psy->dev, "%s %d\n", __func__, error);
 
@@ -326,7 +339,7 @@ static int power_supply_check_supplies(struct power_supply *psy)
 	if (!psy->supplied_from)
 		return -ENOMEM;
 
-	return power_supply_populate_supplied_from(psy);
+	return power_supply_populate_supplied_from(psy, cnt);
 }
 
 struct psy_am_i_supplied_data {
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03 16:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 16:12 [PATCH 00/10] power: supply: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03 16:12 ` [PATCH 01/10] power: supply: bq27xxx: Check for short i2c_transfer() in bq27xxx_battery_i2c_read() Lee Jones
2026-12-03 16:12 ` [PATCH 02/10] power: supply: generic-adc-battery: Initialise bat_work before registering supply Lee Jones
2026-12-03 16:12 ` [PATCH 03/10] power: supply: bq24190: Manage teardown order with devm to prevent UAF Lee Jones
2026-12-03 16:12 ` [PATCH 04/10] power: supply: gpio-charger: Avoid printing uninitialised chargetype Lee Jones
2026-12-03 16:12 ` [PATCH 05/10] power: supply: ab8500_btemp: Remove unused global device list Lee Jones
2026-12-03 16:12 ` Lee Jones [this message]
2026-12-03 16:12 ` [PATCH 07/10] power: supply: core: Reject empty ocv-capacity-table properties Lee Jones
2026-12-03 16:12 ` [PATCH 08/10] power: supply: cpcap-battery: Release battery info in cpcap_battery_update_battery_data() Lee Jones
2026-12-03 16:12 ` [PATCH 09/10] power: supply: qcom_battmgr: Hold battmgr->lock in qcom_battmgr_enable_worker() Lee Jones
2026-12-03 16:12 ` [PATCH 10/10] power: supply: sbs-battery: Always cancel delayed work on suspend Lee Jones

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=20261203161316.319090-7-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=cbou@mail.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rklein@nvidia.com \
    --cc=spyro@f2s.com \
    --cc=sre@kernel.org \
    --cc=stable@vger.kernel.org \
    /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