From: planck kernel size experiment Subject: [PATCH 5/5] net: remove unreachable socket filters and reuseport The appliance creates one TCP listener, one UDP listener and a raw ICMP proof socket. None installs a classic or extended BPF program or asks for SO_REUSEPORT, and all receive ordinary virtio-net pages rather than pfmemalloc reserve pages. Keeping filter.c and sock_reuseport.c therefore adds an unused packet VM, filter plumbing and socket-selection subsystem to the image. Make filter attachment explicitly unsupported, report zero BPF extensions, and retain the non-reuseport behavior of SO_INCOMING_CPU. Removing the two objects also allows CONFIG_BPF=n instead of NET pulling the VM into the kernel. Exact TCP and UDP records plus the final PCAP commit signal check the effect after linking. diff --git a/net/core/Makefile b/net/core/Makefile --- a/net/core/Makefile +++ b/net/core/Makefile @@ -10,8 +10,8 @@ obj-y := sock.o request_sock.o skbuff.o datagram.o stream.o scm.o \ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o obj-y += dev.o dev_api.o dev_addr_lists.o dst.o netevent.o \ - neighbour.o rtnetlink.o utils.o link_watch.o filter.o \ - sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \ + neighbour.o rtnetlink.o utils.o link_watch.o \ + sock_diag.o dev_ioctl.o tso.o \ fib_notifier.o xdp.o flow_offload.o gro.o \ netdev-genl.o netdev-genl-gen.o gso.o diff --git a/net/core/sock.c b/net/core/sock.c --- a/net/core/sock.c +++ b/net/core/sock.c @@ -526,10 +526,6 @@ int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason drop_reason; int err; - err = sk_filter_reason(sk, skb, &drop_reason); - if (err) - goto out; - err = __sock_queue_rcv_skb(sk, skb); switch (err) { case -ENOMEM: @@ -542,7 +538,6 @@ int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb, drop_reason = SKB_NOT_DROPPED_YET; break; } -out: if (reason) *reason = drop_reason; return err; @@ -556,9 +551,6 @@ int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, int rc = NET_RX_SUCCESS; int err; - if (sk_filter_trim_cap(sk, skb, trim_cap, &reason)) - goto discard_and_relse; - skb->dev = NULL; if (sk_rcvqueues_full(sk, READ_ONCE(sk->sk_rcvbuf))) { @@ -1460,54 +1452,13 @@ int sk_setsockopt(struct sock *sk, int level, int optname, WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); break; } - case SO_ATTACH_FILTER: { - struct sock_fprog fprog; - - ret = copy_bpf_fprog_from_user(&fprog, optval, optlen); - if (!ret) - ret = sk_attach_filter(&fprog, sk); - break; - } + case SO_ATTACH_FILTER: case SO_ATTACH_BPF: - ret = -EINVAL; - if (optlen == sizeof(u32)) { - u32 ufd; - - ret = -EFAULT; - if (copy_from_sockptr(&ufd, optval, sizeof(ufd))) - break; - - ret = sk_attach_bpf(ufd, sk); - } - break; - - case SO_ATTACH_REUSEPORT_CBPF: { - struct sock_fprog fprog; - - ret = copy_bpf_fprog_from_user(&fprog, optval, optlen); - if (!ret) - ret = sk_reuseport_attach_filter(&fprog, sk); - break; - } + case SO_ATTACH_REUSEPORT_CBPF: case SO_ATTACH_REUSEPORT_EBPF: - ret = -EINVAL; - if (optlen == sizeof(u32)) { - u32 ufd; - - ret = -EFAULT; - if (copy_from_sockptr(&ufd, optval, sizeof(ufd))) - break; - - ret = sk_reuseport_attach_bpf(ufd, sk); - } - break; - case SO_DETACH_REUSEPORT_BPF: - ret = reuseport_detach_prog(sk); - break; - case SO_DETACH_FILTER: - ret = sk_detach_filter(sk); + ret = -EOPNOTSUPP; break; case SO_LOCK_FILTER: @@ -1579,7 +1530,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname, break; case SO_INCOMING_CPU: - reuseport_update_incoming_cpu(sk, val); + WRITE_ONCE(sk->sk_incoming_cpu, val); break; case SO_CNX_ADVICE: @@ -2039,18 +1990,14 @@ int sk_getsockopt(struct sock *sk, int level, int optname, return sock_getbindtodevice(sk, optval, optlen, len); case SO_GET_FILTER: - len = sk_get_filter(sk, optval, len); - if (len < 0) - return len; - - goto lenout; + return -EOPNOTSUPP; case SO_LOCK_FILTER: v.val = sock_flag(sk, SOCK_FILTER_LOCKED); break; case SO_BPF_EXTENSIONS: - v.val = bpf_tell_extensions(); + v.val = 0; break; case SO_SELECT_ERR_QUEUE: @@ -2344,18 +2291,10 @@ static void __sk_destruct(struct rcu_head *head) { struct sock *sk = container_of(head, struct sock, sk_rcu); struct net *net = sock_net(sk); - struct sk_filter *filter; if (sk->sk_destruct) sk->sk_destruct(sk); - filter = rcu_dereference_check(sk->sk_filter, - refcount_read(&sk->sk_wmem_alloc) == 0); - if (filter) { - sk_filter_uncharge(sk, filter); - RCU_INIT_POINTER(sk->sk_filter, NULL); - } - sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); #ifdef CONFIG_BPF_SYSCALL @@ -2401,11 +2340,6 @@ void sk_destruct(struct sock *sk) { bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE); - if (rcu_access_pointer(sk->sk_reuseport_cb)) { - reuseport_detach_sock(sk); - use_call_rcu = true; - } - if (use_call_rcu) call_rcu(&sk->sk_rcu, __sk_destruct); else @@ -2474,8 +2408,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority, bool lock) { struct proto *prot = READ_ONCE(sk->sk_prot); - struct sk_filter *filter; - bool is_charged = true; struct sock *newsk; newsk = sk_prot_alloc(prot, priority, sk->sk_family); @@ -2537,25 +2469,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority, cgroup_sk_clone(&newsk->sk_cgrp_data); - rcu_read_lock(); - filter = rcu_dereference(sk->sk_filter); - if (filter != NULL) - /* though it's an empty new sock, the charging may fail - * if sysctl_optmem_max was changed between creation of - * original socket and cloning - */ - is_charged = sk_filter_charge(newsk, filter); - RCU_INIT_POINTER(newsk->sk_filter, filter); - rcu_read_unlock(); - - if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) { - /* We need to make sure that we don't uncharge the new - * socket if we couldn't charge it in the first place - * as otherwise we uncharge the parent's filter. - */ - if (!is_charged) - RCU_INIT_POINTER(newsk->sk_filter, NULL); + RCU_INIT_POINTER(newsk->sk_filter, NULL); + if (unlikely(xfrm_sk_clone_policy(newsk, sk))) { goto free; }