1711 lines · 39.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * uprobes-based tracing events * * Copyright (C) IBM Corporation, 2010-2012 * Author:	Srikar Dronamraju <srikar@linux.vnet.ibm.com> */#define pr_fmt(fmt)	"trace_uprobe: " fmt #include <linux/bpf-cgroup.h>#include <linux/security.h>#include <linux/ctype.h>#include <linux/module.h>#include <linux/uaccess.h>#include <linux/uprobes.h>#include <linux/namei.h>#include <linux/string.h>#include <linux/rculist.h>#include <linux/filter.h>#include <linux/percpu.h> #include "trace_dynevent.h"#include "trace_probe.h"#include "trace_probe_tmpl.h" #define UPROBE_EVENT_SYSTEM	"uprobes" struct uprobe_trace_entry_head {	struct trace_entry	ent;	unsigned long		vaddr[];}; #define SIZEOF_TRACE_ENTRY(is_return)			\	(sizeof(struct uprobe_trace_entry_head) +	\	 sizeof(unsigned long) * (is_return ? 2 : 1)) #define DATAOF_TRACE_ENTRY(entry, is_return)		\	((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) static int trace_uprobe_create(const char *raw_command);static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);static int trace_uprobe_release(struct dyn_event *ev);static bool trace_uprobe_is_busy(struct dyn_event *ev);static bool trace_uprobe_match(const char *system, const char *event,			int argc, const char **argv, struct dyn_event *ev); static struct dyn_event_operations trace_uprobe_ops = {	.create = trace_uprobe_create,	.show = trace_uprobe_show,	.is_busy = trace_uprobe_is_busy,	.free = trace_uprobe_release,	.match = trace_uprobe_match,}; /* * uprobe event core functions */struct trace_uprobe {	struct dyn_event		devent;	struct uprobe_consumer		consumer;	struct path			path;	char				*filename;	struct uprobe			*uprobe;	unsigned long			offset;	unsigned long			ref_ctr_offset;	unsigned long __percpu		*nhits;	struct trace_probe		tp;}; static bool is_trace_uprobe(struct dyn_event *ev){	return ev->ops == &trace_uprobe_ops;} static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev){	return container_of(ev, struct trace_uprobe, devent);} /** * for_each_trace_uprobe - iterate over the trace_uprobe list * @pos:	the struct trace_uprobe * for each entry * @dpos:	the struct dyn_event * to use as a loop cursor */#define for_each_trace_uprobe(pos, dpos)	\	for_each_dyn_event(dpos)		\		if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) static int register_uprobe_event(struct trace_uprobe *tu);static int unregister_uprobe_event(struct trace_uprobe *tu); static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs,			     __u64 *data);static int uretprobe_dispatcher(struct uprobe_consumer *con,				unsigned long func, struct pt_regs *regs,				__u64 *data); #ifdef CONFIG_STACK_GROWSUPstatic unsigned long adjust_stack_addr(unsigned long addr, unsigned int n){	return addr - (n * sizeof(long));}#elsestatic unsigned long adjust_stack_addr(unsigned long addr, unsigned int n){	return addr + (n * sizeof(long));}#endif static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n){	unsigned long ret;	unsigned long addr = user_stack_pointer(regs); 	addr = adjust_stack_addr(addr, n); 	if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))		return 0; 	return ret;} /* * Uprobes-specific fetch functions */static nokprobe_inline intprobe_mem_read(void *dest, void *src, size_t size){	void __user *vaddr = (void __force __user *)src; 	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;} static nokprobe_inline intprobe_mem_read_user(void *dest, void *src, size_t size){	return probe_mem_read(dest, src, size);} /* * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max * length and relative data location. */static nokprobe_inline intfetch_store_string(unsigned long addr, void *dest, void *base){	long ret;	u32 loc = *(u32 *)dest;	int maxlen  = get_loc_len(loc);	u8 *dst = get_loc_data(dest, base);	void __user *src = (void __force __user *) addr; 	if (unlikely(!maxlen))		return -ENOMEM; 	if (addr == FETCH_TOKEN_COMM)		ret = strscpy(dst, current->comm, maxlen);	else		ret = strncpy_from_user(dst, src, maxlen);	if (ret >= 0) {		if (ret == maxlen)			dst[ret - 1] = '\0';		else			/*			 * Include the terminating null byte. In this case it			 * was copied by strncpy_from_user but not accounted			 * for in ret.			 */			ret++;		*(u32 *)dest = make_data_loc(ret, (void *)dst - base);	} else		*(u32 *)dest = make_data_loc(0, (void *)dst - base); 	return ret;} static nokprobe_inline intfetch_store_string_user(unsigned long addr, void *dest, void *base){	return fetch_store_string(addr, dest, base);} /* Return the length of string -- including null terminal byte */static nokprobe_inline intfetch_store_strlen(unsigned long addr){	int len;	void __user *vaddr = (void __force __user *) addr; 	if (addr == FETCH_TOKEN_COMM)		len = strlen(current->comm) + 1;	else		len = strnlen_user(vaddr, MAX_STRING_SIZE); 	return (len > MAX_STRING_SIZE) ? 0 : len;} static nokprobe_inline intfetch_store_strlen_user(unsigned long addr){	return fetch_store_strlen(addr);} static unsigned long translate_user_vaddr(unsigned long file_offset){	unsigned long base_addr;	struct uprobe_dispatch_data *udd; 	udd = (void *) current->utask->vaddr; 	base_addr = udd->bp_addr - udd->tu->offset;	return base_addr + file_offset;} /* Note that we don't verify it, since the code does not come from user space */static intprocess_fetch_insn(struct fetch_insn *code, void *rec, void *edata,		   void *dest, void *base){	struct pt_regs *regs = rec;	unsigned long val;	int ret; 	/* 1st stage: get value from context */	switch (code->op) {	case FETCH_OP_REG:		val = regs_get_register(regs, code->param);		break;	case FETCH_OP_STACK:		val = get_user_stack_nth(regs, code->param);		break;	case FETCH_OP_STACKP:		val = user_stack_pointer(regs);		break;	case FETCH_OP_RETVAL:		val = regs_return_value(regs);		break;	case FETCH_OP_COMM:		val = FETCH_TOKEN_COMM;		break;	case FETCH_OP_FOFFS:		val = translate_user_vaddr(code->immediate);		break;	default:		ret = process_common_fetch_insn(code, &val);		if (ret < 0)			return ret;	}	code++; 	return process_fetch_insn_bottom(code, val, dest, base);}NOKPROBE_SYMBOL(process_fetch_insn) static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter){	rwlock_init(&filter->rwlock);	filter->nr_systemwide = 0;	INIT_LIST_HEAD(&filter->perf_events);} static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter){	return !filter->nr_systemwide && list_empty(&filter->perf_events);} static inline bool is_ret_probe(struct trace_uprobe *tu){	return tu->consumer.ret_handler != NULL;} static bool trace_uprobe_is_busy(struct dyn_event *ev){	struct trace_uprobe *tu = to_trace_uprobe(ev); 	return trace_probe_is_enabled(&tu->tp);} static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,					    int argc, const char **argv){	char buf[MAX_ARGSTR_LEN + 1];	int len; 	if (!argc)		return true; 	len = strlen(tu->filename);	if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')		return false; 	if (tu->ref_ctr_offset == 0)		snprintf(buf, sizeof(buf), "0x%0*lx",				(int)(sizeof(void *) * 2), tu->offset);	else		snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",				(int)(sizeof(void *) * 2), tu->offset,				tu->ref_ctr_offset);	if (strcmp(buf, &argv[0][len + 1]))		return false; 	argc--; argv++; 	return trace_probe_match_command_args(&tu->tp, argc, argv);} static bool trace_uprobe_match(const char *system, const char *event,			int argc, const char **argv, struct dyn_event *ev){	struct trace_uprobe *tu = to_trace_uprobe(ev); 	return (event[0] == '\0' ||		strcmp(trace_probe_name(&tu->tp), event) == 0) &&	   (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) &&	   trace_uprobe_match_command_head(tu, argc, argv);} static nokprobe_inline struct trace_uprobe *trace_uprobe_primary_from_call(struct trace_event_call *call){	struct trace_probe *tp; 	tp = trace_probe_primary_from_call(call);	if (WARN_ON_ONCE(!tp))		return NULL; 	return container_of(tp, struct trace_uprobe, tp);} /* * Allocate new trace_uprobe and initialize it (including uprobes). */static struct trace_uprobe *alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret){	struct trace_uprobe *tu;	int ret; 	tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL);	if (!tu)		return ERR_PTR(-ENOMEM); 	tu->nhits = alloc_percpu(unsigned long);	if (!tu->nhits) {		ret = -ENOMEM;		goto error;	} 	ret = trace_probe_init(&tu->tp, event, group, true, nargs);	if (ret < 0)		goto error; 	dyn_event_init(&tu->devent, &trace_uprobe_ops);	tu->consumer.handler = uprobe_dispatcher;	if (is_ret)		tu->consumer.ret_handler = uretprobe_dispatcher;	init_trace_uprobe_filter(tu->tp.event->filter);	return tu; error:	free_percpu(tu->nhits);	kfree(tu); 	return ERR_PTR(ret);} static void free_trace_uprobe(struct trace_uprobe *tu){	if (!tu)		return; 	path_put(&tu->path);	trace_probe_cleanup(&tu->tp);	kfree(tu->filename);	free_percpu(tu->nhits);	kfree(tu);} static struct trace_uprobe *find_probe_event(const char *event, const char *group){	struct dyn_event *pos;	struct trace_uprobe *tu; 	for_each_trace_uprobe(tu, pos)		if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&		    strcmp(trace_probe_group_name(&tu->tp), group) == 0)			return tu; 	return NULL;} /* Unregister a trace_uprobe and probe_event */static int unregister_trace_uprobe(struct trace_uprobe *tu){	int ret; 	if (trace_probe_has_sibling(&tu->tp))		goto unreg; 	/* If there's a reference to the dynamic event */	if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp)))		return -EBUSY; 	ret = unregister_uprobe_event(tu);	if (ret)		return ret; unreg:	dyn_event_remove(&tu->devent);	trace_probe_unlink(&tu->tp);	free_trace_uprobe(tu);	return 0;} static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,					 struct trace_uprobe *comp){	struct trace_probe_event *tpe = orig->tp.event;	struct inode *comp_inode = d_real_inode(comp->path.dentry);	int i; 	list_for_each_entry(orig, &tpe->probes, tp.list) {		if (comp_inode != d_real_inode(orig->path.dentry) ||		    comp->offset != orig->offset)			continue; 		/*		 * trace_probe_compare_arg_type() ensured that nr_args and		 * each argument name and type are same. Let's compare comm.		 */		for (i = 0; i < orig->tp.nr_args; i++) {			if (strcmp(orig->tp.args[i].comm,				   comp->tp.args[i].comm))				break;		} 		if (i == orig->tp.nr_args)			return true;	} 	return false;} static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to){	int ret; 	ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);	if (ret) {		/* Note that argument starts index = 2 */		trace_probe_log_set_index(ret + 1);		trace_probe_log_err(0, DIFF_ARG_TYPE);		return -EEXIST;	}	if (trace_uprobe_has_same_uprobe(to, tu)) {		trace_probe_log_set_index(0);		trace_probe_log_err(0, SAME_PROBE);		return -EEXIST;	} 	/* Append to existing event */	ret = trace_probe_append(&tu->tp, &to->tp);	if (!ret)		dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); 	return ret;} /* * Uprobe with multiple reference counter is not allowed. i.e. * If inode and offset matches, reference counter offset *must* * match as well. Though, there is one exception: If user is * replacing old trace_uprobe with new one(same group/event), * then we allow same uprobe with new reference counter as far * as the new one does not conflict with any other existing * ones. */static int validate_ref_ctr_offset(struct trace_uprobe *new){	struct dyn_event *pos;	struct trace_uprobe *tmp;	struct inode *new_inode = d_real_inode(new->path.dentry); 	for_each_trace_uprobe(tmp, pos) {		if (new_inode == d_real_inode(tmp->path.dentry) &&		    new->offset == tmp->offset &&		    new->ref_ctr_offset != tmp->ref_ctr_offset) {			pr_warn("Reference counter offset mismatch.");			return -EINVAL;		}	}	return 0;} /* Register a trace_uprobe and probe_event */static int register_trace_uprobe(struct trace_uprobe *tu){	struct trace_uprobe *old_tu;	int ret; 

Lines 1–500 of 1711.