All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wang Yufen <wangyufen@huawei.com>
To: <ast@kernel.org>, <john.fastabend@gmail.com>,
	<daniel@iogearbox.net>, <jakub@cloudflare.com>,
	<lmb@cloudflare.com>, <davem@davemloft.net>, <kafai@fb.com>,
	<dsahern@kernel.org>, <kuba@kernel.org>, <songliubraving@fb.com>,
	<yhs@fb.com>, <kpsingh@kernel.org>
Cc: <netdev@vger.kernel.org>, <bpf@vger.kernel.org>,
	Wang Yufen <wangyufen@huawei.com>
Subject: [PATCH bpf-next] bpf, sockmap: Manual deletion of sockmap elements in user mode is not allowed
Date: Mon, 14 Mar 2022 20:44:32 +0800	[thread overview]
Message-ID: <20220314124432.3050394-1-wangyufen@huawei.com> (raw)

A tcp socket in a sockmap. If user invokes bpf_map_delete_elem to delete
the sockmap element, the tcp socket will switch to use the TCP protocol
stack to send and receive packets. The switching process may cause some
issues, such as if some msgs exist in the ingress queue and are cleared
by sk_psock_drop(), the packets are lost, and the tcp data is abnormal.

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
---
 include/uapi/linux/bpf.h | 3 +++
 kernel/bpf/syscall.c     | 2 ++
 net/core/sock_map.c      | 3 +++
 3 files changed, 8 insertions(+)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 4eebea830613..1dab090f271c 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1218,6 +1218,9 @@ enum {
 
 /* Create a map that is suitable to be an inner map with dynamic max entries */
 	BPF_F_INNER_MAP		= (1U << 12),
+
+/* This should only be used for bpf_map_delete_elem called by user. */
+	BPF_F_TCP_SOCKMAP	= (1U << 13),
 };
 
 /* Flags for BPF_PROG_QUERY. */
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index db402ebc5570..57aa98087322 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1232,7 +1232,9 @@ static int map_delete_elem(union bpf_attr *attr)
 
 	bpf_disable_instrumentation();
 	rcu_read_lock();
+	map->map_flags |= BPF_F_TCP_SOCKMAP;
 	err = map->ops->map_delete_elem(map, key);
+	map->map_flags &= ~BPF_F_TCP_SOCKMAP;
 	rcu_read_unlock();
 	bpf_enable_instrumentation();
 	maybe_wait_bpf_programs(map);
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 2d213c4011db..5b90a35d1d23 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -914,6 +914,9 @@ static int sock_hash_delete_elem(struct bpf_map *map, void *key)
 	struct bpf_shtab_elem *elem;
 	int ret = -ENOENT;
 
+	if (map->map_flags & BPF_F_TCP_SOCKMAP)
+		return -EOPNOTSUPP;
+
 	hash = sock_hash_bucket_hash(key, key_size);
 	bucket = sock_hash_select_bucket(htab, hash);
 
-- 
2.25.1


             reply	other threads:[~2022-03-14 12:34 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-14 12:44 Wang Yufen [this message]
2022-03-14 15:30 ` [PATCH bpf-next] bpf, sockmap: Manual deletion of sockmap elements in user mode is not allowed Jakub Sitnicki
2022-03-15  7:24   ` wangyufen
2022-03-15 12:12     ` Jakub Sitnicki
2022-03-15 16:25       ` Daniel Borkmann
     [not found]         ` <f5a45e95-bac2-e1be-2d7b-5e6d55f9b408@huawei.com>
2022-03-16  5:23           ` John Fastabend
2022-03-16 14:57           ` Jakub Sitnicki
2022-03-16  0:36       ` Cong Wang
2022-03-16  3:25       ` wangyufen

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=20220314124432.3050394-1-wangyufen@huawei.com \
    --to=wangyufen@huawei.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=jakub@cloudflare.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lmb@cloudflare.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.