Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3555365pxb; Mon, 24 Jan 2022 12:07:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJzlDKB2wko3a5R9JBP0w4Rr3HPRdRd48bjarQrPyB+4WaJVCDe7ml4UBF40JEuTZhZUlwNJ X-Received: by 2002:a63:5c41:: with SMTP id n1mr12792462pgm.280.1643054840336; Mon, 24 Jan 2022 12:07:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643054840; cv=none; d=google.com; s=arc-20160816; b=qEdh5NfHY7R8ckTRgV9ELpNDoKLRT2waJUIoQEUUKXgtLAsg7pQYF9zmyVFfllphIQ 4Nckpj9IoVTH7A68RR63XtvTIf/S6rkCSj9E1vbM8qi1ofJoHY0q+DN9YB1UsFv56heX hjegAt4lioDa61g1mfNSCVLZp0owOgH8mtgy1PhIGWuhmwMZC/ZIsyUgbIklEAqyTITw yiaAj22FqLZhcgVYDqinA8bc6RO6M6uNCAgHImmcBj11c3qihvmJy/EcpzXwT7ZWZIGF LbooN7yvj6ya9U9PEUVDeU2fG/6Mgl2HSMZMVrUnktr47djCZvab+7RbLf0515t5oyQZ 8qPQ== 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=Qdt0Jr+dQhzvhH3Nr2bJeFYMT0yKmPQ3q6rujrvtkl8=; b=ecCGVm3IMsJb2+06JHLJm/Fx0pmbN7nkefTF6+kznS41kal98/zGVb0WZr5Q2KImpS eRBAaOcFCLTpZ6yiHwV3zvP9RK2UGG5casSfkUnbpmEMWuz3w9LliCODAezVpw6+ZPv5 8RRxnFdsvekQTscE6zH7khbQOLrErilMXYpHYMiNLCAnzVZWHmzslojqplbVYvYTKVp4 X2aJ3u+FBXhMSo1FZRl+RSMRGAArejsVW3IZYsKI4kYTEyaq+CrWzagMnk9CIUVQT19S xbzRQbnM9bX2TSLH7WaMpE1y+UnQsMxU0vvbZQy4xczkADVcEIWFPVjRzkYS3q2PbugZ jNWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=M0BVcOeJ; 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 k15si18734931pga.245.2022.01.24.12.07.06; Mon, 24 Jan 2022 12:07:20 -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=M0BVcOeJ; 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 S1346198AbiAXTFM (ORCPT + 99 others); Mon, 24 Jan 2022 14:05:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344772AbiAXS7M (ORCPT ); Mon, 24 Jan 2022 13:59:12 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1BD8C06127B; Mon, 24 Jan 2022 10:56:21 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 51E5D61416; Mon, 24 Jan 2022 18:56:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FD98C340E5; Mon, 24 Jan 2022 18:56:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643050580; bh=Oo2lX6416TxPeLNk85BzSqjJdoOIIwJmRJQe4N3bdMg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M0BVcOeJ+nfxQhXf65FCIfG+HFwaBwPDzjDJwH8/3Z+f1/58rsARlO5z6XV2Bb3+8 TO4R7OjvkydxD/j3gyb6HjlzKm3vA9OG5C9Az1yhlDy+ETcxjZhGScUigjsTngqUIz A1imRAj9yJR0NwU0H5iPOGLBr3xNMMd752u3kAkg= 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 4.9 015/157] nfc: llcp: fix NULL error pointer dereference on sendmsg() after failed bind() Date: Mon, 24 Jan 2022 19:41:45 +0100 Message-Id: <20220124183933.281022426@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183932.787526760@linuxfoundation.org> References: <20220124183932.787526760@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 @@ -796,6 +796,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);