From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
To: Siddh Raman Pant <code@siddh.me>,
davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com
Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
syzkaller-bugs@googlegroups.com,
syzbot+bbe84a4010eeea00982d@syzkaller.appspotmail.com
Subject: Re: [syzbot] [net?] [nfc?] KASAN: slab-use-after-free Read in nfc_alloc_send_skb
Date: Fri, 17 Nov 2023 13:48:56 +0100 [thread overview]
Message-ID: <bdbb321b-64e4-4e21-bcf8-e1d201f0a5dc@linaro.org> (raw)
In-Reply-To: <7824cf85-178f-4fca-8058-b9a1f49d3113@siddh.me>
On 16/11/2023 17:55, Siddh Raman Pant wrote:
> TLDR: Different stages of 1 and 2 can race with each other causing UAF.
>
> 1. llcp_sock_sendmsg -> nfc_llcp_send_ui_frame -> loop call (nfc_alloc_send_skb(nfc_dev))
>
> 2. virtual_ncidev_close -> [... -> nfc_llcp_socket_release -> ...] -> [... -> nfc_free_device]
>
> ---
>
> Hi,
>
> I've been trying to fix this bug for some time but ending up getting
> stuck every now and then. If someone could give more inputs or fix it,
> it will be really helpful.
>
> This bug is due to racing between sendmsg and freeing of nfc_dev.
>
> 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.
> (1.1) Checking for LLCP_CLOSED in llcp_sock_sendmsg() does make
> the racing less likely. For -smp 6 it did not trigger on
> my PC, leading me to naively think that was the solution
> until syzbot told me quite some time later that it isn't.
>
> (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).
> (2.1) By this I mean just moving the nfc_dev access from
> nfc_alloc_send_skb to inside this function, be it
> inside or outside the loop, naturally doesn't work.
>
> When an nfc_dev is freed and we happened to get headroom and tailroom,
> PDU skb seems to be not allocated and ENXIO is returned.
>
> I tried to look at other code in net subsystem to get an idea how other
> places handle it, but accessing device later in the codepath does not
> seem to not be a norm. So I am starting to think some refactoring of the
> locking logic may be needed (or maybe RCU protect headroom and tailroom?).
>
> I don't know if I'm correct, but anyways where does one start?
Any checks would need to have proper locking. Or at least barriers...
Adding checks without locks usually does not solve race conditions.
Other start is proper ref counting, so the structures are not released
too early. We have several bugs like this in NFC before, so you can take
a look at their fixes.
Best regards,
Krzysztof
next prev parent reply other threads:[~2023-11-17 12:49 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-09 8:36 [syzbot] [net?] [nfc?] KASAN: slab-use-after-free Read in nfc_alloc_send_skb syzbot
2023-11-13 12:04 ` Siddh Raman Pant
2023-11-13 13:33 ` syzbot
2023-11-13 12:43 ` Siddh Raman Pant
2023-11-13 13:48 ` syzbot
2023-11-14 12:06 ` Siddh Raman Pant
2023-11-14 12:31 ` syzbot
2023-11-16 16:55 ` Siddh Raman Pant
2023-11-17 12:48 ` Krzysztof Kozlowski [this message]
2023-11-17 13:17 ` Siddh Raman Pant
2023-11-25 17:17 ` Siddh Raman Pant
2023-11-25 17:33 ` syzbot
2023-11-25 18:18 ` Siddh Raman Pant
2023-11-25 18:54 ` syzbot
2023-11-25 19:06 ` Siddh Raman Pant
2023-11-25 19:45 ` syzbot
2023-12-02 14:12 ` Siddh Raman Pant
2023-12-02 14:37 ` syzbot
2023-12-02 14:14 ` Siddh Raman Pant
2023-12-02 14:56 ` syzbot
2023-12-09 9:27 ` Siddh Raman Pant
2023-12-09 9:44 ` syzbot
2023-12-09 9:55 ` Siddh Raman Pant
2023-12-09 10:20 ` syzbot
2023-12-09 10:39 ` Siddh Raman Pant
2023-12-09 11:03 ` syzbot
2023-12-11 8:44 ` Paolo Abeni
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=bdbb321b-64e4-4e21-bcf8-e1d201f0a5dc@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 \
--cc=syzkaller-bugs@googlegroups.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;
as well as URLs for NNTP newsgroup(s).