netfilter: nf_conntrack: restrict runtime expect hashsize modifications
[safe/jmp/linux-2.6] / kernel / kprobes.c
index dae198b..b7df302 100644 (file)
@@ -90,6 +90,9 @@ static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  */
 static struct kprobe_blackpoint kprobe_blacklist[] = {
        {"preempt_schedule",},
+       {"native_get_debugreg",},
+       {"irq_entries_start",},
+       {"common_interrupt",},
        {NULL}    /* Terminator */
 };
 
@@ -103,7 +106,7 @@ static struct kprobe_blackpoint kprobe_blacklist[] = {
 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
 
 struct kprobe_insn_page {
-       struct hlist_node hlist;
+       struct list_head list;
        kprobe_opcode_t *insns;         /* Page of instruction slots */
        char slot_used[INSNS_PER_PAGE];
        int nused;
@@ -117,7 +120,7 @@ enum kprobe_slot_state {
 };
 
 static DEFINE_MUTEX(kprobe_insn_mutex);        /* Protects kprobe_insn_pages */
-static struct hlist_head kprobe_insn_pages;
+static LIST_HEAD(kprobe_insn_pages);
 static int kprobe_garbage_slots;
 static int collect_garbage_slots(void);
 
@@ -152,10 +155,9 @@ loop_end:
 static kprobe_opcode_t __kprobes *__get_insn_slot(void)
 {
        struct kprobe_insn_page *kip;
-       struct hlist_node *pos;
 
  retry:
-       hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
+       list_for_each_entry(kip, &kprobe_insn_pages, list) {
                if (kip->nused < INSNS_PER_PAGE) {
                        int i;
                        for (i = 0; i < INSNS_PER_PAGE; i++) {
@@ -189,8 +191,8 @@ static kprobe_opcode_t __kprobes *__get_insn_slot(void)
                kfree(kip);
                return NULL;
        }
-       INIT_HLIST_NODE(&kip->hlist);
-       hlist_add_head(&kip->hlist, &kprobe_insn_pages);
+       INIT_LIST_HEAD(&kip->list);
+       list_add(&kip->list, &kprobe_insn_pages);
        memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
        kip->slot_used[0] = SLOT_USED;
        kip->nused = 1;
@@ -219,12 +221,8 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
                 * so as not to have to set it up again the
                 * next time somebody inserts a probe.
                 */
-               hlist_del(&kip->hlist);
-               if (hlist_empty(&kprobe_insn_pages)) {
-                       INIT_HLIST_NODE(&kip->hlist);
-                       hlist_add_head(&kip->hlist,
-                                      &kprobe_insn_pages);
-               } else {
+               if (!list_is_singular(&kprobe_insn_pages)) {
+                       list_del(&kip->list);
                        module_free(NULL, kip->insns);
                        kfree(kip);
                }
@@ -235,18 +233,13 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
 
 static int __kprobes collect_garbage_slots(void)
 {
-       struct kprobe_insn_page *kip;
-       struct hlist_node *pos, *next;
-       int safety;
+       struct kprobe_insn_page *kip, *next;
 
        /* Ensure no-one is preepmted on the garbages */
-       mutex_unlock(&kprobe_insn_mutex);
-       safety = check_safety();
-       mutex_lock(&kprobe_insn_mutex);
-       if (safety != 0)
+       if (check_safety())
                return -EAGAIN;
 
-       hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
+       list_for_each_entry_safe(kip, next, &kprobe_insn_pages, list) {
                int i;
                if (kip->ngarbage == 0)
                        continue;
@@ -264,19 +257,17 @@ static int __kprobes collect_garbage_slots(void)
 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
 {
        struct kprobe_insn_page *kip;
-       struct hlist_node *pos;
 
        mutex_lock(&kprobe_insn_mutex);
-       hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
+       list_for_each_entry(kip, &kprobe_insn_pages, list) {
                if (kip->insns <= slot &&
                    slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
                        int i = (slot - kip->insns) / MAX_INSN_SIZE;
                        if (dirty) {
                                kip->slot_used[i] = SLOT_DIRTY;
                                kip->ngarbage++;
-                       } else {
+                       } else
                                collect_one_slot(kip, i);
-                       }
                        break;
                }
        }
@@ -319,6 +310,22 @@ struct kprobe __kprobes *get_kprobe(void *addr)
        return NULL;
 }
 
+/* Arm a kprobe with text_mutex */
+static void __kprobes arm_kprobe(struct kprobe *kp)
+{
+       mutex_lock(&text_mutex);
+       arch_arm_kprobe(kp);
+       mutex_unlock(&text_mutex);
+}
+
+/* Disarm a kprobe with text_mutex */
+static void __kprobes disarm_kprobe(struct kprobe *kp)
+{
+       mutex_lock(&text_mutex);
+       arch_disarm_kprobe(kp);
+       mutex_unlock(&text_mutex);
+}
+
 /*
  * Aggregate handlers for multiple kprobes support - these handlers
  * take care of invoking the individual kprobe handlers on p->list
@@ -328,7 +335,7 @@ static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
        struct kprobe *kp;
 
        list_for_each_entry_rcu(kp, &p->list, list) {
-               if (kp->pre_handler && !kprobe_gone(kp)) {
+               if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
                        set_kprobe_instance(kp);
                        if (kp->pre_handler(kp, regs))
                                return 1;
@@ -344,7 +351,7 @@ static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
        struct kprobe *kp;
 
        list_for_each_entry_rcu(kp, &p->list, list) {
-               if (kp->post_handler && !kprobe_gone(kp)) {
+               if (kp->post_handler && likely(!kprobe_disabled(kp))) {
                        set_kprobe_instance(kp);
                        kp->post_handler(kp, regs, flags);
                        reset_kprobe_instance();
@@ -523,6 +530,7 @@ static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
 */
 static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
 {
+       BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
        if (p->break_handler) {
                if (ap->break_handler)
                        return -EEXIST;
@@ -532,6 +540,13 @@ static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
                list_add_rcu(&p->list, &ap->list);
        if (p->post_handler && !ap->post_handler)
                ap->post_handler = aggr_post_handler;
+
+       if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
+               ap->flags &= ~KPROBE_FLAG_DISABLED;
+               if (!kprobes_all_disarmed)
+                       /* Arm the breakpoint again. */
+                       arm_kprobe(ap);
+       }
        return 0;
 }
 
@@ -592,20 +607,36 @@ static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
                         * freed by unregister_kprobe.
                         */
                        return ret;
-               /* Clear gone flag to prevent allocating new slot again. */
-               ap->flags &= ~KPROBE_FLAG_GONE;
+
                /*
-                * If the old_p has gone, its breakpoint has been disarmed.
-                * We have to arm it again after preparing real kprobes.
+                * Clear gone flag to prevent allocating new slot again, and
+                * set disabled flag because it is not armed yet.
                 */
-               if (!kprobes_all_disarmed)
-                       arch_arm_kprobe(ap);
+               ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
+                           | KPROBE_FLAG_DISABLED;
        }
 
        copy_kprobe(ap, p);
        return add_new_kprobe(ap, p);
 }
 
+/* Try to disable aggr_kprobe, and return 1 if succeeded.*/
+static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
+{
+       struct kprobe *kp;
+
+       list_for_each_entry_rcu(kp, &p->list, list) {
+               if (!kprobe_disabled(kp))
+                       /*
+                        * There is an active probe on the list.
+                        * We can't disable aggr_kprobe.
+                        */
+                       return 0;
+       }
+       p->flags |= KPROBE_FLAG_DISABLED;
+       return 1;
+}
+
 static int __kprobes in_kprobes_functions(unsigned long addr)
 {
        struct kprobe_blackpoint *kb;
@@ -645,6 +676,40 @@ static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
        return (kprobe_opcode_t *)(((char *)addr) + p->offset);
 }
 
+/* Check passed kprobe is valid and return kprobe in kprobe_table. */
+static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
+{
+       struct kprobe *old_p, *list_p;
+
+       old_p = get_kprobe(p->addr);
+       if (unlikely(!old_p))
+               return NULL;
+
+       if (p != old_p) {
+               list_for_each_entry_rcu(list_p, &old_p->list, list)
+                       if (list_p == p)
+                       /* kprobe p is a valid probe */
+                               goto valid;
+               return NULL;
+       }
+valid:
+       return old_p;
+}
+
+/* Return error if the kprobe is being re-registered */
+static inline int check_kprobe_rereg(struct kprobe *p)
+{
+       int ret = 0;
+       struct kprobe *old_p;
+
+       mutex_lock(&kprobe_mutex);
+       old_p = __get_valid_kprobe(p);
+       if (old_p)
+               ret = -EINVAL;
+       mutex_unlock(&kprobe_mutex);
+       return ret;
+}
+
 int __kprobes register_kprobe(struct kprobe *p)
 {
        int ret = 0;
@@ -657,14 +722,20 @@ int __kprobes register_kprobe(struct kprobe *p)
                return -EINVAL;
        p->addr = addr;
 
+       ret = check_kprobe_rereg(p);
+       if (ret)
+               return ret;
+
        preempt_disable();
-       if (!__kernel_text_address((unsigned long) p->addr) ||
+       if (!kernel_text_address((unsigned long) p->addr) ||
            in_kprobes_functions((unsigned long) p->addr)) {
                preempt_enable();
                return -EINVAL;
        }
 
-       p->flags = 0;
+       /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
+       p->flags &= KPROBE_FLAG_DISABLED;
+
        /*
         * Check if are we probing a module.
         */
@@ -709,7 +780,7 @@ int __kprobes register_kprobe(struct kprobe *p)
        hlist_add_head_rcu(&p->hlist,
                       &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
 
-       if (!kprobes_all_disarmed)
+       if (!kprobes_all_disarmed && !kprobe_disabled(p))
                arch_arm_kprobe(p);
 
 out_unlock_text:
@@ -731,18 +802,10 @@ static int __kprobes __unregister_kprobe_top(struct kprobe *p)
 {
        struct kprobe *old_p, *list_p;
 
-       old_p = get_kprobe(p->addr);
-       if (unlikely(!old_p))
+       old_p = __get_valid_kprobe(p);
+       if (old_p == NULL)
                return -EINVAL;
 
-       if (p != old_p) {
-               list_for_each_entry_rcu(list_p, &old_p->list, list)
-                       if (list_p == p)
-                       /* kprobe p is a valid probe */
-                               goto valid_p;
-               return -EINVAL;
-       }
-valid_p:
        if (old_p == p ||
            (old_p->pre_handler == aggr_pre_handler &&
             list_is_singular(&old_p->list))) {
@@ -751,11 +814,8 @@ valid_p:
                 * enabled and not gone - otherwise, the breakpoint would
                 * already have been removed. We save on flushing icache.
                 */
-               if (!kprobes_all_disarmed && !kprobe_gone(old_p)) {
-                       mutex_lock(&text_mutex);
-                       arch_disarm_kprobe(p);
-                       mutex_unlock(&text_mutex);
-               }
+               if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
+                       disarm_kprobe(p);
                hlist_del_rcu(&old_p->hlist);
        } else {
                if (p->break_handler && !kprobe_gone(p))
@@ -769,6 +829,11 @@ valid_p:
                }
 noclean:
                list_del_rcu(&p->list);
+               if (!kprobe_disabled(old_p)) {
+                       try_to_disable_aggr_kprobe(old_p);
+                       if (!kprobes_all_disarmed && kprobe_disabled(old_p))
+                               disarm_kprobe(old_p);
+               }
        }
        return 0;
 }
@@ -970,9 +1035,9 @@ int __kprobes register_kretprobe(struct kretprobe *rp)
        /* Pre-allocate memory for max kretprobe instances */
        if (rp->maxactive <= 0) {
 #ifdef CONFIG_PREEMPT
-               rp->maxactive = max(10, 2 * NR_CPUS);
+               rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
 #else
-               rp->maxactive = NR_CPUS;
+               rp->maxactive = num_possible_cpus();
 #endif
        }
        spin_lock_init(&rp->lock);
@@ -1078,6 +1143,7 @@ static int __kprobes pre_handler_kretprobe(struct kprobe *p,
 static void __kprobes kill_kprobe(struct kprobe *p)
 {
        struct kprobe *kp;
+
        p->flags |= KPROBE_FLAG_GONE;
        if (p->pre_handler == aggr_pre_handler) {
                /*
@@ -1096,6 +1162,13 @@ static void __kprobes kill_kprobe(struct kprobe *p)
        arch_remove_kprobe(p);
 }
 
+void __kprobes dump_kprobe(struct kprobe *kp)
+{
+       printk(KERN_WARNING "Dumping kprobe:\n");
+       printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
+              kp->symbol_name, kp->addr, kp->offset);
+}
+
 /* Module notifier call back, checking kprobes on the module */
 static int __kprobes kprobes_module_callback(struct notifier_block *nb,
                                             unsigned long val, void *data)
@@ -1219,12 +1292,18 @@ static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
        else
                kprobe_type = "k";
        if (sym)
-               seq_printf(pi, "%p  %s  %s+0x%x  %s %s\n", p->addr, kprobe_type,
-                       sym, offset, (modname ? modname : " "),
-                       (kprobe_gone(p) ? "[GONE]" : ""));
+               seq_printf(pi, "%p  %s  %s+0x%x  %s %s%s\n",
+                       p->addr, kprobe_type, sym, offset,
+                       (modname ? modname : " "),
+                       (kprobe_gone(p) ? "[GONE]" : ""),
+                       ((kprobe_disabled(p) && !kprobe_gone(p)) ?
+                        "[DISABLED]" : ""));
        else
-               seq_printf(pi, "%p  %s  %p %s\n", p->addr, kprobe_type, p->addr,
-                       (kprobe_gone(p) ? "[GONE]" : ""));
+               seq_printf(pi, "%p  %s  %p %s%s\n",
+                       p->addr, kprobe_type, p->addr,
+                       (kprobe_gone(p) ? "[GONE]" : ""),
+                       ((kprobe_disabled(p) && !kprobe_gone(p)) ?
+                        "[DISABLED]" : ""));
 }
 
 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
@@ -1270,7 +1349,7 @@ static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
        return 0;
 }
 
-static struct seq_operations kprobes_seq_ops = {
+static const struct seq_operations kprobes_seq_ops = {
        .start = kprobe_seq_start,
        .next  = kprobe_seq_next,
        .stop  = kprobe_seq_stop,
@@ -1282,13 +1361,78 @@ static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
        return seq_open(filp, &kprobes_seq_ops);
 }
 
-static struct file_operations debugfs_kprobes_operations = {
+static const struct file_operations debugfs_kprobes_operations = {
        .open           = kprobes_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = seq_release,
 };
 
+/* Disable one kprobe */
+int __kprobes disable_kprobe(struct kprobe *kp)
+{
+       int ret = 0;
+       struct kprobe *p;
+
+       mutex_lock(&kprobe_mutex);
+
+       /* Check whether specified probe is valid. */
+       p = __get_valid_kprobe(kp);
+       if (unlikely(p == NULL)) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       /* If the probe is already disabled (or gone), just return */
+       if (kprobe_disabled(kp))
+               goto out;
+
+       kp->flags |= KPROBE_FLAG_DISABLED;
+       if (p != kp)
+               /* When kp != p, p is always enabled. */
+               try_to_disable_aggr_kprobe(p);
+
+       if (!kprobes_all_disarmed && kprobe_disabled(p))
+               disarm_kprobe(p);
+out:
+       mutex_unlock(&kprobe_mutex);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(disable_kprobe);
+
+/* Enable one kprobe */
+int __kprobes enable_kprobe(struct kprobe *kp)
+{
+       int ret = 0;
+       struct kprobe *p;
+
+       mutex_lock(&kprobe_mutex);
+
+       /* Check whether specified probe is valid. */
+       p = __get_valid_kprobe(kp);
+       if (unlikely(p == NULL)) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (kprobe_gone(kp)) {
+               /* This kprobe has gone, we couldn't enable it. */
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!kprobes_all_disarmed && kprobe_disabled(p))
+               arm_kprobe(p);
+
+       p->flags &= ~KPROBE_FLAG_DISABLED;
+       if (p != kp)
+               kp->flags &= ~KPROBE_FLAG_DISABLED;
+out:
+       mutex_unlock(&kprobe_mutex);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(enable_kprobe);
+
 static void __kprobes arm_all_kprobes(void)
 {
        struct hlist_head *head;
@@ -1306,7 +1450,7 @@ static void __kprobes arm_all_kprobes(void)
        for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
                head = &kprobe_table[i];
                hlist_for_each_entry_rcu(p, node, head, hlist)
-                       if (!kprobe_gone(p))
+                       if (!kprobe_disabled(p))
                                arch_arm_kprobe(p);
        }
        mutex_unlock(&text_mutex);
@@ -1338,7 +1482,7 @@ static void __kprobes disarm_all_kprobes(void)
        for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
                head = &kprobe_table[i];
                hlist_for_each_entry_rcu(p, node, head, hlist) {
-                       if (!arch_trampoline_kprobe(p) && !kprobe_gone(p))
+                       if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
                                arch_disarm_kprobe(p);
                }
        }
@@ -1399,7 +1543,7 @@ static ssize_t write_enabled_file_bool(struct file *file,
        return count;
 }
 
-static struct file_operations fops_kp = {
+static const struct file_operations fops_kp = {
        .read =         read_enabled_file_bool,
        .write =        write_enabled_file_bool,
 };