Re: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a function

2017-04-12 Thread Masami Hiramatsu
On Wed, 12 Apr 2017 16:28:24 +0530
"Naveen N. Rao"  wrote:

> 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.

Looks good to me.

Acked-by: Masami Hiramatsu 

Thanks!

> 
> Suggested-by: Masami Hiramatsu 
> Signed-off-by: Naveen N. Rao 
> ---
>  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(-)
> 
> diff --git a/arch/powerpc/include/asm/kprobes.h 
> b/arch/powerpc/include/asm/kprobes.h
> index 0503c98b2117..a843884aafaf 100644
> --- a/arch/powerpc/include/asm/kprobes.h
> +++ b/arch/powerpc/include/asm/kprobes.h
> @@ -61,59 +61,6 @@ extern kprobe_opcode_t optprobe_template_end[];
>  #define MAX_OPTINSN_SIZE (optprobe_template_end - 
> optprobe_template_entry)
>  #define RELATIVEJUMP_SIZEsizeof(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  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  */   \
> - 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
>  
> diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c
> index 331751701fed..a7aa7394954d 100644
> --- a/arch/powerpc/kernel/kprobes.c
> +++ b/arch/powerpc/kernel/kprobes.c
> @@ -42,6 +42,64 @@ 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 *addr;
> +
> +#ifdef PPC64_ELF_ABI

RE: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a function

2017-04-18 Thread David Laight
From: Naveen N. Rao
> Sent: 12 April 2017 11:58
...
> +kprobe_opcode_t *kprobe_lookup_name(const char *name)
> +{
...
> + 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  */
> + 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;

If the ':' is 'a way down' name[] then although the strncpy() won't
overrun dot_name[] the rest of the code can.

The strncat() call is particularly borked.

David



RE: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a function

2017-04-19 Thread Naveen N. Rao

Excerpts from David Laight's message of April 18, 2017 18:22:

From: Naveen N. Rao

Sent: 12 April 2017 11:58

...

+kprobe_opcode_t *kprobe_lookup_name(const char *name)
+{

...

+   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  */
+   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;


If the ':' is 'a way down' name[] then although the strncpy() won't
overrun dot_name[] the rest of the code can.


Nice catch, thanks David!
We need to be validating the length of 'name'. I'll put out a patch for 
that.


As an aside, I'm not sure I follow what you mean when you say that the 
strncpy() won't overrun dot_name[]. If we have a name[] longer than 
sizeof(dot_name) with the ':' after that, the strncpy() can also overrun 
dot_name[].



- Naveen



The strncat() call is particularly borked.

David






RE: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a function

2017-04-19 Thread David Laight
From: Naveen N. Rao
> Sent: 19 April 2017 09:09
> To: David Laight; Michael Ellerman
> Cc: linux-ker...@vger.kernel.org; linuxppc-dev@lists.ozlabs.org; Masami 
> Hiramatsu; Ingo Molnar
> Subject: RE: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a 
> function
> 
> Excerpts from David Laight's message of April 18, 2017 18:22:
> > From: Naveen N. Rao
> >> Sent: 12 April 2017 11:58
> > ...
> >> +kprobe_opcode_t *kprobe_lookup_name(const char *name)
> >> +{
> > ...
> >> +  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  */
> >> +  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;
> >
> > If the ':' is 'a way down' name[] then although the strncpy() won't
> > overrun dot_name[] the rest of the code can.
> 
> Nice catch, thanks David!
> We need to be validating the length of 'name'. I'll put out a patch for
> that.

Silent truncation is almost certainly wrong here.

> As an aside, I'm not sure I follow what you mean when you say that the
> strncpy() won't overrun dot_name[]. If we have a name[] longer than
> sizeof(dot_name) with the ':' after that, the strncpy() can also overrun
> dot_name[].

Yes, that should just be a memcpy(), as should the strncat().

Using strncpy() where the length is other than the size of the target buffer
should be banned. Not that it ever does what people expect.
strncat() is even worse.

David






Re: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a function

2017-04-19 Thread 'Naveen N. Rao'
On 2017/04/19 08:48AM, David Laight wrote:
> From: Naveen N. Rao
> > Sent: 19 April 2017 09:09
> > To: David Laight; Michael Ellerman
> > Cc: linux-ker...@vger.kernel.org; linuxppc-dev@lists.ozlabs.org; Masami 
> > Hiramatsu; Ingo Molnar
> > Subject: RE: [PATCH v2 1/5] kprobes: convert kprobe_lookup_name() to a 
> > function
> > 
> > Excerpts from David Laight's message of April 18, 2017 18:22:
> > > From: Naveen N. Rao
> > >> Sent: 12 April 2017 11:58
> > > ...
> > >> +kprobe_opcode_t *kprobe_lookup_name(const char *name)
> > >> +{
> > > ...
> > >> +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  */
> > >> +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;
> > >
> > > If the ':' is 'a way down' name[] then although the strncpy() won't
> > > overrun dot_name[] the rest of the code can.
> > 
> > Nice catch, thanks David!
> > We need to be validating the length of 'name'. I'll put out a patch for
> > that.
> 
> Silent truncation is almost certainly wrong here.

Indeed. This will be handled by the earlier validation to ensure that 
the module name as well as the symbol name are within the expected 
lengths.

> 
> > As an aside, I'm not sure I follow what you mean when you say that the
> > strncpy() won't overrun dot_name[]. If we have a name[] longer than
> > sizeof(dot_name) with the ':' after that, the strncpy() can also overrun
> > dot_name[].
> 
> Yes, that should just be a memcpy(), as should the strncat().
> 
> Using strncpy() where the length is other than the size of the target buffer
> should be banned. Not that it ever does what people expect.
> strncat() is even worse.

Sure, but with the proper validation, I still think string functions are 
convenient and useful here. I agree about your view on strncat(), so 
I'll switch to strlcat() instead.

Thanks,
Naveen