Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4846238rdb; Tue, 12 Dec 2023 10:50:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHR3Bl8ndILfRLihfPMr2OPCjC1mY32SphWR4ilUITz98a6hdc76TfhGuu9N6yjvFkuQAxE X-Received: by 2002:a17:90b:19d0:b0:286:c0e2:83f1 with SMTP id nm16-20020a17090b19d000b00286c0e283f1mr3487621pjb.32.1702407049883; Tue, 12 Dec 2023 10:50:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702407049; cv=pass; d=google.com; s=arc-20160816; b=N5ovpDYFvq7g8OekSaxee/WSYWUSvkL5uVUQnRGQNHONhkbURyh5m8hkfZWSrj2paT w0mzMjIqbWIIJma2n1cJSX8hjgmgx+5sp8pyGOeLy+NIuxVmUHQm/ljkkKuSvE1sKOR3 TUDjHPKO/2lV1QEKiN2/NwolzxzDwzgqrcA2uoCf8mzk3VPHLZlKxWKcli3N3CfggTkS RvHsc8PuEN8M8ecwCeGiGzJnnF/LJB3igHkfLTjqY9SO1wCMWRaWcLHxQIals1MGDClu rpJ7eNhB+xwIPCYzSwgofvB9gsrVuOews1daxlh9nozOjXW4EVVG4ZcDMxmKZ5taPY4y p4vQ== ARC-Message-Signature: i=2; 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:dkim-signature; bh=Y9BKQtIfmMm0sVY++cu3R+sS5q4tdRr2o2bcRqoIeps=; fh=RACn3Dz57j2O2Diwusdo+qDZ4fbRHVAux3E9Xar2zXI=; b=lmfojIE1jqeOKu00m+p+yTPlT6xbjzowZ7euFVLuSYcHkgTm5+ZeOgoHKckwnxS0pd G7HNv5SD7/CM+6iu9quRrmXs1YZbwrFnJr2b8FmAY/F+qY4wKzxA6V4/D3Qkg4lSLrY/ elEyeuOz6aS88xL2D/TA21sRiJtyqIzPBaDyDfuPeqa7ncDTI0NjKdCSDPhlbGaDkFnq ssUtXhURZkVFCmOh7n0v89q3a4WWVsK+jfOjTJuwI8kUvbp17XwdYjFi0UYkF8hHR4bz 7P6bHkPg7Fxrs7GrZRBatqvhvsmhc70e2SuiFu7iKZocDkNvuQw6HGulsnzVAX47pMy6 kcWg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=qJDjqT1P; 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-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id d24-20020a17090ab31800b0028a9b910230si3332425pjr.116.2023.12.12.10.50.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 10:50:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=qJDjqT1P; 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-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siddh.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E511C807C6C0; Tue, 12 Dec 2023 10:50:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376701AbjLLStz (ORCPT + 99 others); Tue, 12 Dec 2023 13:49:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376638AbjLLStv (ORCPT ); Tue, 12 Dec 2023 13:49:51 -0500 Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EB41D0; Tue, 12 Dec 2023 10:49:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702406967; cv=none; d=zohomail.in; s=zohoarc; b=OEDbM/7Nh+ThCBsFEQCeUubWdsTOEL0mGX/WzouBF5FpVS4sOTYuO2tOUkbBrOTds2pYNTJCQ5CIYGSn3R1uZnZnOq1swBXt782sMcecgey0PZNjaEycKghALTTK02XTlzFkb2hOym5ITH/n0alRrAyKnVEd3ru7WCElqbY2qW8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1702406967; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=Y9BKQtIfmMm0sVY++cu3R+sS5q4tdRr2o2bcRqoIeps=; b=CJqBDTw3OYrKBVuWr/2Kro6k7cevoqsJY2QRpxKn4AdYKipiHrgAlh9FnsGSViqMgzkTiJwqrrwOFUgT+z5w3FCa2R7dwDqRalqhhaHbgCEbxpEwosAkj1ReDaLHBvANVUCVxCrwZ1NYH1V8tFasJ5BjPOBac02YSMjAb3YDjMw= 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=1702406967; 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=Y9BKQtIfmMm0sVY++cu3R+sS5q4tdRr2o2bcRqoIeps=; b=qJDjqT1P2Rcgd2vYjnvrmn/yBZknNWDIc7p+MFgb5DfZoxdjMuvY+bsslJBPKNpm H69CGMzXQtO6Jxt/FTHtdGJXgkTn75sjhhTxWVVPK0UhbSPhYzEfhxAU7ghlUXUFEC0 DNK6TC2esVHvQ7dEuHNEtuJFPr1ZgJf/CsyVpAj8= Received: from kampyooter.. (182.69.31.144 [182.69.31.144]) by mx.zoho.in with SMTPS id 1702406965316224.42754580873793; Wed, 13 Dec 2023 00:19:25 +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 v5 0/2] nfc: Fix UAF during datagram sending caused by missing refcounting Date: Wed, 13 Dec 2023 00:19:18 +0530 Message-ID: X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 10:50:16 -0800 (PST) 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 | 72 +++++++++++++++++++++++++++++---------------- net/nfc/llcp_sock.c | 5 ++++ 2 files changed, 52 insertions(+), 25 deletions(-) -- 2.42.0