From: David Miller <davem@davemloft.net>
To: daniel@iogearbox.net
Cc: ast@kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH net] socket, bpf: fix sk_filter use after free in sk_clone_lock
Date: Wed, 22 Mar 2017 15:37:35 -0700 (PDT) [thread overview]
Message-ID: <20170322.153735.1284654162173125944.davem@davemloft.net> (raw)
In-Reply-To: <3016fb20a3666f0db138b85049f9000ecd33a1f6.1490184193.git.daniel@iogearbox.net>
From: Daniel Borkmann <daniel@iogearbox.net>
Date: Wed, 22 Mar 2017 13:08:08 +0100
> In sk_clone_lock(), we create a new socket and inherit most of the
> parent's members via sock_copy() which memcpy()'s various sections.
> Now, in case the parent socket had a BPF socket filter attached,
> then newsk->sk_filter points to the same instance as the original
> sk->sk_filter.
>
> sk_filter_charge() is then called on the newsk->sk_filter to take a
> reference and should that fail due to hitting max optmem, we bail
> out and release the newsk instance.
>
> The issue is that commit 278571baca2a ("net: filter: simplify socket
> charging") wrongly combined the dismantle path with the failure path
> of xfrm_sk_clone_policy(). This means, even when charging failed, we
> call sk_free_unlock_clone() on the newsk, which then still points to
> the same sk_filter as the original sk.
>
> Thus, sk_free_unlock_clone() calls into __sk_destruct() eventually
> where it tests for present sk_filter and calls sk_filter_uncharge()
> on it, which potentially lets sk_omem_alloc wrap around and releases
> the eBPF prog and sk_filter structure from the (still intact) parent.
>
> Fix it by making sure that when sk_filter_charge() failed, we reset
> newsk->sk_filter back to NULL before passing to sk_free_unlock_clone(),
> so that we don't mess with the parents sk_filter.
>
> Only if xfrm_sk_clone_policy() fails, we did reach the point where
> either the parent's filter was NULL and as a result newsk's as well
> or where we previously had a successful sk_filter_charge(), thus for
> that case, we do need sk_filter_uncharge() to release the prior taken
> reference on sk_filter.
>
> Fixes: 278571baca2a ("net: filter: simplify socket charging")
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> Acked-by: Alexei Starovoitov <ast@kernel.org>
Applied and queued up for -stable, thanks Daniel.
prev parent reply other threads:[~2017-03-22 22:37 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-22 12:08 [PATCH net] socket, bpf: fix sk_filter use after free in sk_clone_lock Daniel Borkmann
2017-03-22 22:37 ` David Miller [this message]
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=20170322.153735.1284654162173125944.davem@davemloft.net \
--to=davem@davemloft.net \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
/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).