From 821c2ed6e2df804039b994c52615fd6bc756c8ab Mon Sep 17 00:00:00 2001 From: "Prasanna S. Panchamukhi" Date: Wed, 30 Mar 2005 16:40:16 -0800 Subject: [PATCH] Kprobes: Allow/deny probes on int3/breakpoint instruction? Kprobes did an improper exit when a probe is inserted on an int3 instruction. In case of normal execution of int3/breakpoint instruction, it oops!. Probe on an int3 instruction was not handled properly by the kprobes, it generated faults after oops! doing an improper exit with holding the lock. This fix employes a bit different method to handle probe on an int3/breakpoint instruction. On execution of an int3/breakpoint instruction (placed by kprobe), kprobes_handler() is called which sets it for single stepping in-line(it does not matter whether we single step out-of-line/inline since the single stepping instruction is same). Now it single steps on int3/breakpoint instruction here, entering kprobes_handler() once again. Kprobes now check's the status that it is single stepping and avoids the recursion. It runs down through the trap handler and oops messages is seen on the console since it executed int3/breakpoint instruction. Here the kprobes single stepping handler never gets called. Is this behaviour acceptable ? Or should we avoid putting probes on an int3 /breakpoint instruction ? How should it handle such situations? Below is the patch to allow probes on an int3/breakpoint instruction. This patch fixes the above problem by doing a proper exit while avoiding recursion. Signed-off-by: Prasanna S Panchamukhi Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- arch/i386/kernel/kprobes.c | 12 +++++++++++- arch/ppc64/kernel/kprobes.c | 12 +++++++++++- arch/sparc64/kernel/kprobes.c | 16 ++++++++++++++-- arch/x86_64/kernel/kprobes.c | 13 +++++++++++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c index 82776ab94e3ab..6716816592434 100644 --- a/arch/i386/kernel/kprobes.c +++ b/arch/i386/kernel/kprobes.c @@ -84,7 +84,11 @@ static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) { regs->eflags |= TF_MASK; regs->eflags &= ~IF_MASK; - regs->eip = (unsigned long)&p->ainsn.insn; + /*single step inline if the instruction is an int3*/ + if (p->opcode == BREAKPOINT_INSTRUCTION) + regs->eip = (unsigned long)p->addr; + else + regs->eip = (unsigned long)&p->ainsn.insn; } /* @@ -117,6 +121,12 @@ static int kprobe_handler(struct pt_regs *regs) Disarm the probe we just hit, and ignore it. */ p = get_kprobe(addr); if (p) { + if (kprobe_status == KPROBE_HIT_SS) { + regs->eflags &= ~TF_MASK; + regs->eflags |= kprobe_saved_eflags; + unlock_kprobes(); + goto no_kprobe; + } disarm_kprobe(p, regs); ret = 1; } else { diff --git a/arch/ppc64/kernel/kprobes.c b/arch/ppc64/kernel/kprobes.c index ba524723c6b94..103daaf735734 100644 --- a/arch/ppc64/kernel/kprobes.c +++ b/arch/ppc64/kernel/kprobes.c @@ -71,7 +71,11 @@ static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs) static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) { regs->msr |= MSR_SE; - regs->nip = (unsigned long)&p->ainsn.insn; + /*single step inline if it a breakpoint instruction*/ + if (p->opcode == BREAKPOINT_INSTRUCTION) + regs->nip = (unsigned long)p->addr; + else + regs->nip = (unsigned long)&p->ainsn.insn; } static inline int kprobe_handler(struct pt_regs *regs) @@ -86,6 +90,12 @@ static inline int kprobe_handler(struct pt_regs *regs) Disarm the probe we just hit, and ignore it. */ p = get_kprobe(addr); if (p) { + if (kprobe_status == KPROBE_HIT_SS) { + regs->msr &= ~MSR_SE; + regs->msr |= kprobe_saved_msr; + unlock_kprobes(); + goto no_kprobe; + } disarm_kprobe(p, regs); ret = 1; } else { diff --git a/arch/sparc64/kernel/kprobes.c b/arch/sparc64/kernel/kprobes.c index 491bf28a4370d..7066d7ba667a5 100644 --- a/arch/sparc64/kernel/kprobes.c +++ b/arch/sparc64/kernel/kprobes.c @@ -68,8 +68,14 @@ static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL); regs->tstate |= TSTATE_PIL; - regs->tpc = (unsigned long) &p->ainsn.insn[0]; - regs->tnpc = (unsigned long) &p->ainsn.insn[1]; + /*single step inline, if it a breakpoint instruction*/ + if (p->opcode == BREAKPOINT_INSTRUCTION) { + regs->tpc = (unsigned long) p->addr; + regs->tnpc = current_kprobe_orig_tnpc; + } else { + regs->tpc = (unsigned long) &p->ainsn.insn[0]; + regs->tnpc = (unsigned long) &p->ainsn.insn[1]; + } } static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs) @@ -97,6 +103,12 @@ static int kprobe_handler(struct pt_regs *regs) */ p = get_kprobe(addr); if (p) { + if (kprobe_status == KPROBE_HIT_SS) { + regs->tstate = ((regs->tstate & ~TSTATE_PIL) | + current_kprobe_orig_tstate_pil); + unlock_kprobes(); + goto no_kprobe; + } disarm_kprobe(p, regs); ret = 1; } else { diff --git a/arch/x86_64/kernel/kprobes.c b/arch/x86_64/kernel/kprobes.c index 87f43dd7878f3..4f2a852299b63 100644 --- a/arch/x86_64/kernel/kprobes.c +++ b/arch/x86_64/kernel/kprobes.c @@ -233,8 +233,11 @@ static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) { regs->eflags |= TF_MASK; regs->eflags &= ~IF_MASK; - - regs->rip = (unsigned long)p->ainsn.insn; + /*single step inline if the instruction is an int3*/ + if (p->opcode == BREAKPOINT_INSTRUCTION) + regs->rip = (unsigned long)p->addr; + else + regs->rip = (unsigned long)p->ainsn.insn; } /* @@ -256,6 +259,12 @@ int kprobe_handler(struct pt_regs *regs) Disarm the probe we just hit, and ignore it. */ p = get_kprobe(addr); if (p) { + if (kprobe_status == KPROBE_HIT_SS) { + regs->eflags &= ~TF_MASK; + regs->eflags |= kprobe_saved_rflags; + unlock_kprobes(); + goto no_kprobe; + } disarm_kprobe(p, regs); ret = 1; } else { -- cgit 1.2.3-korg