Skip to contentSkip to conversation

scsi: mpt3sas: Fix out-of-bounds cpu_msix_table read on CPU hotplug

PATCH Applied Louis Sautier 3 participants 3 messages Nov 18 – Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: mpt3sas: Fix out-of-bounds cpu_msix_table read on CPU hotplug

mpt3sas_base_attach() allocates cpu_msix_table based on the highest CPU online at probe time, but _base_get_msix_index() indexes it with raw_smp_processor_id() without a bounds check. Once a CPU with a higher id comes online, I/O submitted from it causes an out-of-bounds read of the table. The value read is sent to the firmware as the reply queue index. When it is not a valid queue, the IOC faults and I/O fails.

On a SAS3224 (FW 16.00.01.00) running v7.4-rc3 with KASAN, booted with maxcpus=1 and CPUs 1-95 onlined later, read-only fio pinned to each CPU in turn triggers, on CPU 1:

  BUG: KASAN: slab-out-of-bounds in _base_get_msix_index+0x1de/0x230 [mpt3sas]
  Read of size 1 at addr ffff88812fdf51c1 by task kworker/1:1H/1528

and, on CPU 16:

mpt3sas_cm0: fault_state(0x1500)!
mpt3sas_cm0: sending diag reset !!
I/O error, dev sda, sector 3873068048 op 0x0:(READ) flags 0x800000 phys_seg 1 prio class 2
mpt3sas_cm0: removing unresponding devices: start
sd 0:0:0:0: [sda] Synchronizing SCSI cache

Allocate the table based on nr_cpu_ids instead, and map every CPU in each reply queue's affinity mask, including offline ones. Managed IRQ affinity already spreads the vectors over all possible CPUs, so a CPU onlined later uses the queue whose affinity mask contains it.

The online-mask restriction and the bounds check added by commit 4a8842de8db4 ("scsi: mpt3sas: fix an out of bound write") are no longer needed, since the table now has an entry for every possible CPU.

When smp_affinity_enable=0 or a reply queue has no affinity mask, the fallback path still sets only the entries of the CPUs online at probe time. A CPU onlined later then reads 0 from the zeroed table, and queue 0 is always a valid queue.

With the change, the same fio test runs without errors, also while CPUs 1-95 are taken offline and back online in a loop. With maxcpus=12 (12 reply queues), I/O from each of the 96 CPUs completes on the vector whose affinity mask contains that CPU.

Fixes f92363d12359 ("[SCSI] mpt3sas: add new driver supporting 12GB SAS")
Assisted-by Claude:claude-opus-5-5

---
The table is used when there is a single blk-mq hardware queue, i.e. on SAS2 and gen 3 chips, and on gen 3.5 chips with host_tagset_enable=0, smp_affinity_enable=0 or a single non-high-iops reply queue. Gen 3.5 chips also use it for internal commands. It is not used in msix_load_balance mode.

SAS2 and gen 3.5 cards were not tested.

This patch does not fix the fallback path in _base_assign_reply_queues(), which walks cpu_online_mask without cpus_read_lock(). A CPU going offline during probe can make cpumask_next() return nr_cpu_ids, and the loop then writes the byte after the end of the table.

 drivers/scsi/mpt3sas/mpt3sas_base.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +8 -8
index 5c957044eded..b33a3e49608d 100644
@@ -3264,11 +3264,8 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
 				goto fall_back;
 			}
 
-			for_each_cpu_and(cpu, mask, cpu_online_mask) {
-				if (cpu >= ioc->cpu_msix_table_sz)
-					break;
+			for_each_cpu(cpu, mask)
 				ioc->cpu_msix_table[cpu] = reply_q->msix_index;
-			}
 		}
 		return;
 	}
@@ -8409,15 +8406,18 @@ int
 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
 {
 	int r, i, rc;
-	int cpu_id, last_cpu_id = 0;
 
 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
 
 	/* setup cpu_msix_table */
 	ioc->cpu_count = num_online_cpus();
-	for_each_online_cpu(cpu_id)
-		last_cpu_id = cpu_id;
-	ioc->cpu_msix_table_sz = last_cpu_id + 1;
+	/*
+	 * Allocate an entry for every possible CPU, because
+	 * _base_get_msix_index() indexes the table with
+	 * raw_smp_processor_id(), which can be a CPU brought online after
+	 * the adapter was attached.
+	 */
+	ioc->cpu_msix_table_sz = nr_cpu_ids;
 	ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
 	ioc->reply_queue_count = 1;
 	if (!ioc->cpu_msix_table) {

base-commit: 141b164bbe186423e295b139e40e8e2d11cf1341

Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] scsi: mpt3sas: ignored non-EAGAIN error…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] scsi: mpt3sas: ignored non-EAGAIN error in mpt3sas_base_attach() [1]
--

commit 68d13dcfec52a6dc8ae0711092e3869b07e277ea
Author: Louis Sautier <[email protected]>

scsi: mpt3sas: Fix out-of-bounds cpu_msix_table read on CPU hotplug

This commit allocates cpu_msix_table with nr_cpu_ids entries instead of sizing it from the highest online CPU at probe time, and maps all CPUs in each reply queue's affinity mask so CPUs onlined after probe do not cause out-of-bounds reads in _base_get_msix_index().

Quoted 4 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +0 -0
index 5c957044eded1..b33a3e49608dd 100644

[ ... ]

Quoted 13 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +0 -4
@@ -8409,15 +8406,18 @@ int
 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
 {
 	int r, i, rc;
-	int cpu_id, last_cpu_id = 0;
 
 	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
 
 	/* setup cpu_msix_table */
 	ioc->cpu_count = num_online_cpus();
-	for_each_online_cpu(cpu_id)
-		last_cpu_id = cpu_id;
-	ioc->cpu_msix_table_sz = last_cpu_id + 1;

drivers/scsi/mpt3sas/mpt3sas_base.c int · line 8423

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +7 -0
+	/*
+	 * Allocate an entry for every possible CPU, because
+	 * _base_get_msix_index() indexes the table with
+	 * raw_smp_processor_id(), which can be a CPU brought online after
+	 * the adapter was attached.
+	 */
+	ioc->cpu_msix_table_sz = nr_cpu_ids;
 	ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
 	ioc->reply_queue_count = 1;
 	if (!ioc->cpu_msix_table) {

[Severity: High]
This problem wasn't introduced by this patch, but later in mpt3sas_base_attach() [1], does a failure from the first _base_make_ioc_operational() call get ignored when it returns an error other than -EAGAIN?

drivers/scsi/mpt3sas/mpt3sas_base.c:mpt3sas_base_attach() {
    ...
	r = _base_make_ioc_operational(ioc);
	if (r == -EAGAIN) {
		r = _base_make_ioc_operational(ioc);
		if (r)
			goto out_free_resources;
	}
    ...
	return 0;
}

If _base_make_ioc_operational() fails in _base_static_config_pages() or _base_event_notification() with an error such as -EFAULT, mpt3sas_base_attach() falls through and returns 0 instead of jumping to out_free_resources, leaving _scsih_probe() with a partially initialized adapter.

[1] https://sashiko.dev/#/bug/linux-864ab92c-c52e-45ab-b16b-b4499e83a698

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Louis Sautier
Quoted 8 lines · Louis Sautier

mpt3sas_base_attach() allocates cpu_msix_table based on the highest CPU online at probe time, but _base_get_msix_index() indexes it with raw_smp_processor_id() without a bounds check. Once a CPU with a higher id comes online, I/O submitted from it causes an out-of-bounds read of the table. The value read is sent to the firmware as the reply queue index. When it is not a valid queue, the IOC faults and I/O fails.

[...]

Applied to 7.4/scsi-fixes, thanks!

[1/1] scsi: mpt3sas: Fix out-of-bounds cpu_msix_table read on CPU hotplug

      https://git.kernel.org/mkp/scsi/c/14bc5d239ddd
Signature · 1 line
Martin K. Petersen