Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3095339pxb; Tue, 12 Oct 2021 21:35:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbBNh4cMMtsuuknRZvTqH1shrVMzUgux8x9ezVPEj1dqTn7CNiSGG/zJfM/B5ISo5kh+Z2 X-Received: by 2002:a17:90b:388a:: with SMTP id mu10mr11085738pjb.0.1634099713509; Tue, 12 Oct 2021 21:35:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634099713; cv=none; d=google.com; s=arc-20160816; b=fDF9oiWw8+BKC1tfK4fygPs+nmybyR3pe9KE0MehkB9/9KYwPn7m0DcVeyIQICYIOp 6AqvdVPRVjLgHR7FNBxAZDn58PbjHICDcTkgMTcsUUAyQKD0YwfkK0HXnnjC3iJ/yFI4 /Qd09McLPSeDwZ939OpRhVSx5uQDw43Y5I3xINR+VaWrehFXhV8kDCv/5AHTx+6e+9tQ ii8kPonCb5oGb/ogrABEj6Q7h24SEMzWIpl0Ev6kaV73KNR46dZI6NINpGcEmgAW9+CL SEGkJL5EhaH1tdej2qZgOJiEL9ODwUMONIgdShrMtHOI5DoJ9bjm2Ilp6qdZzn0o2Pf0 B+5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=/YKVs7/YxIkL/C7+9CPVbsQrqNbGIVGI5svLVs4Ew74=; b=YXI5q3gn8uGavr3L7wJUlCKqrd5CMixhlVnpr7fcOlxdUMCniZb7sIqaduzAL+uJT5 9fuvGQ+WhLT/9D1idwR5LgEgvWHJo3RqQUd0RdKz3EbGzusshaNmRmsPNQdqh20WaDJZ QVWNE/qM4/ts5BFaUzf0IsqI/PsiYwAPVnYgkfYIl0Z/RTLPA7U8uQqycyitsRBDarWv xW/+b08PPS0gMl6rzttNPbZc7ljgh4h+LRjZhDykgTZxKIhVEc6REr5z5gB0CugkfBLC oxMSVDJuxdN9ygIfHG7URXWagFBDGz+Bs80ygfFqdQbN4x2NHx3HXdb0xU4jLX/CKWTW Lh8g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b195si18140178pga.316.2021.10.12.21.35.00; Tue, 12 Oct 2021 21:35:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230231AbhJMEfW (ORCPT + 99 others); Wed, 13 Oct 2021 00:35:22 -0400 Received: from spam.zju.edu.cn ([61.164.42.155]:47338 "EHLO zju.edu.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229881AbhJMEfS (ORCPT ); Wed, 13 Oct 2021 00:35:18 -0400 Received: from localhost.localdomain (unknown [222.205.7.222]) by mail-app4 (Coremail) with SMTP id cS_KCgCXDSkQYWZhWpicAw--.35224S4; Wed, 13 Oct 2021 12:31:12 +0800 (CST) From: Lin Ma To: linux-nfc@lists.01.org Cc: krzysztof.kozlowski@canonical.com, davem@davemloft.net, kuba@kernel.org, bongsu.jeon@samsung.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Lin Ma Subject: [PATCH] NFC: NULL out conn_info reference when conn is closed Date: Wed, 13 Oct 2021 12:30:52 +0800 Message-Id: <20211013043052.16379-1-linma@zju.edu.cn> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: cS_KCgCXDSkQYWZhWpicAw--.35224S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: qtrwiiyqvtljo62m3hxhgxhubq/ Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The nci_core_conn_close_rsp_packet() function will release the conn_info with given conn_id. However, this reference of this object may still held by other places like ndev->rf_conn_info in routine: .. -> nci_recv_frame() -> nci_rx_work() -> nci_rsp_packet() -> nci_rf_disc_rsp_packet() -> devm_kzalloc() ndev->rf_conn_info = conn_info; or ndev->hci_dev->conn_info in routine: .. -> nci_recv_frame() -> nci_rx_work() -> nci_rsp_packet() -> nci_core_conn_create_rsp_packet() -> devm_kzalloc() ndev->hci_dev->conn_info = conn_info; If these two places are not NULL out, potential UAF can be exploited by the attacker when emulating an UART NFC device. This patch compares the deallocating object with the two places and writes NULL to prevent that. Signed-off-by: Lin Ma --- net/nfc/nci/rsp.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/nfc/nci/rsp.c b/net/nfc/nci/rsp.c index a2e72c003805..99de76e5e855 100644 --- a/net/nfc/nci/rsp.c +++ b/net/nfc/nci/rsp.c @@ -334,6 +334,14 @@ static void nci_core_conn_close_rsp_packet(struct nci_dev *ndev, ndev->cur_conn_id); if (conn_info) { list_del(&conn_info->list); + /* Other places held conn_info like + * ndev->hci_dev->conn_info, ndev->rf_conn_info + * need to be NULL out. + */ + if (ndev->hci_dev->conn_info == conn_info) + ndev->hci_dev->conn_info = NULL; + if (ndev->rf_conn_info == conn_info) + ndev->rf_conn_info = NULL; devm_kfree(&ndev->nfc_dev->dev, conn_info); } } -- 2.33.0