3880 lines · 99.7 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com * Copyright (c) 2016 Facebook */#include <linux/kernel.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/bpf.h>#include <linux/bpf_verifier.h>#include <linux/bpf_perf_event.h>#include <linux/btf.h>#include <linux/filter.h>#include <linux/uaccess.h>#include <linux/ctype.h>#include <linux/kprobes.h>#include <linux/spinlock.h>#include <linux/syscalls.h>#include <linux/error-injection.h>#include <linux/btf_ids.h>#include <linux/bpf_lsm.h>#include <linux/fprobe.h>#include <linux/bsearch.h>#include <linux/sort.h>#include <linux/key.h>#include <linux/namei.h>#include <linux/file.h> #include <net/bpf_sk_storage.h> #include <uapi/linux/bpf.h>#include <uapi/linux/btf.h> #include <asm/tlb.h> #include "trace_probe.h"#include "trace.h" #define CREATE_TRACE_POINTS#include "bpf_trace.h" #define bpf_event_rcu_dereference(p)					\	rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) #define MAX_UPROBE_MULTI_CNT (1U << 20)#define MAX_KPROBE_MULTI_CNT (1U << 20)#define MAX_TRACING_MULTI_CNT (1U << 20) #ifdef CONFIG_MODULESstruct bpf_trace_module {	struct module *module;	struct list_head list;}; static LIST_HEAD(bpf_trace_modules);static DEFINE_MUTEX(bpf_module_mutex); static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name){	struct bpf_raw_event_map *btp, *ret = NULL;	struct bpf_trace_module *btm;	unsigned int i; 	mutex_lock(&bpf_module_mutex);	list_for_each_entry(btm, &bpf_trace_modules, list) {		for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {			btp = &btm->module->bpf_raw_events[i];			if (!strcmp(btp->tp->name, name)) {				if (try_module_get(btm->module))					ret = btp;				goto out;			}		}	}out:	mutex_unlock(&bpf_module_mutex);	return ret;}#elsestatic struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name){	return NULL;}#endif /* CONFIG_MODULES */ u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,				  u64 flags, const struct btf **btf,				  s32 *btf_id);static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx); static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx); /** * trace_call_bpf - invoke BPF program * @call: tracepoint event * @ctx: opaque context pointer * * kprobe handlers execute BPF programs via this helper. * Can be used from static tracepoints in the future. * * Return: BPF programs always return an integer which is interpreted by * kprobe handler as: * 0 - return from kprobe (event is filtered out) * 1 - store kprobe event into ring buffer * Other values are reserved and currently alias to 1 */unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx){	unsigned int ret; 	cant_sleep(); 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {		/*		 * since some bpf program is already running on this cpu,		 * don't call into another bpf program (same or different)		 * and don't send kprobe event into ring-buffer,		 * so return zero here		 */		rcu_read_lock();		bpf_prog_inc_misses_counters(rcu_dereference(call->prog_array));		rcu_read_unlock();		ret = 0;		goto out;	} 	/*	 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock	 * to all call sites, we did a bpf_prog_array_valid() there to check	 * whether call->prog_array is empty or not, which is	 * a heuristic to speed up execution.	 *	 * If bpf_prog_array_valid() fetched prog_array was	 * non-NULL, we go into trace_call_bpf() and do the actual	 * proper rcu_dereference() under RCU lock.	 * If it turns out that prog_array is NULL then, we bail out.	 * For the opposite, if the bpf_prog_array_valid() fetched pointer	 * was NULL, you'll skip the prog_array with the risk of missing	 * out of events when it was updated in between this and the	 * rcu_dereference() which is accepted risk.	 */	rcu_read_lock();	ret = bpf_prog_run_array(rcu_dereference(call->prog_array),				 ctx, bpf_prog_run);	rcu_read_unlock();  out:	__this_cpu_dec(bpf_prog_active); 	return ret;} /** * trace_call_bpf_faultable - invoke BPF program in faultable context * @call: tracepoint event * @ctx: opaque context pointer * * Variant of trace_call_bpf() for faultable tracepoints (syscall * tracepoints). Supports sleepable BPF programs by using rcu_tasks_trace * for lifetime protection and bpf_prog_run_array_sleepable() for per-program * RCU flavor selection, following the uprobe pattern. * * Per-program recursion protection is provided by * bpf_prog_run_array_sleepable(). Global bpf_prog_active is not * needed because syscall tracepoints cannot self-recurse. * * Must be called from a faultable/preemptible context. */unsigned int trace_call_bpf_faultable(struct trace_event_call *call, void *ctx){	struct bpf_prog_array *prog_array; 	might_fault();	guard(rcu_tasks_trace)(); 	prog_array = rcu_dereference_check(call->prog_array,					   rcu_read_lock_trace_held());	return bpf_prog_run_array_sleepable(prog_array, ctx, bpf_prog_run);} #ifdef CONFIG_BPF_KPROBE_OVERRIDEBPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc){	regs_set_return_value(regs, rc);	override_function_with_return(regs);	return 0;} static const struct bpf_func_proto bpf_override_return_proto = {	.func		= bpf_override_return,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_CTX,	.arg2_type	= ARG_ANYTHING,};#endif static __always_inline intbpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr){	int ret; 	ret = copy_from_user_nofault(dst, unsafe_ptr, size);	if (unlikely(ret < 0))		memset(dst, 0, size);	return ret;} BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,	   const void __user *, unsafe_ptr){	return bpf_probe_read_user_common(dst, size, unsafe_ptr);} const struct bpf_func_proto bpf_probe_read_user_proto = {	.func		= bpf_probe_read_user,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,}; static __always_inline intbpf_probe_read_user_str_common(void *dst, u32 size,			       const void __user *unsafe_ptr){	int ret; 	/*	 * NB: We rely on strncpy_from_user() not copying junk past the NUL	 * terminator into `dst`.	 *	 * strncpy_from_user() does long-sized strides in the fast path. If the	 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,	 * then there could be junk after the NUL in `dst`. If user takes `dst`	 * and keys a hash map with it, then semantically identical strings can	 * occupy multiple entries in the map.	 */	ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);	if (unlikely(ret < 0))		memset(dst, 0, size);	return ret;} BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,	   const void __user *, unsafe_ptr){	return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);} const struct bpf_func_proto bpf_probe_read_user_str_proto = {	.func		= bpf_probe_read_user_str,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,}; BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,	   const void *, unsafe_ptr){	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);} const struct bpf_func_proto bpf_probe_read_kernel_proto = {	.func		= bpf_probe_read_kernel,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,}; static __always_inline intbpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr){	int ret; 	/*	 * The strncpy_from_kernel_nofault() call will likely not fill the	 * entire buffer, but that's okay in this circumstance as we're probing	 * arbitrary memory anyway similar to bpf_probe_read_*() and might	 * as well probe the stack. Thus, memory is explicitly cleared	 * only in error case, so that improper users ignoring return	 * code altogether don't copy garbage; otherwise length of string	 * is returned that can be used for bpf_perf_event_output() et al.	 */	ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);	if (unlikely(ret < 0))		memset(dst, 0, size);	return ret;} BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,	   const void *, unsafe_ptr){	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);} const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {	.func		= bpf_probe_read_kernel_str,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,}; #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACEBPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,	   const void *, unsafe_ptr){	if ((unsigned long)unsafe_ptr < TASK_SIZE) {		return bpf_probe_read_user_common(dst, size,				(__force void __user *)unsafe_ptr);	}	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);} static const struct bpf_func_proto bpf_probe_read_compat_proto = {	.func		= bpf_probe_read_compat,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,}; BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,	   const void *, unsafe_ptr){	if ((unsigned long)unsafe_ptr < TASK_SIZE) {		return bpf_probe_read_user_str_common(dst, size,				(__force void __user *)unsafe_ptr);	}	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);} static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {	.func		= bpf_probe_read_compat_str,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,	.arg3_type	= ARG_ANYTHING,};#endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,	   u32, size){	/*	 * Ensure we're in user context which is safe for the helper to	 * run. This helper has no business in a kthread.	 *	 * access_ok() should prevent writing to non-user memory, but in	 * some situations (nommu, temporary switch, etc) access_ok() does	 * not provide enough validation, hence the check on KERNEL_DS.	 *	 * nmi_uaccess_okay() ensures the probe is not run in an interim	 * state, when the task or mm are switched. This is specifically	 * required to prevent the use of temporary mm.	 */ 	if (unlikely(in_interrupt() ||		     current->flags & (PF_KTHREAD | PF_EXITING)))		return -EPERM;	if (unlikely(!nmi_uaccess_okay()))		return -EPERM; 	return copy_to_user_nofault(unsafe_ptr, src, size);} static const struct bpf_func_proto bpf_probe_write_user_proto = {	.func		= bpf_probe_write_user,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_ANYTHING,	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,	.arg3_type	= ARG_CONST_SIZE,}; #define MAX_TRACE_PRINTK_VARARGS	3#define BPF_TRACE_PRINTK_SIZE		1024 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,	   u64, arg2, u64, arg3){	u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };	struct bpf_bprintf_data data = {		.get_bin_args	= true,		.get_buf	= true,	};	int ret; 	ret = bpf_bprintf_prepare(fmt, fmt_size, args,				  MAX_TRACE_PRINTK_VARARGS, &data);	if (ret < 0)		return ret; 	ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args); 	trace_bpf_trace_printk(data.buf); 	bpf_bprintf_cleanup(&data); 	return ret;} static const struct bpf_func_proto bpf_trace_printk_proto = {	.func		= bpf_trace_printk,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,	.arg2_type	= ARG_CONST_SIZE,}; static void __set_printk_clr_event(struct work_struct *work){	/*	 * This program might be calling bpf_trace_printk,	 * so enable the associated bpf_trace/bpf_trace_printk event.	 * Repeat this each time as it is possible a user has	 * disabled bpf_trace_printk events.  By loading a program	 * calling bpf_trace_printk() however the user has expressed	 * the intent to see such events.	 */	if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))		pr_warn_ratelimited("could not enable bpf_trace_printk events");}static DECLARE_WORK(set_printk_work, __set_printk_clr_event); const struct bpf_func_proto *bpf_get_trace_printk_proto(void){	schedule_work(&set_printk_work);	return &bpf_trace_printk_proto;} BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,	   u32, data_len){	struct bpf_bprintf_data data = {		.get_bin_args	= true,		.get_buf	= true,	};	int ret, num_args; 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||	    (data_len && !args))		return -EINVAL;	num_args = data_len / 8; 	ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);	if (ret < 0)		return ret; 	ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args); 	trace_bpf_trace_printk(data.buf); 	bpf_bprintf_cleanup(&data); 	return ret;} static const struct bpf_func_proto bpf_trace_vprintk_proto = {	.func		= bpf_trace_vprintk,	.gpl_only	= true,	.ret_type	= RET_INTEGER,	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,	.arg2_type	= ARG_CONST_SIZE,	.arg3_type	= ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,	.arg4_type	= ARG_CONST_SIZE_OR_ZERO,}; const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void){	schedule_work(&set_printk_work);	return &bpf_trace_vprintk_proto;} BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,	   const void *, args, u32, data_len){	struct bpf_bprintf_data data = {		.get_bin_args	= true,	};	int err, num_args; 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||	    (data_len && !args))		return -EINVAL;	num_args = data_len / 8; 

Lines 1–500 of 3880.