Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread kbuild test robot
Hi Masami,

[auto build test ERROR on tip/x86/core]
[also build test ERROR on v4.13-rc2 next-20170724]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/kprobes-x86-Do-not-jump-optimize-kprobes-on-irq-entry-code/20170725-121438
config: i386-randconfig-x019-201730 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=i386 

All errors (new ones prefixed by >>):

   arch/x86/kernel/unwind_frame.c: In function 'in_entry_code':
>> arch/x86/kernel/unwind_frame.c:95:14: error: '__irqentry_text_start' 
>> undeclared (first use in this function)
 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 ^
   arch/x86/kernel/unwind_frame.c:95:14: note: each undeclared identifier is 
reported only once for each function it appears in
>> arch/x86/kernel/unwind_frame.c:95:46: error: '__irqentry_text_end' 
>> undeclared (first use in this function)
 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 ^~~

vim +/__irqentry_text_start +95 arch/x86/kernel/unwind_frame.c

24d86f59 Josh Poimboeuf   2016-10-27   86  
6b4d2348 Masami Hiramatsu 2017-07-24   87  bool in_entry_code(unsigned long ip)
a8b7a923 Josh Poimboeuf   2017-04-12   88  {
a8b7a923 Josh Poimboeuf   2017-04-12   89   char *addr = (char *)ip;
a8b7a923 Josh Poimboeuf   2017-04-12   90  
a8b7a923 Josh Poimboeuf   2017-04-12   91   if (addr >= __entry_text_start 
&& addr < __entry_text_end)
a8b7a923 Josh Poimboeuf   2017-04-12   92   return true;
a8b7a923 Josh Poimboeuf   2017-04-12   93  
6b4d2348 Masami Hiramatsu 2017-07-24   94  #if 
defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
a8b7a923 Josh Poimboeuf   2017-04-12  @95   if (addr >= 
__irqentry_text_start && addr < __irqentry_text_end)
a8b7a923 Josh Poimboeuf   2017-04-12   96   return true;
a8b7a923 Josh Poimboeuf   2017-04-12   97  #endif
a8b7a923 Josh Poimboeuf   2017-04-12   98  
a8b7a923 Josh Poimboeuf   2017-04-12   99   return false;
a8b7a923 Josh Poimboeuf   2017-04-12  100  }
a8b7a923 Josh Poimboeuf   2017-04-12  101  

:: The code at line 95 was first introduced by commit
:: a8b7a92318b6d7779f6d8e9aa6ba0e3de01a8943 x86/unwind: Silence 
entry-related warnings

:: TO: Josh Poimboeuf 
:: CC: Ingo Molnar 

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip


Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread kbuild test robot
Hi Masami,

[auto build test ERROR on tip/x86/core]
[also build test ERROR on v4.13-rc2 next-20170724]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/kprobes-x86-Do-not-jump-optimize-kprobes-on-irq-entry-code/20170725-121438
config: i386-randconfig-x019-201730 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=i386 

All errors (new ones prefixed by >>):

   arch/x86/kernel/unwind_frame.c: In function 'in_entry_code':
>> arch/x86/kernel/unwind_frame.c:95:14: error: '__irqentry_text_start' 
>> undeclared (first use in this function)
 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 ^
   arch/x86/kernel/unwind_frame.c:95:14: note: each undeclared identifier is 
reported only once for each function it appears in
>> arch/x86/kernel/unwind_frame.c:95:46: error: '__irqentry_text_end' 
>> undeclared (first use in this function)
 if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 ^~~

vim +/__irqentry_text_start +95 arch/x86/kernel/unwind_frame.c

24d86f59 Josh Poimboeuf   2016-10-27   86  
6b4d2348 Masami Hiramatsu 2017-07-24   87  bool in_entry_code(unsigned long ip)
a8b7a923 Josh Poimboeuf   2017-04-12   88  {
a8b7a923 Josh Poimboeuf   2017-04-12   89   char *addr = (char *)ip;
a8b7a923 Josh Poimboeuf   2017-04-12   90  
a8b7a923 Josh Poimboeuf   2017-04-12   91   if (addr >= __entry_text_start 
&& addr < __entry_text_end)
a8b7a923 Josh Poimboeuf   2017-04-12   92   return true;
a8b7a923 Josh Poimboeuf   2017-04-12   93  
6b4d2348 Masami Hiramatsu 2017-07-24   94  #if 
defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
a8b7a923 Josh Poimboeuf   2017-04-12  @95   if (addr >= 
__irqentry_text_start && addr < __irqentry_text_end)
a8b7a923 Josh Poimboeuf   2017-04-12   96   return true;
a8b7a923 Josh Poimboeuf   2017-04-12   97  #endif
a8b7a923 Josh Poimboeuf   2017-04-12   98  
a8b7a923 Josh Poimboeuf   2017-04-12   99   return false;
a8b7a923 Josh Poimboeuf   2017-04-12  100  }
a8b7a923 Josh Poimboeuf   2017-04-12  101  

:: The code at line 95 was first introduced by commit
:: a8b7a92318b6d7779f6d8e9aa6ba0e3de01a8943 x86/unwind: Silence 
entry-related warnings

:: TO: Josh Poimboeuf 
:: CC: Ingo Molnar 

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip


Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
On Tue, 25 Jul 2017 09:15:29 +0900
Masami Hiramatsu  wrote:

> > arch/x86/kernel/unwind_frame.c is only compiled when CONFIG_FRAME_POINTER
> > is enabled, but CONFIG_KPROBES does not depend on it.
> > 
> > Is unwind_frame.c really where in_entry_code() should be implemented, now
> > that its symbol becomes exposed to other compile units ?
> 
> At least kprobes needs it. (as far 
> It seems we can move it in arch/x86/entry/common.c or 
> somewhere in header. (arm has arch/arm/include/asm/traps.h for
> that purpose)

As far as I can see, that code is only for unwinder or kprobes,
and only arm/arm64 has similar code.

I've decided to make this fix kprobe/opt.c local, since this fix
is also needed for stable. I'll send v2 patch soon.

Thanks,

-- 
Masami Hiramatsu 


Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
On Tue, 25 Jul 2017 09:15:29 +0900
Masami Hiramatsu  wrote:

> > arch/x86/kernel/unwind_frame.c is only compiled when CONFIG_FRAME_POINTER
> > is enabled, but CONFIG_KPROBES does not depend on it.
> > 
> > Is unwind_frame.c really where in_entry_code() should be implemented, now
> > that its symbol becomes exposed to other compile units ?
> 
> At least kprobes needs it. (as far 
> It seems we can move it in arch/x86/entry/common.c or 
> somewhere in header. (arm has arch/arm/include/asm/traps.h for
> that purpose)

As far as I can see, that code is only for unwinder or kprobes,
and only arm/arm64 has similar code.

I've decided to make this fix kprobe/opt.c local, since this fix
is also needed for stable. I'll send v2 patch soon.

Thanks,

-- 
Masami Hiramatsu 


Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
On Mon, 24 Jul 2017 14:28:36 + (UTC)
Mathieu Desnoyers  wrote:

> - On Jul 24, 2017, at 9:50 AM, Masami Hiramatsu mhira...@kernel.org wrote:
> 
> > Since the kernel segment registers are not prepared at the
> > entry of irq-entry code, if a kprobe on such code is
> > jump-optimized, accessing per-cpu variables may cause
> > kernel panic.
> > However, if the kprobe is not optimized, it kicks int3
> > exception and set segment registers correctly.
> > 
> > This checks probe-address and if it is in irq-entry code,
> > it prohibits optimizing such kprobes. This means we can
> > continuously probing such interrupt handlers by kprobes
> > but it is not optimized anymore.
> > 
> > Signed-off-by: Masami Hiramatsu 
> > Reported-by: Francis Deslauriers 
> > Tested-by: Francis Deslauriers 
> > ---
> > arch/x86/entry/entry_64.S  |2 +-
> > arch/x86/include/asm/unwind.h  |1 +
> > arch/x86/kernel/kprobes/opt.c  |4 ++--
> > arch/x86/kernel/unwind_frame.c |4 ++--
> > 4 files changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
> > index aa58155..9652c34 100644
> > --- a/arch/x86/entry/entry_64.S
> > +++ b/arch/x86/entry/entry_64.S
> > @@ -766,7 +766,7 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
> > #endif
> > 
> > /* Make sure APIC interrupt handlers end up in the irqentry section: */
> > -#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
> > +#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) ||
> > defined(CONFIG_KPROBES)
> > # define PUSH_SECTION_IRQENTRY  .pushsection .irqentry.text, "ax"
> > # define POP_SECTION_IRQENTRY   .popsection
> > #else
> > diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
> > index e667649..a9896fb9 100644
> > --- a/arch/x86/include/asm/unwind.h
> > +++ b/arch/x86/include/asm/unwind.h
> > @@ -28,6 +28,7 @@ void __unwind_start(struct unwind_state *state, struct
> > task_struct *task,
> > bool unwind_next_frame(struct unwind_state *state);
> > 
> > unsigned long unwind_get_return_address(struct unwind_state *state);
> > +bool in_entry_code(unsigned long ip);
> > 
> > static inline bool unwind_done(struct unwind_state *state)
> > {
> > diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
> > index 69ea0bc..a51c144 100644
> > --- a/arch/x86/kernel/kprobes/opt.c
> > +++ b/arch/x86/kernel/kprobes/opt.c
> > @@ -39,6 +39,7 @@
> > #include 
> > #include 
> > #include 
> > +#include 
> > 
> > #include "common.h"
> > 
> > @@ -253,8 +254,7 @@ static int can_optimize(unsigned long paddr)
> >  * Do not optimize in the entry code due to the unstable
> >  * stack handling.
> >  */
> > -   if ((paddr >= (unsigned long)__entry_text_start) &&
> > -   (paddr <  (unsigned long)__entry_text_end))
> > +   if (in_entry_code(paddr))
> > return 0;
> > 
> > /* Check there is enough space for a relative jump. */
> > diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
> > index b9389d7..95123ce 100644
> > --- a/arch/x86/kernel/unwind_frame.c
> > +++ b/arch/x86/kernel/unwind_frame.c
> > @@ -84,14 +84,14 @@ static size_t regs_size(struct pt_regs *regs)
> > return sizeof(*regs);
> > }
> > 
> > -static bool in_entry_code(unsigned long ip)
> > +bool in_entry_code(unsigned long ip)
> > {
> > char *addr = (char *)ip;
> > 
> > if (addr >= __entry_text_start && addr < __entry_text_end)
> > return true;
> > 
> > -#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
> > +#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) ||
> > defined(CONFIG_KPROBES)
> 
> Hi Masami,
> 
> With this patch applied on top of v4.12.3, I can generate a configuration
> with CONFIG_KPROBES=y and CONFIG_FRAME_POINTER=n, which leads to:
> 
> arch/x86/built-in.o: In function `can_optimize':
> /home/efficios/git/linux/arch/x86/kernel/kprobes/opt.c:250: undefined 
> reference to `in_entry_code'
> 
> (see attached .config)

Oops, right.

> arch/x86/kernel/unwind_frame.c is only compiled when CONFIG_FRAME_POINTER
> is enabled, but CONFIG_KPROBES does not depend on it.
> 
> Is unwind_frame.c really where in_entry_code() should be implemented, now
> that its symbol becomes exposed to other compile units ?

At least kprobes needs it. (as far 
It seems we can move it in arch/x86/entry/common.c or 
somewhere in header. (arm has arch/arm/include/asm/traps.h for
that purpose)

Thanks,

> 
> Thanks,
> 
> Mathieu
> 
> 
> > if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
> > return true;
> >  #endif
> 
> -- 
> Mathieu Desnoyers
> EfficiOS Inc.
> http://www.efficios.com


-- 
Masami Hiramatsu 


Re: [PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
On Mon, 24 Jul 2017 14:28:36 + (UTC)
Mathieu Desnoyers  wrote:

> - On Jul 24, 2017, at 9:50 AM, Masami Hiramatsu mhira...@kernel.org wrote:
> 
> > Since the kernel segment registers are not prepared at the
> > entry of irq-entry code, if a kprobe on such code is
> > jump-optimized, accessing per-cpu variables may cause
> > kernel panic.
> > However, if the kprobe is not optimized, it kicks int3
> > exception and set segment registers correctly.
> > 
> > This checks probe-address and if it is in irq-entry code,
> > it prohibits optimizing such kprobes. This means we can
> > continuously probing such interrupt handlers by kprobes
> > but it is not optimized anymore.
> > 
> > Signed-off-by: Masami Hiramatsu 
> > Reported-by: Francis Deslauriers 
> > Tested-by: Francis Deslauriers 
> > ---
> > arch/x86/entry/entry_64.S  |2 +-
> > arch/x86/include/asm/unwind.h  |1 +
> > arch/x86/kernel/kprobes/opt.c  |4 ++--
> > arch/x86/kernel/unwind_frame.c |4 ++--
> > 4 files changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
> > index aa58155..9652c34 100644
> > --- a/arch/x86/entry/entry_64.S
> > +++ b/arch/x86/entry/entry_64.S
> > @@ -766,7 +766,7 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
> > #endif
> > 
> > /* Make sure APIC interrupt handlers end up in the irqentry section: */
> > -#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
> > +#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) ||
> > defined(CONFIG_KPROBES)
> > # define PUSH_SECTION_IRQENTRY  .pushsection .irqentry.text, "ax"
> > # define POP_SECTION_IRQENTRY   .popsection
> > #else
> > diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
> > index e667649..a9896fb9 100644
> > --- a/arch/x86/include/asm/unwind.h
> > +++ b/arch/x86/include/asm/unwind.h
> > @@ -28,6 +28,7 @@ void __unwind_start(struct unwind_state *state, struct
> > task_struct *task,
> > bool unwind_next_frame(struct unwind_state *state);
> > 
> > unsigned long unwind_get_return_address(struct unwind_state *state);
> > +bool in_entry_code(unsigned long ip);
> > 
> > static inline bool unwind_done(struct unwind_state *state)
> > {
> > diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
> > index 69ea0bc..a51c144 100644
> > --- a/arch/x86/kernel/kprobes/opt.c
> > +++ b/arch/x86/kernel/kprobes/opt.c
> > @@ -39,6 +39,7 @@
> > #include 
> > #include 
> > #include 
> > +#include 
> > 
> > #include "common.h"
> > 
> > @@ -253,8 +254,7 @@ static int can_optimize(unsigned long paddr)
> >  * Do not optimize in the entry code due to the unstable
> >  * stack handling.
> >  */
> > -   if ((paddr >= (unsigned long)__entry_text_start) &&
> > -   (paddr <  (unsigned long)__entry_text_end))
> > +   if (in_entry_code(paddr))
> > return 0;
> > 
> > /* Check there is enough space for a relative jump. */
> > diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
> > index b9389d7..95123ce 100644
> > --- a/arch/x86/kernel/unwind_frame.c
> > +++ b/arch/x86/kernel/unwind_frame.c
> > @@ -84,14 +84,14 @@ static size_t regs_size(struct pt_regs *regs)
> > return sizeof(*regs);
> > }
> > 
> > -static bool in_entry_code(unsigned long ip)
> > +bool in_entry_code(unsigned long ip)
> > {
> > char *addr = (char *)ip;
> > 
> > if (addr >= __entry_text_start && addr < __entry_text_end)
> > return true;
> > 
> > -#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
> > +#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) ||
> > defined(CONFIG_KPROBES)
> 
> Hi Masami,
> 
> With this patch applied on top of v4.12.3, I can generate a configuration
> with CONFIG_KPROBES=y and CONFIG_FRAME_POINTER=n, which leads to:
> 
> arch/x86/built-in.o: In function `can_optimize':
> /home/efficios/git/linux/arch/x86/kernel/kprobes/opt.c:250: undefined 
> reference to `in_entry_code'
> 
> (see attached .config)

Oops, right.

> arch/x86/kernel/unwind_frame.c is only compiled when CONFIG_FRAME_POINTER
> is enabled, but CONFIG_KPROBES does not depend on it.
> 
> Is unwind_frame.c really where in_entry_code() should be implemented, now
> that its symbol becomes exposed to other compile units ?

At least kprobes needs it. (as far 
It seems we can move it in arch/x86/entry/common.c or 
somewhere in header. (arm has arch/arm/include/asm/traps.h for
that purpose)

Thanks,

> 
> Thanks,
> 
> Mathieu
> 
> 
> > if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
> > return true;
> >  #endif
> 
> -- 
> Mathieu Desnoyers
> EfficiOS Inc.
> http://www.efficios.com


-- 
Masami Hiramatsu 


[PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
Since the kernel segment registers are not prepared at the
entry of irq-entry code, if a kprobe on such code is
jump-optimized, accessing per-cpu variables may cause
kernel panic.
However, if the kprobe is not optimized, it kicks int3
exception and set segment registers correctly.

This checks probe-address and if it is in irq-entry code,
it prohibits optimizing such kprobes. This means we can
continuously probing such interrupt handlers by kprobes
but it is not optimized anymore.

Signed-off-by: Masami Hiramatsu 
Reported-by: Francis Deslauriers 
Tested-by: Francis Deslauriers 
---
 arch/x86/entry/entry_64.S  |2 +-
 arch/x86/include/asm/unwind.h  |1 +
 arch/x86/kernel/kprobes/opt.c  |4 ++--
 arch/x86/kernel/unwind_frame.c |4 ++--
 4 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index aa58155..9652c34 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -766,7 +766,7 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
 #endif
 
 /* Make sure APIC interrupt handlers end up in the irqentry section: */
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
 # define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
 # define POP_SECTION_IRQENTRY  .popsection
 #else
diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
index e667649..a9896fb9 100644
--- a/arch/x86/include/asm/unwind.h
+++ b/arch/x86/include/asm/unwind.h
@@ -28,6 +28,7 @@ void __unwind_start(struct unwind_state *state, struct 
task_struct *task,
 bool unwind_next_frame(struct unwind_state *state);
 
 unsigned long unwind_get_return_address(struct unwind_state *state);
+bool in_entry_code(unsigned long ip);
 
 static inline bool unwind_done(struct unwind_state *state)
 {
diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 69ea0bc..a51c144 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -39,6 +39,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "common.h"
 
@@ -253,8 +254,7 @@ static int can_optimize(unsigned long paddr)
 * Do not optimize in the entry code due to the unstable
 * stack handling.
 */
-   if ((paddr >= (unsigned long)__entry_text_start) &&
-   (paddr <  (unsigned long)__entry_text_end))
+   if (in_entry_code(paddr))
return 0;
 
/* Check there is enough space for a relative jump. */
diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index b9389d7..95123ce 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -84,14 +84,14 @@ static size_t regs_size(struct pt_regs *regs)
return sizeof(*regs);
 }
 
-static bool in_entry_code(unsigned long ip)
+bool in_entry_code(unsigned long ip)
 {
char *addr = (char *)ip;
 
if (addr >= __entry_text_start && addr < __entry_text_end)
return true;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
return true;
 #endif



[PATCH -tip] [BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

2017-07-24 Thread Masami Hiramatsu
Since the kernel segment registers are not prepared at the
entry of irq-entry code, if a kprobe on such code is
jump-optimized, accessing per-cpu variables may cause
kernel panic.
However, if the kprobe is not optimized, it kicks int3
exception and set segment registers correctly.

This checks probe-address and if it is in irq-entry code,
it prohibits optimizing such kprobes. This means we can
continuously probing such interrupt handlers by kprobes
but it is not optimized anymore.

Signed-off-by: Masami Hiramatsu 
Reported-by: Francis Deslauriers 
Tested-by: Francis Deslauriers 
---
 arch/x86/entry/entry_64.S  |2 +-
 arch/x86/include/asm/unwind.h  |1 +
 arch/x86/kernel/kprobes/opt.c  |4 ++--
 arch/x86/kernel/unwind_frame.c |4 ++--
 4 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index aa58155..9652c34 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -766,7 +766,7 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
 #endif
 
 /* Make sure APIC interrupt handlers end up in the irqentry section: */
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
 # define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
 # define POP_SECTION_IRQENTRY  .popsection
 #else
diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
index e667649..a9896fb9 100644
--- a/arch/x86/include/asm/unwind.h
+++ b/arch/x86/include/asm/unwind.h
@@ -28,6 +28,7 @@ void __unwind_start(struct unwind_state *state, struct 
task_struct *task,
 bool unwind_next_frame(struct unwind_state *state);
 
 unsigned long unwind_get_return_address(struct unwind_state *state);
+bool in_entry_code(unsigned long ip);
 
 static inline bool unwind_done(struct unwind_state *state)
 {
diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 69ea0bc..a51c144 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -39,6 +39,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "common.h"
 
@@ -253,8 +254,7 @@ static int can_optimize(unsigned long paddr)
 * Do not optimize in the entry code due to the unstable
 * stack handling.
 */
-   if ((paddr >= (unsigned long)__entry_text_start) &&
-   (paddr <  (unsigned long)__entry_text_end))
+   if (in_entry_code(paddr))
return 0;
 
/* Check there is enough space for a relative jump. */
diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index b9389d7..95123ce 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -84,14 +84,14 @@ static size_t regs_size(struct pt_regs *regs)
return sizeof(*regs);
 }
 
-static bool in_entry_code(unsigned long ip)
+bool in_entry_code(unsigned long ip)
 {
char *addr = (char *)ip;
 
if (addr >= __entry_text_start && addr < __entry_text_end)
return true;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) || 
defined(CONFIG_KPROBES)
if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
return true;
 #endif