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

reuseport_array.c (8734B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 * Copyright (c) 2018 Facebook
      4 */
      5#include <linux/bpf.h>
      6#include <linux/err.h>
      7#include <linux/sock_diag.h>
      8#include <net/sock_reuseport.h>
      9#include <linux/btf_ids.h>
     10
     11struct reuseport_array {
     12	struct bpf_map map;
     13	struct sock __rcu *ptrs[];
     14};
     15
     16static struct reuseport_array *reuseport_array(struct bpf_map *map)
     17{
     18	return (struct reuseport_array *)map;
     19}
     20
     21/* The caller must hold the reuseport_lock */
     22void bpf_sk_reuseport_detach(struct sock *sk)
     23{
     24	uintptr_t sk_user_data;
     25
     26	write_lock_bh(&sk->sk_callback_lock);
     27	sk_user_data = (uintptr_t)sk->sk_user_data;
     28	if (sk_user_data & SK_USER_DATA_BPF) {
     29		struct sock __rcu **socks;
     30
     31		socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
     32		WRITE_ONCE(sk->sk_user_data, NULL);
     33		/*
     34		 * Do not move this NULL assignment outside of
     35		 * sk->sk_callback_lock because there is
     36		 * a race with reuseport_array_free()
     37		 * which does not hold the reuseport_lock.
     38		 */
     39		RCU_INIT_POINTER(*socks, NULL);
     40	}
     41	write_unlock_bh(&sk->sk_callback_lock);
     42}
     43
     44static int reuseport_array_alloc_check(union bpf_attr *attr)
     45{
     46	if (attr->value_size != sizeof(u32) &&
     47	    attr->value_size != sizeof(u64))
     48		return -EINVAL;
     49
     50	return array_map_alloc_check(attr);
     51}
     52
     53static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key)
     54{
     55	struct reuseport_array *array = reuseport_array(map);
     56	u32 index = *(u32 *)key;
     57
     58	if (unlikely(index >= array->map.max_entries))
     59		return NULL;
     60
     61	return rcu_dereference(array->ptrs[index]);
     62}
     63
     64/* Called from syscall only */
     65static int reuseport_array_delete_elem(struct bpf_map *map, void *key)
     66{
     67	struct reuseport_array *array = reuseport_array(map);
     68	u32 index = *(u32 *)key;
     69	struct sock *sk;
     70	int err;
     71
     72	if (index >= map->max_entries)
     73		return -E2BIG;
     74
     75	if (!rcu_access_pointer(array->ptrs[index]))
     76		return -ENOENT;
     77
     78	spin_lock_bh(&reuseport_lock);
     79
     80	sk = rcu_dereference_protected(array->ptrs[index],
     81				       lockdep_is_held(&reuseport_lock));
     82	if (sk) {
     83		write_lock_bh(&sk->sk_callback_lock);
     84		WRITE_ONCE(sk->sk_user_data, NULL);
     85		RCU_INIT_POINTER(array->ptrs[index], NULL);
     86		write_unlock_bh(&sk->sk_callback_lock);
     87		err = 0;
     88	} else {
     89		err = -ENOENT;
     90	}
     91
     92	spin_unlock_bh(&reuseport_lock);
     93
     94	return err;
     95}
     96
     97static void reuseport_array_free(struct bpf_map *map)
     98{
     99	struct reuseport_array *array = reuseport_array(map);
    100	struct sock *sk;
    101	u32 i;
    102
    103	/*
    104	 * ops->map_*_elem() will not be able to access this
    105	 * array now. Hence, this function only races with
    106	 * bpf_sk_reuseport_detach() which was triggered by
    107	 * close() or disconnect().
    108	 *
    109	 * This function and bpf_sk_reuseport_detach() are
    110	 * both removing sk from "array".  Who removes it
    111	 * first does not matter.
    112	 *
    113	 * The only concern here is bpf_sk_reuseport_detach()
    114	 * may access "array" which is being freed here.
    115	 * bpf_sk_reuseport_detach() access this "array"
    116	 * through sk->sk_user_data _and_ with sk->sk_callback_lock
    117	 * held which is enough because this "array" is not freed
    118	 * until all sk->sk_user_data has stopped referencing this "array".
    119	 *
    120	 * Hence, due to the above, taking "reuseport_lock" is not
    121	 * needed here.
    122	 */
    123
    124	/*
    125	 * Since reuseport_lock is not taken, sk is accessed under
    126	 * rcu_read_lock()
    127	 */
    128	rcu_read_lock();
    129	for (i = 0; i < map->max_entries; i++) {
    130		sk = rcu_dereference(array->ptrs[i]);
    131		if (sk) {
    132			write_lock_bh(&sk->sk_callback_lock);
    133			/*
    134			 * No need for WRITE_ONCE(). At this point,
    135			 * no one is reading it without taking the
    136			 * sk->sk_callback_lock.
    137			 */
    138			sk->sk_user_data = NULL;
    139			write_unlock_bh(&sk->sk_callback_lock);
    140			RCU_INIT_POINTER(array->ptrs[i], NULL);
    141		}
    142	}
    143	rcu_read_unlock();
    144
    145	/*
    146	 * Once reaching here, all sk->sk_user_data is not
    147	 * referencing this "array". "array" can be freed now.
    148	 */
    149	bpf_map_area_free(array);
    150}
    151
    152static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
    153{
    154	int numa_node = bpf_map_attr_numa_node(attr);
    155	struct reuseport_array *array;
    156
    157	if (!bpf_capable())
    158		return ERR_PTR(-EPERM);
    159
    160	/* allocate all map elements and zero-initialize them */
    161	array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
    162	if (!array)
    163		return ERR_PTR(-ENOMEM);
    164
    165	/* copy mandatory map attributes */
    166	bpf_map_init_from_attr(&array->map, attr);
    167
    168	return &array->map;
    169}
    170
    171int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
    172				       void *value)
    173{
    174	struct sock *sk;
    175	int err;
    176
    177	if (map->value_size != sizeof(u64))
    178		return -ENOSPC;
    179
    180	rcu_read_lock();
    181	sk = reuseport_array_lookup_elem(map, key);
    182	if (sk) {
    183		*(u64 *)value = __sock_gen_cookie(sk);
    184		err = 0;
    185	} else {
    186		err = -ENOENT;
    187	}
    188	rcu_read_unlock();
    189
    190	return err;
    191}
    192
    193static int
    194reuseport_array_update_check(const struct reuseport_array *array,
    195			     const struct sock *nsk,
    196			     const struct sock *osk,
    197			     const struct sock_reuseport *nsk_reuse,
    198			     u32 map_flags)
    199{
    200	if (osk && map_flags == BPF_NOEXIST)
    201		return -EEXIST;
    202
    203	if (!osk && map_flags == BPF_EXIST)
    204		return -ENOENT;
    205
    206	if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP)
    207		return -ENOTSUPP;
    208
    209	if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6)
    210		return -ENOTSUPP;
    211
    212	if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM)
    213		return -ENOTSUPP;
    214
    215	/*
    216	 * sk must be hashed (i.e. listening in the TCP case or binded
    217	 * in the UDP case) and
    218	 * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
    219	 *
    220	 * Also, sk will be used in bpf helper that is protected by
    221	 * rcu_read_lock().
    222	 */
    223	if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse)
    224		return -EINVAL;
    225
    226	/* READ_ONCE because the sk->sk_callback_lock may not be held here */
    227	if (READ_ONCE(nsk->sk_user_data))
    228		return -EBUSY;
    229
    230	return 0;
    231}
    232
    233/*
    234 * Called from syscall only.
    235 * The "nsk" in the fd refcnt.
    236 * The "osk" and "reuse" are protected by reuseport_lock.
    237 */
    238int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
    239				       void *value, u64 map_flags)
    240{
    241	struct reuseport_array *array = reuseport_array(map);
    242	struct sock *free_osk = NULL, *osk, *nsk;
    243	struct sock_reuseport *reuse;
    244	u32 index = *(u32 *)key;
    245	uintptr_t sk_user_data;
    246	struct socket *socket;
    247	int err, fd;
    248
    249	if (map_flags > BPF_EXIST)
    250		return -EINVAL;
    251
    252	if (index >= map->max_entries)
    253		return -E2BIG;
    254
    255	if (map->value_size == sizeof(u64)) {
    256		u64 fd64 = *(u64 *)value;
    257
    258		if (fd64 > S32_MAX)
    259			return -EINVAL;
    260		fd = fd64;
    261	} else {
    262		fd = *(int *)value;
    263	}
    264
    265	socket = sockfd_lookup(fd, &err);
    266	if (!socket)
    267		return err;
    268
    269	nsk = socket->sk;
    270	if (!nsk) {
    271		err = -EINVAL;
    272		goto put_file;
    273	}
    274
    275	/* Quick checks before taking reuseport_lock */
    276	err = reuseport_array_update_check(array, nsk,
    277					   rcu_access_pointer(array->ptrs[index]),
    278					   rcu_access_pointer(nsk->sk_reuseport_cb),
    279					   map_flags);
    280	if (err)
    281		goto put_file;
    282
    283	spin_lock_bh(&reuseport_lock);
    284	/*
    285	 * Some of the checks only need reuseport_lock
    286	 * but it is done under sk_callback_lock also
    287	 * for simplicity reason.
    288	 */
    289	write_lock_bh(&nsk->sk_callback_lock);
    290
    291	osk = rcu_dereference_protected(array->ptrs[index],
    292					lockdep_is_held(&reuseport_lock));
    293	reuse = rcu_dereference_protected(nsk->sk_reuseport_cb,
    294					  lockdep_is_held(&reuseport_lock));
    295	err = reuseport_array_update_check(array, nsk, osk, reuse, map_flags);
    296	if (err)
    297		goto put_file_unlock;
    298
    299	sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY |
    300		SK_USER_DATA_BPF;
    301	WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
    302	rcu_assign_pointer(array->ptrs[index], nsk);
    303	free_osk = osk;
    304	err = 0;
    305
    306put_file_unlock:
    307	write_unlock_bh(&nsk->sk_callback_lock);
    308
    309	if (free_osk) {
    310		write_lock_bh(&free_osk->sk_callback_lock);
    311		WRITE_ONCE(free_osk->sk_user_data, NULL);
    312		write_unlock_bh(&free_osk->sk_callback_lock);
    313	}
    314
    315	spin_unlock_bh(&reuseport_lock);
    316put_file:
    317	fput(socket->file);
    318	return err;
    319}
    320
    321/* Called from syscall */
    322static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
    323					void *next_key)
    324{
    325	struct reuseport_array *array = reuseport_array(map);
    326	u32 index = key ? *(u32 *)key : U32_MAX;
    327	u32 *next = (u32 *)next_key;
    328
    329	if (index >= array->map.max_entries) {
    330		*next = 0;
    331		return 0;
    332	}
    333
    334	if (index == array->map.max_entries - 1)
    335		return -ENOENT;
    336
    337	*next = index + 1;
    338	return 0;
    339}
    340
    341BTF_ID_LIST_SINGLE(reuseport_array_map_btf_ids, struct, reuseport_array)
    342const struct bpf_map_ops reuseport_array_ops = {
    343	.map_meta_equal = bpf_map_meta_equal,
    344	.map_alloc_check = reuseport_array_alloc_check,
    345	.map_alloc = reuseport_array_alloc,
    346	.map_free = reuseport_array_free,
    347	.map_lookup_elem = reuseport_array_lookup_elem,
    348	.map_get_next_key = reuseport_array_get_next_key,
    349	.map_delete_elem = reuseport_array_delete_elem,
    350	.map_btf_id = &reuseport_array_map_btf_ids[0],
    351};