netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 0/2] net: core: Two Helper function about socket information
@ 2017-02-02 20:59 Chenbo Feng
  2017-02-02 20:59 ` [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF Chenbo Feng
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Chenbo Feng @ 2017-02-02 20:59 UTC (permalink / raw)
  To: David S . Miller, Alexei Starovoitov, netdev
  Cc: Willem de Bruijn, Lorenzo Colitti, Chenbo Feng

Introduce two eBpf helper function to get the socket cookie and
socket uid for each packet. The helper function is useful when
the *sk field inside sk_buff is not empty.

Chenbo Feng (2):
  Add a helper function to get socket cookie in eBPF
  Add a eBPF helper function to retrieve socket uid

 include/linux/bpf.h       |  2 ++
 include/linux/sock_diag.h |  1 +
 include/uapi/linux/bpf.h  | 16 +++++++++++++++-
 net/core/filter.c         | 34 ++++++++++++++++++++++++++++++++++
 net/core/sock_diag.c      |  3 ++-
 5 files changed, 54 insertions(+), 2 deletions(-)

-- 
2.11.0.483.g087da7b7c-goog

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

* [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF
  2017-02-02 20:59 [PATCH net-next 0/2] net: core: Two Helper function about socket information Chenbo Feng
@ 2017-02-02 20:59 ` Chenbo Feng
  2017-02-02 21:23   ` Daniel Borkmann
  2017-02-02 20:59 ` [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid Chenbo Feng
  2017-02-03  0:13 ` [PATCH net-next 0/2] net: core: Two Helper function about socket information Alexei Starovoitov
  2 siblings, 1 reply; 14+ messages in thread
From: Chenbo Feng @ 2017-02-02 20:59 UTC (permalink / raw)
  To: David S . Miller, Alexei Starovoitov, netdev
  Cc: Willem de Bruijn, Lorenzo Colitti, Chenbo Feng, Chenbo Feng

From: Chenbo Feng <fengc@google.com>

Retrieve the socket cookie generated by sock_gen_cookie() from a sk_buff
with a known socket. Generates a new cookie if one was not yet set.If
the socket pointer inside sk_buff is NULL, 0 is returned. The helper
function coud be useful in monitoring per socket networking traffic
statistics and provide a unique socket identifier per namespace.

Signed-off-by: Chenbo Feng <chenbofeng.kernel@gmail.com>
---
 include/linux/bpf.h       |  1 +
 include/linux/sock_diag.h |  1 +
 include/uapi/linux/bpf.h  |  9 ++++++++-
 net/core/filter.c         | 15 +++++++++++++++
 net/core/sock_diag.c      |  3 ++-
 5 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 3ed1f3b1d594..3f2e0af28c6e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -349,6 +349,7 @@ extern const struct bpf_func_proto bpf_get_current_comm_proto;
 extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
 extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
 extern const struct bpf_func_proto bpf_get_stackid_proto;
+extern const struct bpf_func_proto bpf_get_socket_cookie_proto;
 
 /* Shared helpers among cBPF and eBPF. */
 void bpf_user_rnd_init_once(void);
diff --git a/include/linux/sock_diag.h b/include/linux/sock_diag.h
index a0596ca0e80a..a2f8109bb215 100644
--- a/include/linux/sock_diag.h
+++ b/include/linux/sock_diag.h
@@ -24,6 +24,7 @@ void sock_diag_unregister(const struct sock_diag_handler *h);
 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
 
+u64 sock_gen_cookie(struct sock *sk);
 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
 
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 0eb0e87dbe9f..62ee5fab08e5 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -430,6 +430,12 @@ union bpf_attr {
  *     @xdp_md: pointer to xdp_md
  *     @delta: An positive/negative integer to be added to xdp_md.data
  *     Return: 0 on success or negative on error
+ *
+ * u64 bpf_bpf_get_socket_cookie(skb)
+ *     Get the cookie for the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
+ *     field is missing inside sk_buff
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -476,7 +482,8 @@ union bpf_attr {
 	FN(set_hash_invalid),		\
 	FN(get_numa_node_id),		\
 	FN(skb_change_head),		\
-	FN(xdp_adjust_head),
+	FN(xdp_adjust_head),		\
+	FN(get_socket_cookie),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
diff --git a/net/core/filter.c b/net/core/filter.c
index 1969b3f118c1..913b14d3b484 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -26,6 +26,7 @@
 #include <linux/mm.h>
 #include <linux/fcntl.h>
 #include <linux/socket.h>
+#include <linux/sock_diag.h>
 #include <linux/in.h>
 #include <linux/inet.h>
 #include <linux/netdevice.h>
@@ -2597,6 +2598,18 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = {
 	.arg5_type	= ARG_CONST_STACK_SIZE,
 };
 
+BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
+{
+	return skb->sk ? sock_gen_cookie(skb->sk) : 0;
+}
+
+const struct bpf_func_proto bpf_get_socket_cookie_proto = {
+	.func           = bpf_get_socket_cookie,
+	.gpl_only       = false,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX,
+};
+
 static const struct bpf_func_proto *
 sk_filter_func_proto(enum bpf_func_id func_id)
 {
@@ -2620,6 +2633,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
 	case BPF_FUNC_trace_printk:
 		if (capable(CAP_SYS_ADMIN))
 			return bpf_get_trace_printk_proto();
+	case BPF_FUNC_get_socket_cookie:
+		return &bpf_get_socket_cookie_proto;
 	default:
 		return NULL;
 	}
diff --git a/net/core/sock_diag.c b/net/core/sock_diag.c
index 6b10573cc9fa..b07b0a784f33 100644
--- a/net/core/sock_diag.c
+++ b/net/core/sock_diag.c
@@ -19,7 +19,7 @@ static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
 static DEFINE_MUTEX(sock_diag_table_mutex);
 static struct workqueue_struct *broadcast_wq;
 
-static u64 sock_gen_cookie(struct sock *sk)
+u64 sock_gen_cookie(struct sock *sk)
 {
 	while (1) {
 		u64 res = atomic64_read(&sk->sk_cookie);
@@ -30,6 +30,7 @@ static u64 sock_gen_cookie(struct sock *sk)
 		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
 	}
 }
+EXPORT_SYMBOL_GPL(sock_gen_cookie);
 
 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
 {
-- 
2.11.0.483.g087da7b7c-goog

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

* [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-02 20:59 [PATCH net-next 0/2] net: core: Two Helper function about socket information Chenbo Feng
  2017-02-02 20:59 ` [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF Chenbo Feng
@ 2017-02-02 20:59 ` Chenbo Feng
  2017-02-02 21:32   ` Daniel Borkmann
  2017-02-03  0:13 ` [PATCH net-next 0/2] net: core: Two Helper function about socket information Alexei Starovoitov
  2 siblings, 1 reply; 14+ messages in thread
From: Chenbo Feng @ 2017-02-02 20:59 UTC (permalink / raw)
  To: David S . Miller, Alexei Starovoitov, netdev
  Cc: Willem de Bruijn, Lorenzo Colitti, Chenbo Feng, Chenbo Feng

From: Chenbo Feng <fengc@google.com>

Returns the owner uid of the socket inside a sk_buff. This is useful to
perform per-UID accounting of network traffic or per-UID packet
filtering.

Signed-off-by: Chenbo Feng <chenbofeng.kernel@gmail.com>
---
 include/linux/bpf.h      |  1 +
 include/uapi/linux/bpf.h |  9 ++++++++-
 net/core/filter.c        | 19 +++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 3f2e0af28c6e..c775ca4678b7 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -350,6 +350,7 @@ extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
 extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
 extern const struct bpf_func_proto bpf_get_stackid_proto;
 extern const struct bpf_func_proto bpf_get_socket_cookie_proto;
+extern const struct bpf_func_proto bpf_get_socket_uid_proto;
 
 /* Shared helpers among cBPF and eBPF. */
 void bpf_user_rnd_init_once(void);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62ee5fab08e5..dad4dc7aca49 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -436,6 +436,12 @@ union bpf_attr {
  *     @skb: pointer to skb
  *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
  *     field is missing inside sk_buff
+ *
+ * u32 bpf_get_socket_uid(skb)
+ *     Get the owner uid of the socket stored inside sk_buff.
+ *     @skb: pointer to skb
+ *     Return: uid of the socket owner on success or 0 if the socket pointer
+ *     inside sk_buff is NULL
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -483,7 +489,8 @@ union bpf_attr {
 	FN(get_numa_node_id),		\
 	FN(skb_change_head),		\
 	FN(xdp_adjust_head),		\
-	FN(get_socket_cookie),
+	FN(get_socket_cookie),	\
+	FN(get_socket_uid),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
diff --git a/net/core/filter.c b/net/core/filter.c
index 913b14d3b484..bc36ed72551f 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2610,6 +2610,23 @@ const struct bpf_func_proto bpf_get_socket_cookie_proto = {
 	.arg1_type      = ARG_PTR_TO_CTX,
 };
 
+BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
+{
+	struct sock *sk;
+	kuid_t kuid;
+
+	sk = skb->sk;
+	kuid = sock_net_uid(dev_net(skb->dev), sk);
+	return (u32)kuid.val;
+}
+
+const struct bpf_func_proto bpf_get_socket_uid_proto = {
+	.func           = bpf_get_socket_uid,
+	.gpl_only       = false,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_PTR_TO_CTX,
+};
+
 static const struct bpf_func_proto *
 sk_filter_func_proto(enum bpf_func_id func_id)
 {
@@ -2635,6 +2652,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
 			return bpf_get_trace_printk_proto();
 	case BPF_FUNC_get_socket_cookie:
 		return &bpf_get_socket_cookie_proto;
+	case BPF_FUNC_get_socket_uid:
+		return &bpf_get_socket_uid_proto;
 	default:
 		return NULL;
 	}
-- 
2.11.0.483.g087da7b7c-goog

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

* Re: [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF
  2017-02-02 20:59 ` [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF Chenbo Feng
@ 2017-02-02 21:23   ` Daniel Borkmann
  0 siblings, 0 replies; 14+ messages in thread
From: Daniel Borkmann @ 2017-02-02 21:23 UTC (permalink / raw)
  To: Chenbo Feng, David S . Miller, Alexei Starovoitov, netdev
  Cc: Willem de Bruijn, Lorenzo Colitti, Chenbo Feng

On 02/02/2017 09:59 PM, Chenbo Feng wrote:
> From: Chenbo Feng <fengc@google.com>
>
> Retrieve the socket cookie generated by sock_gen_cookie() from a sk_buff
> with a known socket. Generates a new cookie if one was not yet set.If
> the socket pointer inside sk_buff is NULL, 0 is returned. The helper
> function coud be useful in monitoring per socket networking traffic
> statistics and provide a unique socket identifier per namespace.
>
> Signed-off-by: Chenbo Feng <chenbofeng.kernel@gmail.com>
> ---
>   include/linux/bpf.h       |  1 +
>   include/linux/sock_diag.h |  1 +
>   include/uapi/linux/bpf.h  |  9 ++++++++-
>   net/core/filter.c         | 15 +++++++++++++++
>   net/core/sock_diag.c      |  3 ++-
>   5 files changed, 27 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 3ed1f3b1d594..3f2e0af28c6e 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -349,6 +349,7 @@ extern const struct bpf_func_proto bpf_get_current_comm_proto;
>   extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
>   extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
>   extern const struct bpf_func_proto bpf_get_stackid_proto;
> +extern const struct bpf_func_proto bpf_get_socket_cookie_proto;

I don't think this needs to be added here.

>   /* Shared helpers among cBPF and eBPF. */
>   void bpf_user_rnd_init_once(void);
> diff --git a/include/linux/sock_diag.h b/include/linux/sock_diag.h
> index a0596ca0e80a..a2f8109bb215 100644
> --- a/include/linux/sock_diag.h
> +++ b/include/linux/sock_diag.h
> @@ -24,6 +24,7 @@ void sock_diag_unregister(const struct sock_diag_handler *h);
>   void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
>   void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
>
> +u64 sock_gen_cookie(struct sock *sk);
>   int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
>   void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 0eb0e87dbe9f..62ee5fab08e5 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -430,6 +430,12 @@ union bpf_attr {
>    *     @xdp_md: pointer to xdp_md
>    *     @delta: An positive/negative integer to be added to xdp_md.data
>    *     Return: 0 on success or negative on error
> + *
> + * u64 bpf_bpf_get_socket_cookie(skb)
> + *     Get the cookie for the socket stored inside sk_buff.
> + *     @skb: pointer to skb
> + *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
> + *     field is missing inside sk_buff
>    */
>   #define __BPF_FUNC_MAPPER(FN)		\
>   	FN(unspec),			\
> @@ -476,7 +482,8 @@ union bpf_attr {
>   	FN(set_hash_invalid),		\
>   	FN(get_numa_node_id),		\
>   	FN(skb_change_head),		\
> -	FN(xdp_adjust_head),
> +	FN(xdp_adjust_head),		\
> +	FN(get_socket_cookie),

Looks like you need a rebase to net-next, since probe_read_str() helper
was added in the meantime.

>   /* integer value in 'imm' field of BPF_CALL instruction selects which helper
>    * function eBPF program intends to call
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 1969b3f118c1..913b14d3b484 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -26,6 +26,7 @@
>   #include <linux/mm.h>
>   #include <linux/fcntl.h>
>   #include <linux/socket.h>
> +#include <linux/sock_diag.h>
>   #include <linux/in.h>
>   #include <linux/inet.h>
>   #include <linux/netdevice.h>
> @@ -2597,6 +2598,18 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = {
>   	.arg5_type	= ARG_CONST_STACK_SIZE,
>   };
>
> +BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
> +{
> +	return skb->sk ? sock_gen_cookie(skb->sk) : 0;
> +}
> +
> +const struct bpf_func_proto bpf_get_socket_cookie_proto = {

static const struct bpf_func_proto ...

> +	.func           = bpf_get_socket_cookie,
> +	.gpl_only       = false,
> +	.ret_type       = RET_INTEGER,
> +	.arg1_type      = ARG_PTR_TO_CTX,
> +};
> +
>   static const struct bpf_func_proto *
>   sk_filter_func_proto(enum bpf_func_id func_id)
>   {
> @@ -2620,6 +2633,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
>   	case BPF_FUNC_trace_printk:
>   		if (capable(CAP_SYS_ADMIN))
>   			return bpf_get_trace_printk_proto();
> +	case BPF_FUNC_get_socket_cookie:
> +		return &bpf_get_socket_cookie_proto;
>   	default:
>   		return NULL;
>   	}
> diff --git a/net/core/sock_diag.c b/net/core/sock_diag.c
> index 6b10573cc9fa..b07b0a784f33 100644
> --- a/net/core/sock_diag.c
> +++ b/net/core/sock_diag.c
> @@ -19,7 +19,7 @@ static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
>   static DEFINE_MUTEX(sock_diag_table_mutex);
>   static struct workqueue_struct *broadcast_wq;
>
> -static u64 sock_gen_cookie(struct sock *sk)
> +u64 sock_gen_cookie(struct sock *sk)
>   {
>   	while (1) {
>   		u64 res = atomic64_read(&sk->sk_cookie);
> @@ -30,6 +30,7 @@ static u64 sock_gen_cookie(struct sock *sk)
>   		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
>   	}
>   }
> +EXPORT_SYMBOL_GPL(sock_gen_cookie);

Also no need to export via EXPORT_SYMBOL_GPL().

>   int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
>   {
>

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-02 20:59 ` [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid Chenbo Feng
@ 2017-02-02 21:32   ` Daniel Borkmann
  2017-02-03  0:00     ` Lorenzo Colitti
  0 siblings, 1 reply; 14+ messages in thread
From: Daniel Borkmann @ 2017-02-02 21:32 UTC (permalink / raw)
  To: Chenbo Feng, David S . Miller, Alexei Starovoitov, netdev
  Cc: Willem de Bruijn, Lorenzo Colitti, Chenbo Feng

On 02/02/2017 09:59 PM, Chenbo Feng wrote:
> From: Chenbo Feng <fengc@google.com>
>
> Returns the owner uid of the socket inside a sk_buff. This is useful to
> perform per-UID accounting of network traffic or per-UID packet
> filtering.
>
> Signed-off-by: Chenbo Feng <chenbofeng.kernel@gmail.com>
> ---
>   include/linux/bpf.h      |  1 +
>   include/uapi/linux/bpf.h |  9 ++++++++-
>   net/core/filter.c        | 19 +++++++++++++++++++
>   3 files changed, 28 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 3f2e0af28c6e..c775ca4678b7 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -350,6 +350,7 @@ extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
>   extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
>   extern const struct bpf_func_proto bpf_get_stackid_proto;
>   extern const struct bpf_func_proto bpf_get_socket_cookie_proto;
> +extern const struct bpf_func_proto bpf_get_socket_uid_proto;

Likewise, no need to add it here.

>   /* Shared helpers among cBPF and eBPF. */
>   void bpf_user_rnd_init_once(void);
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 62ee5fab08e5..dad4dc7aca49 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -436,6 +436,12 @@ union bpf_attr {
>    *     @skb: pointer to skb
>    *     Return: 8 Bytes non-decreasing number on success or 0 if the socket
>    *     field is missing inside sk_buff
> + *
> + * u32 bpf_get_socket_uid(skb)
> + *     Get the owner uid of the socket stored inside sk_buff.
> + *     @skb: pointer to skb
> + *     Return: uid of the socket owner on success or 0 if the socket pointer
> + *     inside sk_buff is NULL
>    */
>   #define __BPF_FUNC_MAPPER(FN)		\
>   	FN(unspec),			\
> @@ -483,7 +489,8 @@ union bpf_attr {
>   	FN(get_numa_node_id),		\
>   	FN(skb_change_head),		\
>   	FN(xdp_adjust_head),		\
> -	FN(get_socket_cookie),
> +	FN(get_socket_cookie),	\
> +	FN(get_socket_uid),
>
>   /* integer value in 'imm' field of BPF_CALL instruction selects which helper
>    * function eBPF program intends to call
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 913b14d3b484..bc36ed72551f 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -2610,6 +2610,23 @@ const struct bpf_func_proto bpf_get_socket_cookie_proto = {
>   	.arg1_type      = ARG_PTR_TO_CTX,
>   };
>
> +BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
> +{
> +	struct sock *sk;
> +	kuid_t kuid;

Nit: already init them above.

> +	sk = skb->sk;
> +	kuid = sock_net_uid(dev_net(skb->dev), sk);

Don't you need to test for fullsock? Do you mean something like below?

struct sock *sk = skb->sk;
kuid_t kuid = sock_net_uid(net, sk && sk_fullsock(sk) ?
			   sk : NULL);

> +	return (u32)kuid.val;
> +}
> +
> +const struct bpf_func_proto bpf_get_socket_uid_proto = {

static ...

> +	.func           = bpf_get_socket_uid,
> +	.gpl_only       = false,
> +	.ret_type       = RET_INTEGER,
> +	.arg1_type      = ARG_PTR_TO_CTX,
> +};
> +
>   static const struct bpf_func_proto *
>   sk_filter_func_proto(enum bpf_func_id func_id)
>   {
> @@ -2635,6 +2652,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
>   			return bpf_get_trace_printk_proto();
>   	case BPF_FUNC_get_socket_cookie:
>   		return &bpf_get_socket_cookie_proto;
> +	case BPF_FUNC_get_socket_uid:
> +		return &bpf_get_socket_uid_proto;
>   	default:
>   		return NULL;
>   	}
>

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-02 21:32   ` Daniel Borkmann
@ 2017-02-03  0:00     ` Lorenzo Colitti
  2017-02-03  0:28       ` Daniel Borkmann
  2017-02-03  0:31       ` Eric Dumazet
  0 siblings, 2 replies; 14+ messages in thread
From: Lorenzo Colitti @ 2017-02-03  0:00 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Chenbo Feng, David S . Miller, Alexei Starovoitov,
	netdev@vger.kernel.org, Willem de Bruijn, Chenbo Feng

On Fri, Feb 3, 2017 at 6:32 AM, Daniel Borkmann <daniel@iogearbox.net> wrote:
>> +       sk = skb->sk;
>> +       kuid = sock_net_uid(dev_net(skb->dev), sk);
>
>
> Don't you need to test for fullsock? Do you mean something like below?

It should be safe to call sock_net_uid on any type of socket
(including NULL). sk_uid was added to struct sock in 86741ec25462
("net: core: Add a UID field to struct sock.")

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

* Re: [PATCH net-next 0/2] net: core: Two Helper function about socket information
  2017-02-02 20:59 [PATCH net-next 0/2] net: core: Two Helper function about socket information Chenbo Feng
  2017-02-02 20:59 ` [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF Chenbo Feng
  2017-02-02 20:59 ` [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid Chenbo Feng
@ 2017-02-03  0:13 ` Alexei Starovoitov
  2017-02-03 16:19   ` Lorenzo Colitti
  2 siblings, 1 reply; 14+ messages in thread
From: Alexei Starovoitov @ 2017-02-03  0:13 UTC (permalink / raw)
  To: Chenbo Feng
  Cc: David S . Miller, Alexei Starovoitov, netdev, Willem de Bruijn,
	Lorenzo Colitti

On Thu, Feb 02, 2017 at 12:59:48PM -0800, Chenbo Feng wrote:
> Introduce two eBpf helper function to get the socket cookie and
> socket uid for each packet. The helper function is useful when
> the *sk field inside sk_buff is not empty.

Looks nice.
I'd like to see a sample code/test for it though.
I think sk_uid is self explanatory, but cookie is less so.
Please explain how you plan to use it.

Thanks

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  0:00     ` Lorenzo Colitti
@ 2017-02-03  0:28       ` Daniel Borkmann
  2017-02-03  0:31       ` Eric Dumazet
  1 sibling, 0 replies; 14+ messages in thread
From: Daniel Borkmann @ 2017-02-03  0:28 UTC (permalink / raw)
  To: Lorenzo Colitti
  Cc: Chenbo Feng, David S . Miller, Alexei Starovoitov,
	netdev@vger.kernel.org, Willem de Bruijn, Chenbo Feng

On 02/03/2017 01:00 AM, Lorenzo Colitti wrote:
> On Fri, Feb 3, 2017 at 6:32 AM, Daniel Borkmann <daniel@iogearbox.net> wrote:
>>> +       sk = skb->sk;
>>> +       kuid = sock_net_uid(dev_net(skb->dev), sk);
>>
>> Don't you need to test for fullsock? Do you mean something like below?
>
> It should be safe to call sock_net_uid on any type of socket
> (including NULL). sk_uid was added to struct sock in 86741ec25462
> ("net: core: Add a UID field to struct sock.")

Hmm, maybe I'm missing something, but then shouldn't this sit in
struct sock_common for being 'safe'? F.e. struct inet_timewait_sock
wouldn't have it ...

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  0:00     ` Lorenzo Colitti
  2017-02-03  0:28       ` Daniel Borkmann
@ 2017-02-03  0:31       ` Eric Dumazet
  2017-02-03  1:18         ` Lorenzo Colitti
  1 sibling, 1 reply; 14+ messages in thread
From: Eric Dumazet @ 2017-02-03  0:31 UTC (permalink / raw)
  To: Lorenzo Colitti
  Cc: Daniel Borkmann, Chenbo Feng, David S . Miller,
	Alexei Starovoitov, netdev@vger.kernel.org, Willem de Bruijn,
	Chenbo Feng

On Fri, 2017-02-03 at 09:00 +0900, Lorenzo Colitti wrote:
> On Fri, Feb 3, 2017 at 6:32 AM, Daniel Borkmann <daniel@iogearbox.net> wrote:
> >> +       sk = skb->sk;
> >> +       kuid = sock_net_uid(dev_net(skb->dev), sk);
> >
> >
> > Don't you need to test for fullsock? Do you mean something like below?
> 
> It should be safe to call sock_net_uid on any type of socket
> (including NULL). sk_uid was added to struct sock in 86741ec25462
> ("net: core: Add a UID field to struct sock.")

But a request socket or a timewait socket do not have this field.

Daniel point is valid.

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  0:31       ` Eric Dumazet
@ 2017-02-03  1:18         ` Lorenzo Colitti
  2017-02-03  1:51           ` Eric Dumazet
  0 siblings, 1 reply; 14+ messages in thread
From: Lorenzo Colitti @ 2017-02-03  1:18 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Daniel Borkmann, Chenbo Feng, David S . Miller,
	Alexei Starovoitov, netdev@vger.kernel.org, Willem de Bruijn,
	Chenbo Feng

On Fri, Feb 3, 2017 at 9:31 AM, Eric Dumazet <eric.dumazet@gmail.com> wrote:
>> It should be safe to call sock_net_uid on any type of socket
>> (including NULL). sk_uid was added to struct sock in 86741ec25462
>> ("net: core: Add a UID field to struct sock.")
>
> But a request socket or a timewait socket do not have this field.
>
> Daniel point is valid.

My bad. Yes.

It would definitely be useful to have the UID available in request
sockets, and perhaps timewait sockets as well. That could be done by
moving the UID to sock_common, or with something along the lines of:

 static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
 {
+       if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
+               sk = sk->__sk_common.skc_listener;
+       else if (sk && !sk_fullsock(sk))
+               sk = NULL;
+
        return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
 }

Any thoughts on which is better?

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  1:18         ` Lorenzo Colitti
@ 2017-02-03  1:51           ` Eric Dumazet
  2017-02-03  8:25             ` Daniel Borkmann
  2017-02-06  3:25             ` Lorenzo Colitti
  0 siblings, 2 replies; 14+ messages in thread
From: Eric Dumazet @ 2017-02-03  1:51 UTC (permalink / raw)
  To: Lorenzo Colitti
  Cc: Daniel Borkmann, Chenbo Feng, David S . Miller,
	Alexei Starovoitov, netdev@vger.kernel.org, Willem de Bruijn,
	Chenbo Feng

On Fri, 2017-02-03 at 10:18 +0900, Lorenzo Colitti wrote:
> On Fri, Feb 3, 2017 at 9:31 AM, Eric Dumazet <eric.dumazet@gmail.com> wrote:
> >> It should be safe to call sock_net_uid on any type of socket
> >> (including NULL). sk_uid was added to struct sock in 86741ec25462
> >> ("net: core: Add a UID field to struct sock.")
> >
> > But a request socket or a timewait socket do not have this field.
> >
> > Daniel point is valid.
> 
> My bad. Yes.
> 
> It would definitely be useful to have the UID available in request
> sockets, and perhaps timewait sockets as well. That could be done by
> moving the UID to sock_common, or with something along the lines of:
> 
>  static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
>  {
> +       if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
> +               sk = sk->__sk_common.skc_listener;
> +       else if (sk && !sk_fullsock(sk))
> +               sk = NULL;
> +
>         return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
>  }
> 
> Any thoughts on which is better?

You could use

if (sk) {
    sk = sk_to_full_sk(sk);
    if (sk_fullsock(sk))
        return sk->sk_uid;
}

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  1:51           ` Eric Dumazet
@ 2017-02-03  8:25             ` Daniel Borkmann
  2017-02-06  3:25             ` Lorenzo Colitti
  1 sibling, 0 replies; 14+ messages in thread
From: Daniel Borkmann @ 2017-02-03  8:25 UTC (permalink / raw)
  To: Eric Dumazet, Lorenzo Colitti
  Cc: Chenbo Feng, David S . Miller, Alexei Starovoitov,
	netdev@vger.kernel.org, Willem de Bruijn, Chenbo Feng

On 02/03/2017 02:51 AM, Eric Dumazet wrote:
> On Fri, 2017-02-03 at 10:18 +0900, Lorenzo Colitti wrote:
>> On Fri, Feb 3, 2017 at 9:31 AM, Eric Dumazet <eric.dumazet@gmail.com> wrote:
>>>> It should be safe to call sock_net_uid on any type of socket
>>>> (including NULL). sk_uid was added to struct sock in 86741ec25462
>>>> ("net: core: Add a UID field to struct sock.")
>>>
>>> But a request socket or a timewait socket do not have this field.
>>>
>>> Daniel point is valid.
>>
>> My bad. Yes.
>>
>> It would definitely be useful to have the UID available in request
>> sockets, and perhaps timewait sockets as well. That could be done by
>> moving the UID to sock_common, or with something along the lines of:
>>
>>   static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
>>   {
>> +       if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
>> +               sk = sk->__sk_common.skc_listener;
>> +       else if (sk && !sk_fullsock(sk))
>> +               sk = NULL;
>> +
>>          return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
>>   }
>>
>> Any thoughts on which is better?
>
> You could use
>
> if (sk) {
>      sk = sk_to_full_sk(sk);
>      if (sk_fullsock(sk))
>          return sk->sk_uid;
> }

Yeah, if that moves into the sock_net_uid() helper, then you could
remove the sk && sk_fullsock(sk) ? sk : NULL tests from the current
sock_net_uid() call sites such as in tcp code. Maybe then also make
the sock_net_uid() as __always_inline, so that most of the callers
with sock_net_uid(net, NULL) are guaranteed to optimize away their
sk checks at compile time?

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

* Re: [PATCH net-next 0/2] net: core: Two Helper function about socket information
  2017-02-03  0:13 ` [PATCH net-next 0/2] net: core: Two Helper function about socket information Alexei Starovoitov
@ 2017-02-03 16:19   ` Lorenzo Colitti
  0 siblings, 0 replies; 14+ messages in thread
From: Lorenzo Colitti @ 2017-02-03 16:19 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Chenbo Feng, David S . Miller, Alexei Starovoitov,
	netdev@vger.kernel.org, Willem de Bruijn

On Fri, Feb 3, 2017 at 9:13 AM, Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
> I think sk_uid is self explanatory, but cookie is less so.
> Please explain how you plan to use it.

The cookie is the only way to uniquely identify a given socket on the
system (the 5-tuple is not sufficient in the presence of SO_REUSEPORT,
for example). One goal for this infrastructure is to perform
per-socket accounting (ultimately replace the out-of-tree xt_qtaguid
code that is used for per-UID, per-socket-tag accounting on Android).
The socket cookie would be a natural key to use in a BPF map where the
values are socket accounting information.

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

* Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid
  2017-02-03  1:51           ` Eric Dumazet
  2017-02-03  8:25             ` Daniel Borkmann
@ 2017-02-06  3:25             ` Lorenzo Colitti
  1 sibling, 0 replies; 14+ messages in thread
From: Lorenzo Colitti @ 2017-02-06  3:25 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Daniel Borkmann, Chenbo Feng, David S . Miller,
	Alexei Starovoitov, netdev@vger.kernel.org, Willem de Bruijn,
	Chenbo Feng

On Fri, Feb 3, 2017 at 10:51 AM, Eric Dumazet <eric.dumazet@gmail.com> wrote:
> if (sk) {
>     sk = sk_to_full_sk(sk);
>     if (sk_fullsock(sk))
>         return sk->sk_uid;
> }

Sure, though sk_to_full_sk is in inet_sock.h so I have to move some
core around. Options I see:

1. Move sk_to_full_sk from inet_sock.h to sock.h.
2. Move sock_net_uid to inet_sock.h.
3. Move sock_net_uid to sock.c and EXPORT_SYMBOL_GPL it.

Thoughts? #1 seems reasonable, since sk_fullsock is already in sock.h.
#2 would mean that we can't call sock_net_uid from non-inet code.
Currently the only code that accesses sk->sk_uid is inet code, but in
the future perhaps some of the code around the tree that calls
sock_i_uid could be migrated to use at sk->sk_uid instead.

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

end of thread, other threads:[~2017-02-06  3:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-02 20:59 [PATCH net-next 0/2] net: core: Two Helper function about socket information Chenbo Feng
2017-02-02 20:59 ` [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF Chenbo Feng
2017-02-02 21:23   ` Daniel Borkmann
2017-02-02 20:59 ` [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid Chenbo Feng
2017-02-02 21:32   ` Daniel Borkmann
2017-02-03  0:00     ` Lorenzo Colitti
2017-02-03  0:28       ` Daniel Borkmann
2017-02-03  0:31       ` Eric Dumazet
2017-02-03  1:18         ` Lorenzo Colitti
2017-02-03  1:51           ` Eric Dumazet
2017-02-03  8:25             ` Daniel Borkmann
2017-02-06  3:25             ` Lorenzo Colitti
2017-02-03  0:13 ` [PATCH net-next 0/2] net: core: Two Helper function about socket information Alexei Starovoitov
2017-02-03 16:19   ` Lorenzo Colitti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).