Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2883878rdb; Sat, 9 Dec 2023 03:05:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IGqsQ1I6sKh9jlXCJaqYyt7xOAJC9nh3pAnn8jTL/PHZOP6VvcOTEI/cb5pmPy5Cb0Jn/Ga X-Received: by 2002:a05:6a00:2d07:b0:6ce:b7cb:5770 with SMTP id fa7-20020a056a002d0700b006ceb7cb5770mr865597pfb.49.1702119919399; Sat, 09 Dec 2023 03:05:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702119919; cv=pass; d=google.com; s=arc-20160816; b=qFxpCEAkwQyued6zJqFlzUu5dt0eac1x9+qWJTLJYuVgnKfGjw70KFJondnZAIMO5/ hc7es+DObyUF0qpvFpkQvt/8cby6bCwpcQHZvB2WjeLivP2GCI1fEBkOeZzIbMU/mAup 002UZ9iwyx5M9uuU2AYRmztWuEL+Se749KgU1E2tDzaDj0Ioai6lT2qphJWIv5h9ReBU wjNg9yN8PBcXbeLcS56xklpe5IPj1bqx4jHo8KOKz7nRnqdiX6PgnSo3ym00t97KOLIS uEWHPM/4uqIi3mcZoxraEDEc3ZqWUuy9EjrfSTWZwXY8yN6NVG6hovKDge7/0ypjpqtK gy0g== 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=MXhcSyzmKjfefxZKd0I1TvledRLZO0z+eiFwOUBU9X0=; fh=RACn3Dz57j2O2Diwusdo+qDZ4fbRHVAux3E9Xar2zXI=; b=yuVj/zlMg9deNYEhhZ+PqG07JfdtyVVvrXSNrh7tDB3eSGqum+JMPe9HDdkFoLYOel 0W/+vz/kJcSZsSNK0C1yYGqZ4OYkojjRp/cwIkAbGgfwiPYSo4sXdrT93JsH6q4WZzqx 5Xy/NrGPMhLvpvZJ6nK9MmCjp5NzsyG0qpU88WIFJTHIfHf9fSNDHJ7NHzH4qsPKO49H q8m7Gl2JZm1/lozXR2/30XUxze9TZO4CYtInvoG8xy1RngRiylD4MTx4HOTf4ecwfdfn dnDCcJguNLj0iecUKX12aihZzvCF1dGYe8efstaI7STKwtho9FaCZkzi/4q1juauIhmW FsUQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=nGaHO5hV; 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:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id fh13-20020a056a00390d00b006cefb653cecsi280500pfb.146.2023.12.09.03.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 03:05:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=nGaHO5hV; 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:7 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 snail.vger.email (Postfix) with ESMTP id 628D5805F972; Sat, 9 Dec 2023 03:05:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229927AbjLILFF (ORCPT + 99 others); Sat, 9 Dec 2023 06:05:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229538AbjLILFC (ORCPT ); Sat, 9 Dec 2023 06:05:02 -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 05C3210E0; Sat, 9 Dec 2023 03:05:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702119865; cv=none; d=zohomail.in; s=zohoarc; b=ePOROXgNLRYfFMrTZ5fDHg5lgnPbYUnvAeaUNEM4fFG5a7y07UN1sEFQZhBqWX9PwLwYlSapL48xoq/pDmDjZOST5VFx62eF/rmwLoeSoQ8tA0DBs/arV8vpbS/5Utm4CyKjSj6hlXK7lnc+vZNkX1UV18+eGajihZkzCsfnV0c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1702119865; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=MXhcSyzmKjfefxZKd0I1TvledRLZO0z+eiFwOUBU9X0=; b=J+T5te1OKoXrwhQBT7ujz99ae6gpizDOyNjLJaKxADZZNOiyus6mT+uccK2qKCzkLFEJvse5HciZ8l7cytkG7wnhH56PNqL4oqYY5JjoddaYyRcpolgxzxZn/rq/8vo+5nFwoODihhpV74txIaPwWoBgfnjUvmYXIvrJGGDnFw0= 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=1702119865; 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=MXhcSyzmKjfefxZKd0I1TvledRLZO0z+eiFwOUBU9X0=; b=nGaHO5hVfYcm2an8TgYyWoah4TxDbCu1IvMOt7goaGPveqF9i5mza2okyVUYiixR NSfIm9oMoyqAJd5I3Jxn01Zx9bWw8FeAXYz3nkS+50y89awTJ8aipmUb51n/pS9VCTO b0crK/ArjlFDJEqK8P6719mNBQMorY+8x82UeEDo= Received: from kampyooter.. (110.227.243.208 [110.227.243.208]) by mx.zoho.in with SMTPS id 1702119864675642.5508323354376; Sat, 9 Dec 2023 16:34:24 +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 v4 0/2] nfc: Fix UAF during datagram sending caused by missing refcounting Date: Sat, 9 Dec 2023 16:34:19 +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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Sat, 09 Dec 2023 03:05:18 -0800 (PST) (Very sorry for the delay in sending this.) 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 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. 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 | 55 ++++++++++++++++++++++++++++++++++----------- net/nfc/llcp_sock.c | 5 +++++ 2 files changed, 47 insertions(+), 13 deletions(-) -- 2.42.0