Skip to contentSkip to conversation

usb: xhci: fix possible wild pointer

PATCH In review Lu Baolu 3 participants 6 messages Dec 2, 2016 – Dec 5, 2016

Subsystems (3)
lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

PATCH 1/1 usb: xhci: fix possible wild pointer

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

---
 drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -5
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
 	bool second_timeout = false;
 	xhci = (struct xhci_hcd *) data;
 
-	/* mark this command to be cancelled */
 	spin_lock_irqsave(&xhci->lock, flags);
-	if (xhci->current_cmd) {
-		if (xhci->current_cmd->status == COMP_CMD_ABORT)
-			second_timeout = true;
-		xhci->current_cmd->status = COMP_CMD_ABORT;
+	if (!xhci->current_cmd) {
+		spin_unlock_irqrestore(&xhci->lock, flags);
+		return;
 	}
 
+	if (xhci->current_cmd->status == COMP_CMD_ABORT)
+		second_timeout = true;
+	xhci->current_cmd->status = COMP_CMD_ABORT;
+
 	/* Make sure command ring is running before aborting it */
 	hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
 	if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
 		xhci->current_cmd = list_entry(cmd->cmd_list.next,
 					       struct xhci_command, cmd_list);
 		mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+	} else if (xhci->current_cmd == cmd) {
+		xhci->current_cmd = NULL;
+	} else {
+		xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
 	}
 
 event_handled:
Signature · 1 line
2.1.4
Tested-by on v1 1/1
#1 lore raw
Quoted 12 lines · Lu Baolu

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

Quoted 2 lines

Nice catch. I was also curious where set xhci->current_cmd to be NULL when current command is freed.

Quoted 48 lines
---
 drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -6
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
        bool second_timeout = false;
        xhci = (struct xhci_hcd *) data;
 
-       /* mark this command to be cancelled */
        spin_lock_irqsave(&xhci->lock, flags);
-       if (xhci->current_cmd) {
-               if (xhci->current_cmd->status == COMP_CMD_ABORT)
-                       second_timeout = true;
-               xhci->current_cmd->status = COMP_CMD_ABORT;
+       if (!xhci->current_cmd) {
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return;
        }
 
+       if (xhci->current_cmd->status == COMP_CMD_ABORT)
+               second_timeout = true;
+       xhci->current_cmd->status = COMP_CMD_ABORT;
+
        /* Make sure command ring is running before aborting it */
        hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
        if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                xhci->current_cmd = list_entry(cmd->cmd_list.next,
                                               struct xhci_command, cmd_list);
                mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+       } else if (xhci->current_cmd == cmd) {
+               xhci->current_cmd = NULL;
+       } else {
+               xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
        }
 
 event_handled:
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Baolin.wang
Best Regards
author
#2 lore raw

Hi,

Quoted 13 lines · Baolin Wang
Quoted 12 lines · Lu Baolu

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

Quoted 4 lines
Quoted 2 lines

Nice catch. I was also curious where set xhci->current_cmd to be NULL when current command is freed.

Below code does:

    xhci->current_cmd = list_entry(cmd->cmd_list.next,
                                               struct xhci_command, cmd_list);
                mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+       } else if (xhci->current_cmd == cmd) {
+               xhci->current_cmd = NULL;
+       } else {
+               xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
        }

Best regards,
Lu Baolu

Quoted 52 lines
Quoted 48 lines
---
 drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -6
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
        bool second_timeout = false;
        xhci = (struct xhci_hcd *) data;
 
-       /* mark this command to be cancelled */
        spin_lock_irqsave(&xhci->lock, flags);
-       if (xhci->current_cmd) {
-               if (xhci->current_cmd->status == COMP_CMD_ABORT)
-                       second_timeout = true;
-               xhci->current_cmd->status = COMP_CMD_ABORT;
+       if (!xhci->current_cmd) {
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return;
        }
 
+       if (xhci->current_cmd->status == COMP_CMD_ABORT)
+               second_timeout = true;
+       xhci->current_cmd->status = COMP_CMD_ABORT;
+
        /* Make sure command ring is running before aborting it */
        hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
        if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                xhci->current_cmd = list_entry(cmd->cmd_list.next,
                                               struct xhci_command, cmd_list);
                mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+       } else if (xhci->current_cmd == cmd) {
+               xhci->current_cmd = NULL;
+       } else {
+               xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
        }
 
 event_handled:
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 16 lines · Lu Baolu

Hi,

Quoted 13 lines · Baolin Wang
Quoted 12 lines · Lu Baolu

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Nice catch. I was also curious where set xhci->current_cmd to be NULL when current command is freed.

Below code does:

Yes, I just means you did that, but the original code did not. Sorry for confusing.

Quoted 67 lines
    xhci->current_cmd = list_entry(cmd->cmd_list.next,
                                               struct xhci_command, cmd_list);
                mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
drivers/usb/host/xhci-ring.c +4 -0
+       } else if (xhci->current_cmd == cmd) {
+               xhci->current_cmd = NULL;
+       } else {
+               xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
        }

Best regards,
Lu Baolu

Quoted 52 lines
Quoted 48 lines
---
 drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -6
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
        bool second_timeout = false;
        xhci = (struct xhci_hcd *) data;
 
-       /* mark this command to be cancelled */
        spin_lock_irqsave(&xhci->lock, flags);
-       if (xhci->current_cmd) {
-               if (xhci->current_cmd->status == COMP_CMD_ABORT)
-                       second_timeout = true;
-               xhci->current_cmd->status = COMP_CMD_ABORT;
+       if (!xhci->current_cmd) {
+               spin_unlock_irqrestore(&xhci->lock, flags);
+               return;
        }
 
+       if (xhci->current_cmd->status == COMP_CMD_ABORT)
+               second_timeout = true;
+       xhci->current_cmd->status = COMP_CMD_ABORT;
+
        /* Make sure command ring is running before aborting it */
        hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
        if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
                xhci->current_cmd = list_entry(cmd->cmd_list.next,
                                               struct xhci_command, cmd_list);
                mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+       } else if (xhci->current_cmd == cmd) {
+               xhci->current_cmd = NULL;
+       } else {
+               xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
        }
 
 event_handled:
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Baolin.wang
Best Regards
#4 lore raw
In reply to Lu Baolu
Quoted 46 lines · Lu Baolu

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

---
  drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
  1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +7 -5
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
  	bool second_timeout = false;
  	xhci = (struct xhci_hcd *) data;
 
-	/* mark this command to be cancelled */
  	spin_lock_irqsave(&xhci->lock, flags);
-	if (xhci->current_cmd) {
-		if (xhci->current_cmd->status == COMP_CMD_ABORT)
-			second_timeout = true;
-		xhci->current_cmd->status = COMP_CMD_ABORT;
+	if (!xhci->current_cmd) {
+		spin_unlock_irqrestore(&xhci->lock, flags);
+		return;
  	}
 
+	if (xhci->current_cmd->status == COMP_CMD_ABORT)
+		second_timeout = true;
+	xhci->current_cmd->status = COMP_CMD_ABORT;
+
  	/* Make sure command ring is running before aborting it */
  	hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  	if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&

drivers/usb/host/xhci-ring.c static void handle_cmd_completion(struct xhci_hcd *xhci, · line 1433

Quoted 10 lines
drivers/usb/host/xhci-ring.c +4 -0
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
  		xhci->current_cmd = list_entry(cmd->cmd_list.next,
  					       struct xhci_command, cmd_list);
  		mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+	} else if (xhci->current_cmd == cmd) {
+		xhci->current_cmd = NULL;
+	} else {
+		xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
  	}
 
  event_handled:

Thanks,

I might do some tweaking to (or remove) the warn message when applying if that is ok

-Mathias

author
#5 lore raw

Hi,

Quoted 55 lines · Mathias Nyman
Quoted 54 lines · Lu Baolu

handle_cmd_completion() frees a command structure which might be still referenced by xhci->current_cmd. This might cause problem when xhci->current_cmd is accessed after that.

A real-life case could be like this. The host takes a very long time to respond to a command, and the command timer is fired at the same time when the command completion event arrives. The command completion handler frees xhci->current_cmd before the timer function can grab xhci->lock. Afterward, timer function grabs the lock and go ahead with checking and setting members of xhci->current_cmd.

---
  drivers/usb/host/xhci-ring.c | 16 +++++++++++-----
  1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-ring.c +11 -5
index bdf6b13..13e05f6 100644
@@ -1267,14 +1267,16 @@ void xhci_handle_command_timeout(unsigned long data)
      bool second_timeout = false;
      xhci = (struct xhci_hcd *) data;
 
-    /* mark this command to be cancelled */
      spin_lock_irqsave(&xhci->lock, flags);
-    if (xhci->current_cmd) {
-        if (xhci->current_cmd->status == COMP_CMD_ABORT)
-            second_timeout = true;
-        xhci->current_cmd->status = COMP_CMD_ABORT;
+    if (!xhci->current_cmd) {
+        spin_unlock_irqrestore(&xhci->lock, flags);
+        return;
      }
 
+    if (xhci->current_cmd->status == COMP_CMD_ABORT)
+        second_timeout = true;
+    xhci->current_cmd->status = COMP_CMD_ABORT;
+
      /* Make sure command ring is running before aborting it */
      hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
      if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) &&
@@ -1422,6 +1424,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
          xhci->current_cmd = list_entry(cmd->cmd_list.next,
                             struct xhci_command, cmd_list);
          mod_timer(&xhci->cmd_timer, jiffies + XHCI_CMD_DEFAULT_TIMEOUT);
+    } else if (xhci->current_cmd == cmd) {
+        xhci->current_cmd = NULL;
+    } else {
+        xhci_warn(xhci, "WARN current_cmd doesn't match command\n");
      }
 
Quoted 7 lines

drivers/usb/host/xhci-ring.c static void handle_cmd_completion(struct xhci_hcd *xhci, · line 1433

Quoted 2 lines
drivers/usb/host/xhci-ring.c +0 -0
  event_handled:

Thanks,

I might do some tweaking to (or remove) the warn message when applying if that is ok

Sure.

Quoted 3 lines

-Mathias

Best regards,
Lu Baolu