Skip to content

Commit 34e5470

Browse files
f0rm2l1ngregkh
authored andcommitted
NFC: add NCI_UNREG flag to eliminate the race
[ Upstream commit 48b71a9 ] There are two sites that calls queue_work() after the destroy_workqueue() and lead to possible UAF. The first site is nci_send_cmd(), which can happen after the nci_close_device as below nfcmrvl_nci_unregister_dev | nfc_genl_dev_up nci_close_device | flush_workqueue | del_timer_sync | nci_unregister_device | nfc_get_device destroy_workqueue | nfc_dev_up nfc_unregister_device | nci_dev_up device_del | nci_open_device | __nci_request | nci_send_cmd | queue_work !!! Another site is nci_cmd_timer, awaked by the nci_cmd_work from the nci_send_cmd. ... | ... nci_unregister_device | queue_work destroy_workqueue | nfc_unregister_device | ... device_del | nci_cmd_work | mod_timer | ... | nci_cmd_timer | queue_work !!! For the above two UAF, the root cause is that the nfc_dev_up can race between the nci_unregister_device routine. Therefore, this patch introduce NCI_UNREG flag to easily eliminate the possible race. In addition, the mutex_lock in nci_close_device can act as a barrier. Signed-off-by: Lin Ma <linma@zju.edu.cn> Fixes: 6a2968a ("NFC: basic NCI protocol implementation") Reviewed-by: Jakub Kicinski <kuba@kernel.org> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com> Link: https://lore.kernel.org/r/20211116152732.19238-1-linma@zju.edu.cn Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b2a60b4 commit 34e5470

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎include/net/nfc/nci_core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum nci_flag {
3030
NCI_UP,
3131
NCI_DATA_EXCHANGE,
3232
NCI_DATA_EXCHANGE_TO,
33+
NCI_UNREG,
3334
};
3435

3536
/* NCI device states */

‎net/nfc/nci/core.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ static int nci_open_device(struct nci_dev *ndev)
473473

474474
mutex_lock(&ndev->req_lock);
475475

476+
if (test_bit(NCI_UNREG, &ndev->flags)) {
477+
rc = -ENODEV;
478+
goto done;
479+
}
480+
476481
if (test_bit(NCI_UP, &ndev->flags)) {
477482
rc = -EALREADY;
478483
goto done;
@@ -536,6 +541,10 @@ static int nci_open_device(struct nci_dev *ndev)
536541
static int nci_close_device(struct nci_dev *ndev)
537542
{
538543
nci_req_cancel(ndev, ENODEV);
544+
545+
/* This mutex needs to be held as a barrier for
546+
* caller nci_unregister_device
547+
*/
539548
mutex_lock(&ndev->req_lock);
540549

541550
if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
@@ -573,8 +582,8 @@ static int nci_close_device(struct nci_dev *ndev)
573582

574583
del_timer_sync(&ndev->cmd_timer);
575584

576-
/* Clear flags */
577-
ndev->flags = 0;
585+
/* Clear flags except NCI_UNREG */
586+
ndev->flags &= BIT(NCI_UNREG);
578587

579588
mutex_unlock(&ndev->req_lock);
580589

@@ -1259,6 +1268,12 @@ void nci_unregister_device(struct nci_dev *ndev)
12591268
{
12601269
struct nci_conn_info *conn_info, *n;
12611270

1271+
/* This set_bit is not protected with specialized barrier,
1272+
* However, it is fine because the mutex_lock(&ndev->req_lock);
1273+
* in nci_close_device() will help to emit one.
1274+
*/
1275+
set_bit(NCI_UNREG, &ndev->flags);
1276+
12621277
nci_close_device(ndev);
12631278

12641279
destroy_workqueue(ndev->cmd_wq);

0 commit comments

Comments
 (0)