From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
To: Siddh Raman Pant <code@siddh.me>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com
Subject: Re: [PATCH 2/4] nfc: Protect access to nfc_dev in an llcp_sock with a rwlock
Date: Mon, 27 Nov 2023 11:38:16 +0100 [thread overview]
Message-ID: <0c35dd80-8062-4f1b-9127-8a5cb2deca40@linaro.org> (raw)
In-Reply-To: <7c198c2aa08b34045b8f9e0afe3d0b3bf5802180.1700943019.git.code@siddh.me>
On 25/11/2023 21:26, Siddh Raman Pant wrote:
> 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.
This path should have reference to nfc device: nfc_get_device(). Why is
this not sufficient?
>
> 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.
Sorry, I don't understand. What is encountered before freeing?
>
> 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 <code@siddh.me>
> ---
> 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;
I dislike the idea of introducing the third (!!!) lock here. It looks
like a bandaid for this one particular problem.
> 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");
exist?
Best regards,
Krzysztof
next prev parent reply other threads:[~2023-11-27 10:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-25 20:26 [PATCH 0/4] Fix UAF caused by racing datagram sending and freeing of nfc_dev Siddh Raman Pant
2023-11-25 20:26 ` [PATCH 1/4] nfc: Extract nfc_dev access from nfc_alloc_send_skb() into the callers Siddh Raman Pant
2023-11-27 10:10 ` Krzysztof Kozlowski
2023-12-02 13:31 ` Siddh Raman Pant
2023-11-25 20:26 ` [PATCH 2/4] nfc: Protect access to nfc_dev in an llcp_sock with a rwlock Siddh Raman Pant
2023-11-27 10:38 ` Krzysztof Kozlowski [this message]
2023-12-02 13:30 ` Siddh Raman Pant
2023-11-25 20:26 ` [PATCH 3/4] nfc: Do not send datagram if socket state isn't LLCP_BOUND Siddh Raman Pant
2023-11-27 10:54 ` Krzysztof Kozlowski
2023-11-25 20:26 ` [PATCH 4/4] nfc: llcp_sock_sendmsg: Reformat code to make the smaller block indented Siddh Raman Pant
2023-11-27 10:57 ` Krzysztof Kozlowski
2023-11-27 10:04 ` [PATCH 0/4] Fix UAF caused by racing datagram sending and freeing of nfc_dev Krzysztof Kozlowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0c35dd80-8062-4f1b-9127-8a5cb2deca40@linaro.org \
--to=krzysztof.kozlowski@linaro.org \
--cc=code@siddh.me \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®