From patchwork Sat Nov 25 20:26:15 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: 17104 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp2142404vqx; Sat, 25 Nov 2023 12:27:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4jKbelIUn0KB3JursqiBLDk1BLycat4BjcCoSKjm3axhtqhixzqCAg4q7qg38hK2+iQyz X-Received: by 2002:a05:6a21:6d8f:b0:189:f8bc:71a3 with SMTP id wl15-20020a056a216d8f00b00189f8bc71a3mr8104634pzb.37.1700944041748; Sat, 25 Nov 2023 12:27:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700944041; cv=pass; d=google.com; s=arc-20160816; b=nnudbbhwhJueRZWijH+GDZ7+HGT6UuaiWZdv9VPLTO0xc1FoKzh/qt86J1sqF8Z+0g mtMiMHAXnZ+KgUSWitYe4cmopUfJofIt27f3pQIBSSSjYz95BBN/9O9B3YPFORR/Ts55 Nma/IsO1rOetXOK4iB2FGOMrkV/rWVTkkbel+CXTAsQWemFnk0vJ7oEi9XVD8IMSmFbW Tjf35T0wu+bCNB+QFRUXuT7IoRbTehYCjevSp+EfiFV2MOznjmW9zriyO+AmZYZzq3I3 S3Xw3GeOfmgi3jnTQPZMeOGm7BabN9Tbz3wB2qAbMn8+tXxmLNV4X8UK8XeEZINBbG5f AcFg== 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 Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p16-20020a63e650000000b005b74ce91e9fsi6562316pgj.672.2023.11.25.12.27.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 12:27:21 -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 29780803DB3D; Sat, 25 Nov 2023 12:27:19 -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 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:19 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783569099250413416 X-GMAIL-MSGID: 1783569099250413416 (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(-)