#if !defined(CONFIG_OPTPROBES) || !defined(CONFIG_SYSCTL) #define kprobe_sysctls_init() do { } while (0) #endif
staticint kprobes_initialized; /* kprobe_table can be accessed by *-NormalhlisttraversalandRCUadd/delunder'kprobe_mutex'isheld. *Or *-RCUhlisttraversalunderdisablingpreempt(breakpointhandlers)
*/ staticstruct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
/* NOTE: change this value only with 'kprobe_mutex' held */ staticbool kprobes_all_disarmed;
/* This protects 'kprobe_table' and 'optimizing_list' */ static DEFINE_MUTEX(kprobe_mutex); static DEFINE_PER_CPU(struct kprobe *, kprobe_instance);
/* Since the slot array is not protected by rcu, we need a mutex */
guard(mutex)(&c->mutex); do {
guard(rcu)();
list_for_each_entry_rcu(kip, &c->pages, list) { if (kip->nused < slots_per_page(c)) { int i;
for (i = 0; i < slots_per_page(c); i++) { if (kip->slot_used[i] == SLOT_CLEAN) {
kip->slot_used[i] = SLOT_USED;
kip->nused++; return kip->insns + (i * c->insn_size);
}
} /* kip->nused is broken. Fix it. */
kip->nused = slots_per_page(c);
WARN_ON(1);
}
} /* If there are any garbage slots, collect it and try again. */
} while (c->nr_garbage && collect_garbage_slots(c) == 0);
/* All out of space. Need to allocate a new page. */
kip = kmalloc(struct_size(kip, slot_used, slots_per_page(c)), GFP_KERNEL); if (!kip) return NULL;
/* Record the perf ksymbol register event after adding the page */
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsignedlong)kip->insns,
PAGE_SIZE, false, c->sym);
return kip->insns;
}
/* Return true if all garbages are collected, otherwise false. */ staticbool collect_one_slot(struct kprobe_insn_page *kip, int idx)
{
kip->slot_used[idx] = SLOT_CLEAN;
kip->nused--; if (kip->nused != 0) returnfalse;
/* Ensure no-one is interrupted on the garbages */
synchronize_rcu();
list_for_each_entry_safe(kip, next, &c->pages, list) { int i;
if (kip->ngarbage == 0) continue;
kip->ngarbage = 0; /* we will collect all garbages */ for (i = 0; i < slots_per_page(c); i++) { if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) break;
}
}
c->nr_garbage = 0; return0;
}
/* We have preemption disabled.. so it is safe to use __ versions */ staticinlinevoid set_kprobe_instance(struct kprobe *kp)
{
__this_cpu_write(kprobe_instance, kp);
}
/* Don't check i == 0, since that is a breakpoint case. */ for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH / sizeof(kprobe_opcode_t); i++)
p = get_kprobe(addr - i);
if (p && kprobe_optready(p)) {
op = container_of(p, struct optimized_kprobe, kp); if (arch_within_optimized_kprobe(op, addr)) return p;
}
lockdep_assert_held(&text_mutex); /* See comment in do_optimize_kprobes() */
lockdep_assert_cpus_held();
if (!list_empty(&unoptimizing_list))
arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
/* Loop on 'freeing_list' for disarming and removing from kprobe hash list */
list_for_each_entry_safe(op, tmp, &freeing_list, list) { /* Switching from detour code to origin */
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; /* Disarm probes if marked disabled and not gone */ if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp))
arch_disarm_kprobe(&op->kp); if (kprobe_unused(&op->kp)) { /* *Removeunusedprobesfromhashlist.Afterwaiting *forsynchronization,theseprobesarereclaimed. *(reclaimingisdonebydo_free_cleaned_kprobes().)
*/
hlist_del_rcu(&op->kp.hlist);
} else
list_del_init(&op->list);
}
}
/* Reclaim all kprobes on the 'freeing_list' */ staticvoid do_free_cleaned_kprobes(void)
{ struct optimized_kprobe *op, *tmp;
while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
mutex_unlock(&kprobe_mutex);
/* This will also make 'optimizing_work' execute immmediately */
flush_delayed_work(&optimizing_work); /* 'optimizing_work' might not have been queued yet, relax */
cpu_relax();
mutex_lock(&kprobe_mutex);
}
}
/* Wait for completing optimization and unoptimization */ void wait_for_kprobe_optimizer(void)
{
guard(mutex)(&kprobe_mutex);
list_for_each_entry(_op, &unoptimizing_list, list) { if (op == _op) returntrue;
}
returnfalse;
}
/* Optimize kprobe if p is ready to be optimized */ staticvoid optimize_kprobe(struct kprobe *p)
{ struct optimized_kprobe *op;
/* Check if the kprobe is disabled or not ready for optimization. */ if (!kprobe_optready(p) || !kprobes_allow_optimization ||
(kprobe_disabled(p) || kprobes_all_disarmed)) return;
/* kprobes with 'post_handler' can not be optimized */ if (p->post_handler) return;
op = container_of(p, struct optimized_kprobe, kp);
/* Check there is no other kprobes at the optimized instructions */ if (arch_check_optimized_kprobe(op) < 0) return;
/* Check if it is already optimized. */ if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { if (optprobe_queued_unopt(op)) { /* This is under unoptimizing. Just dequeue the probe */
list_del_init(&op->list);
} return;
}
op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
/* *Onthe'unoptimizing_list'and'optimizing_list', *'op'musthaveOPTIMIZEDflag
*/ if (WARN_ON_ONCE(!list_empty(&op->list))) return;
/* Short cut to direct unoptimizing */ staticvoid force_unoptimize_kprobe(struct optimized_kprobe *op)
{
lockdep_assert_cpus_held();
arch_unoptimize_kprobe(op);
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
}
/* Unoptimize a kprobe if p is optimized */ staticvoid unoptimize_kprobe(struct kprobe *p, bool force)
{ struct optimized_kprobe *op;
if (!kprobe_aggrprobe(p) || kprobe_disarmed(p)) return; /* This is not an optprobe nor optimized */
op = container_of(p, struct optimized_kprobe, kp); if (!kprobe_optimized(p)) return;
if (!list_empty(&op->list)) { if (optprobe_queued_unopt(op)) { /* Queued in unoptimizing queue */ if (force) { /* *Forciblyunoptimizethekprobehere,andqueueit *inthefreeinglistforreleaseafterwards.
*/
force_unoptimize_kprobe(op);
list_move(&op->list, &freeing_list);
}
} else { /* Dequeue from the optimizing queue */
list_del_init(&op->list);
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
} return;
}
/* Optimized kprobe case */ if (force) { /* Forcibly update the code: this is a special case */
force_unoptimize_kprobe(op);
} else {
list_add(&op->list, &unoptimizing_list);
kick_kprobe_optimizer();
}
}
op = container_of(p, struct optimized_kprobe, kp); if (!list_empty(&op->list)) /* Dequeue from the (un)optimization queue */
list_del_init(&op->list);
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
if (kprobe_unused(p)) { /* *Unusedkprobeisonunoptimizingorfreeinglist.Wemoveit *tofreeing_listandletthekprobe_optimizer()removeitfrom *thekprobehashlistandfreeit.
*/ if (optprobe_queued_unopt(op))
list_move(&op->list, &freeing_list);
}
/* Don't touch the code, because it is already freed. */
arch_remove_optimized_kprobe(op);
}
/* Impossible to optimize ftrace-based kprobe. */ if (kprobe_ftrace(p)) return;
/* For preparing optimization, jump_label_text_reserved() is called. */
guard(cpus_read_lock)();
guard(jump_label_lock)();
guard(mutex)(&text_mutex);
ap = alloc_aggr_kprobe(p); if (!ap) return;
op = container_of(ap, struct optimized_kprobe, kp); if (!arch_prepared_optinsn(&op->optinsn)) { /* If failed to setup optimizing, fallback to kprobe. */
arch_remove_optimized_kprobe(op);
kfree(op); return;
}
init_aggr_kprobe(ap, p);
optimize_kprobe(ap); /* This just kicks optimizer thread. */
}
guard(mutex)(&kprobe_mutex); /* If optimization is already allowed, just return. */ if (kprobes_allow_optimization) return;
cpus_read_lock();
kprobes_allow_optimization = true; for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
head = &kprobe_table[i];
hlist_for_each_entry(p, head, hlist) if (!kprobe_disabled(p))
optimize_kprobe(p);
}
cpus_read_unlock();
pr_info("kprobe jump-optimization is enabled. All kprobes are optimized if possible.\n");
}
guard(mutex)(&kprobe_mutex); /* If optimization is already prohibited, just return. */ if (!kprobes_allow_optimization) return;
cpus_read_lock();
kprobes_allow_optimization = false; for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
head = &kprobe_table[i];
hlist_for_each_entry(p, head, hlist) { if (!kprobe_disabled(p))
unoptimize_kprobe(p, false);
}
}
cpus_read_unlock(); /* Wait for unoptimizing completion. */
wait_for_kprobe_optimizer_locked();
pr_info("kprobe jump-optimization is disabled. All kprobes are based on software breakpoint.\n");
}
static DEFINE_MUTEX(kprobe_sysctl_mutex); staticint sysctl_kprobes_optimization; staticint proc_kprobes_optimization_handler(conststruct ctl_table *table, int write, void *buffer,
size_t *length, loff_t *ppos)
{ int ret;
/* Put a breakpoint for a probe. */ staticvoid __arm_kprobe(struct kprobe *p)
{ struct kprobe *_p;
lockdep_assert_held(&text_mutex);
/* Find the overlapping optimized kprobes. */
_p = get_optimized_kprobe(p->addr); if (unlikely(_p)) /* Fallback to unoptimized kprobe */
unoptimize_kprobe(_p, true);
arch_arm_kprobe(p);
optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
}
/* Remove the breakpoint of a probe. */ staticvoid __disarm_kprobe(struct kprobe *p, bool reopt)
{ struct kprobe *_p;
lockdep_assert_held(&text_mutex);
/* Try to unoptimize */
unoptimize_kprobe(p, kprobes_all_disarmed);
if (!kprobe_queued(p)) {
arch_disarm_kprobe(p); /* If another kprobe was blocked, re-optimize it. */
_p = get_optimized_kprobe(p->addr); if (unlikely(_p) && reopt)
optimize_kprobe(_p);
} /* *TODO:Sinceunoptimizationandrealdisarmingwillbedoneby *theworkerthread,wecannotcheckwhetheranotherprobeare *unoptimizedbecauseofthisprobehere.Itshouldbere-optimized *bytheworkerthread.
*/
}
#else/* !CONFIG_OPTPROBES */
#define optimize_kprobe(p) do {} while (0) #define unoptimize_kprobe(p, f) do {} while (0) #define kill_optimized_kprobe(p) do {} while (0) #define prepare_optimized_kprobe(p) do {} while (0) #define try_to_optimize_kprobe(p) do {} while (0) #define __arm_kprobe(p) arch_arm_kprobe(p) #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) #define kprobe_disarmed(p) kprobe_disabled(p) #define wait_for_kprobe_optimizer_locked() \
lockdep_assert_held(&kprobe_mutex)
staticint __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, int *cnt)
{ int ret;
lockdep_assert_held(&kprobe_mutex);
ret = ftrace_set_filter_ip(ops, (unsignedlong)p->addr, 0, 0); if (WARN_ONCE(ret < 0, "Failed to arm kprobe-ftrace at %pS (error %d)\n", p->addr, ret)) return ret;
if (*cnt == 0) {
ret = register_ftrace_function(ops); if (WARN(ret < 0, "Failed to register kprobe-ftrace (error %d)\n", ret)) { /* *Atthispoint,sinecopsisnotregistered,weshouldbesefefrom *registeringemptyfilter.
*/
ftrace_set_filter_ip(ops, (unsignedlong)p->addr, 1, 0); return ret;
}
}
/* Add the new probe to 'ap->list'. */ staticint add_new_kprobe(struct kprobe *ap, struct kprobe *p)
{ if (p->post_handler)
unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
list_add_rcu(&p->list, &ap->list); if (p->post_handler && !ap->post_handler)
ap->post_handler = aggr_post_handler;
return0;
}
/* *Fillintherequiredfieldsoftheaggregatorkprobe.Replacethe *earlierkprobeinthehlistwiththeaggregatorkprobe.
*/ staticvoid init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
{ /* Copy the insn slot of 'p' to 'ap'. */
copy_kprobe(p, ap);
flush_insn_slot(ap);
ap->addr = p->addr;
ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
ap->pre_handler = aggr_pre_handler; /* We don't care the kprobe which has gone. */ if (p->post_handler && !kprobe_gone(p))
ap->post_handler = aggr_post_handler;
/* *Thisregistersthesecondorsubsequentkprobeatthesameaddress.
*/ staticint register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
{ int ret = 0; struct kprobe *ap = orig_p;
scoped_guard(cpus_read_lock) { /* For preparing optimization, jump_label_text_reserved() is called */
guard(jump_label_lock)();
guard(mutex)(&text_mutex);
if (!kprobe_aggrprobe(orig_p)) { /* If 'orig_p' is not an 'aggr_kprobe', create new one. */
ap = alloc_aggr_kprobe(orig_p); if (!ap) return -ENOMEM;
init_aggr_kprobe(ap, orig_p);
} elseif (kprobe_unused(ap)) { /* This probe is going to die. Rescue it */
ret = reuse_unused_kprobe(ap); if (ret) return ret;
}
if (kprobe_gone(ap)) { /* *Attemptingtoinsertnewprobeatthesamelocationthat *hadaprobeinthemodulevaddrareawhichalready *freed.So,theinstructionslothasalreadybeen *released.Weneedanewslotforthenewprobe.
*/
ret = arch_prepare_kprobe(ap); if (ret) /* *Eveniffailtoallocatenewslot,don'tneedto *freethe'ap'.Itwillbeusednexttime,or *freedbyunregister_kprobe().
*/ return ret;
/* Prepare optimized instructions if possible. */
prepare_optimized_kprobe(ap);
/* Check if the address is on a suffixed-symbol */ if (!lookup_symbol_name(addr, symname)) {
p = strchr(symname, '.'); if (!p) returnfalse;
*p = '\0';
addr = (unsignedlong)kprobe_lookup_name(symname, 0); if (addr) return __within_kprobe_blacklist(addr);
} returnfalse;
}
staticint check_kprobe_address_safe(struct kprobe *p, struct module **probed_mod)
{ int ret;
ret = check_ftrace_location(p); if (ret) return ret;
guard(jump_label_lock)();
/* Ensure the address is in a text area, and find a module if exists. */
*probed_mod = NULL; if (!core_kernel_text((unsignedlong) p->addr)) {
guard(rcu)();
*probed_mod = __module_text_address((unsignedlong) p->addr); if (!(*probed_mod)) return -EINVAL;
/* *Wemustholdarefcountoftheprobedmodulewhileupdating *itscodetoprohibitunexpectedunloading.
*/ if (unlikely(!try_module_get(*probed_mod))) return -ENOENT;
} /* Ensure it is not in reserved area. */ if (in_gate_area_no_mm((unsignedlong) p->addr) ||
within_kprobe_blacklist((unsignedlong) p->addr) ||
jump_label_text_reserved(p->addr, p->addr) ||
static_call_text_reserved(p->addr, p->addr) ||
find_bug((unsignedlong)p->addr) ||
is_cfi_preamble_symbol((unsignedlong)p->addr)) {
module_put(*probed_mod); return -EINVAL;
}
/* Get module refcount and reject __init functions for loaded modules. */ if (IS_ENABLED(CONFIG_MODULES) && *probed_mod) { /* *Ifthemodulefreed'.init.text',wecouldn'tinsert *kprobesinthere.
*/ if (within_module_init((unsignedlong)p->addr, *probed_mod) &&
!module_is_coming(*probed_mod)) {
module_put(*probed_mod); return -ENOENT;
}
}
return0;
}
staticint __register_kprobe(struct kprobe *p)
{ int ret; struct kprobe *old_p;
guard(mutex)(&kprobe_mutex);
old_p = get_kprobe(p->addr); if (old_p) /* Since this may unoptimize 'old_p', locking 'text_mutex'. */ return register_aggr_kprobe(old_p, p);
scoped_guard(cpus_read_lock) { /* Prevent text modification */
guard(mutex)(&text_mutex);
ret = prepare_kprobe(p); if (ret) return ret;
}
if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
ret = arm_kprobe(p); if (ret) {
hlist_del_rcu(&p->hlist);
synchronize_rcu();
}
}
/* Try to optimize kprobe */
try_to_optimize_kprobe(p); return0;
}
int register_kprobe(struct kprobe *p)
{ int ret; struct module *probed_mod;
kprobe_opcode_t *addr; bool on_func_entry;
/* Canonicalize probe address from symbol */
addr = _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry); if (IS_ERR(addr)) return PTR_ERR(addr);
p->addr = addr;
ret = warn_kprobe_rereg(p); if (ret) return ret;
/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
p->flags &= KPROBE_FLAG_DISABLED; if (on_func_entry)
p->flags |= KPROBE_FLAG_ON_FUNC_ENTRY;
p->nmissed = 0;
INIT_LIST_HEAD(&p->list);
ret = check_kprobe_address_safe(p, &probed_mod); if (ret) return ret;
ret = __register_kprobe(p);
if (probed_mod)
module_put(probed_mod);
return ret;
}
EXPORT_SYMBOL_GPL(register_kprobe);
/* Check if all probes on the 'ap' are disabled. */ staticbool aggr_kprobe_disabled(struct kprobe *ap)
{ struct kprobe *kp;
lockdep_assert_held(&kprobe_mutex);
list_for_each_entry(kp, &ap->list, list) if (!kprobe_disabled(kp)) /* *Sincethereisanactiveprobeonthelist, *wecan'tdisablethis'ap'.
*/ returnfalse;
if (list_empty(&p->list)) /* This is an independent kprobe */
arch_remove_kprobe(p); elseif (list_is_singular(&p->list)) { /* This is the last child of an aggrprobe */
ap = list_entry(p->list.next, struct kprobe, list);
list_del(&p->list);
free_aggr_kprobe(ap);
} /* Otherwise, do nothing. */
}
int register_kprobes(struct kprobe **kps, int num)
{ int i, ret = 0;
if (num <= 0) return -EINVAL; for (i = 0; i < num; i++) {
ret = register_kprobe(kps[i]); if (ret < 0) { if (i > 0)
unregister_kprobes(kps, i); break;
}
} return ret;
}
EXPORT_SYMBOL_GPL(register_kprobes);
void unregister_kprobes(struct kprobe **kps, int num)
{ int i;
if (num <= 0) return;
scoped_guard(mutex, &kprobe_mutex) { for (i = 0; i < num; i++) if (__unregister_kprobe_top(kps[i]) < 0)
kps[i]->addr = NULL;
}
synchronize_rcu(); for (i = 0; i < num; i++) if (kps[i]->addr)
__unregister_kprobe_bottom(kps[i]);
}
EXPORT_SYMBOL_GPL(unregister_kprobes);
staticstruct notifier_block kprobe_exceptions_nb = {
.notifier_call = kprobe_exceptions_notify,
.priority = 0x7fffffff /* we need to be notified first */
};
#ifdef CONFIG_KRETPROBES
#if !defined(CONFIG_KRETPROBE_ON_RETHOOK)
/* callbacks for objpool of kretprobe instances */ staticint kretprobe_init_inst(void *nod, void *context)
{ struct kretprobe_instance *ri = nod;
if (!rph) return;
rp->rph = NULL;
objpool_fini(&rph->pool);
}
/* This assumes the 'tsk' is the current task or the is not running. */ static kprobe_opcode_t *__kretprobe_find_ret_addr(struct task_struct *tsk, struct llist_node **cur)
{ struct kretprobe_instance *ri = NULL; struct llist_node *node = *cur;
if (!node)
node = tsk->kretprobe_instances.first; else
node = node->next;
while (node) {
ri = container_of(node, struct kretprobe_instance, llist); if (ri->ret_addr != kretprobe_trampoline_addr()) {
*cur = node; return ri->ret_addr;
}
node = node->next;
} return NULL;
}
NOKPROBE_SYMBOL(__kretprobe_find_ret_addr);
/* Find correct address and all nodes for this frame. */
correct_ret_addr = __kretprobe_find_ret_addr(current, &node); if (!correct_ret_addr) {
pr_err("kretprobe: Return address not found, not execute handler. Maybe there is a bug in the kernel.\n");
BUG_ON(1);
}
/* Run the user handler of the nodes. */
first = current->kretprobe_instances.first; while (first) {
ri = container_of(first, struct kretprobe_instance, llist);
if (WARN_ON_ONCE(ri->fp != frame_pointer)) break;
rp = get_kretprobe(ri); if (rp && rp->handler) { struct kprobe *prev = kprobe_running();
rp->nmissed = 0; /* Establish function entry probe point */
ret = register_kprobe(&rp->kp); if (ret != 0) {
rethook_free(rp->rh);
rp->rh = NULL;
} #else/* !CONFIG_KRETPROBE_ON_RETHOOK */
rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL); if (!rp->rph) return -ENOMEM;
if (objpool_init(&rp->rph->pool, rp->maxactive, rp->data_size + sizeof(struct kretprobe_instance), GFP_KERNEL,
rp->rph, kretprobe_init_inst, kretprobe_fini_pool)) {
kfree(rp->rph);
rp->rph = NULL; return -ENOMEM;
}
rcu_assign_pointer(rp->rph->rp, rp);
rp->nmissed = 0; /* Establish function entry probe point */
ret = register_kprobe(&rp->kp); if (ret != 0)
free_rp_inst(rp); #endif return ret;
}
EXPORT_SYMBOL_GPL(register_kretprobe);
int register_kretprobes(struct kretprobe **rps, int num)
{ int ret = 0, i;
if (num <= 0) return -EINVAL; for (i = 0; i < num; i++) {
ret = register_kretprobe(rps[i]); if (ret < 0) { if (i > 0)
unregister_kretprobes(rps, i); break;
}
} return ret;
}
EXPORT_SYMBOL_GPL(register_kretprobes);
/* Enable one kprobe */ int enable_kprobe(struct kprobe *kp)
{ int ret = 0; struct kprobe *p;
guard(mutex)(&kprobe_mutex);
/* Check whether specified probe is valid. */
p = __get_valid_kprobe(kp); if (unlikely(p == NULL)) return -EINVAL;
if (kprobe_gone(kp)) /* This kprobe has gone, we couldn't enable it. */ return -EINVAL;
if (p != kp)
kp->flags &= ~KPROBE_FLAG_DISABLED;
if (!kprobes_all_disarmed && kprobe_disabled(p)) {
p->flags &= ~KPROBE_FLAG_DISABLED;
ret = arm_kprobe(p); if (ret) {
p->flags |= KPROBE_FLAG_DISABLED; if (p != kp)
kp->flags |= KPROBE_FLAG_DISABLED;
}
} return ret;
}
EXPORT_SYMBOL_GPL(enable_kprobe);
/* Caller must NOT call this in usual path. This is only for critical case */ void dump_kprobe(struct kprobe *kp)
{
pr_err("Dump kprobe:\n.symbol_name = %s, .offset = %x, .addr = %pS\n",
kp->symbol_name, kp->offset, kp->addr);
}
NOKPROBE_SYMBOL(dump_kprobe);
if (!kernel_text_address(entry) ||
!kallsyms_lookup_size_offset(entry, &size, &offset)) return -EINVAL;
ent = kmalloc(sizeof(*ent), GFP_KERNEL); if (!ent) return -ENOMEM;
ent->start_addr = entry;
ent->end_addr = entry + size;
INIT_LIST_HEAD(&ent->list);
list_add_tail(&ent->list, &kprobe_blacklist);
return (int)size;
}
/* Add all symbols in given area into kprobe blacklist */ int kprobe_add_area_blacklist(unsignedlong start, unsignedlong end)
{ unsignedlong entry; int ret = 0;
for (entry = start; entry < end; entry += ret) {
ret = kprobe_add_ksym_blacklist(entry); if (ret < 0) return ret; if (ret == 0) /* In case of alias symbol */
ret = 1;
} return0;
}
for (iter = start; iter < end; iter++) {
entry = (unsignedlong)dereference_symbol_descriptor((void *)*iter);
ret = kprobe_add_ksym_blacklist(entry); if (ret == -EINVAL) continue; if (ret < 0) return ret;
}
/* Symbols in '__kprobes_text' are blacklisted */
ret = kprobe_add_area_blacklist((unsignedlong)__kprobes_text_start,
(unsignedlong)__kprobes_text_end); if (ret) return ret;
/* Symbols in 'noinstr' section are blacklisted */
ret = kprobe_add_area_blacklist((unsignedlong)__noinstr_text_start,
(unsignedlong)__noinstr_text_end);
return ret ? : arch_populate_kprobe_blacklist();
}
#ifdef CONFIG_MODULES /* Remove all symbols in given area from kprobe blacklist */ staticvoid kprobe_remove_area_blacklist(unsignedlong start, unsignedlong end)
{ struct kprobe_blacklist_entry *ent, *n;
list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { if (ent->start_addr < start || ent->start_addr >= end) continue;
list_del(&ent->list);
kfree(ent);
}
}
/* Kill all kprobes on initmem because the target code has been freed. */ for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
head = &kprobe_table[i];
hlist_for_each_entry(p, head, hlist) { if (start <= (void *)p->addr && (void *)p->addr < end)
kill_kprobe(p);
}
}
}
staticint __init init_kprobes(void)
{ int i, err;
/* FIXME allocate the probe table, currently defined statically */ /* initialize all list heads */ for (i = 0; i < KPROBE_TABLE_SIZE; i++)
INIT_HLIST_HEAD(&kprobe_table[i]);
err = populate_kprobe_blacklist(__start_kprobe_blacklist,
__stop_kprobe_blacklist); if (err)
pr_err("Failed to populate blacklist (error %d), kprobes not restricted, be careful using them!\n", err);
if (kretprobe_blacklist_size) { /* lookup the function address from its name */ for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
kretprobe_blacklist[i].addr =
kprobe_lookup_name(kretprobe_blacklist[i].name, 0); if (!kretprobe_blacklist[i].addr)
pr_err("Failed to lookup symbol '%s' for kretprobe blacklist. Maybe the target function is removed or renamed.\n",
kretprobe_blacklist[i].name);
}
}
/* By default, kprobes are armed */
kprobes_all_disarmed = false;
staticint arm_all_kprobes(void)
{ struct hlist_head *head; struct kprobe *p; unsignedint i, total = 0, errors = 0; int err, ret = 0;
guard(mutex)(&kprobe_mutex);
/* If kprobes are armed, just return */ if (!kprobes_all_disarmed) return0;
/* *optimize_kprobe()calledbyarm_kprobe()checks *kprobes_all_disarmed,sosetkprobes_all_disarmedbefore *arm_kprobe.
*/
kprobes_all_disarmed = false; /* Arming kprobes doesn't optimize kprobe itself */ for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
head = &kprobe_table[i]; /* Arm all kprobes on a best-effort basis */
hlist_for_each_entry(p, head, hlist) { if (!kprobe_disabled(p)) {
err = arm_kprobe(p); if (err) {
errors++;
ret = err;
}
total++;
}
}
}
if (errors)
pr_warn("Kprobes globally enabled, but failed to enable %d out of %d probes. Please check which kprobes are kept disabled via debugfs.\n",
errors, total); else
pr_info("Kprobes globally enabled\n");
return ret;
}
staticint disarm_all_kprobes(void)
{ struct hlist_head *head; struct kprobe *p; unsignedint i, total = 0, errors = 0; int err, ret = 0;
guard(mutex)(&kprobe_mutex);
/* If kprobes are already disarmed, just return */ if (kprobes_all_disarmed) return0;
kprobes_all_disarmed = true;
for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
head = &kprobe_table[i]; /* Disarm all kprobes on a best-effort basis */
hlist_for_each_entry(p, head, hlist) { if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
err = disarm_kprobe(p, false); if (err) {
errors++;
ret = err;
}
total++;
}
}
}
if (errors)
pr_warn("Kprobes globally disabled, but failed to disable %d out of %d probes. Please check which kprobes are kept enabled via debugfs.\n",
errors, total); else
pr_info("Kprobes globally disabled\n");
/* Wait for disarming all kprobes by optimizer */
wait_for_kprobe_optimizer_locked(); return ret;
}
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.85Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.