All of lore.kernel.org
 help / color / mirror / Atom feed
* [bpf-next PATCH 0/3] bpf: Add sk_msg helpers
@ 2020-05-13 19:23 John Fastabend
  2020-05-13 19:23 ` [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg John Fastabend
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: John Fastabend @ 2020-05-13 19:23 UTC (permalink / raw)
  To: ast, daniel; +Cc: lmb, bpf, john.fastabend, jakub, netdev

This series adds helpers for sk_msg program type.

Add a set of helpers that makes sk_msg more useful. Notable add the
*current_task* getters and the probe_* routines guarded by a capabilities
check for CAP_SYS_ADMIN.

 BPF_FUNC_perf_event_output
 BPF_FUNC_get_current_uid_gid
 BPF_FUNC_get_current_pid_tgid
 BPF_FUNC_get_current_cgroup_id
 BPF_FUNC_get_current_ancestor_cgroup_id
 BPF_FUNC_get_cgroup_classid

 BPF_FUNC_get_current_task
 BPF_FUNC_current_task_under_cgroup
 BPF_FUNC_probe_read_user
 BPF_FUNC_probe_read_kernel
 BPF_FUNC_probe_read
 BPF_FUNC_probe_read_user_str
 BPF_FUNC_probe_read_kernel_str
 BPF_FUNC_probe_read_str

 BPF_FUNC_sk_storage_get
 BPF_FUNC_sk_storage_delete

I have an RFC test for sk_storage_* patch that is pending merge of
other sockmap_test patches.

---

John Fastabend (3):
      bpf: sk_msg add some generic helpers that may be useful from sk_msg
      bpf: sk_msg helpers for probe_* and *current_task*
      bpf: sk_msg add get socket storage helpers


 include/uapi/linux/bpf.h |    2 +
 kernel/trace/bpf_trace.c |   16 ++++++-----
 net/core/filter.c        |   65 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 8 deletions(-)

--
Signature

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg
  2020-05-13 19:23 [bpf-next PATCH 0/3] bpf: Add sk_msg helpers John Fastabend
@ 2020-05-13 19:23 ` John Fastabend
  2020-05-14  6:58   ` Yonghong Song
  2020-05-13 19:24 ` [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task* John Fastabend
  2020-05-13 19:24 ` [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers John Fastabend
  2 siblings, 1 reply; 9+ messages in thread
From: John Fastabend @ 2020-05-13 19:23 UTC (permalink / raw)
  To: ast, daniel; +Cc: lmb, bpf, john.fastabend, jakub, netdev

Add these generic helpers that may be useful to use from sk_msg programs.
The helpers do not depend on ctx so we can simply add them here,

 BPF_FUNC_perf_event_output
 BPF_FUNC_get_current_uid_gid
 BPF_FUNC_get_current_pid_tgid
 BPF_FUNC_get_current_comm
 BPF_FUNC_get_current_cgroup_id
 BPF_FUNC_get_current_ancestor_cgroup_id
 BPF_FUNC_get_cgroup_classid

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 net/core/filter.c |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/net/core/filter.c b/net/core/filter.c
index da06349..45b4a16 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -6380,6 +6380,22 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_msg_push_data_proto;
 	case BPF_FUNC_msg_pop_data:
 		return &bpf_msg_pop_data_proto;
+	case BPF_FUNC_perf_event_output:
+		return &bpf_event_output_data_proto;
+	case BPF_FUNC_get_current_uid_gid:
+		return &bpf_get_current_uid_gid_proto;
+	case BPF_FUNC_get_current_pid_tgid:
+		return &bpf_get_current_pid_tgid_proto;
+#ifdef CONFIG_CGROUPS
+	case BPF_FUNC_get_current_cgroup_id:
+		return &bpf_get_current_cgroup_id_proto;
+	case BPF_FUNC_get_current_ancestor_cgroup_id:
+		return &bpf_get_current_ancestor_cgroup_id_proto;
+#endif
+#ifdef CONFIG_CGROUP_NET_CLASSID
+	case BPF_FUNC_get_cgroup_classid:
+		return &bpf_get_cgroup_classid_curr_proto;
+#endif
 	default:
 		return bpf_base_func_proto(func_id);
 	}


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task*
  2020-05-13 19:23 [bpf-next PATCH 0/3] bpf: Add sk_msg helpers John Fastabend
  2020-05-13 19:23 ` [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg John Fastabend
@ 2020-05-13 19:24 ` John Fastabend
  2020-05-14  7:21   ` Yonghong Song
  2020-05-14  8:02   ` Daniel Borkmann
  2020-05-13 19:24 ` [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers John Fastabend
  2 siblings, 2 replies; 9+ messages in thread
From: John Fastabend @ 2020-05-13 19:24 UTC (permalink / raw)
  To: ast, daniel; +Cc: lmb, bpf, john.fastabend, jakub, netdev

Often it is useful when applying policy to know something about the
task. If the administrator has CAP_SYS_ADMIN rights then they can
use kprobe + sk_msg and link the two programs together to accomplish
this. However, this is a bit clunky and also means we have to call
sk_msg program and kprobe program when we could just use a single
program and avoid passing metadata through sk_msg/skb, socket, etc.

To accomplish this add probe_* helpers to sk_msg programs guarded
by a CAP_SYS_ADMIN check. New supported helpers are the following,

 BPF_FUNC_get_current_task
 BPF_FUNC_current_task_under_cgroup
 BPF_FUNC_probe_read_user
 BPF_FUNC_probe_read_kernel
 BPF_FUNC_probe_read
 BPF_FUNC_probe_read_user_str
 BPF_FUNC_probe_read_kernel_str
 BPF_FUNC_probe_read_str

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 kernel/trace/bpf_trace.c |   16 ++++++++--------
 net/core/filter.c        |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index d961428..abe6721 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -147,7 +147,7 @@ BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
 	return ret;
 }
 
-static const struct bpf_func_proto bpf_probe_read_user_proto = {
+const struct bpf_func_proto bpf_probe_read_user_proto = {
 	.func		= bpf_probe_read_user,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -167,7 +167,7 @@ BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
 	return ret;
 }
 
-static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
+const struct bpf_func_proto bpf_probe_read_user_str_proto = {
 	.func		= bpf_probe_read_user_str,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -198,7 +198,7 @@ BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
 }
 
-static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
+const struct bpf_func_proto bpf_probe_read_kernel_proto = {
 	.func		= bpf_probe_read_kernel,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -213,7 +213,7 @@ BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
 }
 
-static const struct bpf_func_proto bpf_probe_read_compat_proto = {
+const struct bpf_func_proto bpf_probe_read_compat_proto = {
 	.func		= bpf_probe_read_compat,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -253,7 +253,7 @@ BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
 }
 
-static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
+const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
 	.func		= bpf_probe_read_kernel_str,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -268,7 +268,7 @@ BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
 }
 
-static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
+const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
 	.func		= bpf_probe_read_compat_str,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -874,7 +874,7 @@ BPF_CALL_0(bpf_get_current_task)
 	return (long) current;
 }
 
-static const struct bpf_func_proto bpf_get_current_task_proto = {
+const struct bpf_func_proto bpf_get_current_task_proto = {
 	.func		= bpf_get_current_task,
 	.gpl_only	= true,
 	.ret_type	= RET_INTEGER,
@@ -895,7 +895,7 @@ BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
 	return task_under_cgroup_hierarchy(current, cgrp);
 }
 
-static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
+const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
 	.func           = bpf_current_task_under_cgroup,
 	.gpl_only       = false,
 	.ret_type       = RET_INTEGER,
diff --git a/net/core/filter.c b/net/core/filter.c
index 45b4a16..d1c4739 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -6362,6 +6362,15 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
 
+const struct bpf_func_proto bpf_current_task_under_cgroup_proto __weak;
+const struct bpf_func_proto bpf_get_current_task_proto __weak;
+const struct bpf_func_proto bpf_probe_read_user_proto __weak;
+const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
+const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
+const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
+const struct bpf_func_proto bpf_probe_read_compat_proto __weak;
+const struct bpf_func_proto bpf_probe_read_compat_str_proto __weak;
+
 static const struct bpf_func_proto *
 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 {
@@ -6397,6 +6406,31 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_get_cgroup_classid_curr_proto;
 #endif
 	default:
+		break;
+	}
+
+	if (!capable(CAP_SYS_ADMIN))
+		return bpf_base_func_proto(func_id);
+
+	/* All helpers below are for CAP_SYS_ADMIN only */
+	switch (func_id) {
+	case BPF_FUNC_get_current_task:
+		return &bpf_get_current_task_proto;
+	case BPF_FUNC_current_task_under_cgroup:
+		return &bpf_current_task_under_cgroup_proto;
+	case BPF_FUNC_probe_read_user:
+		return &bpf_probe_read_user_proto;
+	case BPF_FUNC_probe_read_kernel:
+		return &bpf_probe_read_kernel_proto;
+	case BPF_FUNC_probe_read:
+		return &bpf_probe_read_compat_proto;
+	case BPF_FUNC_probe_read_user_str:
+		return &bpf_probe_read_user_str_proto;
+	case BPF_FUNC_probe_read_kernel_str:
+		return &bpf_probe_read_kernel_str_proto;
+	case BPF_FUNC_probe_read_str:
+		return &bpf_probe_read_compat_str_proto;
+	default:
 		return bpf_base_func_proto(func_id);
 	}
 }


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers
  2020-05-13 19:23 [bpf-next PATCH 0/3] bpf: Add sk_msg helpers John Fastabend
  2020-05-13 19:23 ` [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg John Fastabend
  2020-05-13 19:24 ` [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task* John Fastabend
@ 2020-05-13 19:24 ` John Fastabend
  2020-05-14  7:26   ` Yonghong Song
  2 siblings, 1 reply; 9+ messages in thread
From: John Fastabend @ 2020-05-13 19:24 UTC (permalink / raw)
  To: ast, daniel; +Cc: lmb, bpf, john.fastabend, jakub, netdev

Add helpers to use local socket storage.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 include/uapi/linux/bpf.h |    2 ++
 net/core/filter.c        |   15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index bfb31c1..3ca7cfd 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3607,6 +3607,8 @@ struct sk_msg_md {
 	__u32 remote_port;	/* Stored in network byte order */
 	__u32 local_port;	/* stored in host byte order */
 	__u32 size;		/* Total size of sk_msg */
+
+	__bpf_md_ptr(struct bpf_sock *, sk); /* current socket */
 };
 
 struct sk_reuseport_md {
diff --git a/net/core/filter.c b/net/core/filter.c
index d1c4739..c42adc8 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -6395,6 +6395,10 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_get_current_uid_gid_proto;
 	case BPF_FUNC_get_current_pid_tgid:
 		return &bpf_get_current_pid_tgid_proto;
+	case BPF_FUNC_sk_storage_get:
+		return &bpf_sk_storage_get_proto;
+	case BPF_FUNC_sk_storage_delete:
+		return &bpf_sk_storage_delete_proto;
 #ifdef CONFIG_CGROUPS
 	case BPF_FUNC_get_current_cgroup_id:
 		return &bpf_get_current_cgroup_id_proto;
@@ -7243,6 +7247,11 @@ static bool sk_msg_is_valid_access(int off, int size,
 		if (size != sizeof(__u64))
 			return false;
 		break;
+	case offsetof(struct sk_msg_md, sk):
+		if (size != sizeof(__u64))
+			return false;
+		info->reg_type = PTR_TO_SOCKET;
+		break;
 	case bpf_ctx_range(struct sk_msg_md, family):
 	case bpf_ctx_range(struct sk_msg_md, remote_ip4):
 	case bpf_ctx_range(struct sk_msg_md, local_ip4):
@@ -8577,6 +8586,12 @@ static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
 				      si->dst_reg, si->src_reg,
 				      offsetof(struct sk_msg_sg, size));
 		break;
+
+	case offsetof(struct sk_msg_md, sk):
+		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
+				      si->dst_reg, si->src_reg,
+				      offsetof(struct sk_msg, sk));
+		break;
 	}
 
 	return insn - insn_buf;


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg
  2020-05-13 19:23 ` [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg John Fastabend
@ 2020-05-14  6:58   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-05-14  6:58 UTC (permalink / raw)
  To: John Fastabend, ast, daniel; +Cc: lmb, bpf, jakub, netdev



On 5/13/20 12:23 PM, John Fastabend wrote:
> Add these generic helpers that may be useful to use from sk_msg programs.
> The helpers do not depend on ctx so we can simply add them here,
> 
>   BPF_FUNC_perf_event_output
>   BPF_FUNC_get_current_uid_gid
>   BPF_FUNC_get_current_pid_tgid
>   BPF_FUNC_get_current_comm
>   BPF_FUNC_get_current_cgroup_id
>   BPF_FUNC_get_current_ancestor_cgroup_id
>   BPF_FUNC_get_cgroup_classid
> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task*
  2020-05-13 19:24 ` [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task* John Fastabend
@ 2020-05-14  7:21   ` Yonghong Song
  2020-05-14 13:30     ` John Fastabend
  2020-05-14  8:02   ` Daniel Borkmann
  1 sibling, 1 reply; 9+ messages in thread
From: Yonghong Song @ 2020-05-14  7:21 UTC (permalink / raw)
  To: John Fastabend, ast, daniel; +Cc: lmb, bpf, jakub, netdev



On 5/13/20 12:24 PM, John Fastabend wrote:
> Often it is useful when applying policy to know something about the
> task. If the administrator has CAP_SYS_ADMIN rights then they can
> use kprobe + sk_msg and link the two programs together to accomplish
> this. However, this is a bit clunky and also means we have to call
> sk_msg program and kprobe program when we could just use a single
> program and avoid passing metadata through sk_msg/skb, socket, etc.
> 
> To accomplish this add probe_* helpers to sk_msg programs guarded
> by a CAP_SYS_ADMIN check. New supported helpers are the following,
> 
>   BPF_FUNC_get_current_task
>   BPF_FUNC_current_task_under_cgroup
>   BPF_FUNC_probe_read_user
>   BPF_FUNC_probe_read_kernel
>   BPF_FUNC_probe_read
>   BPF_FUNC_probe_read_user_str
>   BPF_FUNC_probe_read_kernel_str
>   BPF_FUNC_probe_read_str

I think this is a good idea. But this will require bpf program
to be GPLed, probably it will be okay. Currently, for capabilities,
it is CAP_SYS_ADMIN now, in the future, it may be CAP_PERFMON.

Also, do we want to remove BPF_FUNC_probe_read and
BPF_FUNC_probe_read_str from the list? Since we
introduce helpers to new program types, we can deprecate
these two helpers right away.

The new helpers will be subject to new security lockdown
rules which may have impact on networking bpf programs
on particular setup.

> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> ---
>   kernel/trace/bpf_trace.c |   16 ++++++++--------
>   net/core/filter.c        |   34 ++++++++++++++++++++++++++++++++++
>   2 files changed, 42 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index d961428..abe6721 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -147,7 +147,7 @@ BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
>   	return ret;
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_user_proto = {
> +const struct bpf_func_proto bpf_probe_read_user_proto = {
>   	.func		= bpf_probe_read_user,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -167,7 +167,7 @@ BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
>   	return ret;
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
> +const struct bpf_func_proto bpf_probe_read_user_str_proto = {
>   	.func		= bpf_probe_read_user_str,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -198,7 +198,7 @@ BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
>   	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
> +const struct bpf_func_proto bpf_probe_read_kernel_proto = {
>   	.func		= bpf_probe_read_kernel,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -213,7 +213,7 @@ BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
>   	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, true);
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_compat_proto = {
> +const struct bpf_func_proto bpf_probe_read_compat_proto = {
>   	.func		= bpf_probe_read_compat,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -253,7 +253,7 @@ BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
>   	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
> +const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
>   	.func		= bpf_probe_read_kernel_str,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -268,7 +268,7 @@ BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
>   	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, true);
>   }
>   
> -static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
> +const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
>   	.func		= bpf_probe_read_compat_str,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -874,7 +874,7 @@ BPF_CALL_0(bpf_get_current_task)
>   	return (long) current;
>   }
>   
> -static const struct bpf_func_proto bpf_get_current_task_proto = {
> +const struct bpf_func_proto bpf_get_current_task_proto = {
>   	.func		= bpf_get_current_task,
>   	.gpl_only	= true,
>   	.ret_type	= RET_INTEGER,
> @@ -895,7 +895,7 @@ BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
>   	return task_under_cgroup_hierarchy(current, cgrp);
>   }
>   
> -static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
> +const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
>   	.func           = bpf_current_task_under_cgroup,
>   	.gpl_only       = false,
>   	.ret_type       = RET_INTEGER,
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 45b4a16..d1c4739 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -6362,6 +6362,15 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>   const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
>   const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
>   
> +const struct bpf_func_proto bpf_current_task_under_cgroup_proto __weak;
> +const struct bpf_func_proto bpf_get_current_task_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_user_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_compat_proto __weak;
> +const struct bpf_func_proto bpf_probe_read_compat_str_proto __weak;
> +
>   static const struct bpf_func_proto *
>   sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>   {
> @@ -6397,6 +6406,31 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>   		return &bpf_get_cgroup_classid_curr_proto;
>   #endif
>   	default:
> +		break;
> +	}
> +
> +	if (!capable(CAP_SYS_ADMIN))
> +		return bpf_base_func_proto(func_id);
> +
> +	/* All helpers below are for CAP_SYS_ADMIN only */
> +	switch (func_id) {
> +	case BPF_FUNC_get_current_task:
> +		return &bpf_get_current_task_proto;
> +	case BPF_FUNC_current_task_under_cgroup:
> +		return &bpf_current_task_under_cgroup_proto;
> +	case BPF_FUNC_probe_read_user:
> +		return &bpf_probe_read_user_proto;
> +	case BPF_FUNC_probe_read_kernel:
> +		return &bpf_probe_read_kernel_proto;
> +	case BPF_FUNC_probe_read:
> +		return &bpf_probe_read_compat_proto;
> +	case BPF_FUNC_probe_read_user_str:
> +		return &bpf_probe_read_user_str_proto;
> +	case BPF_FUNC_probe_read_kernel_str:
> +		return &bpf_probe_read_kernel_str_proto;
> +	case BPF_FUNC_probe_read_str:
> +		return &bpf_probe_read_compat_str_proto;
> +	default:
>   		return bpf_base_func_proto(func_id);

If we can get a consensus here, I think we can even folding all
these bpf helpers (get_current_task, ..., probe_read_kernel_str)
to bpf_base_func_proto, so any bpf program types including
other networking types can use them.
Any concerns?

>   	}
>   }
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers
  2020-05-13 19:24 ` [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers John Fastabend
@ 2020-05-14  7:26   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2020-05-14  7:26 UTC (permalink / raw)
  To: John Fastabend, ast, daniel; +Cc: lmb, bpf, jakub, netdev



On 5/13/20 12:24 PM, John Fastabend wrote:
> Add helpers to use local socket storage.
> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> ---
>   include/uapi/linux/bpf.h |    2 ++
>   net/core/filter.c        |   15 +++++++++++++++
>   2 files changed, 17 insertions(+)
> 
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index bfb31c1..3ca7cfd 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3607,6 +3607,8 @@ struct sk_msg_md {
>   	__u32 remote_port;	/* Stored in network byte order */
>   	__u32 local_port;	/* stored in host byte order */
>   	__u32 size;		/* Total size of sk_msg */
> +
> +	__bpf_md_ptr(struct bpf_sock *, sk); /* current socket */
>   };

Sync changes to tools/include/uapi/linux/bpf.h?

For this patch and previous patches, it would be good we got some
selftests to exercise some newly-added helpers.

>   
>   struct sk_reuseport_md {
> diff --git a/net/core/filter.c b/net/core/filter.c
> index d1c4739..c42adc8 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -6395,6 +6395,10 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>   		return &bpf_get_current_uid_gid_proto;
>   	case BPF_FUNC_get_current_pid_tgid:
>   		return &bpf_get_current_pid_tgid_proto;
> +	case BPF_FUNC_sk_storage_get:
> +		return &bpf_sk_storage_get_proto;
> +	case BPF_FUNC_sk_storage_delete:
> +		return &bpf_sk_storage_delete_proto;
>   #ifdef CONFIG_CGROUPS
>   	case BPF_FUNC_get_current_cgroup_id:
>   		return &bpf_get_current_cgroup_id_proto;
> @@ -7243,6 +7247,11 @@ static bool sk_msg_is_valid_access(int off, int size,
>   		if (size != sizeof(__u64))
>   			return false;
>   		break;
> +	case offsetof(struct sk_msg_md, sk):
> +		if (size != sizeof(__u64))
> +			return false;
> +		info->reg_type = PTR_TO_SOCKET;
> +		break;
>   	case bpf_ctx_range(struct sk_msg_md, family):
>   	case bpf_ctx_range(struct sk_msg_md, remote_ip4):
>   	case bpf_ctx_range(struct sk_msg_md, local_ip4):
> @@ -8577,6 +8586,12 @@ static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
>   				      si->dst_reg, si->src_reg,
>   				      offsetof(struct sk_msg_sg, size));
>   		break;
> +
> +	case offsetof(struct sk_msg_md, sk):
> +		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
> +				      si->dst_reg, si->src_reg,
> +				      offsetof(struct sk_msg, sk));
> +		break;
>   	}
>   
>   	return insn - insn_buf;
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task*
  2020-05-13 19:24 ` [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task* John Fastabend
  2020-05-14  7:21   ` Yonghong Song
@ 2020-05-14  8:02   ` Daniel Borkmann
  1 sibling, 0 replies; 9+ messages in thread
From: Daniel Borkmann @ 2020-05-14  8:02 UTC (permalink / raw)
  To: John Fastabend, ast; +Cc: lmb, bpf, jakub, netdev

On 5/13/20 9:24 PM, John Fastabend wrote:
> Often it is useful when applying policy to know something about the
> task. If the administrator has CAP_SYS_ADMIN rights then they can
> use kprobe + sk_msg and link the two programs together to accomplish
> this. However, this is a bit clunky and also means we have to call
> sk_msg program and kprobe program when we could just use a single
> program and avoid passing metadata through sk_msg/skb, socket, etc.
> 
> To accomplish this add probe_* helpers to sk_msg programs guarded
> by a CAP_SYS_ADMIN check. New supported helpers are the following,
> 
>   BPF_FUNC_get_current_task
>   BPF_FUNC_current_task_under_cgroup
>   BPF_FUNC_probe_read_user
>   BPF_FUNC_probe_read_kernel
>   BPF_FUNC_probe_read
>   BPF_FUNC_probe_read_user_str
>   BPF_FUNC_probe_read_kernel_str
>   BPF_FUNC_probe_read_str

Given the current discussion in the other thread with Linus et al, please
don't add more users for BPF_FUNC_probe_read and BPF_FUNC_probe_read_str
as I'm cooking up a patch to disable them on non-x86, and cleanups from
Christoph would make them less efficient than the *_user/_kernel{,_str}()
versions anyway, so lets only add the latter.

Thanks,
Daniel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task*
  2020-05-14  7:21   ` Yonghong Song
@ 2020-05-14 13:30     ` John Fastabend
  0 siblings, 0 replies; 9+ messages in thread
From: John Fastabend @ 2020-05-14 13:30 UTC (permalink / raw)
  To: Yonghong Song, John Fastabend, ast, daniel; +Cc: lmb, bpf, jakub, netdev

Yonghong Song wrote:
> 
> 
> On 5/13/20 12:24 PM, John Fastabend wrote:
> > Often it is useful when applying policy to know something about the
> > task. If the administrator has CAP_SYS_ADMIN rights then they can
> > use kprobe + sk_msg and link the two programs together to accomplish
> > this. However, this is a bit clunky and also means we have to call
> > sk_msg program and kprobe program when we could just use a single
> > program and avoid passing metadata through sk_msg/skb, socket, etc.
> > 
> > To accomplish this add probe_* helpers to sk_msg programs guarded
> > by a CAP_SYS_ADMIN check. New supported helpers are the following,
> > 
> >   BPF_FUNC_get_current_task
> >   BPF_FUNC_current_task_under_cgroup
> >   BPF_FUNC_probe_read_user
> >   BPF_FUNC_probe_read_kernel
> >   BPF_FUNC_probe_read
> >   BPF_FUNC_probe_read_user_str
> >   BPF_FUNC_probe_read_kernel_str
> >   BPF_FUNC_probe_read_str
> 
> I think this is a good idea. But this will require bpf program
> to be GPLed, probably it will be okay. Currently, for capabilities,
> it is CAP_SYS_ADMIN now, in the future, it may be CAP_PERFMON.

Right.

> 
> Also, do we want to remove BPF_FUNC_probe_read and
> BPF_FUNC_probe_read_str from the list? Since we
> introduce helpers to new program types, we can deprecate
> these two helpers right away.

Removed, Daniel had the same comment.

> 
> The new helpers will be subject to new security lockdown
> rules which may have impact on networking bpf programs
> on particular setup.

But only if these helpers are used. If not everything should
be the same I think.

> 
> > 
> > Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> > ---

[...]

> > @@ -6397,6 +6406,31 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
> >   		return &bpf_get_cgroup_classid_curr_proto;
> >   #endif
> >   	default:
> > +		break;
> > +	}
> > +
> > +	if (!capable(CAP_SYS_ADMIN))
> > +		return bpf_base_func_proto(func_id);
> > +
> > +	/* All helpers below are for CAP_SYS_ADMIN only */
> > +	switch (func_id) {
> > +	case BPF_FUNC_get_current_task:
> > +		return &bpf_get_current_task_proto;
> > +	case BPF_FUNC_current_task_under_cgroup:
> > +		return &bpf_current_task_under_cgroup_proto;
> > +	case BPF_FUNC_probe_read_user:
> > +		return &bpf_probe_read_user_proto;
> > +	case BPF_FUNC_probe_read_kernel:
> > +		return &bpf_probe_read_kernel_proto;
> > +	case BPF_FUNC_probe_read:
> > +		return &bpf_probe_read_compat_proto;
> > +	case BPF_FUNC_probe_read_user_str:
> > +		return &bpf_probe_read_user_str_proto;
> > +	case BPF_FUNC_probe_read_kernel_str:
> > +		return &bpf_probe_read_kernel_str_proto;
> > +	case BPF_FUNC_probe_read_str:
> > +		return &bpf_probe_read_compat_str_proto;
> > +	default:
> >   		return bpf_base_func_proto(func_id);
> 
> If we can get a consensus here, I think we can even folding all
> these bpf helpers (get_current_task, ..., probe_read_kernel_str)
> to bpf_base_func_proto, so any bpf program types including
> other networking types can use them.
> Any concerns?
> 

Nothing comes to mind. I'm OK to move them into base if folks
agree its useful there. I was putting them where I have a known
use case at the moment but doesn't bother me to make them more
widely available.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-05-14 13:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 19:23 [bpf-next PATCH 0/3] bpf: Add sk_msg helpers John Fastabend
2020-05-13 19:23 ` [bpf-next PATCH 1/3] bpf: sk_msg add some generic helpers that may be useful from sk_msg John Fastabend
2020-05-14  6:58   ` Yonghong Song
2020-05-13 19:24 ` [bpf-next PATCH 2/3] bpf: sk_msg helpers for probe_* and *current_task* John Fastabend
2020-05-14  7:21   ` Yonghong Song
2020-05-14 13:30     ` John Fastabend
2020-05-14  8:02   ` Daniel Borkmann
2020-05-13 19:24 ` [bpf-next PATCH 3/3] bpf: sk_msg add get socket storage helpers John Fastabend
2020-05-14  7:26   ` Yonghong Song

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.