Skip to contentSkip to conversation

Fix collisions in socket cookie generation

PATCH Superseded Daniel Borkmann 2 participants 7 messages Aug 8, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

cover Fix collisions in socket cookie generation

This change makes the socket cookie generator as a global counter instead of per netns in order to fix cookie collisions for BPF use cases we ran into. See main patch #1 for more details.

Given the change is small/trivial and fixes an issue we're seeing my preference would be net tree (though it cleanly applies to net-next as well). Went for net tree instead of bpf tree here given the main change is in net/core/sock_diag.c, but either way would be fine with me.

Thanks a lot!

Daniel Borkmann (2):
sock: make cookie generation global instead of per netns
bpf: sync bpf.h to tools infrastructure

 include/net/net_namespace.h    |  1 -
 include/uapi/linux/bpf.h       |  4 ++--
 net/core/sock_diag.c           |  3 ++-
 tools/include/uapi/linux/bpf.h | 11 +++++++----
 4 files changed, 11 insertions(+), 8 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/2 sock: make cookie generation global instead of per netns

Generating and retrieving socket cookies are a useful feature that is exposed to BPF for various program types through bpf_get_socket_cookie() helper.

The fact that the cookie counter is per netns is quite a limitation for BPF in practice in particular for programs in host namespace that use socket cookies as part of a map lookup key since they will be causing socket cookie collisions e.g. when attached to BPF cgroup hooks or cls_bpf on tc egress in host namespace handling container traffic from veths or ipvlan slaves. Change the counter to be global instead.

Socket cookie consumers must assume the value as opqaue in any case. The cookie does not guarantee an always unique identifier since it could wrap in fabricated corner cases where two sockets could end up holding the same cookie, but is good enough to be used as a hint for many use cases; not every socket must have a cookie generated hence knowledge of the counter value does not provide much value either way.

Cc Eric Dumazet <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Martynas Pumputis <[email protected]>
---
 include/net/net_namespace.h | 1 -
 include/uapi/linux/bpf.h    | 4 ++--
 net/core/sock_diag.c        | 3 ++-
 3 files changed, 4 insertions(+), 4 deletions(-)
include/net/net_namespace.h +0 -1
index 4a9da951a794..cb668bc2692d 100644
@@ -61,7 +61,6 @@ struct net {
 	spinlock_t		rules_mod_lock;
 
 	u32			hash_mix;
-	atomic64_t		cookie_gen;
 
 	struct list_head	list;		/* list of network namespaces */
 	struct list_head	exit_list;	/* To linked to call pernet exit
include/uapi/linux/bpf.h +2 -2
index fa1c753dcdbc..a5aa7d3ac6a1 100644
@@ -1466,8 +1466,8 @@ union bpf_attr {
  * 		If no cookie has been set yet, generate a new cookie. Once
  * 		generated, the socket cookie remains stable for the life of the
  * 		socket. This helper can be useful for monitoring per socket
- * 		networking traffic statistics as it provides a unique socket
- * 		identifier per namespace.
+ * 		networking traffic statistics as it provides a global socket
+ * 		identifier that can be assumed unique.
  * 	Return
  * 		A 8-byte long non-decreasing number on success, or 0 if the
  * 		socket field is missing inside *skb*.
net/core/sock_diag.c +2 -1
index 3312a5849a97..c13ffbd33d8d 100644
@@ -19,6 +19,7 @@ static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
 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 atomic64_t cookie_gen;
 
 u64 sock_gen_cookie(struct sock *sk)
 {
@@ -27,7 +28,7 @@ u64 sock_gen_cookie(struct sock *sk)
 
 		if (res)
 			return res;
-		res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
+		res = atomic64_inc_return(&cookie_gen);
 		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
 	}
 }
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH net 1/2] sock: make cookie generation global instead of per netns

Quoted 6 lines · Daniel Borkmann

Socket cookie consumers must assume the value as opqaue in any case. The cookie does not guarantee an always unique identifier since it could wrap in fabricated corner cases where two sockets could end up holding the same cookie,

What do you mean by this ?

Cookie is guaranteed to be unique, it is from a 64bit counter...

There should be no collision.

Quoted 66 lines

but is good enough to be used as a hint for
many use cases; not every socket must have a cookie generated hence knowledge of the counter value does not provide much value either way.

Cc Eric Dumazet <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Martynas Pumputis <[email protected]>
---
 include/net/net_namespace.h | 1 -
 include/uapi/linux/bpf.h    | 4 ++--
 net/core/sock_diag.c        | 3 ++-
 3 files changed, 4 insertions(+), 4 deletions(-)
include/net/net_namespace.h +0 -1
index 4a9da951a794..cb668bc2692d 100644
@@ -61,7 +61,6 @@ struct net {
        spinlock_t              rules_mod_lock;
 
        u32                     hash_mix;
-       atomic64_t              cookie_gen;
 
        struct list_head        list;           /* list of network namespaces */
        struct list_head        exit_list;      /* To linked to call pernet exit
include/uapi/linux/bpf.h +2 -2
index fa1c753dcdbc..a5aa7d3ac6a1 100644
@@ -1466,8 +1466,8 @@ union bpf_attr {
  *             If no cookie has been set yet, generate a new cookie. Once
  *             generated, the socket cookie remains stable for the life of the
  *             socket. This helper can be useful for monitoring per socket
- *             networking traffic statistics as it provides a unique socket
- *             identifier per namespace.
+ *             networking traffic statistics as it provides a global socket
+ *             identifier that can be assumed unique.
  *     Return
  *             A 8-byte long non-decreasing number on success, or 0 if the
  *             socket field is missing inside *skb*.
net/core/sock_diag.c +2 -2
index 3312a5849a97..c13ffbd33d8d 100644
@@ -19,6 +19,7 @@ static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
 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 atomic64_t cookie_gen;
 
 u64 sock_gen_cookie(struct sock *sk)
 {
@@ -27,7 +28,7 @@ u64 sock_gen_cookie(struct sock *sk)
 
                if (res)
                        return res;
-               res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
+               res = atomic64_inc_return(&cookie_gen);
                atomic64_cmpxchg(&sk->sk_cookie, 0, res);
        }
 }
--

2.17.1

author maintainer
#3 lore raw

Re: [PATCH net 1/2] sock: make cookie generation global instead of per netns

Quoted 7 lines · Eric Dumazet
Quoted 4 lines · Daniel Borkmann

Socket cookie consumers must assume the value as opqaue in any case. The cookie does not guarantee an always unique identifier since it could wrap in fabricated corner cases where two sockets could end up holding the same cookie,

Quoted 5 lines

What do you mean by this ?

Cookie is guaranteed to be unique, it is from a 64bit counter...

There should be no collision.

I meant the [theoretical] corner case where socket_1 has cookie X and we'd create, trigger sock_gen_cookie() to increment, close socket in a loop until we wrap and get another cookie X for socket_2; agree it's impractical and for little gain anyway. So in practice there should be no collision which is what I tried to say.

Thanks,
Daniel

#4 lore raw

Re: [PATCH net 1/2] sock: make cookie generation global instead of per netns

Quoted 13 lines · Daniel Borkmann
Quoted 11 lines · Eric Dumazet
Quoted 4 lines · Daniel Borkmann

Socket cookie consumers must assume the value as opqaue in any case. The cookie does not guarantee an always unique identifier since it could wrap in fabricated corner cases where two sockets could end up holding the same cookie,

What do you mean by this ?

Cookie is guaranteed to be unique, it is from a 64bit counter...

Quoted 7 lines
Quoted 1 line

There should be no collision.

I meant the [theoretical] corner case where socket_1 has cookie X and we'd create, trigger sock_gen_cookie() to increment, close socket in a loop until we wrap and get another cookie X for socket_2; agree it's impractical and for little gain anyway. So in practice there should be no collision which is what I tried to say.

If a 64bit counter, updated by one unit at a time could overflow during the lifetime of a host,
I would agree with you, but this can not happen, even if we succeed to make 1 billion
locked increments per second (this would still need 584 years)

I would prefer not mentioning something that can not possibly happen in your changelog ;)

author maintainer
#5 lore raw

Re: [PATCH net 1/2] sock: make cookie generation global instead of per netns

Quoted 21 lines · Eric Dumazet
Quoted 12 lines · Daniel Borkmann
Quoted 11 lines · Eric Dumazet
Quoted 4 lines · Daniel Borkmann

Socket cookie consumers must assume the value as opqaue in any case. The cookie does not guarantee an always unique identifier since it could wrap in fabricated corner cases where two sockets could end up holding the same cookie,

What do you mean by this ?

Cookie is guaranteed to be unique, it is from a 64bit counter...

Quoted 7 lines
Quoted 1 line

There should be no collision.

I meant the [theoretical] corner case where socket_1 has cookie X and we'd create, trigger sock_gen_cookie() to increment, close socket in a loop until we wrap and get another cookie X for socket_2; agree it's impractical and for little gain anyway. So in practice there should be no collision which is what I tried to say.

Quoted 8 lines

If a 64bit counter, updated by one unit at a time could overflow during the lifetime of a host,
I would agree with you, but this can not happen, even if we succeed to make 1 billion
locked increments per second (this would still need 584 years)

I would prefer not mentioning something that can not possibly happen in your changelog ;)

Yep fair enough, makes sense. I'll fix it :)

author maintainer
#6 lore raw

PATCH 2/2 bpf: sync bpf.h to tools infrastructure

In reply to Daniel Borkmann

Pull in updates in BPF helper function description.

---
 tools/include/uapi/linux/bpf.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/include/uapi/linux/bpf.h +7 -4
index 4e455018da65..a5aa7d3ac6a1 100644
@@ -1466,8 +1466,8 @@ union bpf_attr {
  * 		If no cookie has been set yet, generate a new cookie. Once
  * 		generated, the socket cookie remains stable for the life of the
  * 		socket. This helper can be useful for monitoring per socket
- * 		networking traffic statistics as it provides a unique socket
- * 		identifier per namespace.
+ * 		networking traffic statistics as it provides a global socket
+ * 		identifier that can be assumed unique.
  * 	Return
  * 		A 8-byte long non-decreasing number on success, or 0 if the
  * 		socket field is missing inside *skb*.
@@ -1571,8 +1571,11 @@ union bpf_attr {
  * 		but this is only implemented for native XDP (with driver
  * 		support) as of this writing).
  *
- * 		All values for *flags* are reserved for future usage, and must
- * 		be left at zero.
+ * 		The lower two bits of *flags* are used as the return code if
+ * 		the map lookup fails. This is so that the return value can be
+ * 		one of the XDP program return codes up to XDP_TX, as chosen by
+ * 		the caller. Any higher bits in the *flags* argument must be
+ * 		unset.
  *
  * 		When used to redirect packets to net devices, this helper
  * 		provides a high performance increase over **bpf_redirect**\ ().
Signature · 1 line
2.17.1