Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3623724pxb; Mon, 24 Jan 2022 13:46:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJxvN7bD8X+JGfwbObLG8CVXaYU41AJQIj3VM81JirpzU5hHe9yOlAfUVe3dGqjRSrUy3Ndd X-Received: by 2002:a17:902:8bc5:b0:149:88fc:5560 with SMTP id r5-20020a1709028bc500b0014988fc5560mr16439902plo.144.1643060784722; Mon, 24 Jan 2022 13:46:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643060784; cv=none; d=google.com; s=arc-20160816; b=IsmAm2/Pks+odcBb6ATn9p2JhvcveNo5RcRje0VucYNvRZkXeaIoYyJkdvOFlqSvct 4h22DiCUinosBU1PHPN9TCW0sakMBL8NJvg9Lf4RDmugdcRGAbgEjm8A2j3GHs90OejQ GOw6n1Rc7c5XxBX1mXWesKXopFxLB1wVAiEzO5IX15cSFxrdgjlvcceRkcXneZRYg+lU DE+mu9o08QQ+bJdu0jOoNxw5izvGqIOYThRfM6a+a5pw0jWbMpu3PbZr19hkOZfr4RI7 MtJEM95GZ9iN/yz4fW9DHogx7FH0ShQW3XuXlhNSxe2F2OX+guZmGlNofh9XkttVOVtR ESiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Z+oM0jApQQMP924dGWqTRlQVyjC9+WoM780yK47lmRg=; b=fKU/LsXzSK+zEop5DXCmImucouU5mSqQFpvnFxbqQ9y3jgpNaafQ8oL2U1iLUL3w0A IRb2+xRB3ThWvXNwo0Gwvod5wVnzpmLB+uZsx+R1i/993bISBnqY7o0TXpNfb0dsrhoX IDL0s2iLZddSZiztBrrpLYJN/91Tbv+Szyi74iIdPsYRnE7aLq6uDRrbjKPPEkRIgkDC mcAnXrYHSEgDSJ6AKc/MLaC5lWTU365Ajfgy4BVeD4W53PBEoV7VKAZDA+dreXnsotNj AcdPbHoWwrFe8CaZYPNib+xOzRAs8pdxlVXMbFCyCSz+9npb1p+NYCoEms9V8ymoCdLy MhSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fGcfkbLa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ml7si429158pjb.127.2022.01.24.13.46.10; Mon, 24 Jan 2022 13:46:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fGcfkbLa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1451056AbiAXVWD (ORCPT + 99 others); Mon, 24 Jan 2022 16:22:03 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:50694 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442263AbiAXUyF (ORCPT ); Mon, 24 Jan 2022 15:54:05 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id CB39FB81057; Mon, 24 Jan 2022 20:54:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06A03C340E5; Mon, 24 Jan 2022 20:54:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643057641; bh=DJ/cYkr8MzqT7hAfv92BLVMqYFrXoXo4x8FHMhWFLhY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fGcfkbLaDVVm198A5cZ2HKt1l0v9NB1sq1yBxmUOETbudFczMc5gJ7smNHrlLCkIb pr2SKm9k/eW7xHuZcg2bC1F3Feqt6EwI5UfUcB+nSWTmlpxN2LqSZnYhHGx4BE6d/j Jm22oy1LR8jebJ1VQxVCN1HbaZcWVE169kNbc5W8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Krzysztof Kozlowski , "David S. Miller" , syzbot+7f23bcddf626e0593a39@syzkaller.appspotmail.com Subject: [PATCH 5.16 0014/1039] nfc: llcp: fix NULL error pointer dereference on sendmsg() after failed bind() Date: Mon, 24 Jan 2022 19:30:03 +0100 Message-Id: <20220124184125.622205194@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Krzysztof Kozlowski commit dded08927ca3c31a5c37f8e7f95fe98770475dd4 upstream. Syzbot detected a NULL pointer dereference of nfc_llcp_sock->dev pointer (which is a 'struct nfc_dev *') with calls to llcp_sock_sendmsg() after a failed llcp_sock_bind(). The message being sent is a SOCK_DGRAM. KASAN report: BUG: KASAN: null-ptr-deref in nfc_alloc_send_skb+0x2d/0xc0 Read of size 4 at addr 00000000000005c8 by task llcp_sock_nfc_a/899 CPU: 5 PID: 899 Comm: llcp_sock_nfc_a Not tainted 5.16.0-rc6-next-20211224-00001-gc6437fbf18b0 #125 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 Call Trace: dump_stack_lvl+0x45/0x59 ? nfc_alloc_send_skb+0x2d/0xc0 __kasan_report.cold+0x117/0x11c ? mark_lock+0x480/0x4f0 ? nfc_alloc_send_skb+0x2d/0xc0 kasan_report+0x38/0x50 nfc_alloc_send_skb+0x2d/0xc0 nfc_llcp_send_ui_frame+0x18c/0x2a0 ? nfc_llcp_send_i_frame+0x230/0x230 ? __local_bh_enable_ip+0x86/0xe0 ? llcp_sock_connect+0x470/0x470 ? llcp_sock_connect+0x470/0x470 sock_sendmsg+0x8e/0xa0 ____sys_sendmsg+0x253/0x3f0 ... The issue was visible only with multiple simultaneous calls to bind() and sendmsg(), which resulted in most of the bind() calls to fail. The bind() was failing on checking if there is available WKS/SDP/SAP (respective bit in 'struct nfc_llcp_local' fields). When there was no available WKS/SDP/SAP, the bind returned error but the sendmsg() to such socket was able to trigger mentioned NULL pointer dereference of nfc_llcp_sock->dev. The code looks simply racy and currently it protects several paths against race with checks for (!nfc_llcp_sock->local) which is NULL-ified in error paths of bind(). The llcp_sock_sendmsg() did not have such check but called function nfc_llcp_send_ui_frame() had, although not protected with lock_sock(). Therefore the race could look like (same socket is used all the time): CPU0 CPU1 ==== ==== llcp_sock_bind() - lock_sock() - success - release_sock() - return 0 llcp_sock_sendmsg() - lock_sock() - release_sock() llcp_sock_bind(), same socket - lock_sock() - error - nfc_llcp_send_ui_frame() - if (!llcp_sock->local) - llcp_sock->local = NULL - nfc_put_device(dev) - dereference llcp_sock->dev - release_sock() - return -ERRNO The nfc_llcp_send_ui_frame() checked llcp_sock->local outside of the lock, which is racy and ineffective check. Instead, its caller llcp_sock_sendmsg(), should perform the check inside lock_sock(). Reported-and-tested-by: syzbot+7f23bcddf626e0593a39@syzkaller.appspotmail.com Fixes: b874dec21d1c ("NFC: Implement LLCP connection less Tx path") Cc: Signed-off-by: Krzysztof Kozlowski Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/nfc/llcp_sock.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/net/nfc/llcp_sock.c +++ b/net/nfc/llcp_sock.c @@ -789,6 +789,11 @@ static int llcp_sock_sendmsg(struct sock lock_sock(sk); + if (!llcp_sock->local) { + release_sock(sk); + return -ENODEV; + } + if (sk->sk_type == SOCK_DGRAM) { DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, msg->msg_name);