Skip to contentSkip to conversation

bus: mhi: ep: Fix off by one in mhi_ep_process_cmd_ring()

PATCH Applied Dan Carpenter 3 participants 5 messages Jan 26, 2023 – Jan 27, 2023

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

PATCH 1/1 bus: mhi: ep: Fix off by one in mhi_ep_process_cmd_ring()

The > comparison should be changed to >= to prevent an out of bounds access into the mhi_cntrl->mhi_chan[] array. The mhi_cntrl->mhi_chan[] array is allocated in mhi_ep_chan_init() and has mhi_cntrl->max_chan elements.

Fixes 2527ad44ddb2 ("bus: mhi: ep: Check if the channel is supported by the controller")
---
 drivers/bus/mhi/ep/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/mhi/ep/main.c +1 -1
index bcaaba97ef63..be2d56e7f392 100644
@@ -125,7 +125,7 @@ static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_ele
 	ch_id = MHI_TRE_GET_CMD_CHID(el);
 
 	/* Check if the channel is supported by the controller */
-	if ((ch_id > mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
+	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
 		dev_err(dev, "Channel (%u) not supported!\n", ch_id);
 		return -ENODEV;
 	}
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Dan Carpenter

The > comparison should be changed to >= to prevent an out of bounds access into the mhi_cntrl->mhi_chan[] array. The mhi_cntrl->mhi_chan[] array is allocated in mhi_ep_chan_init() and has mhi_cntrl->max_chan elements.

You're right. I scanned through that file and there are other spots that don't check that the channel ID is in range, though I think this is the one where it's reading it from an external source. I.e., the other places are aleady known to be correct. (Maybe Mani can comment.)

Quoted 20 lines
Fixes 2527ad44ddb2 ("bus: mhi: ep: Check if the channel is supported by the controller")
---
  drivers/bus/mhi/ep/main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/mhi/ep/main.c +1 -1
index bcaaba97ef63..be2d56e7f392 100644
@@ -125,7 +125,7 @@ static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_ele
  	ch_id = MHI_TRE_GET_CMD_CHID(el);
 
  	/* Check if the channel is supported by the controller */
-	if ((ch_id > mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
+	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
  		dev_err(dev, "Channel (%u) not supported!\n", ch_id);
  		return -ENODEV;
  	}
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

The > comparison should be changed to >= to prevent an out of bounds access into the mhi_cntrl->mhi_chan[] array. The mhi_cntrl->mhi_chan[] array is allocated in mhi_ep_chan_init() and has mhi_cntrl->max_chan elements.

Fixes 2527ad44ddb2 ("bus: mhi: ep: Check if the channel is supported by the controller")

Thanks,
Mani

Quoted 20 lines
---
 drivers/bus/mhi/ep/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/mhi/ep/main.c +1 -1
index bcaaba97ef63..be2d56e7f392 100644
@@ -125,7 +125,7 @@ static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_ele
 	ch_id = MHI_TRE_GET_CMD_CHID(el);
 
 	/* Check if the channel is supported by the controller */
-	if ((ch_id > mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
+	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
 		dev_err(dev, "Channel (%u) not supported!\n", ch_id);
 		return -ENODEV;
 	}
Signature · 1 line
2.35.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#2 lore raw
In reply to Alex Elder
Quoted 6 lines · Alex Elder
Quoted 4 lines · Dan Carpenter

The > comparison should be changed to >= to prevent an out of bounds access into the mhi_cntrl->mhi_chan[] array. The mhi_cntrl->mhi_chan[] array is allocated in mhi_ep_chan_init() and has mhi_cntrl->max_chan elements.

Quoted 7 lines

You're right. I scanned through that file and there are other spots that don't check that the channel ID is in range, though I think this is the one where it's reading it from an external source. I.e., the other places are aleady known to be correct. (Maybe Mani can comment.)

Right. This is the only place we get the channel id from the host, so that's why the check is needed only here.

Quoted 1 line

Thanks,
Mani

Quoted 21 lines
Quoted 20 lines
Fixes 2527ad44ddb2 ("bus: mhi: ep: Check if the channel is supported by the controller")
---
  drivers/bus/mhi/ep/main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/mhi/ep/main.c +1 -1
index bcaaba97ef63..be2d56e7f392 100644
@@ -125,7 +125,7 @@ static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_ele
  	ch_id = MHI_TRE_GET_CMD_CHID(el);
  	/* Check if the channel is supported by the controller */
-	if ((ch_id > mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
+	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
  		dev_err(dev, "Channel (%u) not supported!\n", ch_id);
  		return -ENODEV;
  	}
Signature · 1 line
மணிவண்ணன் சதாசிவம்
maintainer Applied
#4 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

The > comparison should be changed to >= to prevent an out of bounds access into the mhi_cntrl->mhi_chan[] array. The mhi_cntrl->mhi_chan[] array is allocated in mhi_ep_chan_init() and has mhi_cntrl->max_chan elements.

Fixes 2527ad44ddb2 ("bus: mhi: ep: Check if the channel is supported by the controller")

Applied to mhi-next!

Thanks,
Mani

Quoted 20 lines
---
 drivers/bus/mhi/ep/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/bus/mhi/ep/main.c +1 -1
index bcaaba97ef63..be2d56e7f392 100644
@@ -125,7 +125,7 @@ static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_ele
 	ch_id = MHI_TRE_GET_CMD_CHID(el);
 
 	/* Check if the channel is supported by the controller */
-	if ((ch_id > mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
+	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
 		dev_err(dev, "Channel (%u) not supported!\n", ch_id);
 		return -ENODEV;
 	}
Signature · 1 line
2.35.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்