From patchwork Sat Nov 25 20:26:16 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: 13468667 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="kfic7x93" Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8667F127; Sat, 25 Nov 2023 12:27:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700943997; cv=none; d=zohomail.in; s=zohoarc; b=BGfGn3DCDFmOnkDsztjNxOlpWIHIH2sruFlmgC7D8pkdi9qKFpSlaYh+ugi8u8cgDMdWM3KNN0FGXlX57MLnelbQhc9lv40CgfgbssO5FKhZHazb5jeH2W5nmwQXJq/3Vvw4hvPmS0ZWP1lMqfSXfuK8Nk6tTlRuLq/pyN3Lq7c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1700943997; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=khXX+6WIAnh+nbfhSLVUt+KViQdJnf6YZ3f3zq0qJyA=; b=Ncih+qyH3NB0WdbOHkljRWMEgbiratVKpxabNsTYoETjMo2gF0Af6WgPwIjA9LotQ3M59jDhH2O4MyeRZ3N0R4iv2B2sKaR/6l5u5txcAs62zCDmLSQkIJUHEk0m490/JR8F5hyJ1dZuBLePXuwSV4spADb3LrCor1iMvvbSijo= 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=1700943997; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=khXX+6WIAnh+nbfhSLVUt+KViQdJnf6YZ3f3zq0qJyA=; b=kfic7x93BI2BMMABAGJ7+jpOVjI7JhUXRyUg1ZCPqgR4fqJisHvm9T5DTztDiHIL QeImSU3M7tV6efOBTccqiEGas8C+0QsaVNUK/etmQl305JMlar9YSipc8NxwYgtC7MH 9LVhKie744FkyWWj+DWy22VBX4w01fmJBjjUQ+Sc= Received: from kampyooter.. (110.226.61.26 [110.226.61.26]) by mx.zoho.in with SMTPS id 1700943994641928.1437515996658; Sun, 26 Nov 2023 01:56:34 +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 1/4] nfc: Extract nfc_dev access from nfc_alloc_send_skb() into the callers Date: Sun, 26 Nov 2023 01:56:16 +0530 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ZohoMailClient: External X-Patchwork-Delegate: kuba@kernel.org The only reason why nfc_dev was accessed inside nfc_alloc_send_skb() is for getting the headroom and tailroom values. This can cause UAF to be reported from nfc_alloc_send_skb(), but the callers are responsible for managing the device access, and thus the UAF being reported, as the callers (like nfc_llcp_send_ui_frame()) may repeatedly call this function, and this function will repeatedly try to get the same headroom and tailroom values. Thus, put the nfc_dev access responsibility on the callers and accept the headroom and tailroom values directly. Signed-off-by: Siddh Raman Pant --- include/net/nfc/nfc.h | 6 +++--- net/nfc/core.c | 14 +++++++------- net/nfc/llcp_commands.c | 20 ++++++++++++++------ net/nfc/rawsock.c | 8 ++++++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h index 5dee575fbe86..efe20a9a8499 100644 --- a/include/net/nfc/nfc.h +++ b/include/net/nfc/nfc.h @@ -260,9 +260,9 @@ static inline const char *nfc_device_name(const struct nfc_dev *dev) return dev_name(&dev->dev); } -struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, - unsigned int flags, unsigned int size, - unsigned int *err); +struct sk_buff *nfc_alloc_send_skb(struct sock *sk, unsigned int flags, + unsigned int size, int headroom, + int tailroom, unsigned int *err); struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp); int nfc_set_remote_general_bytes(struct nfc_dev *dev, diff --git a/net/nfc/core.c b/net/nfc/core.c index eb2c0959e5b6..1f7d20971f6f 100644 --- a/net/nfc/core.c +++ b/net/nfc/core.c @@ -705,25 +705,25 @@ EXPORT_SYMBOL(nfc_tm_deactivated); /** * nfc_alloc_send_skb - allocate a skb for data exchange responses * - * @dev: device sending the response * @sk: socket sending the response * @flags: MSG_DONTWAIT flag * @size: size to allocate + * @headroom: Extra headroom, in addition to size + * @tailroom: Extra tailroom, in addition to size * @err: pointer to memory to store the error code */ -struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, - unsigned int flags, unsigned int size, - unsigned int *err) +struct sk_buff *nfc_alloc_send_skb(struct sock *sk, unsigned int flags, + unsigned int size, int headroom, + int tailroom, unsigned int *err) { struct sk_buff *skb; unsigned int total_size; - total_size = size + - dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; + total_size = size + headroom + tailroom + NFC_HEADER_SIZE; skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); if (skb) - skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); + skb_reserve(skb, headroom + NFC_HEADER_SIZE); return skb; } diff --git a/net/nfc/llcp_commands.c b/net/nfc/llcp_commands.c index e2680a3bef79..39c7c59bbf66 100644 --- a/net/nfc/llcp_commands.c +++ b/net/nfc/llcp_commands.c @@ -314,13 +314,17 @@ static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, u8 cmd, u16 size) { struct sk_buff *skb; - int err; + int err, headroom, tailroom; if (sock->ssap == 0) return NULL; - skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, - size + LLCP_HEADER_SIZE, &err); + headroom = sock->dev->tx_headroom; + tailroom = sock->dev->tx_tailroom; + + skb = nfc_alloc_send_skb(&sock->sk, MSG_DONTWAIT, + size + LLCP_HEADER_SIZE, headroom, tailroom, + &err); if (skb == NULL) { pr_err("Could not allocate PDU\n"); return NULL; @@ -734,7 +738,7 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, size_t frag_len = 0, remaining_len; u8 *msg_ptr, *msg_data; u16 remote_miu; - int err; + int err, headroom, tailroom; pr_debug("Send UI frame len %zd\n", len); @@ -751,6 +755,9 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, return -EFAULT; } + headroom = sock->dev->tx_headroom; + tailroom = sock->dev->tx_tailroom; + remaining_len = len; msg_ptr = msg_data; @@ -763,8 +770,9 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, pr_debug("Fragment %zd bytes remaining %zd", frag_len, remaining_len); - pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0, - frag_len + LLCP_HEADER_SIZE, &err); + pdu = nfc_alloc_send_skb(&sock->sk, 0, + frag_len + LLCP_HEADER_SIZE, + headroom, tailroom, &err); if (pdu == NULL) { pr_err("Could not allocate PDU (error=%d)\n", err); len -= remaining_len; diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c index 5125392bb68e..fab1facb7105 100644 --- a/net/nfc/rawsock.c +++ b/net/nfc/rawsock.c @@ -207,7 +207,7 @@ static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) struct sock *sk = sock->sk; struct nfc_dev *dev = nfc_rawsock(sk)->dev; struct sk_buff *skb; - int rc; + int rc, headroom, tailroom; pr_debug("sock=%p sk=%p len=%zu\n", sock, sk, len); @@ -217,7 +217,11 @@ static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) if (sock->state != SS_CONNECTED) return -ENOTCONN; - skb = nfc_alloc_send_skb(dev, sk, msg->msg_flags, len, &rc); + headroom = dev->tx_headroom; + tailroom = dev->tx_tailroom; + + skb = nfc_alloc_send_skb(sk, msg->msg_flags, len, headroom, tailroom, + &rc); if (skb == NULL) return rc; From patchwork Sat Nov 25 20:26:17 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: 13468666 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="RLde9bkg" Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39C0710B; Sat, 25 Nov 2023 12:27:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700943997; cv=none; d=zohomail.in; s=zohoarc; b=E8pah2PkEyu0QS7s/qU7SRkza+Lo2QrfNd8H2RvTuTTW98TFkbP0eFvnIypIW8EYd/VA6Z5IWyE6ruqwi7PEPSZa4I6w0kcpfg/N0/v7LRdAUmgFGV6V0f4bm//ZygCARLOdUILMi+x5JFocB7C1LC5GC1KmkkZjkTpJOmBQyJo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1700943997; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=BEgkYV/g4NVqjVg4g/pZSrbzunesAbsgXj6OKtD8qnY=; b=LkK8FuB9+0K1zgJJzuOWWpy0/A6KrVm5/bB5ATAPM5D4ooYdoqfjCdvpe4iOeNRAWpH4ZN7kV6QHPsxmzMT5M+xE1VWl6+SwrsZM+FMqvUMusNOMV1+TD2LEIH2zWV7zmanP3rnmFN+mj+bOvuYq1GnVSrfyWUHrh4P9KDcVdp0= 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=1700943997; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=BEgkYV/g4NVqjVg4g/pZSrbzunesAbsgXj6OKtD8qnY=; b=RLde9bkgAi7h1vbXSpCfVyrbn5NIW/vzyqsmig0vjGOF7ocCwiuNI/FLKMgWdg9+ PcE6wrzCT8VrkZYMi61PSl3x9i2LQtCDAl+jr7ghPTQs55Fe1eXbbW3sk7u15pG5WQX W94W4cdUGV1j4L4DinGAyVRNxmb1iG/jXSuhU954= Received: from kampyooter.. (110.226.61.26 [110.226.61.26]) by mx.zoho.in with SMTPS id 1700943995162403.864052366456; Sun, 26 Nov 2023 01:56:35 +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, syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com Subject: [PATCH 2/4] nfc: Protect access to nfc_dev in an llcp_sock with a rwlock Date: Sun, 26 Nov 2023 01:56:17 +0530 Message-ID: <7c198c2aa08b34045b8f9e0afe3d0b3bf5802180.1700943019.git.code@siddh.me> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ZohoMailClient: External X-Patchwork-Delegate: kuba@kernel.org llcp_sock_sendmsg() calls nfc_llcp_send_ui_frame(), which accesses the nfc_dev from the llcp_sock for getting the headroom and tailroom needed for skb allocation. Parallely, the nfc_dev can be freed via the nfc_unregister_device() codepath (nfc_release() being called due to the class unregister in nfc_exit()), leading to the UAF reported by Syzkaller. We have the following call tree before freeing: nfc_unregister_device() -> nfc_llcp_unregister_device() -> local_cleanup() -> nfc_llcp_socket_release() nfc_llcp_socket_release() sets the state of sockets to LLCP_CLOSED, and this is encountered necessarily before any freeing of nfc_dev. Thus, add a rwlock in struct llcp_sock to synchronize access to nfc_dev. nfc_dev in an llcp_sock will be NULLed in a write critical section when socket state has been set to closed. Thus, we can avoid the UAF by bailing out from a read critical section upon seeing NULL. Since this is repeated multiple times in nfc_llcp_socket_release(), extract the behaviour into a new function. Reported-and-tested-by: syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=bbe84a4010eeea00982d Signed-off-by: Siddh Raman Pant --- net/nfc/llcp.h | 1 + net/nfc/llcp_commands.c | 27 ++++++++++++++++++++++++--- net/nfc/llcp_core.c | 31 +++++++++++++++++++------------ net/nfc/llcp_sock.c | 2 ++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/net/nfc/llcp.h b/net/nfc/llcp.h index d8345ed57c95..800cbe8e3d6b 100644 --- a/net/nfc/llcp.h +++ b/net/nfc/llcp.h @@ -102,6 +102,7 @@ struct nfc_llcp_local { struct nfc_llcp_sock { struct sock sk; struct nfc_dev *dev; + rwlock_t rw_dev_lock; struct nfc_llcp_local *local; u32 target_idx; u32 nfc_protocol; diff --git a/net/nfc/llcp_commands.c b/net/nfc/llcp_commands.c index 39c7c59bbf66..b132830bc206 100644 --- a/net/nfc/llcp_commands.c +++ b/net/nfc/llcp_commands.c @@ -315,13 +315,24 @@ static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, { struct sk_buff *skb; int err, headroom, tailroom; + unsigned long irq_flags; if (sock->ssap == 0) return NULL; + read_lock_irqsave(&sock->rw_dev_lock, irq_flags); + + if (!sock->dev) { + read_unlock_irqrestore(&sock->rw_dev_lock, irq_flags); + pr_err("NFC device does not exit\n"); + return NULL; + } + headroom = sock->dev->tx_headroom; tailroom = sock->dev->tx_tailroom; + read_unlock_irqrestore(&sock->rw_dev_lock, irq_flags); + skb = nfc_alloc_send_skb(&sock->sk, MSG_DONTWAIT, size + LLCP_HEADER_SIZE, headroom, tailroom, &err); @@ -739,6 +750,7 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, u8 *msg_ptr, *msg_data; u16 remote_miu; int err, headroom, tailroom; + unsigned long irq_flags; pr_debug("Send UI frame len %zd\n", len); @@ -746,6 +758,18 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, if (local == NULL) return -ENODEV; + read_lock_irqsave(&sock->rw_dev_lock, irq_flags); + + if (!sock->dev) { + read_unlock_irqrestore(&sock->rw_dev_lock, irq_flags); + return -ENODEV; + } + + headroom = sock->dev->tx_headroom; + tailroom = sock->dev->tx_tailroom; + + read_unlock_irqrestore(&sock->rw_dev_lock, irq_flags); + msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN); if (msg_data == NULL) return -ENOMEM; @@ -755,9 +779,6 @@ int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap, return -EFAULT; } - headroom = sock->dev->tx_headroom; - tailroom = sock->dev->tx_tailroom; - remaining_len = len; msg_ptr = msg_data; diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c index 1dac28136e6a..a565712d7db8 100644 --- a/net/nfc/llcp_core.c +++ b/net/nfc/llcp_core.c @@ -20,6 +20,22 @@ static LIST_HEAD(llcp_devices); /* Protects llcp_devices list */ static DEFINE_SPINLOCK(llcp_devices_lock); +static inline void nfc_llcp_sock_close(struct nfc_llcp_sock *llcp_sock, int err) +{ + struct sock *sk = &llcp_sock->sk; + unsigned long irq_flags; + + if (err) + sk->sk_err = err; + + sk->sk_state = LLCP_CLOSED; + sk->sk_state_change(sk); + + write_lock_irqsave(&llcp_sock->rw_dev_lock, irq_flags); + llcp_sock->dev = NULL; + write_unlock_irqrestore(&llcp_sock->rw_dev_lock, irq_flags); +} + static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb); void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk) @@ -96,19 +112,13 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, nfc_llcp_accept_unlink(accept_sk); - if (err) - accept_sk->sk_err = err; - accept_sk->sk_state = LLCP_CLOSED; - accept_sk->sk_state_change(sk); + nfc_llcp_sock_close(lsk, err); bh_unlock_sock(accept_sk); } } - if (err) - sk->sk_err = err; - sk->sk_state = LLCP_CLOSED; - sk->sk_state_change(sk); + nfc_llcp_sock_close(llcp_sock, err); bh_unlock_sock(sk); @@ -130,10 +140,7 @@ static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device, nfc_llcp_socket_purge(llcp_sock); - if (err) - sk->sk_err = err; - sk->sk_state = LLCP_CLOSED; - sk->sk_state_change(sk); + nfc_llcp_sock_close(llcp_sock, err); bh_unlock_sock(sk); diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c index 645677f84dba..ef1ab88a5e4f 100644 --- a/net/nfc/llcp_sock.c +++ b/net/nfc/llcp_sock.c @@ -983,6 +983,8 @@ struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp, int k sk->sk_type = type; sk->sk_destruct = llcp_sock_destruct; + rwlock_init(&llcp_sock->rw_dev_lock); + llcp_sock->ssap = 0; llcp_sock->dsap = LLCP_SAP_SDP; llcp_sock->rw = LLCP_MAX_RW + 1; From patchwork Sat Nov 25 20:26:18 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: 13468664 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="AY+2kf2S" Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79D38119; Sat, 25 Nov 2023 12:27:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700943997; cv=none; d=zohomail.in; s=zohoarc; b=dqsZItpanhNpUQhMs5iWna0XZrthWxu22M1e+Vhd1fiZ5ekGGfU8EXJtccoXEY1cp8HDLYnj+4F1UZ7f6sRuJsveD7q+hbYkTUUTZGS+LEwqT5MkZn2gnC+pgSJaLAkfyhNAw3JBM09OTrIhk2+XFpZY30ZL5+5CkI7sMtW0gHo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1700943997; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=zRlFgAJrtEWCzZS3K60IRfSk2CnQKoHBXTn6uGX5VTg=; b=ZoRGS+zGGPM3gYlAP5ciNLZ3GR0vOtkq3y6UzaH0n5pTnUBLYczkvPhC8QSgLwsk8R0l/lBfR/GZjoIWPu+vDOaRyGWXZfdN8KPN7whoKC9jD6siLLBECx/z4/mydkSEAq7H8yXLf4H5chnIVOSdTbMCh2xf23EuRKngPjkqc14= 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=1700943997; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=zRlFgAJrtEWCzZS3K60IRfSk2CnQKoHBXTn6uGX5VTg=; b=AY+2kf2SDxD9sh2zXy27svfV+rJHAtS9zqCRP72MnjNRYAbEGViY2TKp/tRym9/Z nkMOXC986gaW8bpRP6D7+ZHirvEkRuEdyeJ8c0hRdpmrUj4dvv7tfEArJXjMeAjPf8i 3iXJcHNITq6uVFkB0TME/9NQawWby3R4ZG46dvFo= Received: from kampyooter.. (110.226.61.26 [110.226.61.26]) by mx.zoho.in with SMTPS id 1700943995667656.7460159615724; Sun, 26 Nov 2023 01:56:35 +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 3/4] nfc: Do not send datagram if socket state isn't LLCP_BOUND Date: Sun, 26 Nov 2023 01:56:18 +0530 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ZohoMailClient: External X-Patchwork-Delegate: kuba@kernel.org As we know we cannot send the datagram (state can be set to LLCP_CLOSED by nfc_llcp_socket_release()), there is no need to proceed further. Thus, bail out early from llcp_sock_sendmsg(). Signed-off-by: Siddh Raman Pant Reviewed-by: Krzysztof Kozlowski --- net/nfc/llcp_sock.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c index ef1ab88a5e4f..603f2219b62f 100644 --- a/net/nfc/llcp_sock.c +++ b/net/nfc/llcp_sock.c @@ -796,6 +796,11 @@ static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg, } if (sk->sk_type == SOCK_DGRAM) { + if (sk->sk_state != LLCP_BOUND) { + release_sock(sk); + return -ENOTCONN; + } + DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, msg->msg_name); From patchwork Sat Nov 25 20:26:19 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: 13468665 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=siddh.me header.i=code@siddh.me header.b="aDWr574r" Received: from sender-of-o51.zoho.in (sender-of-o51.zoho.in [103.117.158.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B582110; Sat, 25 Nov 2023 12:27:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700943997; cv=none; d=zohomail.in; s=zohoarc; b=CG/6YhYXSiA5o5lvnsjgbz7aLpPRNl4YXm976la8f/2GIZvRCRzdhQb2f4dKW/TT/QNVQaG5auUOiW0MNHU3dIXOfP1xzLahWzQ1BGH6e1E9FL3uJ49uwfJXBNdeLJ3kLLZnfMgDO985BxpQQk6v95aS/gcjB0FniKVyYcaxTWU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1700943997; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=jokTckctzJGZx54kn2OoTfaUts6J5fPPiEFPTgkvFQU=; b=P7LvR7B5qeEGzTC+VoxGb//U+UGwUXPt4R7tYkjO9TwhGl/uQAu87JPGOadm6DIYmqBzd8OG++Tv27oWUrCpVhihB0PbXC/s6qJ4lGOQK3y6lMMKGaMBUaOoM9qoMmWvqkqlMyeS083z02Z3eNVpVR+O1jqSK5+NzhFHoL2OLWg= 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=1700943997; s=zmail; d=siddh.me; i=code@siddh.me; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=jokTckctzJGZx54kn2OoTfaUts6J5fPPiEFPTgkvFQU=; b=aDWr574rrke4JA3n2T/oeX65rRJaWiCazf5v/whS1uwzqo7U6qyXXk28tmoNPI9Q lpTlK02r6wV1f5BkhKTouF+Ae+DLCwPVn4EnNxOMh5plyBRoNAZLKFm59OPQUzOs5/E YacV251mdlRVJQx+A/bz0yu8+Q+5Ckxaw0nVbUK4= Received: from kampyooter.. (110.226.61.26 [110.226.61.26]) by mx.zoho.in with SMTPS id 1700943996154743.8918060401706; Sun, 26 Nov 2023 01:56:36 +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 4/4] nfc: llcp_sock_sendmsg: Reformat code to make the smaller block indented Date: Sun, 26 Nov 2023 01:56:19 +0530 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ZohoMailClient: External X-Patchwork-Delegate: kuba@kernel.org The block for datagram sending is a significantly bigger chunk of the function compared to the other scenario. Thus, put the significantly smaller block inside the if-block. Signed-off-by: Siddh Raman Pant --- net/nfc/llcp_sock.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c index 603f2219b62f..3f1a39e54aa1 100644 --- a/net/nfc/llcp_sock.c +++ b/net/nfc/llcp_sock.c @@ -795,34 +795,32 @@ static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg, return -ENODEV; } - if (sk->sk_type == SOCK_DGRAM) { - if (sk->sk_state != LLCP_BOUND) { - release_sock(sk); - return -ENOTCONN; - } + if (sk->sk_type != SOCK_DGRAM) { + release_sock(sk); - DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, - msg->msg_name); + if (sk->sk_state != LLCP_CONNECTED) + return -ENOTCONN; - if (msg->msg_namelen < sizeof(*addr)) { - release_sock(sk); - return -EINVAL; - } + return nfc_llcp_send_i_frame(llcp_sock, msg, len); + } + if (sk->sk_state != LLCP_BOUND) { release_sock(sk); - - return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap, - msg, len); + return -ENOTCONN; } - if (sk->sk_state != LLCP_CONNECTED) { + DECLARE_SOCKADDR(struct sockaddr_nfc_llcp *, addr, msg->msg_name); + + if (msg->msg_namelen < sizeof(*addr)) { release_sock(sk); - return -ENOTCONN; + return -EINVAL; } release_sock(sk); - return nfc_llcp_send_i_frame(llcp_sock, msg, len); + return nfc_llcp_send_ui_frame(llcp_sock, addr->dsap, addr->ssap, + msg, len); + } static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg,