Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2074204rdh; Sat, 25 Nov 2023 12:27:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IF95Xtkal89zfAp0t5lokbVH0UxQNcdI0uaBRbWGeJcI/P3OR7MaW1XMkImDaieiJJo35Bw X-Received: by 2002:a05:6e02:1be6:b0:357:704a:8699 with SMTP id y6-20020a056e021be600b00357704a8699mr9853098ilv.8.1700944045022; Sat, 25 Nov 2023 12:27:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700944044; cv=pass; d=google.com; s=arc-20160816; b=f1w8/YdRTKfvwX7bMrTxQK+QNtlZ06itaimXwbBsgbrEeH60+V61l3cgfdx7uBhHGs wRrLz1tVr7hCIfWq5Bl2AMLQEcOVWdMg091hp2FbQ9AQnnQdDfCj/hA/auWE49H5/otB pwwoh1DihXiu32ndwIUCpMZrt1gkVynAeaa2txgf8bBpHm9hKH+/33RSGVd9YM4bScyn /RTi2lXqiY0RE+8xDSyIBARrmg6u1XBgD7KEirFgox4WFVggXzRfHrHuTc17/XhUPyXL 27AiY5Is0pihmT4pIaT221Q8bUVj8hVFMackmD4B+e7ODXUCfy+eNsEcT/3pMD3oIxI4 MrFQ== 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=8fzgFKnrdV1ajhUYFpyl+tyr4D7cXS4mqi6t0uH4pTI=; fh=lWFC9d7jJSiNqATg8HCbo845G0U9cpMSp26bBLEa5gM=; b=u0OGwu9Eo9Uy1bou/S7c5JRlmruYP2JebNSRVciQyR9raRIQMoe9sQ/YJkGLcOJ83M b+Vb/8fTF6SfMamj28VkadJTWxJkPBlyUuV9EMat2AeORI6tv8RlhM+63tOV+/h4K3rc i15kLW84mynMTSSLPOnqipW0XxRVjLNXIedqdGggj15TuK04KFRhkXh10lCHOOyhLMR3 XB6pZb6P+uWkNaZo0Jph/Ad9G5lWB7WzWRQ19X9hwxtjpNGXcT+NU3ZkBkcPGFADLUZ4 RjGnW6Hd8hwyyMocEOvQYAFh71O92PLjllhAodkS2dlPL5UxiiZitKOFOVrYGZ8Pgjvj 32Mw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=N4+oBPJm; 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:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id s14-20020a65690e000000b005c203ace543si5981484pgq.774.2023.11.25.12.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 12:27:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@siddh.me header.s=zmail header.b=N4+oBPJm; 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:1 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 morse.vger.email (Postfix) with ESMTP id AA8FA803D825; Sat, 25 Nov 2023 12:27:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229645AbjKYU1F (ORCPT + 99 others); Sat, 25 Nov 2023 15:27:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjKYU1E (ORCPT ); Sat, 25 Nov 2023 15:27:04 -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 3A02810D; Sat, 25 Nov 2023 12:27:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700943995; cv=none; d=zohomail.in; s=zohoarc; b=B8Iqj+BQjBKJDp9O1R0QQqjD2yLh/iboelD27OwSguFeSY0c8rxTET3w34hERF7MYR1XcSwk6Ei34s5zTan3kPEVrJ8wY+tHZU30w3Li3ND52cX0yBImJ772yztdzrs9JXtrxUyik+VDCBe3j8StdppzkYluPZWb8d3sW7HjIr8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1700943995; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=8fzgFKnrdV1ajhUYFpyl+tyr4D7cXS4mqi6t0uH4pTI=; b=feXXwkRk9R5Q8MUm53qVOFCPUWmPwiyeRSk9mIOduFl4dvyxPbMI7vCJvABiEPsdKg3orP+uFXilsfs5+62EEDx+Jmp3C9YJI4j2ajjZ+SNsCB+t/mQ33O8Vq0k2ClVZ+BJftdmC2PHlBwbpTRs1c6RUwJSh6aVpUHgMAIllvyE= 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=1700943995; 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=8fzgFKnrdV1ajhUYFpyl+tyr4D7cXS4mqi6t0uH4pTI=; b=N4+oBPJmzSAtLk9xTgZS6j2WuVO2RTQ6ZqEQXMCu0vEWKyWi4dL1aKtoeLyva4XU mSY/Lkm/VfJXefahoONSXcb9rWI+6i0W3mLOM+7ny3hTk6mq33MT08GUhu+FShdE/+z r0Z6WLRn3ecNaH4zn76F+Q4jZG1eSXySNwX8Wr+Y= Received: from kampyooter.. (110.226.61.26 [110.226.61.26]) by mx.zoho.in with SMTPS id 1700943993929600.5063004964155; Sun, 26 Nov 2023 01:56:33 +0530 (IST) From: Siddh Raman Pant To: Krzysztof Kozlowski , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 0/4] Fix UAF caused by racing datagram sending and freeing of nfc_dev Date: Sun, 26 Nov 2023 01:56:15 +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 morse.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 (morse.vger.email [0.0.0.0]); Sat, 25 Nov 2023 12:27:22 -0800 (PST) (This patchset should be applied in order as changes are dependent.) 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 (a msg with size of 4096 is sent in chunks of 128 in this repro). This loop contains the call to nfc_alloc_send_skb(). Thus, I extracted nfc_dev access from nfc_alloc_send_skb() to the caller, and used a spinlock (rwlock) to protect/serialize access to the nfc_dev to avoid the UAF. Tested with syzkaller. Since state has to be LLCP_BOUND for datagram sending, we can bail out early in llcp_sock_sendmsg(). The last patch is just a code reformat. As the datagram sending is a bigger chunk of the code, we can reduce unnecessary indentation. Please review and let me know if any errors are there, and hopefully this gets accepted. Thanks, Siddh Siddh Raman Pant (4): nfc: Extract nfc_dev access from nfc_alloc_send_skb() into the callers nfc: Protect access to nfc_dev in an llcp_sock with a rwlock nfc: Do not send datagram if socket state isn't LLCP_BOUND nfc: llcp_sock_sendmsg: Reformat code to make the smaller block indented include/net/nfc/nfc.h | 6 +++--- net/nfc/core.c | 14 +++++++------- net/nfc/llcp.h | 1 + net/nfc/llcp_commands.c | 41 +++++++++++++++++++++++++++++++++++------ net/nfc/llcp_core.c | 31 +++++++++++++++++++------------ net/nfc/llcp_sock.c | 31 ++++++++++++++++++------------- net/nfc/rawsock.c | 8 ++++++-- 7 files changed, 89 insertions(+), 43 deletions(-) -- 2.42.0