10046 lines · 258.4 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* Copyright (c) 2018 Facebook */ #include <uapi/linux/btf.h>#include <uapi/linux/bpf.h>#include <uapi/linux/bpf_perf_event.h>#include <uapi/linux/types.h>#include <linux/seq_file.h>#include <linux/compiler.h>#include <linux/ctype.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/anon_inodes.h>#include <linux/file.h>#include <linux/uaccess.h>#include <linux/kernel.h>#include <linux/idr.h>#include <linux/sort.h>#include <linux/bpf_verifier.h>#include <linux/btf.h>#include <linux/btf_ids.h>#include <linux/bpf.h>#include <linux/bpf_lsm.h>#include <linux/skmsg.h>#include <linux/perf_event.h>#include <linux/bsearch.h>#include <linux/kobject.h>#include <linux/string.h>#include <linux/sysfs.h>#include <linux/overflow.h>#include <linux/bitops.h> #include <net/netfilter/nf_bpf_link.h> #include <net/sock.h>#include <net/xdp.h>#include "../tools/lib/bpf/relo_core.h" /* BTF (BPF Type Format) is the meta data format which describes * the data types of BPF program/map.  Hence, it basically focus * on the C programming language which the modern BPF is primary * using. * * ELF Section: * ~~~~~~~~~~~ * The BTF data is stored under the ".BTF" ELF section * * struct btf_type: * ~~~~~~~~~~~~~~~ * Each 'struct btf_type' object describes a C data type. * Depending on the type it is describing, a 'struct btf_type' * object may be followed by more data.  F.e. * To describe an array, 'struct btf_type' is followed by * 'struct btf_array'. * * 'struct btf_type' and any extra data following it are * 4 bytes aligned. * * Type section: * ~~~~~~~~~~~~~ * The BTF type section contains a list of 'struct btf_type' objects. * Each one describes a C type.  Recall from the above section * that a 'struct btf_type' object could be immediately followed by extra * data in order to describe some particular C types. * * type_id: * ~~~~~~~ * Each btf_type object is identified by a type_id.  The type_id * is implicitly implied by the location of the btf_type object in * the BTF type section.  The first one has type_id 1.  The second * one has type_id 2...etc.  Hence, an earlier btf_type has * a smaller type_id. * * A btf_type object may refer to another btf_type object by using * type_id (i.e. the "type" in the "struct btf_type"). * * NOTE that we cannot assume any reference-order. * A btf_type object can refer to an earlier btf_type object * but it can also refer to a later btf_type object. * * For example, to describe "const void *".  A btf_type * object describing "const" may refer to another btf_type * object describing "void *".  This type-reference is done * by specifying type_id: * * [1] CONST (anon) type_id=2 * [2] PTR (anon) type_id=0 * * The above is the btf_verifier debug log: *   - Each line started with "[?]" is a btf_type object *   - [?] is the type_id of the btf_type object. *   - CONST/PTR is the BTF_KIND_XXX *   - "(anon)" is the name of the type.  It just *     happens that CONST and PTR has no name. *   - type_id=XXX is the 'u32 type' in btf_type * * NOTE: "void" has type_id 0 * * String section: * ~~~~~~~~~~~~~~ * The BTF string section contains the names used by the type section. * Each string is referred by an "offset" from the beginning of the * string section. * * Each string is '\0' terminated. * * The first character in the string section must be '\0' * which is used to mean 'anonymous'. Some btf_type may not * have a name. */ /* BTF verification: * * To verify BTF data, two passes are needed. * * Pass #1 * ~~~~~~~ * The first pass is to collect all btf_type objects to * an array: "btf->types". * * Depending on the C type that a btf_type is describing, * a btf_type may be followed by extra data.  We don't know * how many btf_type is there, and more importantly we don't * know where each btf_type is located in the type section. * * Without knowing the location of each type_id, most verifications * cannot be done.  e.g. an earlier btf_type may refer to a later * btf_type (recall the "const void *" above), so we cannot * check this type-reference in the first pass. * * In the first pass, it still does some verifications (e.g. * checking the name is a valid offset to the string section). * * Pass #2 * ~~~~~~~ * The main focus is to resolve a btf_type that is referring * to another type. * * We have to ensure the referring type: * 1) does exist in the BTF (i.e. in btf->types[]) * 2) does not cause a loop: *	struct A { *		struct B b; *	}; * *	struct B { *		struct A a; *	}; * * btf_type_needs_resolve() decides if a btf_type needs * to be resolved. * * The needs_resolve type implements the "resolve()" ops which * essentially does a DFS and detects backedge. * * During resolve (or DFS), different C types have different * "RESOLVED" conditions. * * When resolving a BTF_KIND_STRUCT, we need to resolve all its * members because a member is always referring to another * type.  A struct's member can be treated as "RESOLVED" if * it is referring to a BTF_KIND_PTR.  Otherwise, the * following valid C struct would be rejected: * *	struct A { *		int m; *		struct A *a; *	}; * * When resolving a BTF_KIND_PTR, it needs to keep resolving if * it is referring to another BTF_KIND_PTR.  Otherwise, we cannot * detect a pointer loop, e.g.: * BTF_KIND_CONST -> BTF_KIND_PTR -> BTF_KIND_CONST -> BTF_KIND_PTR + *                        ^                                         | *                        +-----------------------------------------+ * */ #define BITS_PER_U128 (sizeof(u64) * BITS_PER_BYTE * 2)#define BITS_PER_BYTE_MASK (BITS_PER_BYTE - 1)#define BITS_PER_BYTE_MASKED(bits) ((bits) & BITS_PER_BYTE_MASK)#define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3)#define BITS_ROUNDUP_BYTES(bits) \	(BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) #define BTF_INT_MASK 0x0fffffff#define BTF_TYPE_ID_VALID(type_id) ((type_id) <= BTF_MAX_TYPE)#define BTF_STR_OFFSET_VALID(name_off) ((name_off) <= BTF_MAX_NAME_OFFSET) /* 16MB for 64k structs and each has 16 members and * a few MB spaces for the string section. * The hard limit is S32_MAX. */#define BTF_MAX_SIZE (16 * 1024 * 1024) #define for_each_member_from(i, from, struct_type, member)		\	for (i = from, member = btf_type_member(struct_type) + from;	\	     i < btf_type_vlen(struct_type);				\	     i++, member++) #define for_each_vsi_from(i, from, struct_type, member)				\	for (i = from, member = btf_type_var_secinfo(struct_type) + from;	\	     i < btf_type_vlen(struct_type);					\	     i++, member++) DEFINE_IDR(btf_idr);DEFINE_SPINLOCK(btf_idr_lock); enum btf_kfunc_hook {	BTF_KFUNC_HOOK_COMMON,	BTF_KFUNC_HOOK_XDP,	BTF_KFUNC_HOOK_TC,	BTF_KFUNC_HOOK_STRUCT_OPS,	BTF_KFUNC_HOOK_TRACING,	BTF_KFUNC_HOOK_SYSCALL,	BTF_KFUNC_HOOK_FMODRET,	BTF_KFUNC_HOOK_CGROUP,	BTF_KFUNC_HOOK_SCHED_ACT,	BTF_KFUNC_HOOK_SK_SKB,	BTF_KFUNC_HOOK_SOCKET_FILTER,	BTF_KFUNC_HOOK_LWT,	BTF_KFUNC_HOOK_NETFILTER,	BTF_KFUNC_HOOK_KPROBE,	BTF_KFUNC_HOOK_MAX,}; enum {	BTF_KFUNC_SET_MAX_CNT = 256,	BTF_DTOR_KFUNC_MAX_CNT = 256,	BTF_KFUNC_FILTER_MAX_CNT = 16,}; struct btf_kfunc_hook_filter {	btf_kfunc_filter_t filters[BTF_KFUNC_FILTER_MAX_CNT];	u32 nr_filters;}; struct btf_kfunc_set_tab {	struct btf_id_set8 *sets[BTF_KFUNC_HOOK_MAX];	struct btf_kfunc_hook_filter hook_filters[BTF_KFUNC_HOOK_MAX];}; struct btf_id_dtor_kfunc_tab {	u32 cnt;	struct btf_id_dtor_kfunc dtors[];}; struct btf_struct_ops_tab {	u32 cnt;	u32 capacity;	struct bpf_struct_ops_desc ops[];}; struct btf {	void *data;	struct btf_type **types;	u32 *resolved_ids;	u32 *resolved_sizes;	const char *strings;	void *nohdr_data;	struct btf_header hdr;	u32 nr_types; /* includes VOID for base BTF */	u32 named_start_id;	u32 types_size;	u32 data_size;	refcount_t refcnt;	u32 id;	struct rcu_head rcu;	struct btf_kfunc_set_tab *kfunc_set_tab;	struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab;	struct btf_struct_metas *struct_meta_tab;	struct btf_struct_ops_tab *struct_ops_tab;	struct btf_layout *layout; 	/* split BTF support */	struct btf *base_btf;	u32 start_id; /* first type ID in this BTF (0 for base BTF) */	u32 start_str_off; /* first string offset (0 for base BTF) */	char name[MODULE_NAME_LEN];	bool kernel_btf;	__u32 *base_id_map; /* map from distilled base BTF -> vmlinux BTF ids */}; enum verifier_phase {	CHECK_META,	CHECK_TYPE,}; struct resolve_vertex {	const struct btf_type *t;	u32 type_id;	u32 next_member;}; enum visit_state {	NOT_VISITED,	VISITED,	RESOLVED,}; enum resolve_mode {	RESOLVE_TBD,	/* To Be Determined */	RESOLVE_PTR,	/* Resolving for Pointer */	RESOLVE_STRUCT_OR_ARRAY,	/* Resolving for struct/union					 * or array					 */}; #define MAX_RESOLVE_DEPTH 32 struct btf_sec_info {	u32 off;	u32 len;}; struct btf_verifier_env {	struct btf *btf;	u8 *visit_states;	struct resolve_vertex stack[MAX_RESOLVE_DEPTH];	struct bpf_verifier_log log;	u32 log_type_id;	u32 top_stack;	enum verifier_phase phase;	enum resolve_mode resolve_mode;}; static const char * const btf_kind_str[NR_BTF_KINDS] = {	[BTF_KIND_UNKN]		= "UNKNOWN",	[BTF_KIND_INT]		= "INT",	[BTF_KIND_PTR]		= "PTR",	[BTF_KIND_ARRAY]	= "ARRAY",	[BTF_KIND_STRUCT]	= "STRUCT",	[BTF_KIND_UNION]	= "UNION",	[BTF_KIND_ENUM]		= "ENUM",	[BTF_KIND_FWD]		= "FWD",	[BTF_KIND_TYPEDEF]	= "TYPEDEF",	[BTF_KIND_VOLATILE]	= "VOLATILE",	[BTF_KIND_CONST]	= "CONST",	[BTF_KIND_RESTRICT]	= "RESTRICT",	[BTF_KIND_FUNC]		= "FUNC",	[BTF_KIND_FUNC_PROTO]	= "FUNC_PROTO",	[BTF_KIND_VAR]		= "VAR",	[BTF_KIND_DATASEC]	= "DATASEC",	[BTF_KIND_FLOAT]	= "FLOAT",	[BTF_KIND_DECL_TAG]	= "DECL_TAG",	[BTF_KIND_TYPE_TAG]	= "TYPE_TAG",	[BTF_KIND_ENUM64]	= "ENUM64",}; const char *btf_type_str(const struct btf_type *t){	return btf_kind_str[BTF_INFO_KIND(t->info)];} /* Chunk size we use in safe copy of data to be shown. */#define BTF_SHOW_OBJ_SAFE_SIZE		32 /* * This is the maximum size of a base type value (equivalent to a * 128-bit int); if we are at the end of our safe buffer and have * less than 16 bytes space we can't be assured of being able * to copy the next type safely, so in such cases we will initiate * a new copy. */#define BTF_SHOW_OBJ_BASE_TYPE_SIZE	16 /* Type name size */#define BTF_SHOW_NAME_SIZE		80 /* * The suffix of a type that indicates it cannot alias another type when * comparing BTF IDs for kfunc invocations. */#define NOCAST_ALIAS_SUFFIX		"___init" /* * Common data to all BTF show operations. Private show functions can add * their own data to a structure containing a struct btf_show and consult it * in the show callback.  See btf_type_show() below. * * One challenge with showing nested data is we want to skip 0-valued * data, but in order to figure out whether a nested object is all zeros * we need to walk through it.  As a result, we need to make two passes * when handling structs, unions and arrays; the first path simply looks * for nonzero data, while the second actually does the display.  The first * pass is signalled by show->state.depth_check being set, and if we * encounter a non-zero value we set show->state.depth_to_show to * the depth at which we encountered it.  When we have completed the * first pass, we will know if anything needs to be displayed if * depth_to_show > depth.  See btf_[struct,array]_show() for the * implementation of this. * * Another problem is we want to ensure the data for display is safe to * access.  To support this, the anonymous "struct {} obj" tracks the data * object and our safe copy of it.  We copy portions of the data needed * to the object "copy" buffer, but because its size is limited to * BTF_SHOW_OBJ_COPY_LEN bytes, multiple copies may be required as we * traverse larger objects for display. * * The various data type show functions all start with a call to * btf_show_start_type() which returns a pointer to the safe copy * of the data needed (or if BTF_SHOW_UNSAFE is specified, to the * raw data itself).  btf_show_obj_safe() is responsible for * using copy_from_kernel_nofault() to update the safe data if necessary * as we traverse the object's data.  skbuff-like semantics are * used: * * - obj.head points to the start of the toplevel object for display * - obj.size is the size of the toplevel object * - obj.data points to the current point in the original data at *   which our safe data starts.  obj.data will advance as we copy *   portions of the data. * * In most cases a single copy will suffice, but larger data structures * such as "struct task_struct" will require many copies.  The logic in * btf_show_obj_safe() handles the logic that determines if a new * copy_from_kernel_nofault() is needed. */struct btf_show {	u64 flags;	void *target;	/* target of show operation (seq file, buffer) */	__printf(2, 0) void (*showfn)(struct btf_show *show, const char *fmt, va_list args);	const struct btf *btf;	/* below are used during iteration */	struct {		u8 depth;		u8 depth_to_show;		u8 depth_check;		u8 array_member:1,		   array_terminated:1;		u16 array_encoding;		u32 type_id;		int status;			/* non-zero for error */		const struct btf_type *type;		const struct btf_member *member;		char name[BTF_SHOW_NAME_SIZE];	/* space for member name/type */	} state;	struct {		u32 size;		void *head;		void *data;		u8 safe[BTF_SHOW_OBJ_SAFE_SIZE];	} obj;}; struct btf_kind_operations {	s32 (*check_meta)(struct btf_verifier_env *env,			  const struct btf_type *t,			  u32 meta_left);	int (*resolve)(struct btf_verifier_env *env,		       const struct resolve_vertex *v);	int (*check_member)(struct btf_verifier_env *env,			    const struct btf_type *struct_type,			    const struct btf_member *member,			    const struct btf_type *member_type);	int (*check_kflag_member)(struct btf_verifier_env *env,				  const struct btf_type *struct_type,				  const struct btf_member *member,				  const struct btf_type *member_type);	void (*log_details)(struct btf_verifier_env *env,			    const struct btf_type *t);	void (*show)(const struct btf *btf, const struct btf_type *t,			 u32 type_id, void *data, u8 bits_offsets,			 struct btf_show *show);}; static const struct btf_kind_operations * const kind_ops[NR_BTF_KINDS];static struct btf_type btf_void; static int btf_resolve(struct btf_verifier_env *env,		       const struct btf_type *t, u32 type_id); static int btf_func_check(struct btf_verifier_env *env,			  const struct btf_type *t); static bool btf_type_is_modifier(const struct btf_type *t){	/* Some of them is not strictly a C modifier	 * but they are grouped into the same bucket	 * for BTF concern:	 *   A type (t) that refers to another	 *   type through t->type AND its size cannot	 *   be determined without following the t->type.	 *	 * ptr does not fall into this bucket	 * because its size is always sizeof(void *).	 */	switch (BTF_INFO_KIND(t->info)) {	case BTF_KIND_TYPEDEF:	case BTF_KIND_VOLATILE:	case BTF_KIND_CONST:	case BTF_KIND_RESTRICT:	case BTF_KIND_TYPE_TAG:		return true;	} 	return false;} static int btf_start_id(const struct btf *btf)

Lines 1–500 of 10046.