Skip to contentSkip to conversation

net: core: Two Helper function about socket information

PATCH Superseded Chenbo Feng 5 participants 14 messages Feb 2, 2017 – Feb 6, 2017

Subsystems (6)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover net: core: Two Helper function about socket information

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(-)
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#1 lore raw

PATCH 1/2 Add a helper function to get socket cookie in eBPF

From: Chenbo Feng <[email protected]>

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.

---
 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(-)
include/linux/bpf.h +1 -0
index 3ed1f3b1d594..3f2e0af28c6e 100644
@@ -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);
include/linux/sock_diag.h +1 -0
index a0596ca0e80a..a2f8109bb215 100644
@@ -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);
 
include/uapi/linux/bpf.h +8 -1
index 0eb0e87dbe9f..62ee5fab08e5 100644
@@ -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
net/core/filter.c +15 -0
index 1969b3f118c1..913b14d3b484 100644
@@ -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;
 	}
net/core/sock_diag.c +2 -1
index 6b10573cc9fa..b07b0a784f33 100644
@@ -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)
 {
Signature · 1 line
2.11.0.483.g087da7b7c-goog
#3 lore raw

PATCH 2/2 Add a eBPF helper function to retrieve socket uid

In reply to Chenbo Feng

From: Chenbo Feng <[email protected]>

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.

---
 include/linux/bpf.h      |  1 +
 include/uapi/linux/bpf.h |  9 ++++++++-
 net/core/filter.c        | 19 +++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
index 3f2e0af28c6e..c775ca4678b7 100644
@@ -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);
include/uapi/linux/bpf.h +8 -1
index 62ee5fab08e5..dad4dc7aca49 100644
@@ -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
net/core/filter.c +19 -0
index 913b14d3b484..bc36ed72551f 100644
@@ -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;
 	}
Signature · 1 line
2.11.0.483.g087da7b7c-goog
maintainer
#2 lore raw

Re: [PATCH net-next 1/2] Add a helper function to get socket cookie in eBPF

In reply to Chenbo Feng
Quoted 22 lines · Chenbo Feng

From: Chenbo Feng <[email protected]>

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.

---
  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(-)
include/linux/bpf.h +0 -0
index 3ed1f3b1d594..3f2e0af28c6e 100644

include/linux/bpf.h extern const struct bpf_func_proto bpf_get_current_comm_proto; · line 352

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -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.

Quoted 32 lines
include/linux/bpf.h +0 -0
  /* Shared helpers among cBPF and eBPF. */
  void bpf_user_rnd_init_once(void);
include/linux/sock_diag.h +1 -0
index a0596ca0e80a..a2f8109bb215 100644
@@ -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);
 
include/uapi/linux/bpf.h +6 -0
index 0eb0e87dbe9f..62ee5fab08e5 100644
@@ -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),			\

include/uapi/linux/bpf.h union bpf_attr { · line 486

Quoted 6 lines
include/uapi/linux/bpf.h +2 -1
@@ -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.

Quoted 15 lines
include/uapi/linux/bpf.h +0 -0
  /* integer value in 'imm' field of BPF_CALL instruction selects which helper
   * function eBPF program intends to call
net/core/filter.c +1 -0
index 1969b3f118c1..913b14d3b484 100644
@@ -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>

net/core/filter.c static const struct bpf_func_proto bpf_xdp_event_output_proto = { · line 2606

Quoted 9 lines
net/core/filter.c +6 -0
@@ -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 ...

Quoted 32 lines
net/core/filter.c +8 -0
+	.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;
  	}
net/core/sock_diag.c +1 -1
index 6b10573cc9fa..b07b0a784f33 100644
@@ -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);

net/core/sock_diag.c static u64 sock_gen_cookie(struct sock *sk) · line 33

Quoted 4 lines
net/core/sock_diag.c +1 -0
@@ -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().

Quoted 3 lines
net/core/sock_diag.c +0 -0
  int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
  {
 
maintainer
#4 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

In reply to Chenbo Feng
Quoted 18 lines · Chenbo Feng

From: Chenbo Feng <[email protected]>

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.

---
  include/linux/bpf.h      |  1 +
  include/uapi/linux/bpf.h |  9 ++++++++-
  net/core/filter.c        | 19 +++++++++++++++++++
  3 files changed, 28 insertions(+), 1 deletion(-)
include/linux/bpf.h +0 -0
index 3f2e0af28c6e..c775ca4678b7 100644

include/linux/bpf.h extern const struct bpf_func_proto bpf_skb_vlan_push_proto; · line 353

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -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.

Quoted 34 lines
include/linux/bpf.h +0 -0
  /* Shared helpers among cBPF and eBPF. */
  void bpf_user_rnd_init_once(void);
include/uapi/linux/bpf.h +8 -1
index 62ee5fab08e5..dad4dc7aca49 100644
@@ -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
net/core/filter.c +0 -0
index 913b14d3b484..bc36ed72551f 100644

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2616

Quoted 7 lines
net/core/filter.c +4 -0
@@ -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.

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2619

Quoted 2 lines
net/core/filter.c +2 -0
+	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);

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2623

Quoted 4 lines
net/core/filter.c +4 -0
+	return (u32)kuid.val;
+}
+
+const struct bpf_func_proto bpf_get_socket_uid_proto = {

static ...

Quoted 19 lines
net/core/filter.c +8 -0
+	.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;
  	}
#5 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

Quoted 5 lines · Daniel Borkmann

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2619

Quoted 2 lines
net/core/filter.c +2 -0
+       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.")

#12 lore raw
In reply to Chenbo Feng
Quoted 3 lines · 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.

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

maintainer
#6 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

In reply to Lorenzo Colitti
Quoted 9 lines · Lorenzo Colitti
Quoted 4 lines · Daniel Borkmann

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2619

Quoted 2 lines
net/core/filter.c +2 -0
+       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 ...

#7 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

In reply to Lorenzo Colitti
Quoted 10 lines · Lorenzo Colitti
Quoted 5 lines · Daniel Borkmann

net/core/filter.c const struct bpf_func_proto bpf_get_socket_cookie_proto = { · line 2619

Quoted 2 lines
net/core/filter.c +2 -0
+       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.

#8 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

Quoted 7 lines · Eric Dumazet
Quoted 3 lines

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?

#9 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

Quoted 25 lines · Lorenzo Colitti
Quoted 7 lines · Eric Dumazet
Quoted 3 lines

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) {

(quoted diff) +5 -0
+       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);
 }
Quoted 1 line

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;
}
maintainer
#10 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

Quoted 28 lines · Eric Dumazet
Quoted 25 lines · Lorenzo Colitti
Quoted 7 lines · Eric Dumazet
Quoted 3 lines

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) {

(quoted diff) +5 -0
+       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);
  }
Quoted 1 line

Any thoughts on which is better?

Quoted 7 lines

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?

#13 lore raw
In reply to Alexei Starovoitov
Quoted 2 lines · Alexei Starovoitov

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.

#11 lore raw

Re: [PATCH net-next 2/2] Add a eBPF helper function to retrieve socket uid

In reply to Eric Dumazet
Quoted 5 lines · Eric Dumazet
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.