Skip to content

Commit a97e50c

Browse files
borkmanndavem330
authored andcommitted
socket, bpf: fix sk_filter use after free in sk_clone_lock
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 278571b ("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: 278571b ("net: filter: simplify socket charging") Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c64c0b3 commit a97e50c

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

net/core/sock.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,12 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
15441544
is_charged = sk_filter_charge(newsk, filter);
15451545

15461546
if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
1547+
/* We need to make sure that we don't uncharge the new
1548+
* socket if we couldn't charge it in the first place
1549+
* as otherwise we uncharge the parent's filter.
1550+
*/
1551+
if (!is_charged)
1552+
RCU_INIT_POINTER(newsk->sk_filter, NULL);
15471553
sk_free_unlock_clone(newsk);
15481554
newsk = NULL;
15491555
goto out;

0 commit comments

Comments
 (0)