[PATCH v2 14/17] mpi3mr: Fix out-of-bounds read in PCIe topology change events
Chandrakanth Patil <[email protected]>
| Newsgroups | org.kernel.vger.linux-scsi |
|---|---|
| Message-ID | <[email protected]> |
The number of entries in a PCIe topology change event is used to walk
the entry array without being compared against the amount of event
data that was received.
Bound the entry count to the received event data before use.
Fixes: 8e653455547a ("scsi: mpi3mr: Add support for PCIe device event handling")
Signed-off-by: Chandrakanth Patil <[email protected]>
---
v2:
- Cached num_entries in a local variable before bounds check and used it
for the loop condition to prevent re-fetching from DMA memory (TOCTOU).
drivers/scsi/mpi3mr/mpi3mr_os.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/drivers/scsi/mpi3mr/mpi3mr_os.c b/drivers/scsi/mpi3mr/mpi3mr_os.c
index 65a6192cf396..543e9b8e3fc3 100644
--- a/drivers/scsi/mpi3mr/mpi3mr_os.c
+++ b/drivers/scsi/mpi3mr/mpi3mr_os.c
@@ -2761,12 +2761,28 @@ static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
struct mpi3_event_data_pcie_topology_change_list *topo_evt =
(struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
int i;
- u16 handle;
- u8 reason_code;
+ u16 handle, avail_len;
+ u8 reason_code, max_entries, num_entries;
struct mpi3mr_tgt_dev *tgtdev = NULL;
struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
- for (i = 0; i < topo_evt->num_entries; i++) {
+ avail_len = event_reply->event_data_length * 4;
+ if (avail_len < offsetof(struct mpi3_event_data_pcie_topology_change_list, port_entry)) {
+ ioc_err(mrioc, "PCIe topology event: event data too small (%u bytes)\n",
+ avail_len);
+ return;
+ }
+ max_entries = (avail_len -
+ offsetof(struct mpi3_event_data_pcie_topology_change_list, port_entry)) /
+ sizeof(struct mpi3_event_pcie_topo_port_entry);
+ num_entries = topo_evt->num_entries;
+ if (num_entries > max_entries) {
+ ioc_err(mrioc, "PCIe topology event: num_entries(%d) exceeds max(%d)\n",
+ num_entries, max_entries);
+ return;
+ }
+
+ for (i = 0; i < num_entries; i++) {
handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
if (!handle)
continue;
--
2.52.0