diff mbox series

[bpf,4/9] bpf: Acquire map uref in .init_seq_private for sock{map,hash} iterator

Message ID 20220806074019.2756957-5-houtao@huaweicloud.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series fixes for bpf map iterator | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-16
bpf/vmtest-bpf-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 4 maintainers not CCed: edumazet@google.com jakub@cloudflare.com pabeni@redhat.com netdev@vger.kernel.org
netdev/build_clang success Errors and warnings before: 5 this patch: 5
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 2
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 50 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Hou Tao Aug. 6, 2022, 7:40 a.m. UTC
From: Hou Tao <houtao1@huawei.com>

During bpf(BPF_LINK_CREATE), sock_map_iter_attach_target() has already
acquired a map uref, but the uref may be released by bpf_link_release()
during th reading of map iterator.

Fixing it by acquiring an extra map uref in .init_seq_private and
releasing it in .fini_seq_private.

Fixes: 0365351524d7 ("net: Allow iterating sockmap and sockhash")
Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 net/core/sock_map.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

Comments

Yonghong Song Aug. 8, 2022, 2:55 p.m. UTC | #1
On 8/6/22 12:40 AM, Hou Tao wrote:
> From: Hou Tao <houtao1@huawei.com>
> 
> During bpf(BPF_LINK_CREATE), sock_map_iter_attach_target() has already
> acquired a map uref, but the uref may be released by bpf_link_release()
> during th reading of map iterator.
> 
> Fixing it by acquiring an extra map uref in .init_seq_private and
> releasing it in .fini_seq_private.
> 
> Fixes: 0365351524d7 ("net: Allow iterating sockmap and sockhash")
> Signed-off-by: Hou Tao <houtao1@huawei.com>

See my previous reply for some wording issue.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   net/core/sock_map.c | 20 +++++++++++++++++++-
>   1 file changed, 19 insertions(+), 1 deletion(-)
> 
> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index 028813dfecb0..9a9fb9487d63 100644
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -783,13 +783,22 @@ static int sock_map_init_seq_private(void *priv_data,
>   {
>   	struct sock_map_seq_info *info = priv_data;
>   
> +	bpf_map_inc_with_uref(aux->map);
>   	info->map = aux->map;
>   	return 0;
>   }
>   
> +static void sock_map_fini_seq_private(void *priv_data)
> +{
> +	struct sock_map_seq_info *info = priv_data;
> +
> +	bpf_map_put_with_uref(info->map);
> +}
> +
>   static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
>   	.seq_ops		= &sock_map_seq_ops,
>   	.init_seq_private	= sock_map_init_seq_private,
> +	.fini_seq_private	= sock_map_fini_seq_private,
>   	.seq_priv_size		= sizeof(struct sock_map_seq_info),
>   };
>   
> @@ -1369,18 +1378,27 @@ static const struct seq_operations sock_hash_seq_ops = {
>   };
>   
>   static int sock_hash_init_seq_private(void *priv_data,
> -				     struct bpf_iter_aux_info *aux)
> +				      struct bpf_iter_aux_info *aux)
>   {
>   	struct sock_hash_seq_info *info = priv_data;
>   
> +	bpf_map_inc_with_uref(aux->map);
>   	info->map = aux->map;
>   	info->htab = container_of(aux->map, struct bpf_shtab, map);
>   	return 0;
>   }
>   
> +static void sock_hash_fini_seq_private(void *priv_data)
> +{
> +	struct sock_hash_seq_info *info = priv_data;
> +
> +	bpf_map_put_with_uref(info->map);
> +}
> +
>   static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
>   	.seq_ops		= &sock_hash_seq_ops,
>   	.init_seq_private	= sock_hash_init_seq_private,
> +	.fini_seq_private	= sock_hash_fini_seq_private,
>   	.seq_priv_size		= sizeof(struct sock_hash_seq_info),
>   };
>
diff mbox series

Patch

diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 028813dfecb0..9a9fb9487d63 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -783,13 +783,22 @@  static int sock_map_init_seq_private(void *priv_data,
 {
 	struct sock_map_seq_info *info = priv_data;
 
+	bpf_map_inc_with_uref(aux->map);
 	info->map = aux->map;
 	return 0;
 }
 
+static void sock_map_fini_seq_private(void *priv_data)
+{
+	struct sock_map_seq_info *info = priv_data;
+
+	bpf_map_put_with_uref(info->map);
+}
+
 static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
 	.seq_ops		= &sock_map_seq_ops,
 	.init_seq_private	= sock_map_init_seq_private,
+	.fini_seq_private	= sock_map_fini_seq_private,
 	.seq_priv_size		= sizeof(struct sock_map_seq_info),
 };
 
@@ -1369,18 +1378,27 @@  static const struct seq_operations sock_hash_seq_ops = {
 };
 
 static int sock_hash_init_seq_private(void *priv_data,
-				     struct bpf_iter_aux_info *aux)
+				      struct bpf_iter_aux_info *aux)
 {
 	struct sock_hash_seq_info *info = priv_data;
 
+	bpf_map_inc_with_uref(aux->map);
 	info->map = aux->map;
 	info->htab = container_of(aux->map, struct bpf_shtab, map);
 	return 0;
 }
 
+static void sock_hash_fini_seq_private(void *priv_data)
+{
+	struct sock_hash_seq_info *info = priv_data;
+
+	bpf_map_put_with_uref(info->map);
+}
+
 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
 	.seq_ops		= &sock_hash_seq_ops,
 	.init_seq_private	= sock_hash_init_seq_private,
+	.fini_seq_private	= sock_hash_fini_seq_private,
 	.seq_priv_size		= sizeof(struct sock_hash_seq_info),
 };