cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

tcp_bpf.c (15156B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
      3
      4#include <linux/skmsg.h>
      5#include <linux/filter.h>
      6#include <linux/bpf.h>
      7#include <linux/init.h>
      8#include <linux/wait.h>
      9
     10#include <net/inet_common.h>
     11#include <net/tls.h>
     12
     13static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
     14			   struct sk_msg *msg, u32 apply_bytes, int flags)
     15{
     16	bool apply = apply_bytes;
     17	struct scatterlist *sge;
     18	u32 size, copied = 0;
     19	struct sk_msg *tmp;
     20	int i, ret = 0;
     21
     22	tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
     23	if (unlikely(!tmp))
     24		return -ENOMEM;
     25
     26	lock_sock(sk);
     27	tmp->sg.start = msg->sg.start;
     28	i = msg->sg.start;
     29	do {
     30		sge = sk_msg_elem(msg, i);
     31		size = (apply && apply_bytes < sge->length) ?
     32			apply_bytes : sge->length;
     33		if (!sk_wmem_schedule(sk, size)) {
     34			if (!copied)
     35				ret = -ENOMEM;
     36			break;
     37		}
     38
     39		sk_mem_charge(sk, size);
     40		sk_msg_xfer(tmp, msg, i, size);
     41		copied += size;
     42		if (sge->length)
     43			get_page(sk_msg_page(tmp, i));
     44		sk_msg_iter_var_next(i);
     45		tmp->sg.end = i;
     46		if (apply) {
     47			apply_bytes -= size;
     48			if (!apply_bytes)
     49				break;
     50		}
     51	} while (i != msg->sg.end);
     52
     53	if (!ret) {
     54		msg->sg.start = i;
     55		sk_psock_queue_msg(psock, tmp);
     56		sk_psock_data_ready(sk, psock);
     57	} else {
     58		sk_msg_free(sk, tmp);
     59		kfree(tmp);
     60	}
     61
     62	release_sock(sk);
     63	return ret;
     64}
     65
     66static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
     67			int flags, bool uncharge)
     68{
     69	bool apply = apply_bytes;
     70	struct scatterlist *sge;
     71	struct page *page;
     72	int size, ret = 0;
     73	u32 off;
     74
     75	while (1) {
     76		bool has_tx_ulp;
     77
     78		sge = sk_msg_elem(msg, msg->sg.start);
     79		size = (apply && apply_bytes < sge->length) ?
     80			apply_bytes : sge->length;
     81		off  = sge->offset;
     82		page = sg_page(sge);
     83
     84		tcp_rate_check_app_limited(sk);
     85retry:
     86		has_tx_ulp = tls_sw_has_ctx_tx(sk);
     87		if (has_tx_ulp) {
     88			flags |= MSG_SENDPAGE_NOPOLICY;
     89			ret = kernel_sendpage_locked(sk,
     90						     page, off, size, flags);
     91		} else {
     92			ret = do_tcp_sendpages(sk, page, off, size, flags);
     93		}
     94
     95		if (ret <= 0)
     96			return ret;
     97		if (apply)
     98			apply_bytes -= ret;
     99		msg->sg.size -= ret;
    100		sge->offset += ret;
    101		sge->length -= ret;
    102		if (uncharge)
    103			sk_mem_uncharge(sk, ret);
    104		if (ret != size) {
    105			size -= ret;
    106			off  += ret;
    107			goto retry;
    108		}
    109		if (!sge->length) {
    110			put_page(page);
    111			sk_msg_iter_next(msg, start);
    112			sg_init_table(sge, 1);
    113			if (msg->sg.start == msg->sg.end)
    114				break;
    115		}
    116		if (apply && !apply_bytes)
    117			break;
    118	}
    119
    120	return 0;
    121}
    122
    123static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
    124			       u32 apply_bytes, int flags, bool uncharge)
    125{
    126	int ret;
    127
    128	lock_sock(sk);
    129	ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
    130	release_sock(sk);
    131	return ret;
    132}
    133
    134int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg,
    135			  u32 bytes, int flags)
    136{
    137	bool ingress = sk_msg_to_ingress(msg);
    138	struct sk_psock *psock = sk_psock_get(sk);
    139	int ret;
    140
    141	if (unlikely(!psock))
    142		return -EPIPE;
    143
    144	ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
    145			tcp_bpf_push_locked(sk, msg, bytes, flags, false);
    146	sk_psock_put(sk, psock);
    147	return ret;
    148}
    149EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
    150
    151#ifdef CONFIG_BPF_SYSCALL
    152static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
    153			     long timeo)
    154{
    155	DEFINE_WAIT_FUNC(wait, woken_wake_function);
    156	int ret = 0;
    157
    158	if (sk->sk_shutdown & RCV_SHUTDOWN)
    159		return 1;
    160
    161	if (!timeo)
    162		return ret;
    163
    164	add_wait_queue(sk_sleep(sk), &wait);
    165	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
    166	ret = sk_wait_event(sk, &timeo,
    167			    !list_empty(&psock->ingress_msg) ||
    168			    !skb_queue_empty(&sk->sk_receive_queue), &wait);
    169	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
    170	remove_wait_queue(sk_sleep(sk), &wait);
    171	return ret;
    172}
    173
    174static int tcp_bpf_recvmsg_parser(struct sock *sk,
    175				  struct msghdr *msg,
    176				  size_t len,
    177				  int flags,
    178				  int *addr_len)
    179{
    180	struct sk_psock *psock;
    181	int copied;
    182
    183	if (unlikely(flags & MSG_ERRQUEUE))
    184		return inet_recv_error(sk, msg, len, addr_len);
    185
    186	psock = sk_psock_get(sk);
    187	if (unlikely(!psock))
    188		return tcp_recvmsg(sk, msg, len, flags, addr_len);
    189
    190	lock_sock(sk);
    191msg_bytes_ready:
    192	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
    193	if (!copied) {
    194		long timeo;
    195		int data;
    196
    197		if (sock_flag(sk, SOCK_DONE))
    198			goto out;
    199
    200		if (sk->sk_err) {
    201			copied = sock_error(sk);
    202			goto out;
    203		}
    204
    205		if (sk->sk_shutdown & RCV_SHUTDOWN)
    206			goto out;
    207
    208		if (sk->sk_state == TCP_CLOSE) {
    209			copied = -ENOTCONN;
    210			goto out;
    211		}
    212
    213		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
    214		if (!timeo) {
    215			copied = -EAGAIN;
    216			goto out;
    217		}
    218
    219		if (signal_pending(current)) {
    220			copied = sock_intr_errno(timeo);
    221			goto out;
    222		}
    223
    224		data = tcp_msg_wait_data(sk, psock, timeo);
    225		if (data && !sk_psock_queue_empty(psock))
    226			goto msg_bytes_ready;
    227		copied = -EAGAIN;
    228	}
    229out:
    230	release_sock(sk);
    231	sk_psock_put(sk, psock);
    232	return copied;
    233}
    234
    235static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
    236			   int flags, int *addr_len)
    237{
    238	struct sk_psock *psock;
    239	int copied, ret;
    240
    241	if (unlikely(flags & MSG_ERRQUEUE))
    242		return inet_recv_error(sk, msg, len, addr_len);
    243
    244	psock = sk_psock_get(sk);
    245	if (unlikely(!psock))
    246		return tcp_recvmsg(sk, msg, len, flags, addr_len);
    247	if (!skb_queue_empty(&sk->sk_receive_queue) &&
    248	    sk_psock_queue_empty(psock)) {
    249		sk_psock_put(sk, psock);
    250		return tcp_recvmsg(sk, msg, len, flags, addr_len);
    251	}
    252	lock_sock(sk);
    253msg_bytes_ready:
    254	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
    255	if (!copied) {
    256		long timeo;
    257		int data;
    258
    259		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
    260		data = tcp_msg_wait_data(sk, psock, timeo);
    261		if (data) {
    262			if (!sk_psock_queue_empty(psock))
    263				goto msg_bytes_ready;
    264			release_sock(sk);
    265			sk_psock_put(sk, psock);
    266			return tcp_recvmsg(sk, msg, len, flags, addr_len);
    267		}
    268		copied = -EAGAIN;
    269	}
    270	ret = copied;
    271	release_sock(sk);
    272	sk_psock_put(sk, psock);
    273	return ret;
    274}
    275
    276static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
    277				struct sk_msg *msg, int *copied, int flags)
    278{
    279	bool cork = false, enospc = sk_msg_full(msg);
    280	struct sock *sk_redir;
    281	u32 tosend, delta = 0;
    282	u32 eval = __SK_NONE;
    283	int ret;
    284
    285more_data:
    286	if (psock->eval == __SK_NONE) {
    287		/* Track delta in msg size to add/subtract it on SK_DROP from
    288		 * returned to user copied size. This ensures user doesn't
    289		 * get a positive return code with msg_cut_data and SK_DROP
    290		 * verdict.
    291		 */
    292		delta = msg->sg.size;
    293		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
    294		delta -= msg->sg.size;
    295	}
    296
    297	if (msg->cork_bytes &&
    298	    msg->cork_bytes > msg->sg.size && !enospc) {
    299		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
    300		if (!psock->cork) {
    301			psock->cork = kzalloc(sizeof(*psock->cork),
    302					      GFP_ATOMIC | __GFP_NOWARN);
    303			if (!psock->cork)
    304				return -ENOMEM;
    305		}
    306		memcpy(psock->cork, msg, sizeof(*msg));
    307		return 0;
    308	}
    309
    310	tosend = msg->sg.size;
    311	if (psock->apply_bytes && psock->apply_bytes < tosend)
    312		tosend = psock->apply_bytes;
    313
    314	switch (psock->eval) {
    315	case __SK_PASS:
    316		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
    317		if (unlikely(ret)) {
    318			*copied -= sk_msg_free(sk, msg);
    319			break;
    320		}
    321		sk_msg_apply_bytes(psock, tosend);
    322		break;
    323	case __SK_REDIRECT:
    324		sk_redir = psock->sk_redir;
    325		sk_msg_apply_bytes(psock, tosend);
    326		if (!psock->apply_bytes) {
    327			/* Clean up before releasing the sock lock. */
    328			eval = psock->eval;
    329			psock->eval = __SK_NONE;
    330			psock->sk_redir = NULL;
    331		}
    332		if (psock->cork) {
    333			cork = true;
    334			psock->cork = NULL;
    335		}
    336		sk_msg_return(sk, msg, msg->sg.size);
    337		release_sock(sk);
    338
    339		ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
    340
    341		if (eval == __SK_REDIRECT)
    342			sock_put(sk_redir);
    343
    344		lock_sock(sk);
    345		if (unlikely(ret < 0)) {
    346			int free = sk_msg_free_nocharge(sk, msg);
    347
    348			if (!cork)
    349				*copied -= free;
    350		}
    351		if (cork) {
    352			sk_msg_free(sk, msg);
    353			kfree(msg);
    354			msg = NULL;
    355			ret = 0;
    356		}
    357		break;
    358	case __SK_DROP:
    359	default:
    360		sk_msg_free_partial(sk, msg, tosend);
    361		sk_msg_apply_bytes(psock, tosend);
    362		*copied -= (tosend + delta);
    363		return -EACCES;
    364	}
    365
    366	if (likely(!ret)) {
    367		if (!psock->apply_bytes) {
    368			psock->eval =  __SK_NONE;
    369			if (psock->sk_redir) {
    370				sock_put(psock->sk_redir);
    371				psock->sk_redir = NULL;
    372			}
    373		}
    374		if (msg &&
    375		    msg->sg.data[msg->sg.start].page_link &&
    376		    msg->sg.data[msg->sg.start].length) {
    377			if (eval == __SK_REDIRECT)
    378				sk_mem_charge(sk, msg->sg.size);
    379			goto more_data;
    380		}
    381	}
    382	return ret;
    383}
    384
    385static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
    386{
    387	struct sk_msg tmp, *msg_tx = NULL;
    388	int copied = 0, err = 0;
    389	struct sk_psock *psock;
    390	long timeo;
    391	int flags;
    392
    393	/* Don't let internal do_tcp_sendpages() flags through */
    394	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
    395	flags |= MSG_NO_SHARED_FRAGS;
    396
    397	psock = sk_psock_get(sk);
    398	if (unlikely(!psock))
    399		return tcp_sendmsg(sk, msg, size);
    400
    401	lock_sock(sk);
    402	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
    403	while (msg_data_left(msg)) {
    404		bool enospc = false;
    405		u32 copy, osize;
    406
    407		if (sk->sk_err) {
    408			err = -sk->sk_err;
    409			goto out_err;
    410		}
    411
    412		copy = msg_data_left(msg);
    413		if (!sk_stream_memory_free(sk))
    414			goto wait_for_sndbuf;
    415		if (psock->cork) {
    416			msg_tx = psock->cork;
    417		} else {
    418			msg_tx = &tmp;
    419			sk_msg_init(msg_tx);
    420		}
    421
    422		osize = msg_tx->sg.size;
    423		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
    424		if (err) {
    425			if (err != -ENOSPC)
    426				goto wait_for_memory;
    427			enospc = true;
    428			copy = msg_tx->sg.size - osize;
    429		}
    430
    431		err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
    432					       copy);
    433		if (err < 0) {
    434			sk_msg_trim(sk, msg_tx, osize);
    435			goto out_err;
    436		}
    437
    438		copied += copy;
    439		if (psock->cork_bytes) {
    440			if (size > psock->cork_bytes)
    441				psock->cork_bytes = 0;
    442			else
    443				psock->cork_bytes -= size;
    444			if (psock->cork_bytes && !enospc)
    445				goto out_err;
    446			/* All cork bytes are accounted, rerun the prog. */
    447			psock->eval = __SK_NONE;
    448			psock->cork_bytes = 0;
    449		}
    450
    451		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
    452		if (unlikely(err < 0))
    453			goto out_err;
    454		continue;
    455wait_for_sndbuf:
    456		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
    457wait_for_memory:
    458		err = sk_stream_wait_memory(sk, &timeo);
    459		if (err) {
    460			if (msg_tx && msg_tx != psock->cork)
    461				sk_msg_free(sk, msg_tx);
    462			goto out_err;
    463		}
    464	}
    465out_err:
    466	if (err < 0)
    467		err = sk_stream_error(sk, msg->msg_flags, err);
    468	release_sock(sk);
    469	sk_psock_put(sk, psock);
    470	return copied ? copied : err;
    471}
    472
    473static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
    474			    size_t size, int flags)
    475{
    476	struct sk_msg tmp, *msg = NULL;
    477	int err = 0, copied = 0;
    478	struct sk_psock *psock;
    479	bool enospc = false;
    480
    481	psock = sk_psock_get(sk);
    482	if (unlikely(!psock))
    483		return tcp_sendpage(sk, page, offset, size, flags);
    484
    485	lock_sock(sk);
    486	if (psock->cork) {
    487		msg = psock->cork;
    488	} else {
    489		msg = &tmp;
    490		sk_msg_init(msg);
    491	}
    492
    493	/* Catch case where ring is full and sendpage is stalled. */
    494	if (unlikely(sk_msg_full(msg)))
    495		goto out_err;
    496
    497	sk_msg_page_add(msg, page, size, offset);
    498	sk_mem_charge(sk, size);
    499	copied = size;
    500	if (sk_msg_full(msg))
    501		enospc = true;
    502	if (psock->cork_bytes) {
    503		if (size > psock->cork_bytes)
    504			psock->cork_bytes = 0;
    505		else
    506			psock->cork_bytes -= size;
    507		if (psock->cork_bytes && !enospc)
    508			goto out_err;
    509		/* All cork bytes are accounted, rerun the prog. */
    510		psock->eval = __SK_NONE;
    511		psock->cork_bytes = 0;
    512	}
    513
    514	err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
    515out_err:
    516	release_sock(sk);
    517	sk_psock_put(sk, psock);
    518	return copied ? copied : err;
    519}
    520
    521enum {
    522	TCP_BPF_IPV4,
    523	TCP_BPF_IPV6,
    524	TCP_BPF_NUM_PROTS,
    525};
    526
    527enum {
    528	TCP_BPF_BASE,
    529	TCP_BPF_TX,
    530	TCP_BPF_RX,
    531	TCP_BPF_TXRX,
    532	TCP_BPF_NUM_CFGS,
    533};
    534
    535static struct proto *tcpv6_prot_saved __read_mostly;
    536static DEFINE_SPINLOCK(tcpv6_prot_lock);
    537static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
    538
    539static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
    540				   struct proto *base)
    541{
    542	prot[TCP_BPF_BASE]			= *base;
    543	prot[TCP_BPF_BASE].close		= sock_map_close;
    544	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
    545	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
    546
    547	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
    548	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
    549	prot[TCP_BPF_TX].sendpage		= tcp_bpf_sendpage;
    550
    551	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
    552	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
    553
    554	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
    555	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
    556}
    557
    558static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
    559{
    560	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
    561		spin_lock_bh(&tcpv6_prot_lock);
    562		if (likely(ops != tcpv6_prot_saved)) {
    563			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
    564			smp_store_release(&tcpv6_prot_saved, ops);
    565		}
    566		spin_unlock_bh(&tcpv6_prot_lock);
    567	}
    568}
    569
    570static int __init tcp_bpf_v4_build_proto(void)
    571{
    572	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
    573	return 0;
    574}
    575late_initcall(tcp_bpf_v4_build_proto);
    576
    577static int tcp_bpf_assert_proto_ops(struct proto *ops)
    578{
    579	/* In order to avoid retpoline, we make assumptions when we call
    580	 * into ops if e.g. a psock is not present. Make sure they are
    581	 * indeed valid assumptions.
    582	 */
    583	return ops->recvmsg  == tcp_recvmsg &&
    584	       ops->sendmsg  == tcp_sendmsg &&
    585	       ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
    586}
    587
    588int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
    589{
    590	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
    591	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
    592
    593	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
    594		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
    595	}
    596
    597	if (restore) {
    598		if (inet_csk_has_ulp(sk)) {
    599			/* TLS does not have an unhash proto in SW cases,
    600			 * but we need to ensure we stop using the sock_map
    601			 * unhash routine because the associated psock is being
    602			 * removed. So use the original unhash handler.
    603			 */
    604			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
    605			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
    606		} else {
    607			sk->sk_write_space = psock->saved_write_space;
    608			/* Pairs with lockless read in sk_clone_lock() */
    609			WRITE_ONCE(sk->sk_prot, psock->sk_proto);
    610		}
    611		return 0;
    612	}
    613
    614	if (sk->sk_family == AF_INET6) {
    615		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
    616			return -EINVAL;
    617
    618		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
    619	}
    620
    621	/* Pairs with lockless read in sk_clone_lock() */
    622	WRITE_ONCE(sk->sk_prot, &tcp_bpf_prots[family][config]);
    623	return 0;
    624}
    625EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
    626
    627/* If a child got cloned from a listening socket that had tcp_bpf
    628 * protocol callbacks installed, we need to restore the callbacks to
    629 * the default ones because the child does not inherit the psock state
    630 * that tcp_bpf callbacks expect.
    631 */
    632void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
    633{
    634	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
    635	struct proto *prot = newsk->sk_prot;
    636
    637	if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
    638		newsk->sk_prot = sk->sk_prot_creator;
    639}
    640#endif /* CONFIG_BPF_SYSCALL */