From patchwork Mon Aug 31 04:03:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Elmously X-Patchwork-Id: 1354105 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BfxR35T8vz9sTK; Mon, 31 Aug 2020 14:04:15 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1kCb3D-0005OU-LI; Mon, 31 Aug 2020 04:04:11 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kCb2x-0005C7-GT for kernel-team@lists.ubuntu.com; Mon, 31 Aug 2020 04:03:55 +0000 Received: from mail-qv1-f71.google.com ([209.85.219.71]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kCb2x-0006KJ-52 for kernel-team@lists.ubuntu.com; Mon, 31 Aug 2020 04:03:55 +0000 Received: by mail-qv1-f71.google.com with SMTP id q4so2366086qvr.10 for ; Sun, 30 Aug 2020 21:03:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9cQV9biU03HzKPbPdZ0dSHfczrY87zku4RfhAdDSLhc=; b=hoqrMlOE6IGuGDaRLgD5Sjoagg5mgwcvkAoL65ofmvgvZsESEUCfx+jpUNrMLIDum0 wN7A3PgnjhsxoH6m4uWi/IPPpgTapcxmyHMcoB1jYiR7NmUfPmJ38SQ8LlKo6c7C5hqL OWNYSd5ElSy/8Kwvwsr+XxxjgjAs0XZPC2BVONH3vwDKSlAyD9AHWh+jxzTdAy6WR662 wRommMpY/UP0T859ALtqS+8tbuv0jRFeKbDqpu5nltf7QbhoIKnqW/ImVW4vazQpfMoG /ZjV4wZ62UL8N0TAKfH7EWNrHdlYBYPJ1UGCYbNUuHVXPNFLsLO3L/DFti4008fr5EXb b+Fg== X-Gm-Message-State: AOAM530pv/KXjqm2JQboRBjIkS1TEtrREQj4h8QNu5uWXlcdJgaulwTz cmzjBD7SFzmV1veFmGeV5jFr9hISViK9H5PGCJeXbD09rys/uspLnCyXOclMlNclpyPNi3k0JR7 gqFLjFoYXoUnpNSE0d4NpCtlhZ39sOdDMz+opeKZ1QA== X-Received: by 2002:a0c:f64a:: with SMTP id s10mr8431367qvm.196.1598846633890; Sun, 30 Aug 2020 21:03:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvpW4DZRDJ+gF6t+woIiLxAuuaqrCpkwBCorWwnJzgrNc+j1gdb4d6+llg2AkhySBJ3qHaDQ== X-Received: by 2002:a0c:f64a:: with SMTP id s10mr8431353qvm.196.1598846633565; Sun, 30 Aug 2020 21:03:53 -0700 (PDT) Received: from kbuntu2.fuzzbuzz.org (dhcp-24-53-242-104.cable.user.start.ca. [24.53.242.104]) by smtp.gmail.com with ESMTPSA id d9sm7779648qkj.83.2020.08.30.21.03.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Aug 2020 21:03:53 -0700 (PDT) From: Khalid Elmously To: kernel-team@lists.ubuntu.com Subject: [PATCH 12/13] bpf: Don't refcount LISTEN sockets in sk_assign() Date: Mon, 31 Aug 2020 00:03:32 -0400 Message-Id: <20200831040333.6058-13-khalid.elmously@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200831040333.6058-1-khalid.elmously@canonical.com> References: <20200831040333.6058-1-khalid.elmously@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Joe Stringer [ upstream commit 7ae215d23c12a939005f35d1848ca55b6109b9c0 ] Avoid taking a reference on listen sockets by checking the socket type in the sk_assign and in the corresponding skb_steal_sock() code in the the transport layer, and by ensuring that the prefetch free (sock_pfree) function uses the same logic to check whether the socket is refcounted. Suggested-by: Martin KaFai Lau Signed-off-by: Joe Stringer Signed-off-by: Alexei Starovoitov Acked-by: Martin KaFai Lau Link: https://lore.kernel.org/bpf/20200329225342.16317-4-joe@wand.net.nz Signed-off-by: Daniel Borkmann Signed-off-by: Khalid Elmously --- include/net/sock.h | 25 +++++++++++++++++-------- net/core/filter.c | 6 +++--- net/core/sock.c | 3 ++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index 6cb1f0efa01b..e13e9b7c34d1 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2492,6 +2492,21 @@ skb_sk_is_prefetched(struct sk_buff *skb) #endif /* CONFIG_INET */ } +/* This helper checks if a socket is a full socket, + * ie _not_ a timewait or request socket. + */ +static inline bool sk_fullsock(const struct sock *sk) +{ + return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV); +} + +static inline bool +sk_is_refcounted(struct sock *sk) +{ + /* Only full sockets have sk->sk_flags. */ + return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE); +} + /** * skb_steal_sock * @skb to steal the socket from @@ -2504,6 +2519,8 @@ skb_steal_sock(struct sk_buff *skb, bool *refcounted) struct sock *sk = skb->sk; *refcounted = true; + if (skb_sk_is_prefetched(skb)) + *refcounted = sk_is_refcounted(sk); skb->destructor = NULL; skb->sk = NULL; return sk; @@ -2512,14 +2529,6 @@ skb_steal_sock(struct sk_buff *skb, bool *refcounted) return NULL; } -/* This helper checks if a socket is a full socket, - * ie _not_ a timewait or request socket. - */ -static inline bool sk_fullsock(const struct sock *sk) -{ - return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV); -} - /* Checks if this SKB belongs to an HW offloaded socket * and whether any SW fallbacks are required based on dev. * Check decrypted mark in case skb_orphan() cleared socket. diff --git a/net/core/filter.c b/net/core/filter.c index 4033799dafd5..baaeab75327c 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5468,8 +5468,7 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { BPF_CALL_1(bpf_sk_release, struct sock *, sk) { - /* Only full sockets have sk->sk_flags. */ - if (!sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE)) + if (sk_is_refcounted(sk)) sock_gen_put(sk); return 0; } @@ -5995,7 +5994,8 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags) return -ENETUNREACH; if (unlikely(sk->sk_reuseport)) return -ESOCKTNOSUPPORT; - if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) + if (sk_is_refcounted(sk) && + unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) return -ENOENT; skb_orphan(skb); diff --git a/net/core/sock.c b/net/core/sock.c index 036ba43d044a..bcb7affcc1e0 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2071,7 +2071,8 @@ EXPORT_SYMBOL(sock_efree); #ifdef CONFIG_INET void sock_pfree(struct sk_buff *skb) { - sock_gen_put(skb->sk); + if (sk_is_refcounted(skb->sk)) + sock_gen_put(skb->sk); } EXPORT_SYMBOL(sock_pfree); #endif /* CONFIG_INET */