Commit ac7ac432 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'New nf_conntrack kfuncs for insertion, changing timeout, status'

Kumar Kartikeya Dwivedi says:

====================

Introduce the following new kfuncs:
 - bpf_{xdp,skb}_ct_alloc
 - bpf_ct_insert_entry
 - bpf_ct_{set,change}_timeout
 - bpf_ct_{set,change}_status

The setting of timeout and status on allocated or inserted/looked up CT
is same as the ctnetlink interface, hence code is refactored and shared
with the kfuncs. It is ensured allocated CT cannot be passed to kfuncs
that expected inserted CT, and vice versa. Please see individual patches
for details.

Changelog:
----------
v6 -> v7:
v6: https://lore.kernel.org/bpf/20220719132430.19993-1-memxor@gmail.com

 * Use .long to encode flags (Alexei)
 * Fix description of KF_RET_NULL in documentation (Toke)

v5 -> v6:
v5: https://lore.kernel.org/bpf/20220623192637.3866852-1-memxor@gmail.com

 * Introduce kfunc flags, rework verifier to work with them
 * Add documentation for kfuncs
 * Add comment explaining TRUSTED_ARGS kfunc flag (Alexei)
 * Fix missing offset check for trusted arguments (Alexei)
 * Change nf_conntrack test minimum delta value to 8

v4 -> v5:
v4: https://lore.kernel.org/bpf/cover.1653600577.git.lorenzo@kernel.org

 * Drop read-only PTR_TO_BTF_ID approach, use struct nf_conn___init (Alexei)
 * Drop acquire release pair code that is no longer required (Alexei)
 * Disable writes into nf_conn, use dedicated helpers (Florian, Alexei)
 * Refactor and share ctnetlink code for setting timeout and status
 * Do strict type matching on finding __ref suffix on argument to
   prevent passing nf_conn___init as nf_conn (offset = 0, match on walk)
 * Remove bpf_ct_opts parameter from bpf_ct_insert_entry
 * Update selftests for new additions, add more negative tests

v3 -> v4:
v3: https://lore.kernel.org/bpf/cover.1652870182.git.lorenzo@kernel.org

 * split bpf_xdp_ct_add in bpf_xdp_ct_alloc/bpf_skb_ct_alloc and
   bpf_ct_insert_entry
 * add verifier code to properly populate/configure ct entry
 * improve selftests

v2 -> v3:
v2: https://lore.kernel.org/bpf/cover.1652372970.git.lorenzo@kernel.org

 * add bpf_xdp_ct_add and bpf_ct_refresh_timeout kfunc helpers
 * remove conntrack dependency from selftests
 * add support for forcing kfunc args to be referenced and related selftests

v1 -> v2:
v1: https://lore.kernel.org/bpf/1327f8f5696ff2bc60400e8f3b79047914ccc837.1651595019.git.lorenzo@kernel.org

 * add bpf_ct_refresh_timeout kfunc selftest

Kumar Kartikeya Dwivedi (10):
  bpf: Introduce 8-byte BTF set
  tools/resolve_btfids: Add support for 8-byte BTF sets
  bpf: Switch to new kfunc flags infrastructure
  bpf: Add support for forcing kfunc args to be trusted
  bpf: Add documentation for kfuncs
  net: netfilter: Deduplicate code in bpf_{xdp,skb}_ct_lookup
  net: netfilter: Add kfuncs to set and change CT timeout
  selftests/bpf: Add verifier tests for trusted kfunc args
  selftests/bpf: Add negative tests for new nf_conntrack kfuncs
  selftests/bpf: Fix test_verifier failed test in unprivileged mode
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 5cb62b75 e3fa4735
...@@ -19,6 +19,7 @@ that goes into great technical depth about the BPF Architecture. ...@@ -19,6 +19,7 @@ that goes into great technical depth about the BPF Architecture.
faq faq
syscall_api syscall_api
helpers helpers
kfuncs
programs programs
maps maps
bpf_prog_run bpf_prog_run
......
=============================
BPF Kernel Functions (kfuncs)
=============================
1. Introduction
===============
BPF Kernel Functions or more commonly known as kfuncs are functions in the Linux
kernel which are exposed for use by BPF programs. Unlike normal BPF helpers,
kfuncs do not have a stable interface and can change from one kernel release to
another. Hence, BPF programs need to be updated in response to changes in the
kernel.
2. Defining a kfunc
===================
There are two ways to expose a kernel function to BPF programs, either make an
existing function in the kernel visible, or add a new wrapper for BPF. In both
cases, care must be taken that BPF program can only call such function in a
valid context. To enforce this, visibility of a kfunc can be per program type.
If you are not creating a BPF wrapper for existing kernel function, skip ahead
to :ref:`BPF_kfunc_nodef`.
2.1 Creating a wrapper kfunc
----------------------------
When defining a wrapper kfunc, the wrapper function should have extern linkage.
This prevents the compiler from optimizing away dead code, as this wrapper kfunc
is not invoked anywhere in the kernel itself. It is not necessary to provide a
prototype in a header for the wrapper kfunc.
An example is given below::
/* Disables missing prototype warnings */
__diag_push();
__diag_ignore_all("-Wmissing-prototypes",
"Global kfuncs as their definitions will be in BTF");
struct task_struct *bpf_find_get_task_by_vpid(pid_t nr)
{
return find_get_task_by_vpid(nr);
}
__diag_pop();
A wrapper kfunc is often needed when we need to annotate parameters of the
kfunc. Otherwise one may directly make the kfunc visible to the BPF program by
registering it with the BPF subsystem. See :ref:`BPF_kfunc_nodef`.
2.2 Annotating kfunc parameters
-------------------------------
Similar to BPF helpers, there is sometime need for additional context required
by the verifier to make the usage of kernel functions safer and more useful.
Hence, we can annotate a parameter by suffixing the name of the argument of the
kfunc with a __tag, where tag may be one of the supported annotations.
2.2.1 __sz Annotation
---------------------
This annotation is used to indicate a memory and size pair in the argument list.
An example is given below::
void bpf_memzero(void *mem, int mem__sz)
{
...
}
Here, the verifier will treat first argument as a PTR_TO_MEM, and second
argument as its size. By default, without __sz annotation, the size of the type
of the pointer is used. Without __sz annotation, a kfunc cannot accept a void
pointer.
.. _BPF_kfunc_nodef:
2.3 Using an existing kernel function
-------------------------------------
When an existing function in the kernel is fit for consumption by BPF programs,
it can be directly registered with the BPF subsystem. However, care must still
be taken to review the context in which it will be invoked by the BPF program
and whether it is safe to do so.
2.4 Annotating kfuncs
---------------------
In addition to kfuncs' arguments, verifier may need more information about the
type of kfunc(s) being registered with the BPF subsystem. To do so, we define
flags on a set of kfuncs as follows::
BTF_SET8_START(bpf_task_set)
BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
BTF_SET8_END(bpf_task_set)
This set encodes the BTF ID of each kfunc listed above, and encodes the flags
along with it. Ofcourse, it is also allowed to specify no flags.
2.4.1 KF_ACQUIRE flag
---------------------
The KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a
refcounted object. The verifier will then ensure that the pointer to the object
is eventually released using a release kfunc, or transferred to a map using a
referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the
loading of the BPF program until no lingering references remain in all possible
explored states of the program.
2.4.2 KF_RET_NULL flag
----------------------
The KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc
may be NULL. Hence, it forces the user to do a NULL check on the pointer
returned from the kfunc before making use of it (dereferencing or passing to
another helper). This flag is often used in pairing with KF_ACQUIRE flag, but
both are orthogonal to each other.
2.4.3 KF_RELEASE flag
---------------------
The KF_RELEASE flag is used to indicate that the kfunc releases the pointer
passed in to it. There can be only one referenced pointer that can be passed in.
All copies of the pointer being released are invalidated as a result of invoking
kfunc with this flag.
2.4.4 KF_KPTR_GET flag
----------------------
The KF_KPTR_GET flag is used to indicate that the kfunc takes the first argument
as a pointer to kptr, safely increments the refcount of the object it points to,
and returns a reference to the user. The rest of the arguments may be normal
arguments of a kfunc. The KF_KPTR_GET flag should be used in conjunction with
KF_ACQUIRE and KF_RET_NULL flags.
2.4.5 KF_TRUSTED_ARGS flag
--------------------------
The KF_TRUSTED_ARGS flag is used for kfuncs taking pointer arguments. It
indicates that the all pointer arguments will always be refcounted, and have
their offset set to 0. It can be used to enforce that a pointer to a refcounted
object acquired from a kfunc or BPF helper is passed as an argument to this
kfunc without any modifications (e.g. pointer arithmetic) such that it is
trusted and points to the original object. This flag is often used for kfuncs
that operate (change some property, perform some operation) on an object that
was obtained using an acquire kfunc. Such kfuncs need an unchanged pointer to
ensure the integrity of the operation being performed on the expected object.
2.5 Registering the kfuncs
--------------------------
Once the kfunc is prepared for use, the final step to making it visible is
registering it with the BPF subsystem. Registration is done per BPF program
type. An example is shown below::
BTF_SET8_START(bpf_task_set)
BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
BTF_SET8_END(bpf_task_set)
static const struct btf_kfunc_id_set bpf_task_kfunc_set = {
.owner = THIS_MODULE,
.set = &bpf_task_set,
};
static int init_subsystem(void)
{
return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_task_kfunc_set);
}
late_initcall(init_subsystem);
...@@ -1924,7 +1924,8 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, ...@@ -1924,7 +1924,8 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *regs); struct bpf_reg_state *regs);
int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id, const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs); struct bpf_reg_state *regs,
u32 kfunc_flags);
int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
struct bpf_reg_state *reg); struct bpf_reg_state *reg);
int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog, int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
......
...@@ -12,14 +12,43 @@ ...@@ -12,14 +12,43 @@
#define BTF_TYPE_EMIT(type) ((void)(type *)0) #define BTF_TYPE_EMIT(type) ((void)(type *)0)
#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
enum btf_kfunc_type { /* These need to be macros, as the expressions are used in assembler input */
BTF_KFUNC_TYPE_CHECK, #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */
BTF_KFUNC_TYPE_ACQUIRE, #define KF_RELEASE (1 << 1) /* kfunc is a release function */
BTF_KFUNC_TYPE_RELEASE, #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */
BTF_KFUNC_TYPE_RET_NULL, #define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */
BTF_KFUNC_TYPE_KPTR_ACQUIRE, /* Trusted arguments are those which are meant to be referenced arguments with
BTF_KFUNC_TYPE_MAX, * unchanged offset. It is used to enforce that pointers obtained from acquire
}; * kfuncs remain unmodified when being passed to helpers taking trusted args.
*
* Consider
* struct foo {
* int data;
* struct foo *next;
* };
*
* struct bar {
* int data;
* struct foo f;
* };
*
* struct foo *f = alloc_foo(); // Acquire kfunc
* struct bar *b = alloc_bar(); // Acquire kfunc
*
* If a kfunc set_foo_data() wants to operate only on the allocated object, it
* will set the KF_TRUSTED_ARGS flag, which will prevent unsafe usage like:
*
* set_foo_data(f, 42); // Allowed
* set_foo_data(f->next, 42); // Rejected, non-referenced pointer
* set_foo_data(&f->next, 42);// Rejected, referenced, but wrong type
* set_foo_data(&b->f, 42); // Rejected, referenced, but bad offset
*
* In the final case, usually for the purposes of type matching, it is deduced
* by looking at the type of the member at the offset, but due to the
* requirement of trusted argument, this deduction will be strict and not done
* for this case.
*/
#define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
struct btf; struct btf;
struct btf_member; struct btf_member;
...@@ -30,16 +59,7 @@ struct btf_id_set; ...@@ -30,16 +59,7 @@ struct btf_id_set;
struct btf_kfunc_id_set { struct btf_kfunc_id_set {
struct module *owner; struct module *owner;
union { struct btf_id_set8 *set;
struct {
struct btf_id_set *check_set;
struct btf_id_set *acquire_set;
struct btf_id_set *release_set;
struct btf_id_set *ret_null_set;
struct btf_id_set *kptr_acquire_set;
};
struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
};
}; };
struct btf_id_dtor_kfunc { struct btf_id_dtor_kfunc {
...@@ -378,9 +398,9 @@ const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); ...@@ -378,9 +398,9 @@ const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
const char *btf_name_by_offset(const struct btf *btf, u32 offset); const char *btf_name_by_offset(const struct btf *btf, u32 offset);
struct btf *btf_parse_vmlinux(void); struct btf *btf_parse_vmlinux(void);
struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
bool btf_kfunc_id_set_contains(const struct btf *btf, u32 *btf_kfunc_id_set_contains(const struct btf *btf,
enum bpf_prog_type prog_type, enum bpf_prog_type prog_type,
enum btf_kfunc_type type, u32 kfunc_btf_id); u32 kfunc_btf_id);
int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
const struct btf_kfunc_id_set *s); const struct btf_kfunc_id_set *s);
s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
...@@ -397,12 +417,11 @@ static inline const char *btf_name_by_offset(const struct btf *btf, ...@@ -397,12 +417,11 @@ static inline const char *btf_name_by_offset(const struct btf *btf,
{ {
return NULL; return NULL;
} }
static inline bool btf_kfunc_id_set_contains(const struct btf *btf, static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
enum bpf_prog_type prog_type, enum bpf_prog_type prog_type,
enum btf_kfunc_type type,
u32 kfunc_btf_id) u32 kfunc_btf_id)
{ {
return false; return NULL;
} }
static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
const struct btf_kfunc_id_set *s) const struct btf_kfunc_id_set *s)
......
...@@ -8,6 +8,15 @@ struct btf_id_set { ...@@ -8,6 +8,15 @@ struct btf_id_set {
u32 ids[]; u32 ids[];
}; };
struct btf_id_set8 {
u32 cnt;
u32 flags;
struct {
u32 id;
u32 flags;
} pairs[];
};
#ifdef CONFIG_DEBUG_INFO_BTF #ifdef CONFIG_DEBUG_INFO_BTF
#include <linux/compiler.h> /* for __PASTE */ #include <linux/compiler.h> /* for __PASTE */
...@@ -25,7 +34,7 @@ struct btf_id_set { ...@@ -25,7 +34,7 @@ struct btf_id_set {
#define BTF_IDS_SECTION ".BTF_ids" #define BTF_IDS_SECTION ".BTF_ids"
#define ____BTF_ID(symbol) \ #define ____BTF_ID(symbol, word) \
asm( \ asm( \
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \ ".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
".local " #symbol " ; \n" \ ".local " #symbol " ; \n" \
...@@ -33,10 +42,11 @@ asm( \ ...@@ -33,10 +42,11 @@ asm( \
".size " #symbol ", 4; \n" \ ".size " #symbol ", 4; \n" \
#symbol ": \n" \ #symbol ": \n" \
".zero 4 \n" \ ".zero 4 \n" \
word \
".popsection; \n"); ".popsection; \n");
#define __BTF_ID(symbol) \ #define __BTF_ID(symbol, word) \
____BTF_ID(symbol) ____BTF_ID(symbol, word)
#define __ID(prefix) \ #define __ID(prefix) \
__PASTE(prefix, __COUNTER__) __PASTE(prefix, __COUNTER__)
...@@ -46,7 +56,14 @@ asm( \ ...@@ -46,7 +56,14 @@ asm( \
* to 4 zero bytes. * to 4 zero bytes.
*/ */
#define BTF_ID(prefix, name) \ #define BTF_ID(prefix, name) \
__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__)) __BTF_ID(__ID(__BTF_ID__##prefix##__##name##__), "")
#define ____BTF_ID_FLAGS(prefix, name, flags) \
__BTF_ID(__ID(__BTF_ID__##prefix##__##name##__), ".long " #flags "\n")
#define __BTF_ID_FLAGS(prefix, name, flags, ...) \
____BTF_ID_FLAGS(prefix, name, flags)
#define BTF_ID_FLAGS(prefix, name, ...) \
__BTF_ID_FLAGS(prefix, name, ##__VA_ARGS__, 0)
/* /*
* The BTF_ID_LIST macro defines pure (unsorted) list * The BTF_ID_LIST macro defines pure (unsorted) list
...@@ -145,10 +162,51 @@ asm( \ ...@@ -145,10 +162,51 @@ asm( \
".popsection; \n"); \ ".popsection; \n"); \
extern struct btf_id_set name; extern struct btf_id_set name;
/*
* The BTF_SET8_START/END macros pair defines sorted list of
* BTF IDs and their flags plus its members count, with the
* following layout:
*
* BTF_SET8_START(list)
* BTF_ID_FLAGS(type1, name1, flags)
* BTF_ID_FLAGS(type2, name2, flags)
* BTF_SET8_END(list)
*
* __BTF_ID__set8__list:
* .zero 8
* list:
* __BTF_ID__type1__name1__3:
* .zero 4
* .word (1 << 0) | (1 << 2)
* __BTF_ID__type2__name2__5:
* .zero 4
* .word (1 << 3) | (1 << 1) | (1 << 2)
*
*/
#define __BTF_SET8_START(name, scope) \
asm( \
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
"." #scope " __BTF_ID__set8__" #name "; \n" \
"__BTF_ID__set8__" #name ":; \n" \
".zero 8 \n" \
".popsection; \n");
#define BTF_SET8_START(name) \
__BTF_ID_LIST(name, local) \
__BTF_SET8_START(name, local)
#define BTF_SET8_END(name) \
asm( \
".pushsection " BTF_IDS_SECTION ",\"a\"; \n" \
".size __BTF_ID__set8__" #name ", .-" #name " \n" \
".popsection; \n"); \
extern struct btf_id_set8 name;
#else #else
#define BTF_ID_LIST(name) static u32 __maybe_unused name[5]; #define BTF_ID_LIST(name) static u32 __maybe_unused name[5];
#define BTF_ID(prefix, name) #define BTF_ID(prefix, name)
#define BTF_ID_FLAGS(prefix, name, flags)
#define BTF_ID_UNUSED #define BTF_ID_UNUSED
#define BTF_ID_LIST_GLOBAL(name, n) u32 __maybe_unused name[n]; #define BTF_ID_LIST_GLOBAL(name, n) u32 __maybe_unused name[n];
#define BTF_ID_LIST_SINGLE(name, prefix, typename) static u32 __maybe_unused name[1]; #define BTF_ID_LIST_SINGLE(name, prefix, typename) static u32 __maybe_unused name[1];
...@@ -156,6 +214,8 @@ extern struct btf_id_set name; ...@@ -156,6 +214,8 @@ extern struct btf_id_set name;
#define BTF_SET_START(name) static struct btf_id_set __maybe_unused name = { 0 }; #define BTF_SET_START(name) static struct btf_id_set __maybe_unused name = { 0 };
#define BTF_SET_START_GLOBAL(name) static struct btf_id_set __maybe_unused name = { 0 }; #define BTF_SET_START_GLOBAL(name) static struct btf_id_set __maybe_unused name = { 0 };
#define BTF_SET_END(name) #define BTF_SET_END(name)
#define BTF_SET8_START(name) static struct btf_id_set8 __maybe_unused name = { 0 };
#define BTF_SET8_END(name) static struct btf_id_set8 __maybe_unused name = { 0 };
#endif /* CONFIG_DEBUG_INFO_BTF */ #endif /* CONFIG_DEBUG_INFO_BTF */
......
...@@ -84,4 +84,23 @@ void nf_conntrack_lock(spinlock_t *lock); ...@@ -84,4 +84,23 @@ void nf_conntrack_lock(spinlock_t *lock);
extern spinlock_t nf_conntrack_expect_lock; extern spinlock_t nf_conntrack_expect_lock;
/* ctnetlink code shared by both ctnetlink and nf_conntrack_bpf */
#if (IS_BUILTIN(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
(IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) || \
IS_ENABLED(CONFIG_NF_CT_NETLINK))
static inline void __nf_ct_set_timeout(struct nf_conn *ct, u64 timeout)
{
if (timeout > INT_MAX)
timeout = INT_MAX;
WRITE_ONCE(ct->timeout, nfct_time_stamp + (u32)timeout);
}
int __nf_ct_change_timeout(struct nf_conn *ct, u64 cta_timeout);
void __nf_ct_change_status(struct nf_conn *ct, unsigned long on, unsigned long off);
int nf_ct_change_status_common(struct nf_conn *ct, unsigned int status);
#endif
#endif /* _NF_CONNTRACK_CORE_H */ #endif /* _NF_CONNTRACK_CORE_H */
...@@ -213,7 +213,7 @@ enum { ...@@ -213,7 +213,7 @@ enum {
}; };
struct btf_kfunc_set_tab { struct btf_kfunc_set_tab {
struct btf_id_set *sets[BTF_KFUNC_HOOK_MAX][BTF_KFUNC_TYPE_MAX]; struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX];
}; };
struct btf_id_dtor_kfunc_tab { struct btf_id_dtor_kfunc_tab {
...@@ -1616,7 +1616,7 @@ static void btf_free_id(struct btf *btf) ...@@ -1616,7 +1616,7 @@ static void btf_free_id(struct btf *btf)
static void btf_free_kfunc_set_tab(struct btf *btf) static void btf_free_kfunc_set_tab(struct btf *btf)
{ {
struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab; struct btf_kfunc_set_tab *tab = btf->kfunc_set_tab;
int hook, type; int hook;
if (!tab) if (!tab)
return; return;
...@@ -1625,10 +1625,8 @@ static void btf_free_kfunc_set_tab(struct btf *btf) ...@@ -1625,10 +1625,8 @@ static void btf_free_kfunc_set_tab(struct btf *btf)
*/ */
if (btf_is_module(btf)) if (btf_is_module(btf))
goto free_tab; goto free_tab;
for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++) { for (hook = 0; hook < ARRAY_SIZE(tab->sets); hook++)
for (type = 0; type < ARRAY_SIZE(tab->sets[0]); type++) kfree(tab->sets[hook]);
kfree(tab->sets[hook][type]);
}
free_tab: free_tab:
kfree(tab); kfree(tab);
btf->kfunc_set_tab = NULL; btf->kfunc_set_tab = NULL;
...@@ -6172,13 +6170,14 @@ static bool is_kfunc_arg_mem_size(const struct btf *btf, ...@@ -6172,13 +6170,14 @@ static bool is_kfunc_arg_mem_size(const struct btf *btf,
static int btf_check_func_arg_match(struct bpf_verifier_env *env, static int btf_check_func_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id, const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs, struct bpf_reg_state *regs,
bool ptr_to_mem_ok) bool ptr_to_mem_ok,
u32 kfunc_flags)
{ {
enum bpf_prog_type prog_type = resolve_prog_type(env->prog); enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
bool rel = false, kptr_get = false, trusted_arg = false;
struct bpf_verifier_log *log = &env->log; struct bpf_verifier_log *log = &env->log;
u32 i, nargs, ref_id, ref_obj_id = 0; u32 i, nargs, ref_id, ref_obj_id = 0;
bool is_kfunc = btf_is_kernel(btf); bool is_kfunc = btf_is_kernel(btf);
bool rel = false, kptr_get = false;
const char *func_name, *ref_tname; const char *func_name, *ref_tname;
const struct btf_type *t, *ref_t; const struct btf_type *t, *ref_t;
const struct btf_param *args; const struct btf_param *args;
...@@ -6210,10 +6209,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6210,10 +6209,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
if (is_kfunc) { if (is_kfunc) {
/* Only kfunc can be release func */ /* Only kfunc can be release func */
rel = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog), rel = kfunc_flags & KF_RELEASE;
BTF_KFUNC_TYPE_RELEASE, func_id); kptr_get = kfunc_flags & KF_KPTR_GET;
kptr_get = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog), trusted_arg = kfunc_flags & KF_TRUSTED_ARGS;
BTF_KFUNC_TYPE_KPTR_ACQUIRE, func_id);
} }
/* check that BTF function arguments match actual types that the /* check that BTF function arguments match actual types that the
...@@ -6238,10 +6236,19 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6238,10 +6236,19 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
return -EINVAL; return -EINVAL;
} }
/* Check if argument must be a referenced pointer, args + i has
* been verified to be a pointer (after skipping modifiers).
*/
if (is_kfunc && trusted_arg && !reg->ref_obj_id) {
bpf_log(log, "R%d must be referenced\n", regno);
return -EINVAL;
}
ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
ref_tname = btf_name_by_offset(btf, ref_t->name_off); ref_tname = btf_name_by_offset(btf, ref_t->name_off);
if (rel && reg->ref_obj_id) /* Trusted args have the same offset checks as release arguments */
if (trusted_arg || (rel && reg->ref_obj_id))
arg_type |= OBJ_RELEASE; arg_type |= OBJ_RELEASE;
ret = check_func_arg_reg_off(env, reg, regno, arg_type); ret = check_func_arg_reg_off(env, reg, regno, arg_type);
if (ret < 0) if (ret < 0)
...@@ -6339,7 +6346,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ...@@ -6339,7 +6346,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
reg_ref_tname = btf_name_by_offset(reg_btf, reg_ref_tname = btf_name_by_offset(reg_btf,
reg_ref_t->name_off); reg_ref_t->name_off);
if (!btf_struct_ids_match(log, reg_btf, reg_ref_id, if (!btf_struct_ids_match(log, reg_btf, reg_ref_id,
reg->off, btf, ref_id, rel && reg->ref_obj_id)) { reg->off, btf, ref_id,
trusted_arg || (rel && reg->ref_obj_id))) {
bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n", bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n",
func_name, i, func_name, i,
btf_type_str(ref_t), ref_tname, btf_type_str(ref_t), ref_tname,
...@@ -6442,7 +6450,7 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, ...@@ -6442,7 +6450,7 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
return -EINVAL; return -EINVAL;
is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; is_global = prog->aux->func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global); err = btf_check_func_arg_match(env, btf, btf_id, regs, is_global, 0);
/* Compiler optimizations can remove arguments from static functions /* Compiler optimizations can remove arguments from static functions
* or mismatched type can be passed into a global function. * or mismatched type can be passed into a global function.
...@@ -6455,9 +6463,10 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, ...@@ -6455,9 +6463,10 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
const struct btf *btf, u32 func_id, const struct btf *btf, u32 func_id,
struct bpf_reg_state *regs) struct bpf_reg_state *regs,
u32 kfunc_flags)
{ {
return btf_check_func_arg_match(env, btf, func_id, regs, true); return btf_check_func_arg_match(env, btf, func_id, regs, true, kfunc_flags);
} }
/* Convert BTF of a function into bpf_reg_state if possible /* Convert BTF of a function into bpf_reg_state if possible
...@@ -6854,6 +6863,11 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id) ...@@ -6854,6 +6863,11 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id)
return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL; return bsearch(&id, set->ids, set->cnt, sizeof(u32), btf_id_cmp_func) != NULL;
} }
static void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
{
return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
}
enum { enum {
BTF_MODULE_F_LIVE = (1 << 0), BTF_MODULE_F_LIVE = (1 << 0),
}; };
...@@ -7102,16 +7116,16 @@ BTF_TRACING_TYPE_xxx ...@@ -7102,16 +7116,16 @@ BTF_TRACING_TYPE_xxx
/* Kernel Function (kfunc) BTF ID set registration API */ /* Kernel Function (kfunc) BTF ID set registration API */
static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
enum btf_kfunc_type type, struct btf_id_set8 *add_set)
struct btf_id_set *add_set, bool vmlinux_set)
{ {
bool vmlinux_set = !btf_is_module(btf);
struct btf_kfunc_set_tab *tab; struct btf_kfunc_set_tab *tab;
struct btf_id_set *set; struct btf_id_set8 *set;
u32 set_cnt; u32 set_cnt;
int ret; int ret;
if (hook >= BTF_KFUNC_HOOK_MAX || type >= BTF_KFUNC_TYPE_MAX) { if (hook >= BTF_KFUNC_HOOK_MAX) {
ret = -EINVAL; ret = -EINVAL;
goto end; goto end;
} }
...@@ -7127,7 +7141,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7127,7 +7141,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
btf->kfunc_set_tab = tab; btf->kfunc_set_tab = tab;
} }
set = tab->sets[hook][type]; set = tab->sets[hook];
/* Warn when register_btf_kfunc_id_set is called twice for the same hook /* Warn when register_btf_kfunc_id_set is called twice for the same hook
* for module sets. * for module sets.
*/ */
...@@ -7141,7 +7155,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7141,7 +7155,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
* pointer and return. * pointer and return.
*/ */
if (!vmlinux_set) { if (!vmlinux_set) {
tab->sets[hook][type] = add_set; tab->sets[hook] = add_set;
return 0; return 0;
} }
...@@ -7150,7 +7164,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7150,7 +7164,7 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
* and concatenate all individual sets being registered. While each set * and concatenate all individual sets being registered. While each set
* is individually sorted, they may become unsorted when concatenated, * is individually sorted, they may become unsorted when concatenated,
* hence re-sorting the final set again is required to make binary * hence re-sorting the final set again is required to make binary
* searching the set using btf_id_set_contains function work. * searching the set using btf_id_set8_contains function work.
*/ */
set_cnt = set ? set->cnt : 0; set_cnt = set ? set->cnt : 0;
...@@ -7165,8 +7179,8 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7165,8 +7179,8 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
} }
/* Grow set */ /* Grow set */
set = krealloc(tab->sets[hook][type], set = krealloc(tab->sets[hook],
offsetof(struct btf_id_set, ids[set_cnt + add_set->cnt]), offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]),
GFP_KERNEL | __GFP_NOWARN); GFP_KERNEL | __GFP_NOWARN);
if (!set) { if (!set) {
ret = -ENOMEM; ret = -ENOMEM;
...@@ -7174,15 +7188,15 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7174,15 +7188,15 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
} }
/* For newly allocated set, initialize set->cnt to 0 */ /* For newly allocated set, initialize set->cnt to 0 */
if (!tab->sets[hook][type]) if (!tab->sets[hook])
set->cnt = 0; set->cnt = 0;
tab->sets[hook][type] = set; tab->sets[hook] = set;
/* Concatenate the two sets */ /* Concatenate the two sets */
memcpy(set->ids + set->cnt, add_set->ids, add_set->cnt * sizeof(set->ids[0])); memcpy(set->pairs + set->cnt, add_set->pairs, add_set->cnt * sizeof(set->pairs[0]));
set->cnt += add_set->cnt; set->cnt += add_set->cnt;
sort(set->ids, set->cnt, sizeof(set->ids[0]), btf_id_cmp_func, NULL); sort(set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func, NULL);
return 0; return 0;
end: end:
...@@ -7190,38 +7204,25 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, ...@@ -7190,38 +7204,25 @@ static int __btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
return ret; return ret;
} }
static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook, static u32 *__btf_kfunc_id_set_contains(const struct btf *btf,
const struct btf_kfunc_id_set *kset)
{
bool vmlinux_set = !btf_is_module(btf);
int type, ret = 0;
for (type = 0; type < ARRAY_SIZE(kset->sets); type++) {
if (!kset->sets[type])
continue;
ret = __btf_populate_kfunc_set(btf, hook, type, kset->sets[type], vmlinux_set);
if (ret)
break;
}
return ret;
}
static bool __btf_kfunc_id_set_contains(const struct btf *btf,
enum btf_kfunc_hook hook, enum btf_kfunc_hook hook,
enum btf_kfunc_type type,
u32 kfunc_btf_id) u32 kfunc_btf_id)
{ {
struct btf_id_set *set; struct btf_id_set8 *set;
u32 *id;
if (hook >= BTF_KFUNC_HOOK_MAX || type >= BTF_KFUNC_TYPE_MAX) if (hook >= BTF_KFUNC_HOOK_MAX)
return false; return NULL;
if (!btf->kfunc_set_tab) if (!btf->kfunc_set_tab)
return false; return NULL;
set = btf->kfunc_set_tab->sets[hook][type]; set = btf->kfunc_set_tab->sets[hook];
if (!set) if (!set)
return false; return NULL;
return btf_id_set_contains(set, kfunc_btf_id); id = btf_id_set8_contains(set, kfunc_btf_id);
if (!id)
return NULL;
/* The flags for BTF ID are located next to it */
return id + 1;
} }
static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
...@@ -7249,14 +7250,14 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) ...@@ -7249,14 +7250,14 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
* keeping the reference for the duration of the call provides the necessary * keeping the reference for the duration of the call provides the necessary
* protection for looking up a well-formed btf->kfunc_set_tab. * protection for looking up a well-formed btf->kfunc_set_tab.
*/ */
bool btf_kfunc_id_set_contains(const struct btf *btf, u32 *btf_kfunc_id_set_contains(const struct btf *btf,
enum bpf_prog_type prog_type, enum bpf_prog_type prog_type,
enum btf_kfunc_type type, u32 kfunc_btf_id) u32 kfunc_btf_id)
{ {
enum btf_kfunc_hook hook; enum btf_kfunc_hook hook;
hook = bpf_prog_type_to_kfunc_hook(prog_type); hook = bpf_prog_type_to_kfunc_hook(prog_type);
return __btf_kfunc_id_set_contains(btf, hook, type, kfunc_btf_id); return __btf_kfunc_id_set_contains(btf, hook, kfunc_btf_id);
} }
/* This function must be invoked only from initcalls/module init functions */ /* This function must be invoked only from initcalls/module init functions */
...@@ -7283,7 +7284,7 @@ int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, ...@@ -7283,7 +7284,7 @@ int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
return PTR_ERR(btf); return PTR_ERR(btf);
hook = bpf_prog_type_to_kfunc_hook(prog_type); hook = bpf_prog_type_to_kfunc_hook(prog_type);
ret = btf_populate_kfunc_set(btf, hook, kset); ret = btf_populate_kfunc_set(btf, hook, kset->set);
btf_put(btf); btf_put(btf);
return ret; return ret;
} }
......
...@@ -7562,6 +7562,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ...@@ -7562,6 +7562,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
int err, insn_idx = *insn_idx_p; int err, insn_idx = *insn_idx_p;
const struct btf_param *args; const struct btf_param *args;
struct btf *desc_btf; struct btf *desc_btf;
u32 *kfunc_flags;
bool acq; bool acq;
/* skip for now, but return error when we find this in fixup_kfunc_call */ /* skip for now, but return error when we find this in fixup_kfunc_call */
...@@ -7577,18 +7578,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ...@@ -7577,18 +7578,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
func_name = btf_name_by_offset(desc_btf, func->name_off); func_name = btf_name_by_offset(desc_btf, func->name_off);
func_proto = btf_type_by_id(desc_btf, func->type); func_proto = btf_type_by_id(desc_btf, func->type);
if (!btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id);
BTF_KFUNC_TYPE_CHECK, func_id)) { if (!kfunc_flags) {
verbose(env, "calling kernel function %s is not allowed\n", verbose(env, "calling kernel function %s is not allowed\n",
func_name); func_name);
return -EACCES; return -EACCES;
} }
acq = *kfunc_flags & KF_ACQUIRE;
acq = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog),
BTF_KFUNC_TYPE_ACQUIRE, func_id);
/* Check the arguments */ /* Check the arguments */
err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs, *kfunc_flags);
if (err < 0) if (err < 0)
return err; return err;
/* In case of release function, we get register number of refcounted /* In case of release function, we get register number of refcounted
...@@ -7632,8 +7631,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, ...@@ -7632,8 +7631,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
regs[BPF_REG_0].btf = desc_btf; regs[BPF_REG_0].btf = desc_btf;
regs[BPF_REG_0].type = PTR_TO_BTF_ID; regs[BPF_REG_0].type = PTR_TO_BTF_ID;
regs[BPF_REG_0].btf_id = ptr_type_id; regs[BPF_REG_0].btf_id = ptr_type_id;
if (btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), if (*kfunc_flags & KF_RET_NULL) {
BTF_KFUNC_TYPE_RET_NULL, func_id)) {
regs[BPF_REG_0].type |= PTR_MAYBE_NULL; regs[BPF_REG_0].type |= PTR_MAYBE_NULL;
/* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */
regs[BPF_REG_0].id = ++env->id_gen; regs[BPF_REG_0].id = ++env->id_gen;
......
...@@ -691,52 +691,35 @@ noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) ...@@ -691,52 +691,35 @@ noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
{ {
} }
noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
{
}
__diag_pop(); __diag_pop();
ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
BTF_SET_START(test_sk_check_kfunc_ids) BTF_SET8_START(test_sk_check_kfunc_ids)
BTF_ID(func, bpf_kfunc_call_test1) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
BTF_ID(func, bpf_kfunc_call_test2) BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
BTF_ID(func, bpf_kfunc_call_test3) BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
BTF_ID(func, bpf_kfunc_call_test_acquire) BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
BTF_ID(func, bpf_kfunc_call_memb_acquire) BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
BTF_ID(func, bpf_kfunc_call_test_release) BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
BTF_ID(func, bpf_kfunc_call_memb_release) BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
BTF_ID(func, bpf_kfunc_call_memb1_release) BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
BTF_ID(func, bpf_kfunc_call_test_kptr_get) BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET)
BTF_ID(func, bpf_kfunc_call_test_pass_ctx) BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
BTF_ID(func, bpf_kfunc_call_test_pass1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
BTF_ID(func, bpf_kfunc_call_test_pass2) BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
BTF_ID(func, bpf_kfunc_call_test_fail1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
BTF_ID(func, bpf_kfunc_call_test_fail2) BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
BTF_ID(func, bpf_kfunc_call_test_fail3) BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
BTF_ID(func, bpf_kfunc_call_test_mem_len_pass1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
BTF_ID(func, bpf_kfunc_call_test_mem_len_fail1) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
BTF_ID(func, bpf_kfunc_call_test_mem_len_fail2) BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
BTF_SET_END(test_sk_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS)
BTF_SET8_END(test_sk_check_kfunc_ids)
BTF_SET_START(test_sk_acquire_kfunc_ids)
BTF_ID(func, bpf_kfunc_call_test_acquire)
BTF_ID(func, bpf_kfunc_call_memb_acquire)
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
BTF_SET_END(test_sk_acquire_kfunc_ids)
BTF_SET_START(test_sk_release_kfunc_ids)
BTF_ID(func, bpf_kfunc_call_test_release)
BTF_ID(func, bpf_kfunc_call_memb_release)
BTF_ID(func, bpf_kfunc_call_memb1_release)
BTF_SET_END(test_sk_release_kfunc_ids)
BTF_SET_START(test_sk_ret_null_kfunc_ids)
BTF_ID(func, bpf_kfunc_call_test_acquire)
BTF_ID(func, bpf_kfunc_call_memb_acquire)
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
BTF_SET_END(test_sk_ret_null_kfunc_ids)
BTF_SET_START(test_sk_kptr_acquire_kfunc_ids)
BTF_ID(func, bpf_kfunc_call_test_kptr_get)
BTF_SET_END(test_sk_kptr_acquire_kfunc_ids)
static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
u32 size, u32 headroom, u32 tailroom) u32 size, u32 headroom, u32 tailroom)
...@@ -1620,12 +1603,8 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog, ...@@ -1620,12 +1603,8 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,
} }
static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &test_sk_check_kfunc_ids, .set = &test_sk_check_kfunc_ids,
.acquire_set = &test_sk_acquire_kfunc_ids,
.release_set = &test_sk_release_kfunc_ids,
.ret_null_set = &test_sk_ret_null_kfunc_ids,
.kptr_acquire_set = &test_sk_kptr_acquire_kfunc_ids
}; };
BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
......
...@@ -197,17 +197,17 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id, ...@@ -197,17 +197,17 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
} }
} }
BTF_SET_START(bpf_tcp_ca_check_kfunc_ids) BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
BTF_ID(func, tcp_reno_ssthresh) BTF_ID_FLAGS(func, tcp_reno_ssthresh)
BTF_ID(func, tcp_reno_cong_avoid) BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
BTF_ID(func, tcp_reno_undo_cwnd) BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
BTF_ID(func, tcp_slow_start) BTF_ID_FLAGS(func, tcp_slow_start)
BTF_ID(func, tcp_cong_avoid_ai) BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
BTF_SET_END(bpf_tcp_ca_check_kfunc_ids) BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = { static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &bpf_tcp_ca_check_kfunc_ids, .set = &bpf_tcp_ca_check_kfunc_ids,
}; };
static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = { static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
......
...@@ -1154,24 +1154,24 @@ static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = { ...@@ -1154,24 +1154,24 @@ static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = {
.set_state = bbr_set_state, .set_state = bbr_set_state,
}; };
BTF_SET_START(tcp_bbr_check_kfunc_ids) BTF_SET8_START(tcp_bbr_check_kfunc_ids)
#ifdef CONFIG_X86 #ifdef CONFIG_X86
#ifdef CONFIG_DYNAMIC_FTRACE #ifdef CONFIG_DYNAMIC_FTRACE
BTF_ID(func, bbr_init) BTF_ID_FLAGS(func, bbr_init)
BTF_ID(func, bbr_main) BTF_ID_FLAGS(func, bbr_main)
BTF_ID(func, bbr_sndbuf_expand) BTF_ID_FLAGS(func, bbr_sndbuf_expand)
BTF_ID(func, bbr_undo_cwnd) BTF_ID_FLAGS(func, bbr_undo_cwnd)
BTF_ID(func, bbr_cwnd_event) BTF_ID_FLAGS(func, bbr_cwnd_event)
BTF_ID(func, bbr_ssthresh) BTF_ID_FLAGS(func, bbr_ssthresh)
BTF_ID(func, bbr_min_tso_segs) BTF_ID_FLAGS(func, bbr_min_tso_segs)
BTF_ID(func, bbr_set_state) BTF_ID_FLAGS(func, bbr_set_state)
#endif #endif
#endif #endif
BTF_SET_END(tcp_bbr_check_kfunc_ids) BTF_SET8_END(tcp_bbr_check_kfunc_ids)
static const struct btf_kfunc_id_set tcp_bbr_kfunc_set = { static const struct btf_kfunc_id_set tcp_bbr_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &tcp_bbr_check_kfunc_ids, .set = &tcp_bbr_check_kfunc_ids,
}; };
static int __init bbr_register(void) static int __init bbr_register(void)
......
...@@ -485,22 +485,22 @@ static struct tcp_congestion_ops cubictcp __read_mostly = { ...@@ -485,22 +485,22 @@ static struct tcp_congestion_ops cubictcp __read_mostly = {
.name = "cubic", .name = "cubic",
}; };
BTF_SET_START(tcp_cubic_check_kfunc_ids) BTF_SET8_START(tcp_cubic_check_kfunc_ids)
#ifdef CONFIG_X86 #ifdef CONFIG_X86
#ifdef CONFIG_DYNAMIC_FTRACE #ifdef CONFIG_DYNAMIC_FTRACE
BTF_ID(func, cubictcp_init) BTF_ID_FLAGS(func, cubictcp_init)
BTF_ID(func, cubictcp_recalc_ssthresh) BTF_ID_FLAGS(func, cubictcp_recalc_ssthresh)
BTF_ID(func, cubictcp_cong_avoid) BTF_ID_FLAGS(func, cubictcp_cong_avoid)
BTF_ID(func, cubictcp_state) BTF_ID_FLAGS(func, cubictcp_state)
BTF_ID(func, cubictcp_cwnd_event) BTF_ID_FLAGS(func, cubictcp_cwnd_event)
BTF_ID(func, cubictcp_acked) BTF_ID_FLAGS(func, cubictcp_acked)
#endif #endif
#endif #endif
BTF_SET_END(tcp_cubic_check_kfunc_ids) BTF_SET8_END(tcp_cubic_check_kfunc_ids)
static const struct btf_kfunc_id_set tcp_cubic_kfunc_set = { static const struct btf_kfunc_id_set tcp_cubic_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &tcp_cubic_check_kfunc_ids, .set = &tcp_cubic_check_kfunc_ids,
}; };
static int __init cubictcp_register(void) static int __init cubictcp_register(void)
......
...@@ -239,22 +239,22 @@ static struct tcp_congestion_ops dctcp_reno __read_mostly = { ...@@ -239,22 +239,22 @@ static struct tcp_congestion_ops dctcp_reno __read_mostly = {
.name = "dctcp-reno", .name = "dctcp-reno",
}; };
BTF_SET_START(tcp_dctcp_check_kfunc_ids) BTF_SET8_START(tcp_dctcp_check_kfunc_ids)
#ifdef CONFIG_X86 #ifdef CONFIG_X86
#ifdef CONFIG_DYNAMIC_FTRACE #ifdef CONFIG_DYNAMIC_FTRACE
BTF_ID(func, dctcp_init) BTF_ID_FLAGS(func, dctcp_init)
BTF_ID(func, dctcp_update_alpha) BTF_ID_FLAGS(func, dctcp_update_alpha)
BTF_ID(func, dctcp_cwnd_event) BTF_ID_FLAGS(func, dctcp_cwnd_event)
BTF_ID(func, dctcp_ssthresh) BTF_ID_FLAGS(func, dctcp_ssthresh)
BTF_ID(func, dctcp_cwnd_undo) BTF_ID_FLAGS(func, dctcp_cwnd_undo)
BTF_ID(func, dctcp_state) BTF_ID_FLAGS(func, dctcp_state)
#endif #endif
#endif #endif
BTF_SET_END(tcp_dctcp_check_kfunc_ids) BTF_SET8_END(tcp_dctcp_check_kfunc_ids)
static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = { static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &tcp_dctcp_check_kfunc_ids, .set = &tcp_dctcp_check_kfunc_ids,
}; };
static int __init dctcp_register(void) static int __init dctcp_register(void)
......
This diff is collapsed.
...@@ -2786,3 +2786,65 @@ int nf_conntrack_init_net(struct net *net) ...@@ -2786,3 +2786,65 @@ int nf_conntrack_init_net(struct net *net)
free_percpu(net->ct.stat); free_percpu(net->ct.stat);
return ret; return ret;
} }
#if (IS_BUILTIN(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
(IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) || \
IS_ENABLED(CONFIG_NF_CT_NETLINK))
/* ctnetlink code shared by both ctnetlink and nf_conntrack_bpf */
int __nf_ct_change_timeout(struct nf_conn *ct, u64 timeout)
{
if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
return -EPERM;
__nf_ct_set_timeout(ct, timeout);
if (test_bit(IPS_DYING_BIT, &ct->status))
return -ETIME;
return 0;
}
EXPORT_SYMBOL_GPL(__nf_ct_change_timeout);
void __nf_ct_change_status(struct nf_conn *ct, unsigned long on, unsigned long off)
{
unsigned int bit;
/* Ignore these unchangable bits */
on &= ~IPS_UNCHANGEABLE_MASK;
off &= ~IPS_UNCHANGEABLE_MASK;
for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
if (on & (1 << bit))
set_bit(bit, &ct->status);
else if (off & (1 << bit))
clear_bit(bit, &ct->status);
}
}
EXPORT_SYMBOL_GPL(__nf_ct_change_status);
int nf_ct_change_status_common(struct nf_conn *ct, unsigned int status)
{
unsigned long d;
d = ct->status ^ status;
if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
/* unchangeable */
return -EBUSY;
if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
/* SEEN_REPLY bit can only be set */
return -EBUSY;
if (d & IPS_ASSURED && !(status & IPS_ASSURED))
/* ASSURED bit can only be set */
return -EBUSY;
__nf_ct_change_status(ct, status, 0);
return 0;
}
EXPORT_SYMBOL_GPL(nf_ct_change_status_common);
#endif
...@@ -1890,45 +1890,10 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct, ...@@ -1890,45 +1890,10 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct,
} }
#endif #endif
static void
__ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
unsigned long off)
{
unsigned int bit;
/* Ignore these unchangable bits */
on &= ~IPS_UNCHANGEABLE_MASK;
off &= ~IPS_UNCHANGEABLE_MASK;
for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
if (on & (1 << bit))
set_bit(bit, &ct->status);
else if (off & (1 << bit))
clear_bit(bit, &ct->status);
}
}
static int static int
ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[]) ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
{ {
unsigned long d; return nf_ct_change_status_common(ct, ntohl(nla_get_be32(cda[CTA_STATUS])));
unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
d = ct->status ^ status;
if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
/* unchangeable */
return -EBUSY;
if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
/* SEEN_REPLY bit can only be set */
return -EBUSY;
if (d & IPS_ASSURED && !(status & IPS_ASSURED))
/* ASSURED bit can only be set */
return -EBUSY;
__ctnetlink_change_status(ct, status, 0);
return 0;
} }
static int static int
...@@ -2023,16 +1988,7 @@ static int ctnetlink_change_helper(struct nf_conn *ct, ...@@ -2023,16 +1988,7 @@ static int ctnetlink_change_helper(struct nf_conn *ct,
static int ctnetlink_change_timeout(struct nf_conn *ct, static int ctnetlink_change_timeout(struct nf_conn *ct,
const struct nlattr * const cda[]) const struct nlattr * const cda[])
{ {
u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ; return __nf_ct_change_timeout(ct, (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ);
if (timeout > INT_MAX)
timeout = INT_MAX;
WRITE_ONCE(ct->timeout, nfct_time_stamp + (u32)timeout);
if (test_bit(IPS_DYING_BIT, &ct->status))
return -ETIME;
return 0;
} }
#if defined(CONFIG_NF_CONNTRACK_MARK) #if defined(CONFIG_NF_CONNTRACK_MARK)
...@@ -2292,9 +2248,7 @@ ctnetlink_create_conntrack(struct net *net, ...@@ -2292,9 +2248,7 @@ ctnetlink_create_conntrack(struct net *net,
goto err1; goto err1;
timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ; timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
if (timeout > INT_MAX) __nf_ct_set_timeout(ct, timeout);
timeout = INT_MAX;
ct->timeout = (u32)timeout + nfct_time_stamp;
rcu_read_lock(); rcu_read_lock();
if (cda[CTA_HELP]) { if (cda[CTA_HELP]) {
...@@ -2836,7 +2790,7 @@ ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[]) ...@@ -2836,7 +2790,7 @@ ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
* unchangeable bits but do not error out. Also user programs * unchangeable bits but do not error out. Also user programs
* are allowed to clear the bits that they are allowed to change. * are allowed to clear the bits that they are allowed to change.
*/ */
__ctnetlink_change_status(ct, status, ~status); __nf_ct_change_status(ct, status, ~status);
return 0; return 0;
} }
......
...@@ -45,6 +45,19 @@ ...@@ -45,6 +45,19 @@
* .zero 4 * .zero 4
* __BTF_ID__func__vfs_fallocate__4: * __BTF_ID__func__vfs_fallocate__4:
* .zero 4 * .zero 4
*
* set8 - store symbol size into first 4 bytes and sort following
* ID list
*
* __BTF_ID__set8__list:
* .zero 8
* list:
* __BTF_ID__func__vfs_getattr__3:
* .zero 4
* .word (1 << 0) | (1 << 2)
* __BTF_ID__func__vfs_fallocate__5:
* .zero 4
* .word (1 << 3) | (1 << 1) | (1 << 2)
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
...@@ -72,6 +85,7 @@ ...@@ -72,6 +85,7 @@
#define BTF_TYPEDEF "typedef" #define BTF_TYPEDEF "typedef"
#define BTF_FUNC "func" #define BTF_FUNC "func"
#define BTF_SET "set" #define BTF_SET "set"
#define BTF_SET8 "set8"
#define ADDR_CNT 100 #define ADDR_CNT 100
...@@ -84,6 +98,7 @@ struct btf_id { ...@@ -84,6 +98,7 @@ struct btf_id {
}; };
int addr_cnt; int addr_cnt;
bool is_set; bool is_set;
bool is_set8;
Elf64_Addr addr[ADDR_CNT]; Elf64_Addr addr[ADDR_CNT];
}; };
...@@ -231,14 +246,14 @@ static char *get_id(const char *prefix_end) ...@@ -231,14 +246,14 @@ static char *get_id(const char *prefix_end)
return id; return id;
} }
static struct btf_id *add_set(struct object *obj, char *name) static struct btf_id *add_set(struct object *obj, char *name, bool is_set8)
{ {
/* /*
* __BTF_ID__set__name * __BTF_ID__set__name
* name = ^ * name = ^
* id = ^ * id = ^
*/ */
char *id = name + sizeof(BTF_SET "__") - 1; char *id = name + (is_set8 ? sizeof(BTF_SET8 "__") : sizeof(BTF_SET "__")) - 1;
int len = strlen(name); int len = strlen(name);
if (id >= name + len) { if (id >= name + len) {
...@@ -444,9 +459,21 @@ static int symbols_collect(struct object *obj) ...@@ -444,9 +459,21 @@ static int symbols_collect(struct object *obj)
} else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) { } else if (!strncmp(prefix, BTF_FUNC, sizeof(BTF_FUNC) - 1)) {
obj->nr_funcs++; obj->nr_funcs++;
id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1); id = add_symbol(&obj->funcs, prefix, sizeof(BTF_FUNC) - 1);
/* set8 */
} else if (!strncmp(prefix, BTF_SET8, sizeof(BTF_SET8) - 1)) {
id = add_set(obj, prefix, true);
/*
* SET8 objects store list's count, which is encoded
* in symbol's size, together with 'cnt' field hence
* that - 1.
*/
if (id) {
id->cnt = sym.st_size / sizeof(uint64_t) - 1;
id->is_set8 = true;
}
/* set */ /* set */
} else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) { } else if (!strncmp(prefix, BTF_SET, sizeof(BTF_SET) - 1)) {
id = add_set(obj, prefix); id = add_set(obj, prefix, false);
/* /*
* SET objects store list's count, which is encoded * SET objects store list's count, which is encoded
* in symbol's size, together with 'cnt' field hence * in symbol's size, together with 'cnt' field hence
...@@ -571,7 +598,8 @@ static int id_patch(struct object *obj, struct btf_id *id) ...@@ -571,7 +598,8 @@ static int id_patch(struct object *obj, struct btf_id *id)
int *ptr = data->d_buf; int *ptr = data->d_buf;
int i; int i;
if (!id->id && !id->is_set) /* For set, set8, id->id may be 0 */
if (!id->id && !id->is_set && !id->is_set8)
pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name); pr_err("WARN: resolve_btfids: unresolved symbol %s\n", id->name);
for (i = 0; i < id->addr_cnt; i++) { for (i = 0; i < id->addr_cnt; i++) {
...@@ -643,13 +671,13 @@ static int sets_patch(struct object *obj) ...@@ -643,13 +671,13 @@ static int sets_patch(struct object *obj)
} }
idx = idx / sizeof(int); idx = idx / sizeof(int);
base = &ptr[idx] + 1; base = &ptr[idx] + (id->is_set8 ? 2 : 1);
cnt = ptr[idx]; cnt = ptr[idx];
pr_debug("sorting addr %5lu: cnt %6d [%s]\n", pr_debug("sorting addr %5lu: cnt %6d [%s]\n",
(idx + 1) * sizeof(int), cnt, id->name); (idx + 1) * sizeof(int), cnt, id->name);
qsort(base, cnt, sizeof(int), cmp_id); qsort(base, cnt, id->is_set8 ? sizeof(uint64_t) : sizeof(int), cmp_id);
next = rb_next(next); next = rb_next(next);
} }
......
...@@ -148,13 +148,13 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { ...@@ -148,13 +148,13 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
.write = bpf_testmod_test_write, .write = bpf_testmod_test_write,
}; };
BTF_SET_START(bpf_testmod_check_kfunc_ids) BTF_SET8_START(bpf_testmod_check_kfunc_ids)
BTF_ID(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
BTF_SET_END(bpf_testmod_check_kfunc_ids) BTF_SET8_END(bpf_testmod_check_kfunc_ids)
static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = { static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.check_set = &bpf_testmod_check_kfunc_ids, .set = &bpf_testmod_check_kfunc_ids,
}; };
extern int bpf_fentry_test1(int a); extern int bpf_fentry_test1(int a);
......
...@@ -2,13 +2,29 @@ ...@@ -2,13 +2,29 @@
#include <test_progs.h> #include <test_progs.h>
#include <network_helpers.h> #include <network_helpers.h>
#include "test_bpf_nf.skel.h" #include "test_bpf_nf.skel.h"
#include "test_bpf_nf_fail.skel.h"
static char log_buf[1024 * 1024];
struct {
const char *prog_name;
const char *err_msg;
} test_bpf_nf_fail_tests[] = {
{ "alloc_release", "kernel function bpf_ct_release args#0 expected pointer to STRUCT nf_conn but" },
{ "insert_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" },
{ "lookup_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" },
{ "set_timeout_after_insert", "kernel function bpf_ct_set_timeout args#0 expected pointer to STRUCT nf_conn___init but" },
{ "set_status_after_insert", "kernel function bpf_ct_set_status args#0 expected pointer to STRUCT nf_conn___init but" },
{ "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" },
{ "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" },
};
enum { enum {
TEST_XDP, TEST_XDP,
TEST_TC_BPF, TEST_TC_BPF,
}; };
void test_bpf_nf_ct(int mode) static void test_bpf_nf_ct(int mode)
{ {
struct test_bpf_nf *skel; struct test_bpf_nf *skel;
int prog_fd, err; int prog_fd, err;
...@@ -39,14 +55,60 @@ void test_bpf_nf_ct(int mode) ...@@ -39,14 +55,60 @@ void test_bpf_nf_ct(int mode)
ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id"); ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id");
ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup"); ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup");
ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple"); ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple");
ASSERT_EQ(skel->data->test_alloc_entry, 0, "Test for alloc new entry");
ASSERT_EQ(skel->data->test_insert_entry, 0, "Test for insert new entry");
ASSERT_EQ(skel->data->test_succ_lookup, 0, "Test for successful lookup");
/* allow some tolerance for test_delta_timeout value to avoid races. */
ASSERT_GT(skel->bss->test_delta_timeout, 8, "Test for min ct timeout update");
ASSERT_LE(skel->bss->test_delta_timeout, 10, "Test for max ct timeout update");
/* expected status is IPS_SEEN_REPLY */
ASSERT_EQ(skel->bss->test_status, 2, "Test for ct status update ");
end: end:
test_bpf_nf__destroy(skel); test_bpf_nf__destroy(skel);
} }
static void test_bpf_nf_ct_fail(const char *prog_name, const char *err_msg)
{
LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
.kernel_log_size = sizeof(log_buf),
.kernel_log_level = 1);
struct test_bpf_nf_fail *skel;
struct bpf_program *prog;
int ret;
skel = test_bpf_nf_fail__open_opts(&opts);
if (!ASSERT_OK_PTR(skel, "test_bpf_nf_fail__open"))
return;
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
goto end;
bpf_program__set_autoload(prog, true);
ret = test_bpf_nf_fail__load(skel);
if (!ASSERT_ERR(ret, "test_bpf_nf_fail__load must fail"))
goto end;
if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) {
fprintf(stderr, "Expected: %s\n", err_msg);
fprintf(stderr, "Verifier: %s\n", log_buf);
}
end:
test_bpf_nf_fail__destroy(skel);
}
void test_bpf_nf(void) void test_bpf_nf(void)
{ {
int i;
if (test__start_subtest("xdp-ct")) if (test__start_subtest("xdp-ct"))
test_bpf_nf_ct(TEST_XDP); test_bpf_nf_ct(TEST_XDP);
if (test__start_subtest("tc-bpf-ct")) if (test__start_subtest("tc-bpf-ct"))
test_bpf_nf_ct(TEST_TC_BPF); test_bpf_nf_ct(TEST_TC_BPF);
for (i = 0; i < ARRAY_SIZE(test_bpf_nf_fail_tests); i++) {
if (test__start_subtest(test_bpf_nf_fail_tests[i].prog_name))
test_bpf_nf_ct_fail(test_bpf_nf_fail_tests[i].prog_name,
test_bpf_nf_fail_tests[i].err_msg);
}
} }
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#define EINVAL 22 #define EINVAL 22
#define ENOENT 2 #define ENOENT 2
extern unsigned long CONFIG_HZ __kconfig;
int test_einval_bpf_tuple = 0; int test_einval_bpf_tuple = 0;
int test_einval_reserved = 0; int test_einval_reserved = 0;
int test_einval_netns_id = 0; int test_einval_netns_id = 0;
...@@ -16,6 +18,11 @@ int test_eproto_l4proto = 0; ...@@ -16,6 +18,11 @@ int test_eproto_l4proto = 0;
int test_enonet_netns_id = 0; int test_enonet_netns_id = 0;
int test_enoent_lookup = 0; int test_enoent_lookup = 0;
int test_eafnosupport = 0; int test_eafnosupport = 0;
int test_alloc_entry = -EINVAL;
int test_insert_entry = -EAFNOSUPPORT;
int test_succ_lookup = -ENOENT;
u32 test_delta_timeout = 0;
u32 test_status = 0;
struct nf_conn; struct nf_conn;
...@@ -26,31 +33,44 @@ struct bpf_ct_opts___local { ...@@ -26,31 +33,44 @@ struct bpf_ct_opts___local {
u8 reserved[3]; u8 reserved[3];
} __attribute__((preserve_access_index)); } __attribute__((preserve_access_index));
struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32, struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym; struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32, struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym; struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
void bpf_ct_release(struct nf_conn *) __ksym; void bpf_ct_release(struct nf_conn *) __ksym;
void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
int bpf_ct_change_status(struct nf_conn *, u32) __ksym;
static __always_inline void static __always_inline void
nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32, nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32), struct bpf_ct_opts___local *, u32),
struct nf_conn *(*alloc_fn)(void *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32),
void *ctx) void *ctx)
{ {
struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 }; struct bpf_ct_opts___local opts_def = { .l4proto = IPPROTO_TCP, .netns_id = -1 };
struct bpf_sock_tuple bpf_tuple; struct bpf_sock_tuple bpf_tuple;
struct nf_conn *ct; struct nf_conn *ct;
int err;
__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4)); __builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));
ct = func(ctx, NULL, 0, &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_einval_bpf_tuple = opts_def.error; test_einval_bpf_tuple = opts_def.error;
opts_def.reserved[0] = 1; opts_def.reserved[0] = 1;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
opts_def.reserved[0] = 0; opts_def.reserved[0] = 0;
opts_def.l4proto = IPPROTO_TCP; opts_def.l4proto = IPPROTO_TCP;
if (ct) if (ct)
...@@ -59,21 +79,24 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32, ...@@ -59,21 +79,24 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
test_einval_reserved = opts_def.error; test_einval_reserved = opts_def.error;
opts_def.netns_id = -2; opts_def.netns_id = -2;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
opts_def.netns_id = -1; opts_def.netns_id = -1;
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_einval_netns_id = opts_def.error; test_einval_netns_id = opts_def.error;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def) - 1); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def) - 1);
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_einval_len_opts = opts_def.error; test_einval_len_opts = opts_def.error;
opts_def.l4proto = IPPROTO_ICMP; opts_def.l4proto = IPPROTO_ICMP;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
opts_def.l4proto = IPPROTO_TCP; opts_def.l4proto = IPPROTO_TCP;
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
...@@ -81,37 +104,75 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32, ...@@ -81,37 +104,75 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
test_eproto_l4proto = opts_def.error; test_eproto_l4proto = opts_def.error;
opts_def.netns_id = 0xf00f; opts_def.netns_id = 0xf00f;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
opts_def.netns_id = -1; opts_def.netns_id = -1;
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_enonet_netns_id = opts_def.error; test_enonet_netns_id = opts_def.error;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_enoent_lookup = opts_def.error; test_enoent_lookup = opts_def.error;
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def, sizeof(opts_def)); ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4) - 1, &opts_def,
sizeof(opts_def));
if (ct) if (ct)
bpf_ct_release(ct); bpf_ct_release(ct);
else else
test_eafnosupport = opts_def.error; test_eafnosupport = opts_def.error;
bpf_tuple.ipv4.saddr = bpf_get_prandom_u32(); /* src IP */
bpf_tuple.ipv4.daddr = bpf_get_prandom_u32(); /* dst IP */
bpf_tuple.ipv4.sport = bpf_get_prandom_u32(); /* src port */
bpf_tuple.ipv4.dport = bpf_get_prandom_u32(); /* dst port */
ct = alloc_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
if (ct) {
struct nf_conn *ct_ins;
bpf_ct_set_timeout(ct, 10000);
bpf_ct_set_status(ct, IPS_CONFIRMED);
ct_ins = bpf_ct_insert_entry(ct);
if (ct_ins) {
struct nf_conn *ct_lk;
ct_lk = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4),
&opts_def, sizeof(opts_def));
if (ct_lk) {
/* update ct entry timeout */
bpf_ct_change_timeout(ct_lk, 10000);
test_delta_timeout = ct_lk->timeout - bpf_jiffies64();
test_delta_timeout /= CONFIG_HZ;
test_status = IPS_SEEN_REPLY;
bpf_ct_change_status(ct_lk, IPS_SEEN_REPLY);
bpf_ct_release(ct_lk);
test_succ_lookup = 0;
}
bpf_ct_release(ct_ins);
test_insert_entry = 0;
}
test_alloc_entry = 0;
}
} }
SEC("xdp") SEC("xdp")
int nf_xdp_ct_test(struct xdp_md *ctx) int nf_xdp_ct_test(struct xdp_md *ctx)
{ {
nf_ct_test((void *)bpf_xdp_ct_lookup, ctx); nf_ct_test((void *)bpf_xdp_ct_lookup, (void *)bpf_xdp_ct_alloc, ctx);
return 0; return 0;
} }
SEC("tc") SEC("tc")
int nf_skb_ct_test(struct __sk_buff *ctx) int nf_skb_ct_test(struct __sk_buff *ctx)
{ {
nf_ct_test((void *)bpf_skb_ct_lookup, ctx); nf_ct_test((void *)bpf_skb_ct_lookup, (void *)bpf_skb_ct_alloc, ctx);
return 0; return 0;
} }
......
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
struct nf_conn;
struct bpf_ct_opts___local {
s32 netns_id;
s32 error;
u8 l4proto;
u8 reserved[3];
} __attribute__((preserve_access_index));
struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
void bpf_ct_release(struct nf_conn *) __ksym;
void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
int bpf_ct_change_status(struct nf_conn *, u32) __ksym;
SEC("?tc")
int alloc_release(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
bpf_ct_release(ct);
return 0;
}
SEC("?tc")
int insert_insert(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
ct = bpf_ct_insert_entry(ct);
if (!ct)
return 0;
ct = bpf_ct_insert_entry(ct);
return 0;
}
SEC("?tc")
int lookup_insert(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_lookup(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
bpf_ct_insert_entry(ct);
return 0;
}
SEC("?tc")
int set_timeout_after_insert(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
ct = bpf_ct_insert_entry(ct);
if (!ct)
return 0;
bpf_ct_set_timeout(ct, 0);
return 0;
}
SEC("?tc")
int set_status_after_insert(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
ct = bpf_ct_insert_entry(ct);
if (!ct)
return 0;
bpf_ct_set_status(ct, 0);
return 0;
}
SEC("?tc")
int change_timeout_after_alloc(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
bpf_ct_change_timeout(ct, 0);
return 0;
}
SEC("?tc")
int change_status_after_alloc(struct __sk_buff *ctx)
{
struct bpf_ct_opts___local opts = {};
struct bpf_sock_tuple tup = {};
struct nf_conn *ct;
ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
if (!ct)
return 0;
bpf_ct_change_status(ct, 0);
return 0;
}
char _license[] SEC("license") = "GPL";
...@@ -251,6 +251,7 @@ ...@@ -251,6 +251,7 @@
.expected_insns = { PSEUDO_CALL_INSN() }, .expected_insns = { PSEUDO_CALL_INSN() },
.unexpected_insns = { HELPER_CALL_INSN() }, .unexpected_insns = { HELPER_CALL_INSN() },
.result = ACCEPT, .result = ACCEPT,
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
.func_info = { { 0, MAIN_TYPE }, { 16, CALLBACK_TYPE } }, .func_info = { { 0, MAIN_TYPE }, { 16, CALLBACK_TYPE } },
.func_info_cnt = 2, .func_info_cnt = 2,
BTF_TYPES BTF_TYPES
......
...@@ -218,6 +218,59 @@ ...@@ -218,6 +218,59 @@
.result = REJECT, .result = REJECT,
.errstr = "variable ptr_ access var_off=(0x0; 0x7) disallowed", .errstr = "variable ptr_ access var_off=(0x0; 0x7) disallowed",
}, },
{
"calls: invalid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID",
.insns = {
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 16),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
.fixup_kfunc_btf_id = {
{ "bpf_kfunc_call_test_acquire", 3 },
{ "bpf_kfunc_call_test_ref", 8 },
{ "bpf_kfunc_call_test_ref", 10 },
},
.result_unpriv = REJECT,
.result = REJECT,
.errstr = "R1 must be referenced",
},
{
"calls: valid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID",
.insns = {
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
},
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
.fixup_kfunc_btf_id = {
{ "bpf_kfunc_call_test_acquire", 3 },
{ "bpf_kfunc_call_test_ref", 8 },
{ "bpf_kfunc_call_test_release", 10 },
},
.result_unpriv = REJECT,
.result = ACCEPT,
},
{ {
"calls: basic sanity", "calls: basic sanity",
.insns = { .insns = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment