From patchwork Sun Dec 17 13:11:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddh Raman Pant X-Patchwork-Id: 18194 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp696889dyi; Sun, 17 Dec 2023 05:12:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IGOt912MfZswZ3yQbfNdc7J6t7VRKA/lboLNdEYX6KsujfsmCMt2MyTUwSRnRL5cU/qcoNn X-Received: by 2002:a05:6214:d8c:b0:67f:9e3:f128 with SMTP id e12-20020a0562140d8c00b0067f09e3f128mr9392669qve.13.1702818774479; Sun, 17 Dec 2023 05:12:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702818774; cv=pass; d=google.com; s=arc-20160816; b=mHk7HqNYpA+BIpoKB0wET+qM+pLOk2u25S+oi5cH+2/0Xpep37k6SwHSRQBoXlHcY1 XHT1X2syckfnaxp2K3bWJeDUU3Lce56BfmPH24CPxz1L3yqTIyvyqzH1tE7z/+972hBG UGLg9WvVJbX3PvnhUjrbqNqoUQNanttLb44t1t9rWPPSjTVO4hY3YnPUAOUmOIUSK0M5 q0jmKE/ZF8A15p6bnJmGDZvgLkriSSSgqYcpVHE1Ij3l5RUvS8GlwDW6gzcBj++DVgJ6 pWcx981DGnAxN7mIIqpLfrUnCvVvQ8uqICkTZEpt2HFJj8NKSqJrwRS2uUgXd87ZNgrI j9rQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=/DTzar0LgmlyqX+qKR/d8wSc2aOpgmvZm8dRTkblnhw=; fh=RACn3Dz57j2O2Diwusdo+qDZ4fbRHVAux3E9Xar2zXI=; b=Pn/dCJ/GMqrIHU/aJUUZIFUafFEG2cx6vPg/xtH0MaIC0lszfGUV+7GVFK2XXkOuNl LwUUpCxyyJG87+QtT0cFQnutb5Px70WFQ33ntFQhdAHr5jQwCtfAisiMx0KnYSF9duV4 DVTy8//jByVtsrj7j2PKI7Ri2TN3XkvPVSnK989jQTbfb/ke87WIGdlEHLZLerH9Xt5o O8MiwOMeUpxmCmk2MalIB18kuohHgjt8vVw7a0Nxu568B8S2mfjL4f2gvg1YbblqmDcB 1TNr58TtCtjIhT1/Ou/F9pZu+ISH86BEbc40WmOlvlmbloWXe2+tfANJb8CTsBJm5GkR GHfw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=TLGGRC8C; arc=pass (i=1 spf=pass spfdomain=siddh.me dkim=pass dkdomain=siddh.me dmarc=pass fromdomain=siddh.me>); spf=pass (google.com: domain of linux-kernel+bounces-2593-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2593-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c22-20020a0ca9d6000000b0067f29063242si3108779qvb.315.2023.12.17.05.12.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 05:12:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2593-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=TLGGRC8C; arc=pass (i=1 spf=pass spfdomain=siddh.me dkim=pass dkdomain=siddh.me dmarc=pass fromdomain=siddh.me>); spf=pass (google.com: domain of linux-kernel+bounces-2593-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2593-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 47D3B1C20E5C for ; Sun, 17 Dec 2023 13:12:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DCA5844C94; Sun, 17 Dec 2023 13:11:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="TLGGRC8C" X-Original-To: linux-kernel@vger.kernel.org Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B55BB4437F; Sun, 17 Dec 2023 13:11:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=siddh.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=siddh.me ARC-Seal: i=1; a=rsa-sha256; t=1702818673; cv=none; d=zohomail.in; s=zohoarc; b=QmAf8kqV+yTljdzTRSl6UxLpyfJJJ+2YrVm94jAQQmP+GBS8nloqENinopxMhoBsv+sCrQBtU6ap4ooVshJfePJb8Q4Mb++7r5mBz8BURaAo3Y4PyNTuQmgiU1ukKV0Se73tbNJy+YZ3oW6jbOxqT4U9kr2vdsDMZUtk9a1OQBI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1702818673; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=/DTzar0LgmlyqX+qKR/d8wSc2aOpgmvZm8dRTkblnhw=; b=PXPDC8i0dnRSQ+hDqOp0SzptBfV+sixrpsb6MJ+muwEwPpP4bXhn2WxkvvEOVwIqfjwQ+4AOtLnphTRAmXaMqm+ChVgJ3GeOUA1PJR0ECJYyHQctCiJSeBZ3Q9sWk8S7odrTlLnhUUL7iu1k9aDhbGysvgyz/bCg0y/vn2bsaYc= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=siddh.me; spf=pass smtp.mailfrom=code@siddh.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1702818673; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=/DTzar0LgmlyqX+qKR/d8wSc2aOpgmvZm8dRTkblnhw=; b=TLGGRC8Cln5wpHoo1Zi8rO+47k1ocI0cUTyBJVwgUYbYai+6VXDlsUGukiHL1JU3 hCelgVsJH/K9Kve3nCbWUN0dr9y16s6aLxdAImMUgp+XY0MJ7cE+Quid3wWojdio86i Wv14lMYCKJy0YgkNlHvwKkgPzhG/YarPIEXokRX8= Received: from kampyooter.. (122.170.167.40 [122.170.167.40]) by mx.zoho.in with SMTPS id 1702818672684498.387606860955; Sun, 17 Dec 2023 18:41:12 +0530 (IST) From: Siddh Raman Pant To: Krzysztof Kozlowski , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Suman Ghosh Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v6 0/2] nfc: Fix UAF during datagram sending caused by missing refcounting Date: Sun, 17 Dec 2023 18:41:02 +0530 Message-ID: X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ZohoMailClient: External X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785534898847421927 X-GMAIL-MSGID: 1785534898847421927 Changes in v6: - Revert label introduction from v4, and thus also v5 entirely. Changes in v5: - Move reason = LLCP_DM_REJ under the fail_put_sock label. - Checkpatch now warns about == NULL check for new_sk, so fix that, and also at other similar places in the same function. Changes in v4: - Fix put ordering and comments. - Separate freeing in recv() into end labels. - Remove obvious comment and add reasoning. - Picked up r-bs by Suman. Changes in v3: - Fix missing freeing statements. Changes in v2: - Add net-next in patch subject. - Removed unnecessary extra lock and hold nfc_dev ref when holding llcp_sock. - Remove last formatting patch. - Picked up r-b from Krzysztof for LLCP_BOUND patch. --- For connectionless transmission, llcp_sock_sendmsg() codepath will eventually call nfc_alloc_send_skb() which takes in an nfc_dev as an argument for calculating the total size for skb allocation. virtual_ncidev_close() codepath eventually releases socket by calling nfc_llcp_socket_release() (which sets the sk->sk_state to LLCP_CLOSED) and afterwards the nfc_dev will be eventually freed. When an ndev gets freed, llcp_sock_sendmsg() will result in an use-after-free as it (1) doesn't have any checks in place for avoiding the datagram sending. (2) calls nfc_llcp_send_ui_frame(), which also has a do-while loop which can race with freeing. This loop contains the call to nfc_alloc_send_skb() where we dereference the nfc_dev pointer. nfc_dev is being freed because we do not hold a reference to it when we hold a reference to llcp_local. Thus, virtual_ncidev_close() eventually calls nfc_release() due to refcount going to 0. Since state has to be LLCP_BOUND for datagram sending, we can bail out early in llcp_sock_sendmsg(). Please review and let me know if any errors are there, and hopefully this gets accepted. Thanks, Siddh Siddh Raman Pant (2): nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local nfc: Do not send datagram if socket state isn't LLCP_BOUND net/nfc/llcp_core.c | 40 +++++++++++++++++++++++++++++++++++++--- net/nfc/llcp_sock.c | 5 +++++ 2 files changed, 42 insertions(+), 3 deletions(-)