Skip to contentSkip to conversation

powerpc: a few kprobe fixes and refactoring

PATCH Applied Naveen N. Rao 6 participants 38 messages Apr 19, 2017 – Apr 25, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover powerpc: a few kprobe fixes and refactoring

v2:
https://www.mail-archive.com/[email protected]/msg1375870.html

v3 changes:
- Patch 3/5 in the previous series ("powerpc: introduce a new helper to obtain function entry points") has been dropped from this series and will instead be posted as part of the KPROBES_ON_FTRACE patchset.
- Patch 5/5 in the previous series ("powerpc: kprobes: emulate instructions on kprobe handler re-entry") has been split into two patches as recommended by Masami. They represent patches 6/7 and 7/7 in this series.
- Patches 3/7 and 4/7 are new in this series to address review comments from David Laight.

- Naveen

Naveen N. Rao (7):
kprobes: convert kprobe_lookup_name() to a function
powerpc: kprobes: fix handling of function offsets on ABIv2
kprobes: validate the symbol name length
powerpc: kprobes: use safer string functions in kprobe_lookup_name()
powerpc: kprobes: factor out code to emulate instruction into a helper
powerpc: kprobes: emulate instructions on kprobe handler re-entry
powerpc: kprobes: remove duplicate saving of msr

 arch/powerpc/include/asm/kprobes.h |  53 -----------------
 arch/powerpc/kernel/kprobes.c      | 118 ++++++++++++++++++++++++++++++-------
 arch/powerpc/kernel/optprobes.c    |   4 +-
 include/linux/kprobes.h            |   2 +
 kernel/kprobes.c                   |  45 ++++++++++----
 kernel/trace/trace_kprobe.c        |   4 ++
 6 files changed, 137 insertions(+), 89 deletions(-)
Signature · 1 line
2.12.1
#1 lore raw

PATCH v3 1/7 kprobes: convert kprobe_lookup_name() to a function

The macro is now pretty long and ugly on powerpc. In the light of further changes needed here, convert it to a __weak variant to be over-ridden with a nicer looking function.

---
 arch/powerpc/include/asm/kprobes.h | 53 ----------------------------------
 arch/powerpc/kernel/kprobes.c      | 58 ++++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/optprobes.c    |  4 +--
 include/linux/kprobes.h            |  1 +
 kernel/kprobes.c                   | 20 ++++++-------
 5 files changed, 69 insertions(+), 67 deletions(-)
arch/powerpc/include/asm/kprobes.h +0 -53
index 0503c98b2117..a843884aafaf 100644
@@ -61,59 +61,6 @@ extern kprobe_opcode_t optprobe_template_end[];
 #define MAX_OPTINSN_SIZE	(optprobe_template_end - optprobe_template_entry)
 #define RELATIVEJUMP_SIZE	sizeof(kprobe_opcode_t)	/* 4 bytes */
 
-#ifdef PPC64_ELF_ABI_v2
-/* PPC64 ABIv2 needs local entry point */
-#define kprobe_lookup_name(name, addr)					\
-{									\
-	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);		\
-	if (addr)							\
-		addr = (kprobe_opcode_t *)ppc_function_entry(addr);	\
-}
-#elif defined(PPC64_ELF_ABI_v1)
-/*
- * 64bit powerpc ABIv1 uses function descriptors:
- * - Check for the dot variant of the symbol first.
- * - If that fails, try looking up the symbol provided.
- *
- * This ensures we always get to the actual symbol and not the descriptor.
- * Also handle <module:symbol> format.
- */
-#define kprobe_lookup_name(name, addr)					\
-{									\
-	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];		\
-	const char *modsym;							\
-	bool dot_appended = false;					\
-	if ((modsym = strchr(name, ':')) != NULL) {			\
-		modsym++;						\
-		if (*modsym != '\0' && *modsym != '.') {		\
-			/* Convert to <module:.symbol> */		\
-			strncpy(dot_name, name, modsym - name);		\
-			dot_name[modsym - name] = '.';			\
-			dot_name[modsym - name + 1] = '\0';		\
-			strncat(dot_name, modsym,			\
-				sizeof(dot_name) - (modsym - name) - 2);\
-			dot_appended = true;				\
-		} else {						\
-			dot_name[0] = '\0';				\
-			strncat(dot_name, name, sizeof(dot_name) - 1);	\
-		}							\
-	} else if (name[0] != '.') {					\
-		dot_name[0] = '.';					\
-		dot_name[1] = '\0';					\
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);		\
-		dot_appended = true;					\
-	} else {							\
-		dot_name[0] = '\0';					\
-		strncat(dot_name, name, KSYM_NAME_LEN - 1);		\
-	}								\
-	addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);	\
-	if (!addr && dot_appended) {					\
-		/* Let's try the original non-dot symbol lookup	*/	\
-		addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);	\
-	}								\
-}
-#endif
-
 #define flush_insn_slot(p)	do { } while (0)
 #define kretprobe_blacklist_size 0
 
arch/powerpc/kernel/kprobes.c +58 -0

64 lines not on this page · raw message

arch/powerpc/kernel/optprobes.c +2 -2
index 2282bf4e63cd..aefe076d00e0 100644
@@ -243,8 +243,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 	/*
 	 * 2. branch to optimized_callback() and emulate_step()
 	 */
-	kprobe_lookup_name("optimized_callback", op_callback_addr);
-	kprobe_lookup_name("emulate_step", emulate_step_addr);
+	op_callback_addr = kprobe_lookup_name("optimized_callback");
+	emulate_step_addr = kprobe_lookup_name("emulate_step");
 	if (!op_callback_addr || !emulate_step_addr) {
 		WARN(1, "kprobe_lookup_name() failed\n");
 		goto error;
include/linux/kprobes.h +1 -0
index c328e4f7dcad..16f153c84646 100644
@@ -379,6 +379,7 @@ static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
 	return this_cpu_ptr(&kprobe_ctlblk);
 }
 
+kprobe_opcode_t *kprobe_lookup_name(const char *name);
 int register_kprobe(struct kprobe *p);
 void unregister_kprobe(struct kprobe *p);
 int register_kprobes(struct kprobe **kps, int num);
kernel/kprobes.c +8 -12
index 699c5bc51a92..f3421b6b47a3 100644
@@ -58,15 +58,6 @@
 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
 
 
-/*
- * Some oddball architectures like 64bit powerpc have function descriptors
- * so this must be overridable.
- */
-#ifndef kprobe_lookup_name
-#define kprobe_lookup_name(name, addr) \
-	addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
-#endif
-
 static int kprobes_initialized;
 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
@@ -81,6 +72,11 @@ static struct {
 	raw_spinlock_t lock ____cacheline_aligned_in_smp;
 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
 
+kprobe_opcode_t * __weak kprobe_lookup_name(const char *name)
+{
+	return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
+}
+
 static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
 {
 	return &(kretprobe_table_locks[hash].lock);
@@ -1400,7 +1396,7 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
-		kprobe_lookup_name(p->symbol_name, addr);
+		addr = kprobe_lookup_name(p->symbol_name);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
 	}
@@ -2192,8 +2188,8 @@ static int __init init_kprobes(void)
 	if (kretprobe_blacklist_size) {
 		/* lookup the function address from its name */
 		for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
-			kprobe_lookup_name(kretprobe_blacklist[i].name,
-					   kretprobe_blacklist[i].addr);
+			kretprobe_blacklist[i].addr =
+				kprobe_lookup_name(kretprobe_blacklist[i].name);
 			if (!kretprobe_blacklist[i].addr)
 				printk("kretprobe: lookup failed: %s\n",
 				       kretprobe_blacklist[i].name);
Signature · 1 line
2.12.1
#2 lore raw

Re: [v3,1/7] kprobes: convert kprobe_lookup_name() to a function

Quoted 7 lines · Naveen N. Rao

The macro is now pretty long and ugly on powerpc. In the light of further changes needed here, convert it to a __weak variant to be over-ridden with a nicer looking function.

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/49e0b4658fe6aab5bf6bfe0738a86c

cheers

#3 lore raw

PATCH v3 2/7 powerpc: kprobes: fix handling of function offsets on ABIv2

In reply to Naveen N. Rao

commit 239aeba76409 ("perf powerpc: Fix kprobe and kretprobe handling with kallsyms on ppc64le") changed how we use the offset field in struct kprobe on ABIv2. perf now offsets from the GEP (Global entry point) if an offset is specified and otherwise chooses the LEP (Local entry point).

Fix the same in kernel for kprobe API users. We do this by extending kprobe_lookup_name() to accept an additional parameter to indicate the offset specified with the kprobe registration. If offset is 0, we return the local function entry and return the global entry point otherwise.

With:

	# cd /sys/kernel/debug/tracing/
	# echo "p _do_fork" >> kprobe_events
	# echo "p _do_fork+0x10" >> kprobe_events

before this patch:

	# cat ../kprobes/list
	c0000000000d0748  k  _do_fork+0x8    [DISABLED]
	c0000000000d0758  k  _do_fork+0x18    [DISABLED]
	c0000000000412b0  k  kretprobe_trampoline+0x0    [OPTIMIZED]

and after:

	# cat ../kprobes/list
	c0000000000d04c8  k  _do_fork+0x8    [DISABLED]
	c0000000000d04d0  k  _do_fork+0x10    [DISABLED]
	c0000000000412b0  k  kretprobe_trampoline+0x0    [OPTIMIZED]
---
 arch/powerpc/kernel/kprobes.c   | 4 ++--
 arch/powerpc/kernel/optprobes.c | 4 ++--
 include/linux/kprobes.h         | 2 +-
 kernel/kprobes.c                | 7 ++++---
 4 files changed, 9 insertions(+), 8 deletions(-)
arch/powerpc/kernel/kprobes.c +2 -2
index 71c30025cc8a..97b5eed1f76d 100644
@@ -42,14 +42,14 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 
 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
 
-kprobe_opcode_t *kprobe_lookup_name(const char *name)
+kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 {
 	kprobe_opcode_t *addr;
 
 #ifdef PPC64_ELF_ABI_v2
 	/* PPC64 ABIv2 needs local entry point */
 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
-	if (addr)
+	if (addr && !offset)
 		addr = (kprobe_opcode_t *)ppc_function_entry(addr);
 #elif defined(PPC64_ELF_ABI_v1)
 	/*
arch/powerpc/kernel/optprobes.c +2 -2
index aefe076d00e0..ce81a322251c 100644
@@ -243,8 +243,8 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
 	/*
 	 * 2. branch to optimized_callback() and emulate_step()
 	 */
-	op_callback_addr = kprobe_lookup_name("optimized_callback");
-	emulate_step_addr = kprobe_lookup_name("emulate_step");
+	op_callback_addr = kprobe_lookup_name("optimized_callback", 0);
+	emulate_step_addr = kprobe_lookup_name("emulate_step", 0);
 	if (!op_callback_addr || !emulate_step_addr) {
 		WARN(1, "kprobe_lookup_name() failed\n");
 		goto error;
include/linux/kprobes.h +1 -1
index 16f153c84646..1f82a3db00b1 100644
@@ -379,7 +379,7 @@ static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
 	return this_cpu_ptr(&kprobe_ctlblk);
 }
 
-kprobe_opcode_t *kprobe_lookup_name(const char *name);
+kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
 int register_kprobe(struct kprobe *p);
 void unregister_kprobe(struct kprobe *p);
 int register_kprobes(struct kprobe **kps, int num);
kernel/kprobes.c +4 -3
index f3421b6b47a3..6a128f3a7ed1 100644
@@ -72,7 +72,8 @@ static struct {
 	raw_spinlock_t lock ____cacheline_aligned_in_smp;
 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
 
-kprobe_opcode_t * __weak kprobe_lookup_name(const char *name)
+kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
+					unsigned int __unused)
 {
 	return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
 }
@@ -1396,7 +1397,7 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
-		addr = kprobe_lookup_name(p->symbol_name);
+		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
 	}
@@ -2189,7 +2190,7 @@ static int __init init_kprobes(void)
 		/* 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);
+				kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
 			if (!kretprobe_blacklist[i].addr)
 				printk("kretprobe: lookup failed: %s\n",
 				       kretprobe_blacklist[i].name);
Signature · 1 line
2.12.1
#4 lore raw

Re: [v3,2/7] powerpc: kprobes: fix handling of function offsets on ABIv2

Quoted 21 lines · Naveen N. Rao

commit 239aeba76409 ("perf powerpc: Fix kprobe and kretprobe handling with kallsyms on ppc64le") changed how we use the offset field in struct kprobe on ABIv2. perf now offsets from the GEP (Global entry point) if an offset is specified and otherwise chooses the LEP (Local entry point).

Fix the same in kernel for kprobe API users. We do this by extending kprobe_lookup_name() to accept an additional parameter to indicate the offset specified with the kprobe registration. If offset is 0, we return the local function entry and return the global entry point otherwise.

With:

	# cd /sys/kernel/debug/tracing/
	# echo "p _do_fork" >> kprobe_events
	# echo "p _do_fork+0x10" >> kprobe_events

before this patch:

	# cat ../kprobes/list
	c0000000000d0748  k  _do_fork+0x8    [DISABLED]
	c0000000000d0758  k  _do_fork+0x18    [DISABLED]
	c0000000000412b0  k  kretprobe_trampoline+0x0    [OPTIMIZED]
Quoted 8 lines

and after:

	# cat ../kprobes/list
	c0000000000d04c8  k  _do_fork+0x8    [DISABLED]
	c0000000000d04d0  k  _do_fork+0x10    [DISABLED]
	c0000000000412b0  k  kretprobe_trampoline+0x0    [OPTIMIZED]

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/290e3070762ac80e5fc4087d8c4de7

cheers

#5 lore raw

PATCH v3 3/7 kprobes: validate the symbol name length

In reply to Naveen N. Rao

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

---
 include/linux/kprobes.h     |  1 +
 kernel/kprobes.c            | 24 ++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c |  4 ++++
 3 files changed, 29 insertions(+)
include/linux/kprobes.h +1 -0
index 1f82a3db00b1..4ee10fef5135 100644
@@ -405,6 +405,7 @@ int disable_kprobe(struct kprobe *kp);
 int enable_kprobe(struct kprobe *kp);
 
 void dump_kprobe(struct kprobe *kp);
+bool is_valid_kprobe_symbol_name(const char *name);
 
 #else /* !CONFIG_KPROBES: */
 
kernel/kprobes.c +24 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	}
+
+	return true;
+}
+
 /*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
@@ -1397,6 +1419,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1
maintainer
#6 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Quoted 4 lines · On Wed, 19 Apr 2017 18:21:02 +0530 "Naveen N. Rao"…

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

Would we really need this? Of course it may filter out longer strings... anyway such name should be rejected by kallsyms.

[...]

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1385

Quoted 9 lines
kernel/kprobes.c +1 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
+bool is_valid_kprobe_symbol_name(const char *name)

This just check the length of symbol_name buffer, and can contain some invalid chars.

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1392

Quoted 7 lines
kernel/kprobes.c +7 -0
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

If you use strnlen() here, you just need to ensure sym_len < KSYM_NAME_LEN.

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1400

Quoted 8 lines
kernel/kprobes.c +8 -0
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)

Would you mean KSYM_NAME_LEN here?

Quoted 36 lines
kernel/kprobes.c +8 -0
+			return false;
+	}
+
+	return true;
+}
+
 /*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
@@ -1397,6 +1419,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1

Thanks,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#7 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:07:

Quoted 10 lines
Quoted 4 lines · On Wed, 19 Apr 2017 18:21:02 +0530 "Naveen N. Rao"…

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

Would we really need this? Of course it may filter out longer strings... anyway such name should be rejected by kallsyms.

I felt this would be good to have generically, as kallsyms does many string operations on the symbol name, including an unbounded strchr().

Quoted 10 lines

[...]

Quoted 8 lines
kernel/kprobes.c +0 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
Quoted 4 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1385

Quoted 1 line
kernel/kprobes.c +1 -0
+bool is_valid_kprobe_symbol_name(const char *name)

This just check the length of symbol_name buffer, and can contain some invalid chars.

Yes, I kept the function name generic incase we would like to do more validation in future, plus it's shorter than is_valid_kprobe_symbol_name_len() ;-)

Quoted 6 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1390

Quoted 6 lines
kernel/kprobes.c +5 -0
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');

Hmm.. this should be strnchr(). I re-factored the code that moved the strnlen() above this below. I'll fix this.

Quoted 4 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1392

Quoted 2 lines
kernel/kprobes.c +2 -0
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

If you use strnlen() here, you just need to ensure sym_len < KSYM_NAME_LEN.

Hmm.. not sure I follow. Are you saying the check for sym_len <= 0 is not needed?

Quoted 11 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1400

Quoted 9 lines
kernel/kprobes.c +8 -0
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)

Would you mean KSYM_NAME_LEN here?

Oops... nice catch, Thanks!

- Naveen

Quoted 44 lines
Quoted 37 lines
kernel/kprobes.c +8 -0
+			return false;
+	}
+
+	return true;
+}
+
 /*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
@@ -1397,6 +1419,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1

Thanks,

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#8 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Quoted 9 lines · On Wed, 19 Apr 2017 16:38:22 +0000 "Naveen N. Rao"…

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:07:

Quoted 8 lines
Quoted 4 lines · On Wed, 19 Apr 2017 18:21:02 +0530 "Naveen N. Rao"…

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

Quoted 5 lines
Quoted 2 lines

Would we really need this? Of course it may filter out longer strings... anyway such name should be rejected by kallsyms.

I felt this would be good to have generically, as kallsyms does many string operations on the symbol name, including an unbounded strchr().

OK, so this is actually for performance reason.

Quoted 11 lines
Quoted 11 lines

[...]

Quoted 8 lines
kernel/kprobes.c +0 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
Quoted 8 lines
Quoted 4 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1385

Quoted 1 line
kernel/kprobes.c +1 -0
+bool is_valid_kprobe_symbol_name(const char *name)

This just check the length of symbol_name buffer, and can contain some invalid chars.

Yes, I kept the function name generic incase we would like to do more validation in future, plus it's shorter than is_valid_kprobe_symbol_name_len() ;-)

OK, if this is enough general, we'd better define this in kernel/kallsyms.c or in kallsyms.h. Of course the function should be called is_valid_symbol_name(). :-)

Quoted 9 lines
Quoted 5 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1390

Quoted 5 lines
kernel/kprobes.c +5 -0
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');

Hmm.. this should be strnchr(). I re-factored the code that moved the strnlen() above this below. I'll fix this.

Quoted 7 lines
Quoted 4 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1392

Quoted 2 lines
kernel/kprobes.c +2 -0
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

If you use strnlen() here, you just need to ensure sym_len < KSYM_NAME_LEN.

Hmm.. not sure I follow. Are you saying the check for sym_len <= 0 is not needed?

You can check sym_len != 0, but anyway, here we concern about "longer" string (for performance reason), we can focus on such case.
(BTW, could you also check the name != NULL at first?)

So, what I think it can be;

if (strnlen(s+1, KSYM_NAME_LEN) == KSYM_NAME_LEN ||

    (size_t)(s - name) >= MODULE_NAME_LEN)
	return false;

Thanks,

Quoted 60 lines
Quoted 10 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1400

Quoted 8 lines
kernel/kprobes.c +8 -0
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)

Would you mean KSYM_NAME_LEN here?

Oops... nice catch, Thanks!

- Naveen

Quoted 44 lines
Quoted 37 lines
kernel/kprobes.c +8 -0
+			return false;
+	}
+
+	return true;
+}
+
 /*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
@@ -1397,6 +1419,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1

Thanks,

Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
#9 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Excerpts from Masami Hiramatsu's message of April 21, 2017 19:12:

Quoted 21 lines

On Wed, 19 Apr 2017 16:38:22 +0000
"Naveen N. Rao" <[email protected]> wrote:
=20

Quoted 8 lines

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:07:

Quoted 7 lines

On Wed, 19 Apr 2017 18:21:02 +0530
"Naveen N. Rao" <[email protected]> wrote:
=20

Quoted 4 lines

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

Quoted 7 lines
Quoted 3 lines

=20
Would we really need this? Of course it may filter out longer strings... anyway such name should be rejected by kallsyms.

=20
I felt this would be good to have generically, as kallsyms does many=20 string operations on the symbol name, including an unbounded=20 strchr().

=20
OK, so this is actually for performance reason.
=20

Quoted 8 lines
Quoted 8 lines

=20

Quoted 7 lines

=20
[...]

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long add=

Quoted 5 lines
kernel/kprobes.c +0 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long add=

r)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
kernel/kprobes.c +0 -0
 	return false;
 }

=20
+bool is_valid_kprobe_symbol_name(const char *name)

=20
This just check the length of symbol_name buffer, and can contain some invalid chars.

Quoted 8 lines
Quoted 4 lines

=20
Yes, I kept the function name generic incase we would like to do more=20 validation in future, plus it's shorter than=20 is_valid_kprobe_symbol_name_len() ;-)

=20
OK, if this is enough general, we'd better define this in kernel/kallsyms.c or in kallsyms.h. Of course the function should be called is_valid_symbol_name(). :-)

I actually think this should be done in kprobes itself. The primary=20 intent is to perform such validation right when we first obtain the=20 input from the user. In this case, however, kallsyms_lookup_name() is=20 also an exported symbol, so I do think some validation there would be=20 good to have as well.

Quoted 14 lines

=20

Quoted 13 lines
Quoted 5 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr)

Quoted 5 lines
kernel/kprobes.c +5 -0
+{
+	size_t sym_len;
+	char *s;
+
+	s =3D strchr(name, ':');

=20
Hmm.. this should be strnchr(). I re-factored the code that moved the=20 strnlen() above this below. I'll fix this.
=20

Quoted 4 lines

kernel/kprobes.c

Quoted 2 lines
kernel/kprobes.c +2 -0
+	if (s) {
+		sym_len =3D strnlen(s+1, KSYM_NAME_LEN);

=20
If you use strnlen() here, you just need to ensure sym_len < KSYM_NAME=

_LEN.

Quoted 12 lines
Quoted 3 lines

=20
Hmm.. not sure I follow. Are you saying the check for sym_len <=3D 0 is=20 not needed?

=20
You can check sym_len !=3D 0, but anyway, here we concern about "longer" string (for performance reason), we can focus on such case.
(BTW, could you also check the name !=3D NULL at first?) =20
So, what I think it can be;
=20
if (strnlen(s+1, KSYM_NAME_LEN) =3D=3D KSYM_NAME_LEN ||

Quoted 2 lines
    (size_t)(s - name) >=3D MODULE_NAME_LEN)
	return false;

Sure, thanks. I clearly need to refactor this code better!

- Naveen

=

maintainer
#10 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Quoted 13 lines · On Sun, 23 Apr 2017 15:44:32 +0000 "Naveen N. Rao"…
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

+bool is_valid_kprobe_symbol_name(const char *name)

This just check the length of symbol_name buffer, and can contain some invalid chars.

Quoted 7 lines
Quoted 3 lines

Yes, I kept the function name generic incase we would like to do more validation in future, plus it's shorter than is_valid_kprobe_symbol_name_len() ;-)

OK, if this is enough general, we'd better define this in kernel/kallsyms.c or in kallsyms.h. Of course the function should be called is_valid_symbol_name(). :-)

Quoted 5 lines

I actually think this should be done in kprobes itself. The primary intent is to perform such validation right when we first obtain the input from the user. In this case, however, kallsyms_lookup_name() is also an exported symbol, so I do think some validation there would be good to have as well.

IMHO, it is natural that kallsyms will know what is valid symbols. Providing validation function by kprobes means kprobes also knows that, and I concerns that may lead a double standard.

Thanks,

Quoted 33 lines
Quoted 22 lines
Quoted 9 lines
Quoted 5 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1390

Quoted 5 lines
kernel/kprobes.c +5 -0
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');

Hmm.. this should be strnchr(). I re-factored the code that moved the strnlen() above this below. I'll fix this.

Quoted 7 lines
Quoted 4 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1392

Quoted 2 lines
kernel/kprobes.c +2 -0
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

If you use strnlen() here, you just need to ensure sym_len < KSYM_NAME_LEN.

Hmm.. not sure I follow. Are you saying the check for sym_len <= 0 is not needed?

You can check sym_len != 0, but anyway, here we concern about "longer" string (for performance reason), we can focus on such case.
(BTW, could you also check the name != NULL at first?)

Quoted 5 lines

So, what I think it can be;

if (strnlen(s+1, KSYM_NAME_LEN) == KSYM_NAME_LEN ||

    (size_t)(s - name) >= MODULE_NAME_LEN)
	return false;

Sure, thanks. I clearly need to refactor this code better!

- Naveen

Signature · 1 line
Masami Hiramatsu <[email protected]>
#11 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

In reply to Naveen N. Rao
Quoted 16 lines · Naveen N. Rao
kernel/kprobes.c +8 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1402

Quoted 10 lines
kernel/kprobes.c +10 -0
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	}

I think this is probably more elaborate than it needs to be.

Why not just check the string is <= (MODULE_NAME_LEN + KSYM_NAME_LEN) ?

cheers

#12 lore raw

Re: [PATCH v3 3/7] kprobes: validate the symbol name length

Excerpts from Michael Ellerman's message of April 20, 2017 11:38:

Quoted 31 lines
Quoted 16 lines · Naveen N. Rao
kernel/kprobes.c +8 -0
index 6a128f3a7ed1..bb86681c8a10 100644
@@ -1382,6 +1382,28 @@ bool within_kprobe_blacklist(unsigned long addr)
 	return false;
 }
 
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	char *s;
+
+	s = strchr(name, ':');
+	if (s) {
+		sym_len = strnlen(s+1, KSYM_NAME_LEN);

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1402

Quoted 10 lines
kernel/kprobes.c +10 -0
+		if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+			return false;
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	} else {
+		sym_len = strnlen(name, MODULE_NAME_LEN);
+		if (sym_len <= 0 || sym_len >= MODULE_NAME_LEN)
+			return false;
+	}

I think this is probably more elaborate than it needs to be.

Quoted 1 line

Why not just check the string is <= (MODULE_NAME_LEN + KSYM_NAME_LEN) ?

Yes, that would be sufficient for now.

It's probably just me being paranoid, but I felt it's good to have stricter checks for user-provided strings, to guard against future changes to how we process this.

- Naveen

#13 lore raw

PATCH v4 3/7 kprobes: validate the symbol name provided during probe registration

In reply to Naveen N. Rao

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

---
Masami, Michael,
Here's a simplified version that should hopefully be fine. I have simplified the logic to keep the code compact and simple.

- Naveen

 include/linux/kprobes.h     |  1 +
 kernel/kprobes.c            | 30 ++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c |  4 ++++
 3 files changed, 35 insertions(+)
include/linux/kprobes.h +1 -0
index 1f82a3db00b1..4ee10fef5135 100644
@@ -405,6 +405,7 @@ int disable_kprobe(struct kprobe *kp);
 int enable_kprobe(struct kprobe *kp);
 
 void dump_kprobe(struct kprobe *kp);
+bool is_valid_kprobe_symbol_name(const char *name);
 
 #else /* !CONFIG_KPROBES: */
 
kernel/kprobes.c +30 -0
index 6a128f3a7ed1..ff9b1ac72a38 100644
@@ -1383,6 +1383,34 @@ bool within_kprobe_blacklist(unsigned long addr)
 }
 
 /*
+ * We mainly want to ensure that the provided string is of a reasonable length
+ * and is of the form [<mod_name>:]<sym_name>, so that this is safe to process
+ * further.
+ * We don't worry about invalid characters as those will just prevent
+ * matching existing kallsyms.
+ */
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	const char *s;
+
+	s = strnchr(name, ':', MODULE_NAME_LEN + KSYM_NAME_LEN + 1);
+	if (s) {
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0  || sym_len >= MODULE_NAME_LEN)
+			return false;
+		s++;
+	} else
+		s = name;
+
+	sym_len = strnlen(s, KSYM_NAME_LEN);
+	if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+		return false;
+
+	return true;
+}
+
+/*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
  * This returns encoded errors if it fails to look up symbol or invalid
@@ -1397,6 +1425,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1
#14 lore raw

Re: [PATCH v4 3/7] kprobes: validate the symbol name provided during probe registration

a nit or two, below...

Quoted 13 lines · Naveen N. Rao
kernel/kprobes.c +5 -0
index 6a128f3a7ed1..ff9b1ac72a38 100644
@@ -1383,6 +1383,34 @@ bool within_kprobe_blacklist(unsigned long addr)
 }
 
 /*
+ * We mainly want to ensure that the provided string is of a reasonable length
+ * and is of the form [<mod_name>:]<sym_name>, so that this is safe to process
+ * further.
+ * We don't worry about invalid characters as those will just prevent
+ * matching existing kallsyms.

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1400

Quoted 10 lines
kernel/kprobes.c +10 -0
+ */
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	const char *s;
+
+	s = strnchr(name, ':', MODULE_NAME_LEN + KSYM_NAME_LEN + 1);
+	if (s) {
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0  || sym_len >= MODULE_NAME_LEN)

"sym_len <= 0" looks odd here, since sym_len is likely unsigned and would never be less than zero, anyway.

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1407

Quoted 7 lines
kernel/kprobes.c +7 -0
+			return false;
+		s++;
+	} else
+		s = name;
+
+	sym_len = strnlen(s, KSYM_NAME_LEN);
+	if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)

here, too.

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1411

Quoted 4 lines
kernel/kprobes.c +4 -0
+		return false;
+
+	return true;
+}

PC

#15 lore raw

Re: [PATCH v4 3/7] kprobes: validate the symbol name provided during probe registration

Excerpts from Paul Clarke's message of April 21, 2017 18:41:

Quoted 20 lines

a nit or two, below...

Quoted 17 lines · Naveen N. Rao
kernel/kprobes.c +9 -0
index 6a128f3a7ed1..ff9b1ac72a38 100644
@@ -1383,6 +1383,34 @@ bool within_kprobe_blacklist(unsigned long addr)
 }
 
 /*
+ * We mainly want to ensure that the provided string is of a reasonable length
+ * and is of the form [<mod_name>:]<sym_name>, so that this is safe to process
+ * further.
+ * We don't worry about invalid characters as those will just prevent
+ * matching existing kallsyms.
+ */
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
Quoted 8 lines

kernel/kprobes.c bool within_kprobe_blacklist(unsigned long addr) · line 1400

Quoted 6 lines
kernel/kprobes.c +6 -0
+	const char *s;
+
+	s = strnchr(name, ':', MODULE_NAME_LEN + KSYM_NAME_LEN + 1);
+	if (s) {
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0  || sym_len >= MODULE_NAME_LEN)

"sym_len <= 0" looks odd here, since sym_len is likely unsigned and would never be less than zero, anyway.

Ugh.. habits :/
I'll wait for Masami/Michael's feedback before re-spinning.

Thanks for the review,
- Naveen

maintainer
#16 lore raw

Re: [PATCH v4 3/7] kprobes: validate the symbol name provided during probe registration

In reply to Naveen N. Rao
Quoted 10 lines · On Fri, 21 Apr 2017 18:02:33 +0530 "Naveen N. Rao"…

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

---
Masami, Michael,
Here's a simplified version that should hopefully be fine. I have simplified the logic to keep the code compact and simple.

Ok, so as I replied to older version,
- Ensure name != NULL at first.
- Define it in kernel/kallsyms.c as a general routine.

Since this validate the string which will be passed to kallsyms, I think it should be provided by kallsyms.

Thanks,

Quoted 87 lines

- Naveen

 include/linux/kprobes.h     |  1 +
 kernel/kprobes.c            | 30 ++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c |  4 ++++
 3 files changed, 35 insertions(+)
include/linux/kprobes.h +1 -0
index 1f82a3db00b1..4ee10fef5135 100644
@@ -405,6 +405,7 @@ int disable_kprobe(struct kprobe *kp);
 int enable_kprobe(struct kprobe *kp);
 
 void dump_kprobe(struct kprobe *kp);
+bool is_valid_kprobe_symbol_name(const char *name);
 
 #else /* !CONFIG_KPROBES: */
 
kernel/kprobes.c +30 -0
index 6a128f3a7ed1..ff9b1ac72a38 100644
@@ -1383,6 +1383,34 @@ bool within_kprobe_blacklist(unsigned long addr)
 }
 
 /*
+ * We mainly want to ensure that the provided string is of a reasonable length
+ * and is of the form [<mod_name>:]<sym_name>, so that this is safe to process
+ * further.
+ * We don't worry about invalid characters as those will just prevent
+ * matching existing kallsyms.
+ */
+bool is_valid_kprobe_symbol_name(const char *name)
+{
+	size_t sym_len;
+	const char *s;
+
+	s = strnchr(name, ':', MODULE_NAME_LEN + KSYM_NAME_LEN + 1);
+	if (s) {
+		sym_len = (size_t)(s - name);
+		if (sym_len <= 0  || sym_len >= MODULE_NAME_LEN)
+			return false;
+		s++;
+	} else
+		s = name;
+
+	sym_len = strnlen(s, KSYM_NAME_LEN);
+	if (sym_len <= 0 || sym_len >= KSYM_NAME_LEN)
+		return false;
+
+	return true;
+}
+
+/*
  * If we have a symbol_name argument, look it up and add the offset field
  * to it. This way, we can specify a relative address to a symbol.
  * This returns encoded errors if it fails to look up symbol or invalid
@@ -1397,6 +1425,8 @@ static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
 		goto invalid;
 
 	if (p->symbol_name) {
+		if (!is_valid_kprobe_symbol_name(p->symbol_name))
+			return ERR_PTR(-EINVAL);
 		addr = kprobe_lookup_name(p->symbol_name, p->offset);
 		if (!addr)
 			return ERR_PTR(-ENOENT);
kernel/trace/trace_kprobe.c +4 -0
index 5f688cc724f0..bf73e5f31128 100644
@@ -704,6 +704,10 @@ static int create_trace_kprobe(int argc, char **argv)
 			pr_info("Return probe must be used without offset.\n");
 			return -EINVAL;
 		}
+		if (!is_valid_kprobe_symbol_name(symbol)) {
+			pr_info("Symbol name is too long.\n");
+			return -EINVAL;
+		}
 	}
 	argc -= 2; argv += 2;
 
Signature · 1 line
2.12.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#17 lore raw

Re: [PATCH v4 3/7] kprobes: validate the symbol name provided during probe registration

In reply to Naveen N. Rao
Quoted 4 lines · Naveen N. Rao

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

What are we actually trying to protect against here?

If you ignore powerpc for a moment, kprobe_lookup_name() is just kallsyms_lookup_name().

All kallsyms_lookup_name() does with name is strcmp() it against a legitimate symbol name which is at most KSYM_NAME_LEN.

So I don't think any of this validation helps in that case?

In the powerpc version of kprobe_lookup_name() we do need to do some string juggling, for which it helps to know the input is sane. But I think we should just make that code more robust by checking the input before we do anything with it.

cheers

#18 lore raw

Re: [PATCH v4 3/7] kprobes: validate the symbol name provided during probe registration

Excerpts from Michael Ellerman's message of April 22, 2017 11:25:

Quoted 6 lines

"Naveen N. Rao" <[email protected]> writes:
=20

Quoted 4 lines

When a kprobe is being registered, we use the symbol_name field to lookup the address where the probe should be placed. Since this is a user-provided field, let's ensure that the length of the string is within expected limits.

Quoted 10 lines

=20
What are we actually trying to protect against here? =20
If you ignore powerpc for a moment, kprobe_lookup_name() is just kallsyms_lookup_name().
=20
All kallsyms_lookup_name() does with name is strcmp() it against a legitimate symbol name which is at most KSYM_NAME_LEN. =20
So I don't think any of this validation helps in that case?

It does:
https://patchwork.kernel.org/patch/9695139/

It is far too easy to cause a OOPS due to the above, though this is=20 root-only (for modules).

As I stated earlier, I think it is always a good practice to validate=20 inputs right on entry, rather than later. Code gets refactored,=20 different arch support gets added, and so on.

Doing this validation here ensures we don't have to worry about how we=20 process this later, or if another arch has to over-ride=20 kprobe_lookup_name().

Quoted 5 lines

=20
In the powerpc version of kprobe_lookup_name() we do need to do some string juggling, for which it helps to know the input is sane. But I think we should just make that code more robust by checking the input before we do anything with it.

Ok, I will fold those tests in with the powerpc implementation for now=20 and consider a patch against kallsyms_lookup_name(), like Masami=20 recommends.

- Naveen

=

#19 lore raw

PATCH v4 4/7 powerpc/kprobes: Use safer string functions in kprobe_lookup_name()

In reply to Naveen N. Rao

Convert usage of strchr()/strncpy()/strncat() to strnchr()/memcpy()/strlcat() for simpler and safer string manipulation.

---
Changes: Additionally convert the strchr().

 arch/powerpc/kernel/kprobes.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/powerpc/kernel/kprobes.c +6 -7
index 97b5eed1f76d..c73fb6e3b43f 100644
@@ -65,28 +65,27 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
 	const char *modsym;
 	bool dot_appended = false;
-	if ((modsym = strchr(name, ':')) != NULL) {
+	if ((modsym = strnchr(name, ':', MODULE_NAME_LEN)) != NULL) {
 		modsym++;
 		if (*modsym != '\0' && *modsym != '.') {
 			/* Convert to <module:.symbol> */
-			strncpy(dot_name, name, modsym - name);
+			memcpy(dot_name, name, modsym - name);
 			dot_name[modsym - name] = '.';
 			dot_name[modsym - name + 1] = '\0';
-			strncat(dot_name, modsym,
-				sizeof(dot_name) - (modsym - name) - 2);
+			strlcat(dot_name, modsym, sizeof(dot_name));
 			dot_appended = true;
 		} else {
 			dot_name[0] = '\0';
-			strncat(dot_name, name, sizeof(dot_name) - 1);
+			strlcat(dot_name, name, sizeof(dot_name));
 		}
 	} else if (name[0] != '.') {
 		dot_name[0] = '.';
 		dot_name[1] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);
+		strlcat(dot_name, name, sizeof(dot_name));
 		dot_appended = true;
 	} else {
 		dot_name[0] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 1);
+		strlcat(dot_name, name, sizeof(dot_name));
 	}
 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
 	if (!addr && dot_appended) {
Signature · 1 line
2.12.1
#20 lore raw

Re: [PATCH v4 4/7] powerpc/kprobes: Use safer string functions in kprobe_lookup_name()

Quoted 22 lines · Naveen N. Rao

Convert usage of strchr()/strncpy()/strncat() to strnchr()/memcpy()/strlcat() for simpler and safer string manipulation.

---
Changes: Additionally convert the strchr().

 arch/powerpc/kernel/kprobes.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/powerpc/kernel/kprobes.c +1 -1
index 97b5eed1f76d..c73fb6e3b43f 100644
@@ -65,28 +65,27 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
 	const char *modsym;
 	bool dot_appended = false;
-	if ((modsym = strchr(name, ':')) != NULL) {
+	if ((modsym = strnchr(name, ':', MODULE_NAME_LEN)) != NULL) {

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 75

Quoted 10 lines
arch/powerpc/kernel/kprobes.c +2 -3
 		modsym++;
 		if (*modsym != '\0' && *modsym != '.') {
 			/* Convert to <module:.symbol> */
-			strncpy(dot_name, name, modsym - name);
+			memcpy(dot_name, name, modsym - name);
 			dot_name[modsym - name] = '.';
 			dot_name[modsym - name + 1] = '\0';
-			strncat(dot_name, modsym,
-				sizeof(dot_name) - (modsym - name) - 2);
+			strlcat(dot_name, modsym, sizeof(dot_name));

Would it be more efficient here to replace this:
--
dot_name[modsym - name + 1] = '\0';
strlcat(dot_name, modsym, sizeof(dot_name));
--

with this:
strncpy(&dot_name[modsym - name + 1], modsym, KSYM_NAME_LEN);

(So you aren't rescanning dot_name to find the end, when you already know the end position?)

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 79

Quoted 5 lines
arch/powerpc/kernel/kprobes.c +1 -1
 			dot_appended = true;
 		} else {
 			dot_name[0] = '\0';
-			strncat(dot_name, name, sizeof(dot_name) - 1);
+			strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(dot_name, name, sizeof(dot_name));

(and remove the null termination immediately above)

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 84

Quoted 6 lines
arch/powerpc/kernel/kprobes.c +1 -1
 		}
 	} else if (name[0] != '.') {
 		dot_name[0] = '.';
 		dot_name[1] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);
+		strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(&dot_name[1], name, sizeof(dot_name));

(and remove the null termination immediately above)

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 88

Quoted 5 lines
arch/powerpc/kernel/kprobes.c +1 -1
 		dot_appended = true;
 	} else {
 		dot_name[0] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 1);
+		strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(dot_name, name, sizeof(dot_name));

(and remove the null termination immediately above)

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 89

Quoted 1 line
arch/powerpc/kernel/kprobes.c +0 -0
 	}

PC

#21 lore raw

Re: [PATCH v4 4/7] powerpc/kprobes: Use safer string functions in kprobe_lookup_name()

Quoted 9 lines · Paul Clarke

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 84

Quoted 5 lines · Naveen N. Rao
arch/powerpc/kernel/kprobes.c +1 -1
 	} else if (name[0] != '.') {
 		dot_name[0] = '.';
 		dot_name[1] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);
+		strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(&dot_name[1], name, sizeof(dot_name));

oops. s/sizeof(dot_name)/sizeof(dot_name) - 1/

PC

#22 lore raw

Re: [PATCH v4 4/7] powerpc/kprobes: Use safer string functions in kprobe_lookup_name()

In reply to Paul Clarke

Sent too soon. The suggestions don't guarantee null termination. Refined, below. (Sorry for the noise.)

Quoted 31 lines · Paul Clarke
Quoted 13 lines · Naveen N. Rao

Convert usage of strchr()/strncpy()/strncat() to strnchr()/memcpy()/strlcat() for simpler and safer string manipulation.

arch/powerpc/kernel/kprobes.c +1 -1
index 97b5eed1f76d..c73fb6e3b43f 100644
@@ -65,28 +65,27 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
 	const char *modsym;
 	bool dot_appended = false;
-	if ((modsym = strchr(name, ':')) != NULL) {
+	if ((modsym = strnchr(name, ':', MODULE_NAME_LEN)) != NULL) {

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 75

Quoted 10 lines
arch/powerpc/kernel/kprobes.c +2 -3
 		modsym++;
 		if (*modsym != '\0' && *modsym != '.') {
 			/* Convert to <module:.symbol> */
-			strncpy(dot_name, name, modsym - name);
+			memcpy(dot_name, name, modsym - name);
 			dot_name[modsym - name] = '.';
 			dot_name[modsym - name + 1] = '\0';
-			strncat(dot_name, modsym,
-				sizeof(dot_name) - (modsym - name) - 2);
+			strlcat(dot_name, modsym, sizeof(dot_name));

Would it be more efficient here to replace this:
--
dot_name[modsym - name + 1] = '\0';
strlcat(dot_name, modsym, sizeof(dot_name));
--

Quoted 2 lines

with this:
strncpy(&dot_name[modsym - name + 1], modsym, KSYM_NAME_LEN);

keep the null termination, and just adjust the starting point for strlcat:
--
dot_name[modsym - name + 1] = '\0';
strlcat(&dot_name[modsym - name + 1], modsym, KSYM_NAME_LEN); --

Quoted 11 lines

(So you aren't rescanning dot_name to find the end, when you already know the end position?)

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 79

Quoted 5 lines
arch/powerpc/kernel/kprobes.c +1 -1
 			dot_appended = true;
 		} else {
 			dot_name[0] = '\0';
-			strncat(dot_name, name, sizeof(dot_name) - 1);
+			strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(dot_name, name, sizeof(dot_name));

Quoted 1 line

(and remove the null termination immediately above)

nevermind. :-)

Quoted 11 lines

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 84

Quoted 7 lines
arch/powerpc/kernel/kprobes.c +1 -1
 		}
 	} else if (name[0] != '.') {
 		dot_name[0] = '.';
 		dot_name[1] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);
+		strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(&dot_name[1], name, sizeof(dot_name));

Quoted 2 lines

(and remove the null termination immediately above)

nevermind. :-)

Quoted 9 lines

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 88

Quoted 5 lines
arch/powerpc/kernel/kprobes.c +1 -1
 		dot_appended = true;
 	} else {
 		dot_name[0] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 1);
+		strlcat(dot_name, name, sizeof(dot_name));

and here do:
strncpy(dot_name, name, sizeof(dot_name));

Quoted 3 lines

(and remove the null termination immediately above)

Quoted 1 line
arch/powerpc/kernel/kprobes.c +0 -0
 	}

nevermind. :-)

PC

#23 lore raw

Re: [PATCH v4 4/7] powerpc/kprobes: Use safer string functions in kprobe_lookup_name()

Excerpts from Paul Clarke's message of April 21, 2017 19:22:

Quoted 1 line

Sent too soon. The suggestions don't guarantee null termination. Refine=

d, below. (Sorry for the noise.)

Yeah, the string operations here are a bit of a minefield...

Quoted 7 lines

=20

Quoted 3 lines · Paul Clarke
Quoted 2 lines · Naveen N. Rao

Convert usage of strchr()/strncpy()/strncat() to strnchr()/memcpy()/strlcat() for simpler and safer string manipulation.

=20

Quoted 1 line

arch/powerpc/kernel/kprobe=

Quoted 1 line
arch/powerpc/kernel/kprobe= renamed from arch/powerpc/kernel/kprobes.c +0 -0

s.c

Quoted 4 lines
Quoted 4 lines

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *nam=

Quoted 4 lines

index 97b5eed1f76d..c73fb6e3b43f 100644

arch/powerpc/kernel/kprobes.c +0 -0
@@ -65,28 +65,27 @@ kprobe_opcode_t *kprobe_lookup_name(const char *nam=

e, unsigned int offset)

Quoted 5 lines
Quoted 5 lines

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *nam= · line 68

Quoted 5 lines
arch/powerpc/kernel/kprobes.c +1 -1
 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
 	const char *modsym;
 	bool dot_appended =3D false;
-	if ((modsym =3D strchr(name, ':')) !=3D NULL) {
+	if ((modsym =3D strnchr(name, ':', MODULE_NAME_LEN)) !=3D NULL) {

... as I just realized this is an epic FAIL! ;/

I will take my time to redo this.

- Naveen

=

#24 lore raw

PATCH v3 4/7 powerpc: kprobes: use safer string functions in kprobe_lookup_name()

In reply to Naveen N. Rao

Convert usage of strncpy()/strncat() to memcpy()/strlcat() for simpler and safer string manipulation.

---
 arch/powerpc/kernel/kprobes.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/powerpc/kernel/kprobes.c +5 -6
index 97b5eed1f76d..d743bacefa8c 100644
@@ -69,24 +69,23 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 		modsym++;
 		if (*modsym != '\0' && *modsym != '.') {
 			/* Convert to <module:.symbol> */
-			strncpy(dot_name, name, modsym - name);
+			memcpy(dot_name, name, modsym - name);
 			dot_name[modsym - name] = '.';
 			dot_name[modsym - name + 1] = '\0';
-			strncat(dot_name, modsym,
-				sizeof(dot_name) - (modsym - name) - 2);
+			strlcat(dot_name, modsym, sizeof(dot_name));
 			dot_appended = true;
 		} else {
 			dot_name[0] = '\0';
-			strncat(dot_name, name, sizeof(dot_name) - 1);
+			strlcat(dot_name, name, sizeof(dot_name));
 		}
 	} else if (name[0] != '.') {
 		dot_name[0] = '.';
 		dot_name[1] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 2);
+		strlcat(dot_name, name, sizeof(dot_name));
 		dot_appended = true;
 	} else {
 		dot_name[0] = '\0';
-		strncat(dot_name, name, KSYM_NAME_LEN - 1);
+		strlcat(dot_name, name, sizeof(dot_name));
 	}
 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
 	if (!addr && dot_appended) {
Signature · 1 line
2.12.1
#25 lore raw

RE: [PATCH v3 4/7] powerpc: kprobes: use safer string functions in kprobe_lookup_name()

From: Naveen N. Rao

Quoted 1 line

Sent: 19 April 2017 13:51

...

arch/powerpc/kernel/kprobes.c kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset) · line 79

Quoted 3 lines
arch/powerpc/kernel/kprobes.c +1 -1
 			dot_name[0] =3D '\0';
-			strncat(dot_name, name, sizeof(dot_name) - 1);
+			strlcat(dot_name, name, sizeof(dot_name));

...

Is that really zeroing the first byte just so it can append to it?

David

#26 lore raw

PATCH v3 5/7 powerpc: kprobes: factor out code to emulate instruction into a helper

In reply to Naveen N. Rao

No functional changes.

---
 arch/powerpc/kernel/kprobes.c | 52 ++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 21 deletions(-)
arch/powerpc/kernel/kprobes.c +31 -21
index d743bacefa8c..46e8c1e03ce4 100644
@@ -206,6 +206,35 @@ void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
 	regs->link = (unsigned long)kretprobe_trampoline;
 }
 
+int __kprobes try_to_emulate(struct kprobe *p, struct pt_regs *regs)
+{
+	int ret;
+	unsigned int insn = *p->ainsn.insn;
+
+	/* regs->nip is also adjusted if emulate_step returns 1 */
+	ret = emulate_step(regs, insn);
+	if (ret > 0) {
+		/*
+		 * Once this instruction has been boosted
+		 * successfully, set the boostable flag
+		 */
+		if (unlikely(p->ainsn.boostable == 0))
+			p->ainsn.boostable = 1;
+	} else if (ret < 0) {
+		/*
+		 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
+		 * So, we should never get here... but, its still
+		 * good to catch them, just in case...
+		 */
+		printk("Can't step on instruction %x\n", insn);
+		BUG();
+	} else if (ret == 0)
+		/* This instruction can't be boosted */
+		p->ainsn.boostable = -1;
+
+	return ret;
+}
+
 int __kprobes kprobe_handler(struct pt_regs *regs)
 {
 	struct kprobe *p;
@@ -301,18 +330,9 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 
 ss_probe:
 	if (p->ainsn.boostable >= 0) {
-		unsigned int insn = *p->ainsn.insn;
+		ret = try_to_emulate(p, regs);
 
-		/* regs->nip is also adjusted if emulate_step returns 1 */
-		ret = emulate_step(regs, insn);
 		if (ret > 0) {
-			/*
-			 * Once this instruction has been boosted
-			 * successfully, set the boostable flag
-			 */
-			if (unlikely(p->ainsn.boostable == 0))
-				p->ainsn.boostable = 1;
-
 			if (p->post_handler)
 				p->post_handler(p, regs, 0);
 
@@ -320,17 +340,7 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			reset_current_kprobe();
 			preempt_enable_no_resched();
 			return 1;
-		} else if (ret < 0) {
-			/*
-			 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
-			 * So, we should never get here... but, its still
-			 * good to catch them, just in case...
-			 */
-			printk("Can't step on instruction %x\n", insn);
-			BUG();
-		} else if (ret == 0)
-			/* This instruction can't be boosted */
-			p->ainsn.boostable = -1;
+		}
 	}
 	prepare_singlestep(p, regs);
 	kcb->kprobe_status = KPROBE_HIT_SS;
Signature · 1 line
2.12.1
maintainer
#27 lore raw

Re: [PATCH v3 5/7] powerpc: kprobes: factor out code to emulate instruction into a helper

On Wed, 19 Apr 2017 18:21:04 +0530
"Naveen N. Rao" <[email protected]> wrote:

Factor out code to emulate instruction into a try_to_emulate() helper function. This makes ...

Quoted 1 line

No functional changes.

Thanks,

Quoted 89 lines
---
 arch/powerpc/kernel/kprobes.c | 52 ++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 21 deletions(-)
arch/powerpc/kernel/kprobes.c +31 -21
index d743bacefa8c..46e8c1e03ce4 100644
@@ -206,6 +206,35 @@ void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
 	regs->link = (unsigned long)kretprobe_trampoline;
 }
 
+int __kprobes try_to_emulate(struct kprobe *p, struct pt_regs *regs)
+{
+	int ret;
+	unsigned int insn = *p->ainsn.insn;
+
+	/* regs->nip is also adjusted if emulate_step returns 1 */
+	ret = emulate_step(regs, insn);
+	if (ret > 0) {
+		/*
+		 * Once this instruction has been boosted
+		 * successfully, set the boostable flag
+		 */
+		if (unlikely(p->ainsn.boostable == 0))
+			p->ainsn.boostable = 1;
+	} else if (ret < 0) {
+		/*
+		 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
+		 * So, we should never get here... but, its still
+		 * good to catch them, just in case...
+		 */
+		printk("Can't step on instruction %x\n", insn);
+		BUG();
+	} else if (ret == 0)
+		/* This instruction can't be boosted */
+		p->ainsn.boostable = -1;
+
+	return ret;
+}
+
 int __kprobes kprobe_handler(struct pt_regs *regs)
 {
 	struct kprobe *p;
@@ -301,18 +330,9 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 
 ss_probe:
 	if (p->ainsn.boostable >= 0) {
-		unsigned int insn = *p->ainsn.insn;
+		ret = try_to_emulate(p, regs);
 
-		/* regs->nip is also adjusted if emulate_step returns 1 */
-		ret = emulate_step(regs, insn);
 		if (ret > 0) {
-			/*
-			 * Once this instruction has been boosted
-			 * successfully, set the boostable flag
-			 */
-			if (unlikely(p->ainsn.boostable == 0))
-				p->ainsn.boostable = 1;
-
 			if (p->post_handler)
 				p->post_handler(p, regs, 0);
 
@@ -320,17 +340,7 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			reset_current_kprobe();
 			preempt_enable_no_resched();
 			return 1;
-		} else if (ret < 0) {
-			/*
-			 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
-			 * So, we should never get here... but, its still
-			 * good to catch them, just in case...
-			 */
-			printk("Can't step on instruction %x\n", insn);
-			BUG();
-		} else if (ret == 0)
-			/* This instruction can't be boosted */
-			p->ainsn.boostable = -1;
+		}
 	}
 	prepare_singlestep(p, regs);
 	kcb->kprobe_status = KPROBE_HIT_SS;
Signature · 1 line
2.12.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#29 lore raw

PATCH v3 6/7 powerpc: kprobes: emulate instructions on kprobe handler re-entry

In reply to Naveen N. Rao

On kprobe handler re-entry, try to emulate the instruction rather than single stepping always.

---
 arch/powerpc/kernel/kprobes.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/kernel/kprobes.c +8 -0
index 46e8c1e03ce4..067e9863bfdf 100644
@@ -276,6 +276,14 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			kprobes_inc_nmissed_count(p);
 			prepare_singlestep(p, regs);
 			kcb->kprobe_status = KPROBE_REENTER;
+			if (p->ainsn.boostable >= 0) {
+				ret = try_to_emulate(p, regs);
+
+				if (ret > 0) {
+					restore_previous_kprobe(kcb);
+					return 1;
+				}
+			}
 			return 1;
 		} else {
 			if (*addr != BREAKPOINT_INSTRUCTION) {
Signature · 1 line
2.12.1
maintainer
#30 lore raw

Re: [PATCH v3 6/7] powerpc: kprobes: emulate instructions on kprobe handler re-entry

BTW, as I pointed, 5/7 and 6/7 should be merged since this actually makes meaningful change.

Thank you,

Quoted 31 lines · On Wed, 19 Apr 2017 18:21:05 +0530 "Naveen N. Rao"…

On kprobe handler re-entry, try to emulate the instruction rather than single stepping always.

---
 arch/powerpc/kernel/kprobes.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/kernel/kprobes.c +8 -0
index 46e8c1e03ce4..067e9863bfdf 100644
@@ -276,6 +276,14 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			kprobes_inc_nmissed_count(p);
 			prepare_singlestep(p, regs);
 			kcb->kprobe_status = KPROBE_REENTER;
+			if (p->ainsn.boostable >= 0) {
+				ret = try_to_emulate(p, regs);
+
+				if (ret > 0) {
+					restore_previous_kprobe(kcb);
+					return 1;
+				}
+			}
 			return 1;
 		} else {
 			if (*addr != BREAKPOINT_INSTRUCTION) {
Signature · 1 line
2.12.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#31 lore raw

Re: [PATCH v3 6/7] powerpc: kprobes: emulate instructions on kprobe handler re-entry

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:13:

Quoted 3 lines

BTW, as I pointed, 5/7 and 6/7 should be merged since this actually makes meaningful change.

Yes, sorry if I wasn't clear in my previous reply in the (!) previous patch series.

Since this has to go through the powerpc tree, I followed this since I felt that Michael Ellerman prefers to keep functional changes separate from refactoring. I'm fine with either approach.

Michael?

Thanks!
- Naveen

Quoted 43 lines

Thank you,

Quoted 31 lines · On Wed, 19 Apr 2017 18:21:05 +0530 "Naveen N. Rao"…

On kprobe handler re-entry, try to emulate the instruction rather than single stepping always.

---
 arch/powerpc/kernel/kprobes.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/kernel/kprobes.c +8 -0
index 46e8c1e03ce4..067e9863bfdf 100644
@@ -276,6 +276,14 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			kprobes_inc_nmissed_count(p);
 			prepare_singlestep(p, regs);
 			kcb->kprobe_status = KPROBE_REENTER;
+			if (p->ainsn.boostable >= 0) {
+				ret = try_to_emulate(p, regs);
+
+				if (ret > 0) {
+					restore_previous_kprobe(kcb);
+					return 1;
+				}
+			}
 			return 1;
 		} else {
 			if (*addr != BREAKPOINT_INSTRUCTION) {
Signature · 1 line
2.12.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
#32 lore raw

Re: [PATCH v3 6/7] powerpc: kprobes: emulate instructions on kprobe handler re-entry

Quoted 5 lines · Naveen N. Rao

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:13:

Quoted 3 lines

BTW, as I pointed, 5/7 and 6/7 should be merged since this actually makes meaningful change.

Quoted 8 lines

Yes, sorry if I wasn't clear in my previous reply in the (!) previous patch series.

Since this has to go through the powerpc tree, I followed this since I felt that Michael Ellerman prefers to keep functional changes separate from refactoring. I'm fine with either approach.

Michael?

Yeah I'd definitely like this to be two patches. Otherwise when reading the combined diff it's much harder to see the change.

cheers

maintainer Reviewed-by
#33 lore raw

Re: [PATCH v3 6/7] powerpc: kprobes: emulate instructions on kprobe handler re-entry

Quoted 10 lines · On Thu, 20 Apr 2017 16:11:10 +1000 Michael Ellerman <[email protected]> wrote:
Quoted 8 lines · Naveen N. Rao

Excerpts from Masami Hiramatsu's message of April 19, 2017 20:13:

Quoted 3 lines

BTW, as I pointed, 5/7 and 6/7 should be merged since this actually makes meaningful change.

Yes, sorry if I wasn't clear in my previous reply in the (!) previous patch series.

Quoted 8 lines
Quoted 5 lines

Since this has to go through the powerpc tree, I followed this since I felt that Michael Ellerman prefers to keep functional changes separate from refactoring. I'm fine with either approach.

Michael?

Yeah I'd definitely like this to be two patches. Otherwise when reading the combined diff it's much harder to see the change.

OK, let it be so.

Thanks,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#35 lore raw

PATCH v3 7/7 powerpc: kprobes: remove duplicate saving of msr

In reply to Naveen N. Rao

set_current_kprobe() already saves regs->msr into kprobe_saved_msr. Remove the redundant save.

---
 arch/powerpc/kernel/kprobes.c | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/kprobes.c +0 -1
index 067e9863bfdf..5c0a1ffcbcf9 100644
@@ -272,7 +272,6 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			 */
 			save_previous_kprobe(kcb);
 			set_current_kprobe(p, regs, kcb);
-			kcb->kprobe_saved_msr = regs->msr;
 			kprobes_inc_nmissed_count(p);
 			prepare_singlestep(p, regs);
 			kcb->kprobe_status = KPROBE_REENTER;
Signature · 1 line
2.12.1
maintainer Reviewed-by on v3 7/7
#36 lore raw

Re: [PATCH v3 7/7] powerpc: kprobes: remove duplicate saving of msr

Quoted 3 lines · On Wed, 19 Apr 2017 18:21:06 +0530 "Naveen N. Rao"…

set_current_kprobe() already saves regs->msr into kprobe_saved_msr. Remove the redundant save.

Looks good to me.

Thank you,

Quoted 20 lines
---
 arch/powerpc/kernel/kprobes.c | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/kprobes.c +0 -1
index 067e9863bfdf..5c0a1ffcbcf9 100644
@@ -272,7 +272,6 @@ int __kprobes kprobe_handler(struct pt_regs *regs)
 			 */
 			save_previous_kprobe(kcb);
 			set_current_kprobe(p, regs, kcb);
-			kcb->kprobe_saved_msr = regs->msr;
 			kprobes_inc_nmissed_count(p);
 			prepare_singlestep(p, regs);
 			kcb->kprobe_status = KPROBE_REENTER;
Signature · 1 line
2.12.1
Signature · 1 line
Masami Hiramatsu <[email protected]>