Skip to content

Commit d0aaff9

Browse files
Prasanna S PanchamukhiLinus Torvalds
authored andcommitted
[PATCH] Kprobes: prevent possible race conditions generic
There are possible race conditions if probes are placed on routines within the kprobes files and routines used by the kprobes. For example if you put probe on get_kprobe() routines, the system can hang while inserting probes on any routine such as do_fork(). Because while inserting probes on do_fork(), register_kprobes() routine grabs the kprobes spin lock and executes get_kprobe() routine and to handle probe of get_kprobe(), kprobes_handler() gets executed and tries to grab kprobes spin lock, and spins forever. This patch avoids such possible race conditions by preventing probes on routines within the kprobes file and routines used by kprobes. I have modified the patches as per Andi Kleen's suggestion to move kprobes routines and other routines used by kprobes to a seperate section .kprobes.text. Also moved page fault and exception handlers, general protection fault to .kprobes.text section. These patches have been tested on i386, x86_64 and ppc64 architectures, also compiled on ia64 and sparc64 architectures. Signed-off-by: Prasanna S Panchamukhi <prasanna@in.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 505db03 commit d0aaff9

5 files changed

Lines changed: 60 additions & 29 deletions

File tree

‎include/asm-generic/sections.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ extern char _sextratext[] __attribute__((weak));
1212
extern char _eextratext[] __attribute__((weak));
1313
extern char _end[];
1414
extern char __per_cpu_start[], __per_cpu_end[];
15+
extern char __kprobes_text_start[], __kprobes_text_end[];
1516

1617
#endif /* _ASM_GENERIC_SECTIONS_H_ */

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,9 @@
9797
VMLINUX_SYMBOL(__lock_text_start) = .; \
9898
*(.spinlock.text) \
9999
VMLINUX_SYMBOL(__lock_text_end) = .;
100+
101+
#define KPROBES_TEXT \
102+
ALIGN_FUNCTION(); \
103+
VMLINUX_SYMBOL(__kprobes_text_start) = .; \
104+
*(.kprobes.text) \
105+
VMLINUX_SYMBOL(__kprobes_text_end) = .;

‎include/linux/kprobes.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#define KPROBE_REENTER 0x00000004
4343
#define KPROBE_HIT_SSDONE 0x00000008
4444

45+
/* Attach to insert probes on any functions which should be ignored*/
46+
#define __kprobes __attribute__((__section__(".kprobes.text")))
47+
4548
struct kprobe;
4649
struct pt_regs;
4750
struct kretprobe;

‎include/linux/linkage.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
ALIGN; \
3434
name:
3535

36+
#define KPROBE_ENTRY(name) \
37+
.section .kprobes.text, "ax"; \
38+
.globl name; \
39+
ALIGN; \
40+
name:
41+
42+
3643
#endif
3744

3845
#define NORET_TYPE /**/

‎kernel/kprobes.c‎

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/init.h>
3838
#include <linux/module.h>
3939
#include <linux/moduleloader.h>
40+
#include <asm-generic/sections.h>
4041
#include <asm/cacheflush.h>
4142
#include <asm/errno.h>
4243
#include <asm/kdebug.h>
@@ -72,7 +73,7 @@ static struct hlist_head kprobe_insn_pages;
7273
* get_insn_slot() - Find a slot on an executable page for an instruction.
7374
* We allocate an executable page if there's no room on existing ones.
7475
*/
75-
kprobe_opcode_t *get_insn_slot(void)
76+
kprobe_opcode_t __kprobes *get_insn_slot(void)
7677
{
7778
struct kprobe_insn_page *kip;
7879
struct hlist_node *pos;
@@ -117,7 +118,7 @@ kprobe_opcode_t *get_insn_slot(void)
117118
return kip->insns;
118119
}
119120

120-
void free_insn_slot(kprobe_opcode_t *slot)
121+
void __kprobes free_insn_slot(kprobe_opcode_t *slot)
121122
{
122123
struct kprobe_insn_page *kip;
123124
struct hlist_node *pos;
@@ -152,20 +153,20 @@ void free_insn_slot(kprobe_opcode_t *slot)
152153
}
153154

154155
/* Locks kprobe: irqs must be disabled */
155-
void lock_kprobes(void)
156+
void __kprobes lock_kprobes(void)
156157
{
157158
spin_lock(&kprobe_lock);
158159
kprobe_cpu = smp_processor_id();
159160
}
160161

161-
void unlock_kprobes(void)
162+
void __kprobes unlock_kprobes(void)
162163
{
163164
kprobe_cpu = NR_CPUS;
164165
spin_unlock(&kprobe_lock);
165166
}
166167

167168
/* You have to be holding the kprobe_lock */
168-
struct kprobe *get_kprobe(void *addr)
169+
struct kprobe __kprobes *get_kprobe(void *addr)
169170
{
170171
struct hlist_head *head;
171172
struct hlist_node *node;
@@ -183,7 +184,7 @@ struct kprobe *get_kprobe(void *addr)
183184
* Aggregate handlers for multiple kprobes support - these handlers
184185
* take care of invoking the individual kprobe handlers on p->list
185186
*/
186-
static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
187+
static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
187188
{
188189
struct kprobe *kp;
189190

@@ -198,8 +199,8 @@ static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
198199
return 0;
199200
}
200201

201-
static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
202-
unsigned long flags)
202+
static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
203+
unsigned long flags)
203204
{
204205
struct kprobe *kp;
205206

@@ -213,8 +214,8 @@ static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
213214
return;
214215
}
215216

216-
static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
217-
int trapnr)
217+
static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
218+
int trapnr)
218219
{
219220
/*
220221
* if we faulted "during" the execution of a user specified
@@ -227,7 +228,7 @@ static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
227228
return 0;
228229
}
229230

230-
static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
231+
static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
231232
{
232233
struct kprobe *kp = curr_kprobe;
233234
if (curr_kprobe && kp->break_handler) {
@@ -240,7 +241,7 @@ static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
240241
return 0;
241242
}
242243

243-
struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
244+
struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
244245
{
245246
struct hlist_node *node;
246247
struct kretprobe_instance *ri;
@@ -249,7 +250,8 @@ struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
249250
return NULL;
250251
}
251252

252-
static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
253+
static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
254+
*rp)
253255
{
254256
struct hlist_node *node;
255257
struct kretprobe_instance *ri;
@@ -258,7 +260,7 @@ static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
258260
return NULL;
259261
}
260262

261-
void add_rp_inst(struct kretprobe_instance *ri)
263+
void __kprobes add_rp_inst(struct kretprobe_instance *ri)
262264
{
263265
/*
264266
* Remove rp inst off the free list -
@@ -276,7 +278,7 @@ void add_rp_inst(struct kretprobe_instance *ri)
276278
hlist_add_head(&ri->uflist, &ri->rp->used_instances);
277279
}
278280

279-
void recycle_rp_inst(struct kretprobe_instance *ri)
281+
void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
280282
{
281283
/* remove rp inst off the rprobe_inst_table */
282284
hlist_del(&ri->hlist);
@@ -291,7 +293,7 @@ void recycle_rp_inst(struct kretprobe_instance *ri)
291293
kfree(ri);
292294
}
293295

294-
struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
296+
struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
295297
{
296298
return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
297299
}
@@ -302,7 +304,7 @@ struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
302304
* instances associated with this task. These left over instances represent
303305
* probed functions that have been called but will never return.
304306
*/
305-
void kprobe_flush_task(struct task_struct *tk)
307+
void __kprobes kprobe_flush_task(struct task_struct *tk)
306308
{
307309
struct kretprobe_instance *ri;
308310
struct hlist_head *head;
@@ -322,7 +324,8 @@ void kprobe_flush_task(struct task_struct *tk)
322324
* This kprobe pre_handler is registered with every kretprobe. When probe
323325
* hits it will set up the return probe.
324326
*/
325-
static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
327+
static int __kprobes pre_handler_kretprobe(struct kprobe *p,
328+
struct pt_regs *regs)
326329
{
327330
struct kretprobe *rp = container_of(p, struct kretprobe, kp);
328331

@@ -353,7 +356,7 @@ static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
353356
* Add the new probe to old_p->list. Fail if this is the
354357
* second jprobe at the address - two jprobes can't coexist
355358
*/
356-
static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
359+
static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
357360
{
358361
struct kprobe *kp;
359362

@@ -395,7 +398,8 @@ static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
395398
* the intricacies
396399
* TODO: Move kcalloc outside the spinlock
397400
*/
398-
static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
401+
static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
402+
struct kprobe *p)
399403
{
400404
int ret = 0;
401405
struct kprobe *ap;
@@ -434,15 +438,25 @@ static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
434438
spin_unlock_irqrestore(&kprobe_lock, flags);
435439
}
436440

437-
int register_kprobe(struct kprobe *p)
441+
static int __kprobes in_kprobes_functions(unsigned long addr)
442+
{
443+
if (addr >= (unsigned long)__kprobes_text_start
444+
&& addr < (unsigned long)__kprobes_text_end)
445+
return -EINVAL;
446+
return 0;
447+
}
448+
449+
int __kprobes register_kprobe(struct kprobe *p)
438450
{
439451
int ret = 0;
440452
unsigned long flags = 0;
441453
struct kprobe *old_p;
442454

443-
if ((ret = arch_prepare_kprobe(p)) != 0) {
455+
if ((ret = in_kprobes_functions((unsigned long) p->addr)) != 0)
456+
return ret;
457+
if ((ret = arch_prepare_kprobe(p)) != 0)
444458
goto rm_kprobe;
445-
}
459+
446460
spin_lock_irqsave(&kprobe_lock, flags);
447461
old_p = get_kprobe(p->addr);
448462
p->nmissed = 0;
@@ -466,7 +480,7 @@ int register_kprobe(struct kprobe *p)
466480
return ret;
467481
}
468482

469-
void unregister_kprobe(struct kprobe *p)
483+
void __kprobes unregister_kprobe(struct kprobe *p)
470484
{
471485
unsigned long flags;
472486
struct kprobe *old_p;
@@ -487,7 +501,7 @@ static struct notifier_block kprobe_exceptions_nb = {
487501
.priority = 0x7fffffff /* we need to notified first */
488502
};
489503

490-
int register_jprobe(struct jprobe *jp)
504+
int __kprobes register_jprobe(struct jprobe *jp)
491505
{
492506
/* Todo: Verify probepoint is a function entry point */
493507
jp->kp.pre_handler = setjmp_pre_handler;
@@ -496,14 +510,14 @@ int register_jprobe(struct jprobe *jp)
496510
return register_kprobe(&jp->kp);
497511
}
498512

499-
void unregister_jprobe(struct jprobe *jp)
513+
void __kprobes unregister_jprobe(struct jprobe *jp)
500514
{
501515
unregister_kprobe(&jp->kp);
502516
}
503517

504518
#ifdef ARCH_SUPPORTS_KRETPROBES
505519

506-
int register_kretprobe(struct kretprobe *rp)
520+
int __kprobes register_kretprobe(struct kretprobe *rp)
507521
{
508522
int ret = 0;
509523
struct kretprobe_instance *inst;
@@ -540,14 +554,14 @@ int register_kretprobe(struct kretprobe *rp)
540554

541555
#else /* ARCH_SUPPORTS_KRETPROBES */
542556

543-
int register_kretprobe(struct kretprobe *rp)
557+
int __kprobes register_kretprobe(struct kretprobe *rp)
544558
{
545559
return -ENOSYS;
546560
}
547561

548562
#endif /* ARCH_SUPPORTS_KRETPROBES */
549563

550-
void unregister_kretprobe(struct kretprobe *rp)
564+
void __kprobes unregister_kretprobe(struct kretprobe *rp)
551565
{
552566
unsigned long flags;
553567
struct kretprobe_instance *ri;

0 commit comments

Comments
 (0)