mwifiex: don't drop lock between list-retrieval / list-deletion
authorBrian Norris <briannorris@chromium.org>
Fri, 12 May 2017 16:42:02 +0000 (09:42 -0700)
committerKalle Valo <kvalo@codeaurora.org>
Fri, 19 May 2017 06:01:56 +0000 (09:01 +0300)
mwifiex_exec_next_cmd() seems to have a classic TOCTOU race, where we
drop the list lock in between retrieving the next command and deleting
it from the list. This potentially leaves room for someone else to also
retrieve / steal this node from the list (e.g.,
mwifiex_cancel_all_pending_cmd()).

Let's keep holding the lock while we do our 'ps_state' sanity checks.
There should be no harm in continuing to hold this lock for a bit more.

Noticed only by code inspection.

Signed-off-by: Brian Norris <briannorris@chromium.org>
Signed-off-by: Kalle Valo <kvalo@codeaurora.org>
drivers/net/wireless/marvell/mwifiex/cmdevt.c

index 5fd6c53d7b06e7d4170f5bde44ac3b3330d9f549..95221306a4e5c8ec6a3788ed0192cefd5e579d6e 100644 (file)
@@ -761,8 +761,6 @@ int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
        }
        cmd_node = list_first_entry(&adapter->cmd_pending_q,
                                    struct cmd_ctrl_node, list);
-       spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
-                              cmd_pending_q_flags);
 
        host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
        priv = cmd_node->priv;
@@ -771,11 +769,12 @@ int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
                mwifiex_dbg(adapter, ERROR,
                            "%s: cannot send cmd in sleep state,\t"
                            "this should not happen\n", __func__);
+               spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
+                                      cmd_pending_q_flags);
                spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
                return ret;
        }
 
-       spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
        list_del(&cmd_node->list);
        spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
                               cmd_pending_q_flags);