Skip to contentSkip to conversation

sd: limit sd_cmd_type array index 'req.cmd'

PATCH In review P J P 2 participants 3 messages Jan 20, 2016

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 sd: limit sd_cmd_type array index 'req.cmd'

From: Prasad J Pandit <[email protected]>

While processing standard SD commands, the 'req.cmd' value could lead to OOB read when used as an index into 'sd_cmd_type' array. Limit 'req.cmd' value to avoid such an access.

---
 hw/sd/sd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/sd/sd.c +3 -1
index 1a9935c..b800ced 100644
@@ -668,8 +668,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd,
     /* Not interpreting this as an app command */
     sd->card_status &= ~APP_CMD;
 
-    if (sd_cmd_type[req.cmd] == sd_ac || sd_cmd_type[req.cmd] == sd_adtc)
+    if (sd_cmd_type[req.cmd & 0x3F] == sd_ac
+        || sd_cmd_type[req.cmd & 0x3F] == sd_adtc) {
         rca = req.arg >> 16;
+    }
 
     DPRINTF("CMD%d 0x%08x state %d\n", req.cmd, req.arg, sd->state);
     switch (req.cmd) {
Signature · 1 line
2.5.0
#1 lore raw
Quoted 18 lines · P J P

From: Prasad J Pandit <[email protected]>

While processing standard SD commands, the 'req.cmd' value could lead to OOB read when used as an index into 'sd_cmd_type' array. Limit 'req.cmd' value to avoid such an access.

---
 hw/sd/sd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/sd/sd.c +0 -0
index 1a9935c..b800ced 100644
@@ -668,8 +668,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd,
     /* Not interpreting this as an app command */

hw/sd/sd.c static sd_rsp_type_t sd_normal_command(SDState *sd, · line 677

Quoted 10 lines
hw/sd/sd.c +3 -1
     sd->card_status &= ~APP_CMD;
 
-    if (sd_cmd_type[req.cmd] == sd_ac || sd_cmd_type[req.cmd] == sd_adtc)
+    if (sd_cmd_type[req.cmd & 0x3F] == sd_ac
+        || sd_cmd_type[req.cmd & 0x3F] == sd_adtc) {
         rca = req.arg >> 16;
+    }
 
     DPRINTF("CMD%d 0x%08x state %d\n", req.cmd, req.arg, sd->state);
     switch (req.cmd) {

This isn't the only bit of sd.c that assumes that the controller model hasn't passed it an oversize command number -- sd_do_command() calls cmd_valid_while_locked() which uses req.cmd as an index into sd_cmd_class[].

(I think it's right for sd.c to be defensive about this, since it's one place to get right rather than having to have all the controller models DTRT.)

thanks
-- PMM

author
#2 lore raw
+-- On Wed, 20 Jan 2016, Peter Maydell wrote --+
| This isn't the only bit of sd.c that assumes that the controller
| model hasn't passed it an oversize command number --
| sd_do_command() calls cmd_valid_while_locked() which uses req.cmd
| as an index into sd_cmd_class[].

Ah yes; I looked for 'req.cmd', but missed 'req->cmd'. I've sent a revised
patch v2.

Thank you.
--
- P J P
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F