When an NCI data exchange receives a partial NCI_PBF_CONT fragment into ndev->rx_data_reassembly and then terminates due to a data-status error, CORE_INTERFACE_ERROR_NTF, data-exchange timeout, or device close, the buffered fragment is left behind. On subsequent packets or after a close/reopen cycle (including listen/target mode, where NCI_DATA_EXCHANGE is never set), stale bytes are prepended to later responses or leaked until device free. Drop and clear ndev->rx_data_reassembly on all four paths: - In nci_add_rx_data_frag() when a nonzero data status is received. - Unconditionally in nci_core_conn_intf_error_ntf_packet() before the NCI_DATA_EXCHANGE check, matching nci_rf_deactivate_ntf_packet() so target mode is covered too. - In nci_rx_work() when handling a data-exchange timeout. - In nci_close_device() after flush_workqueue(ndev->rx_wq) on both close paths, guarded by req_lock and !NCI_UP. Found by static call-chain audit and code inspection. Fixes: 6a2968aaf50c ("NFC: basic NCI protocol implementation") Fixes: 004161cb52ac ("NFC: Handle error during NCI data exchange") Fixes: c4bf98b220cb ("NFC: Add NCI data exchange timer") Fixes: 98ff416f97c5 ("NFC: nci: Add status byte management in case of error.") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) --- net/nfc/nci/core.c | 15 +++++++++++++-- net/nfc/nci/data.c | 2 ++ net/nfc/nci/ntf.c | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c index efd4e5e..f832251 100644 --- a/net/nfc/nci/core.c +++ b/net/nfc/nci/core.c @@ -582,7 +582,7 @@ static int nci_close_device(struct nci_dev *ndev) ndev->cur_conn_id, -ENODEV); mutex_unlock(&ndev->req_lock); - return 0; + goto flush_rx; } /* Drop RX and TX queues */ @@ -622,9 +622,17 @@ static int nci_close_device(struct nci_dev *ndev) mutex_unlock(&ndev->req_lock); +flush_rx: /* rx_work may take req_lock via nci_deactivate_target */ flush_workqueue(ndev->rx_wq); + mutex_lock(&ndev->req_lock); + if (!test_bit(NCI_UP, &ndev->flags)) { + kfree_skb(ndev->rx_data_reassembly); + ndev->rx_data_reassembly = NULL; + } + mutex_unlock(&ndev->req_lock); + return 0; } @@ -1607,10 +1615,13 @@ static void nci_rx_work(struct work_struct *work) /* check if a data exchange timeout has occurred */ if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { /* complete the data exchange transaction, if exists */ - if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) + if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) { + kfree_skb(ndev->rx_data_reassembly); + ndev->rx_data_reassembly = NULL; nci_data_exchange_complete(ndev, NULL, ndev->cur_conn_id, -ETIMEDOUT); + } clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); } diff --git a/net/nfc/nci/data.c b/net/nfc/nci/data.c index 4253ede..3bbebb9 100644 --- a/net/nfc/nci/data.c +++ b/net/nfc/nci/data.c @@ -220,6 +220,8 @@ static void nci_add_rx_data_frag(struct nci_dev *ndev, if (status) { err = status; + kfree_skb(ndev->rx_data_reassembly); + ndev->rx_data_reassembly = NULL; goto exit; } diff --git a/net/nfc/nci/ntf.c b/net/nfc/nci/ntf.c index f5c9a8a..3ca1009 100644 --- a/net/nfc/nci/ntf.c +++ b/net/nfc/nci/ntf.c @@ -132,6 +132,10 @@ static int nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev, pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id); + /* drop partial rx data packet */ + kfree_skb(ndev->rx_data_reassembly); + ndev->rx_data_reassembly = NULL; + /* complete the data exchange transaction, if exists */ if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO); -- 2.51.0.windows.1