Skip to contentSkip to conversation

net: bpf: hook for inet port bind conflict check

PATCH In review [email protected] 3 participants 3 messages Dec 29, 2021

Subsystems (6)
Networking Network drivers BPF TCP +2 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: bpf: hook for inet port bind conflict check

From: Menglong Dong <[email protected]>

This hook of cgroup is called while TCP/UDP local port bind conflict check. This is different from the 'commit aac3fc320d94 ("bpf: Post-hooks for sys_bind")', as it is also called in autobind case.

For TCP, this hook is called during sys_bind() and autobind. And it is also called during tcp_v4_connect() before hash the sock to ehash, during which src ip, src port, dst ip, and dst port is already allocated, means that we have a chance to determine whether this connect should continue.

This can be useful when we want some applications not to use some port (include auto bind port). For autobind, the kernel has the chance to choose another port.

---
 include/linux/bpf-cgroup.h      |  5 +++++
 include/net/inet_sock.h         | 11 +++++++++++
 include/uapi/linux/bpf.h        |  1 +
 kernel/bpf/syscall.c            |  3 +++
 net/ipv4/inet_connection_sock.c | 10 ++++++++--
 net/ipv4/inet_hashtables.c      |  8 ++++++++
 net/ipv4/udp.c                  | 19 +++++++++++++++----
 7 files changed, 51 insertions(+), 6 deletions(-)
include/linux/bpf-cgroup.h +5 -0
index 11820a430d6c..c81180a2936e 100644
@@ -48,6 +48,7 @@ enum cgroup_bpf_attach_type {
 	CGROUP_INET4_GETSOCKNAME,
 	CGROUP_INET6_GETSOCKNAME,
 	CGROUP_INET_SOCK_RELEASE,
+	CGROUP_INET_LPORT_INUSE,
 	MAX_CGROUP_BPF_ATTACH_TYPE
 };
 
@@ -81,6 +82,7 @@ to_cgroup_bpf_attach_type(enum bpf_attach_type attach_type)
 	CGROUP_ATYPE(CGROUP_INET4_GETSOCKNAME);
 	CGROUP_ATYPE(CGROUP_INET6_GETSOCKNAME);
 	CGROUP_ATYPE(CGROUP_INET_SOCK_RELEASE);
+	CGROUP_ATYPE(CGROUP_INET_LPORT_INUSE);
 	default:
 		return CGROUP_BPF_ATTACH_TYPE_INVALID;
 	}
@@ -263,6 +265,9 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
 #define BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk)				       \
 	BPF_CGROUP_RUN_SK_PROG(sk, CGROUP_INET6_POST_BIND)
 
+#define BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk)			       \
+	BPF_CGROUP_RUN_SK_PROG(sk, CGROUP_INET_LPORT_INUSE)
+
 #define BPF_CGROUP_RUN_SA_PROG(sk, uaddr, atype)				       \
 ({									       \
 	u32 __unused_flags;						       \
include/net/inet_sock.h +11 -0
index 234d70ae5f4c..3a2a8784bffb 100644
@@ -298,6 +298,17 @@ static inline void __inet_sk_copy_descendant(struct sock *sk_to,
 
 int inet_sk_rebuild_header(struct sock *sk);
 
+static inline int inet_bind_conflict(struct sock *sk, int port)
+{
+	int res;
+	int old = sk->sk_num;
+
+	sk->sk_num = port;
+	res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
+	sk->sk_num = old;
+	return res;
+}
+
 /**
  * inet_sk_state_load - read sk->sk_state for lockless contexts
  * @sk: socket pointer
include/uapi/linux/bpf.h +1 -0
index c26871263f1f..849b37bfa3b2 100644
@@ -995,6 +995,7 @@ enum bpf_attach_type {
 	BPF_SK_REUSEPORT_SELECT,
 	BPF_SK_REUSEPORT_SELECT_OR_MIGRATE,
 	BPF_PERF_EVENT,
+	BPF_CGROUP_INET_LPORT_INUSE,
 	__MAX_BPF_ATTACH_TYPE
 };
 
kernel/bpf/syscall.c +3 -0
index ddd81d543203..bec7f49eb0a9 100644
@@ -2088,6 +2088,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
 		case BPF_CGROUP_INET_SOCK_RELEASE:
 		case BPF_CGROUP_INET4_POST_BIND:
 		case BPF_CGROUP_INET6_POST_BIND:
+		case BPF_CGROUP_INET_LPORT_INUSE:
 			return 0;
 		default:
 			return -EINVAL;
@@ -3140,6 +3141,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)
 	case BPF_CGROUP_INET_SOCK_RELEASE:
 	case BPF_CGROUP_INET4_POST_BIND:
 	case BPF_CGROUP_INET6_POST_BIND:
+	case BPF_CGROUP_INET_LPORT_INUSE:
 		return BPF_PROG_TYPE_CGROUP_SOCK;
 	case BPF_CGROUP_INET4_BIND:
 	case BPF_CGROUP_INET6_BIND:
@@ -3311,6 +3313,7 @@ static int bpf_prog_query(const union bpf_attr *attr,
 	case BPF_CGROUP_SYSCTL:
 	case BPF_CGROUP_GETSOCKOPT:
 	case BPF_CGROUP_SETSOCKOPT:
+	case BPF_CGROUP_INET_LPORT_INUSE:
 		return cgroup_bpf_prog_query(attr, uattr);
 	case BPF_LIRC_MODE2:
 		return lirc_prog_query(attr, uattr);
net/ipv4/inet_connection_sock.c +8 -2
index fc2a985f6064..1cf4f374e7e4 100644
@@ -130,10 +130,11 @@ void inet_get_local_port_range(struct net *net, int *low, int *high)
 }
 EXPORT_SYMBOL(inet_get_local_port_range);
 
-static int inet_csk_bind_conflict(const struct sock *sk,
+static int inet_csk_bind_conflict(struct sock *sk,
 				  const struct inet_bind_bucket *tb,
 				  bool relax, bool reuseport_ok)
 {
+	int res;
 	struct sock *sk2;
 	bool reuseport_cb_ok;
 	bool reuse = sk->sk_reuse;
@@ -179,7 +180,10 @@ static int inet_csk_bind_conflict(const struct sock *sk,
 			}
 		}
 	}
-	return sk2 != NULL;
+	res = !!sk2;
+	if (!res)
+		res = inet_bind_conflict(sk, tb->port);
+	return res;
 }
 
 /*
@@ -401,6 +405,8 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
 			goto success;
 		if (inet_csk_bind_conflict(sk, tb, true, true))
 			goto fail_unlock;
+	} else if (inet_bind_conflict(sk, port)) {
+		goto fail_unlock;
 	}
 success:
 	inet_csk_update_fastreuse(tb, sk);
net/ipv4/inet_hashtables.c +8 -0
index 30ab717ff1b8..9cf3efe7be48 100644
@@ -476,6 +476,9 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
 		}
 	}
 
+	if (inet_bind_conflict(sk, lport))
+		goto not_unique;
+
 	/* Must record num and sport now. Otherwise we will see
 	 * in hash table socket with a funny identity.
 	 */
@@ -744,6 +747,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
 		tb = inet_csk(sk)->icsk_bind_hash;
 		spin_lock_bh(&head->lock);
 		if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
+			if (inet_bind_conflict(sk, port))
+				return -EPERM;
 			inet_ehash_nolisten(sk, NULL, NULL);
 			spin_unlock_bh(&head->lock);
 			return 0;
@@ -799,6 +804,9 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
 			}
 		}
 
+		if (inet_bind_conflict(sk, port))
+			goto next_port;
+
 		tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
 					     net, head, port, l3mdev);
 		if (!tb) {
net/ipv4/udp.c +15 -4
index f376c777e8fc..95ef1b47386d 100644
@@ -134,6 +134,7 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
 			       struct sock *sk, unsigned int log)
 {
 	struct sock *sk2;
+	int res = 0;
 	kuid_t uid = sock_i_uid(sk);
 
 	sk_for_each(sk2, &hslot->head) {
@@ -148,16 +149,21 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
 			    uid_eq(uid, sock_i_uid(sk2))) {
 				if (!bitmap)
-					return 0;
+					break;
 			} else {
-				if (!bitmap)
-					return 1;
+				if (!bitmap) {
+					res = 1;
+					break;
+				}
 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
 					  bitmap);
 			}
 		}
 	}
-	return 0;
+
+	if (!res)
+		res = inet_bind_conflict(sk, num);
+	return res;
 }
 
 /*
@@ -192,6 +198,11 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
 		}
 	}
 	spin_unlock(&hslot2->lock);
+	if (!res) {
+		sk->sk_num = num;
+		res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
+		sk->sk_num = 0;
+	}
 	return res;
 }
 
Signature · 1 line
2.30.2
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on net-next/master]

url:    https://github.com/0day-ci/linux/commits/menglong8-dong-gmail-com/net-bpf-hook-for-inet-port-bind-conflict-check/20211229-165656
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git 271d3be1c3b6b0be083a99254a0ecac41789929b
config: hexagon-buildonly-randconfig-r002-20211228 (https://download.01.org/0day-ci/archive/20211229/202112291915.muBKYGuT-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project cd284b7ac0615afc6e0f1a30da2777e361de27a3)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/ca8b713d84b5e09a77bfae226287297f49cd97c9
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review menglong8-dong-gmail-com/net-bpf-hook-for-inet-port-bind-conflict-check/20211229-165656 git checkout ca8b713d84b5e09a77bfae226287297f49cd97c9
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash net/ipv4/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from net/ipv4/route.c:79:
In file included from include/linux/mroute.h:10:
In file included from include/linux/mroute_base.h:10:
In file included from include/net/ip_fib.h:20:
In file included from include/net/inetpeer.h:16:
In file included from include/net/ipv6.h:12:
In file included from include/linux/ipv6.h:92:
In file included from include/linux/tcp.h:20:
In file included from include/net/inet_connection_sock.h:21:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/route.c:869:6: warning: variable 'log_martians' set but not used [-Wunused-but-set-variable]
           int log_martians;
               ^

1 warning and 1 error generated.
--
In file included from net/ipv4/inetpeer.c:22:
In file included from include/net/ip.h:29:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^

1 error generated.
--
In file included from net/ipv4/udp.c:100:
In file included from include/net/icmp.h:19:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
>> net/ipv4/udp.c:203:9: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
                   res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                         ^

2 errors generated.
--
In file included from net/ipv4/igmp.c:92:
In file included from include/net/ip.h:29:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/igmp.c:1905:6: warning: variable 'changerec' set but not used [-Wunused-but-set-variable]
           int     changerec = 0;
                   ^

1 warning and 1 error generated.
--
In file included from net/ipv4/ip_gre.c:19:
In file included from include/linux/tcp.h:20:
In file included from include/net/inet_connection_sock.h:21:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/ip_gre.c:143:15: warning: variable 'data_len' set but not used [-Wunused-but-set-variable]
           unsigned int data_len = 0;
                        ^

1 warning and 1 error generated.
--
In file included from net/ipv4/ah4.c:9:
In file included from include/net/ip.h:29:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/ah4.c:513:4: warning: format specifies type 'unsigned short' but the argument has type 'int' [-Wformat]
                           aalg_desc->uinfo.auth.icv_fullbits / 8);
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/printk.h:523:34: note: expanded from macro 'pr_info'
           printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
                                   ~~~     ^~~~~~~~~~~
   include/linux/printk.h:450:60: note: expanded from macro 'printk'
   #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
                                                       ~~~    ^~~~~~~~~~~
   include/linux/printk.h:422:19: note: expanded from macro 'printk_index_wrap'
                   _p_func(_fmt, ##__VA_ARGS__);                           \
                           ~~~~    ^~~~~~~~~~~

1 warning and 1 error generated.
--
In file included from net/ipv4/esp4.c:8:
In file included from include/net/ip.h:29:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/esp4.c:1116:5: warning: format specifies type 'unsigned short' but the argument has type 'int' [-Wformat]
                                   aalg_desc->uinfo.auth.icv_fullbits / 8);
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/printk.h:523:34: note: expanded from macro 'pr_info'
           printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
                                   ~~~     ^~~~~~~~~~~
   include/linux/printk.h:450:60: note: expanded from macro 'printk'
   #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
                                                       ~~~    ^~~~~~~~~~~
   include/linux/printk.h:422:19: note: expanded from macro 'printk_index_wrap'
                   _p_func(_fmt, ##__VA_ARGS__);                           \
                           ~~~~    ^~~~~~~~~~~

1 warning and 1 error generated.
--
In file included from net/ipv4/netfilter/nf_reject_ipv4.c:7:
In file included from include/net/ip.h:29:

>> include/net/inet_sock.h:307:8: error: implicit declaration of function 'BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE' [-Werror,-Wimplicit-function-declaration]
           res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);
                 ^
   net/ipv4/netfilter/nf_reject_ipv4.c:242:16: warning: variable 'niph' set but not used [-Wunused-but-set-variable]
           struct iphdr *niph;
                         ^

1 warning and 1 error generated.

vim +/BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE +307 include/net/inet_sock.h

300
301 static inline int inet_bind_conflict(struct sock *sk, int port)
302 {
303 int res;
304 int old = sk->sk_num;
305
306 sk->sk_num = port;

 > 307		res = BPF_CGROUP_RUN_PROG_INET_LPORT_INUSE(sk);

308 sk->sk_num = old;
309 return res;
310 }
311

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to [email protected]
Quoted 13 lines · On Wed, Dec 29, 2021 at 12:56 AM <[email protected]> wrote:

From: Menglong Dong <[email protected]>

This hook of cgroup is called while TCP/UDP local port bind conflict check. This is different from the 'commit aac3fc320d94 ("bpf: Post-hooks for sys_bind")', as it is also called in autobind case.

For TCP, this hook is called during sys_bind() and autobind. And it is also called during tcp_v4_connect() before hash the sock to ehash, during which src ip, src port, dst ip, and dst port is already allocated, means that we have a chance to determine whether this connect should continue.

Quoted 3 lines

This can be useful when we want some applications not to use some port (include auto bind port). For autobind, the kernel has the chance to choose another port.

The use case is too vague to consider adding a new hook. Also there are no selftests.