kprobes/x86: Use 5-byte NOP when the code might be modified by ftrace
PATCH v2 1/1 kprobes/x86: Use 5-byte NOP when the code might be modified by ftrace
can_probe() checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, and can_probe() analyzes the buffer instead.
There is a bug that __recover_probed_insn() tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference between arch_prepare_kprobe() and arch_prepare_kprobe_ftrace(). The opcode is stored by arch_copy_kprobe() only from arch_prepare_kprobe().
This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see ftrace_make_nop() and ftrace_nop_replace().
Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on x86_64 with gcc-4.8.3 that supported -mfentry. I modified samples/kprobes/kprobe_example.c and added offset 5 to put the probe right after the fentry area:
--- cut ---
static struct kprobe kp = {
.symbol_name = "do_fork",
+ .offset = 5,
};
--- cut ---
Then I was able to load kprobe_example before jprobe_example but not the other way around:
$> modprobe jprobe_example
$> modprobe kprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide character
It did not make much sense and debugging pointed to the bug described above.
--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)
Changes against v1:
+ always use 5-byte NOP for ftrace location
+ fix indentation of the touched comment
arch/x86/kernel/kprobes/core.c +28 -14
@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr; kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr); +/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); +if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }
Signature · 1 line
1.8.5.6
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:
Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Ingo, could you merge this as an urgent fix?
Thank you!
Quoted 71 lines
Signed-off-by Petr Mladek <[email protected]>--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)Changes against v1:
+ always use 5-byte NOP for ftrace location
+ fix indentation of the touched commentarch/x86/kernel/kprobes/core.c +28
-14@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr; kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr); +/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); +if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 51 lines · Masami Hiramatsu
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Quoted 3 lines
Acked-by Masami Hiramatsu <[email protected]>Ingo, could you merge this as an urgent fix?
Please, wait a bit, see below.
Quoted 22 lines
Thank you!
Quoted 20 lines
Signed-off-by Petr Mladek <[email protected]>--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)Changes against v1:
+ always use 5-byte NOP for ftrace location
+ fix indentation of the touched commentarch/x86/kernel/kprobes/core.c +1
-0@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr;
Quoted 4 lines
arch/x86/kernel/kprobes/core.c static unsigned long · line 229
Quoted 4 lines
arch/x86/kernel/kprobes/core.c +1
-2kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr);
I have just realized that ftrace_location() might return another address if the given one points inside the ftrace_location. This situation is not checked by this patch. I am going to work on v3.
I knew that I should not have sent the patch just before leaving :-(
Best Regards,
Petr
Quoted 56 lines
Quoted 47 lines
arch/x86/kernel/kprobes/core.c +26
-12+/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); +if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 80 lines · Petr Mladek
Quoted 51 lines · Masami Hiramatsu
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Quoted 3 lines
Acked-by Masami Hiramatsu <[email protected]>Ingo, could you merge this as an urgent fix?
Please, wait a bit, see below.
Quoted 22 lines
Thank you!
Quoted 20 lines
Signed-off-by Petr Mladek <[email protected]>--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)Changes against v1:
+ always use 5-byte NOP for ftrace location
+ fix indentation of the touched commentarch/x86/kernel/kprobes/core.c +1
-0@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr;
Quoted 9 lines
Quoted 4 lines
arch/x86/kernel/kprobes/core.c static unsigned long · line 229
Quoted 4 lines
arch/x86/kernel/kprobes/core.c +1
-2kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr);I have just realized that
ftrace_location()might return another address if the given one points inside theftrace_location. This situation is not checked by this patch. I am going to work on v3.
Ah, I see. But when it happens, something goes wrong, since __recover_probed_insn() must be called with the address from where an instruction starts (e.g. function entry or the instruction boundary.)
So, we just need "BUG_ON(faddr && faddr != addr)" for that case.
Thank you,
Quoted 68 lines
I knew that I should not have sent the patch just before leaving :-(
Best Regards,
PetrQuoted 39 lines
Quoted 39 lines
arch/x86/kernel/kprobes/core.c +22
-12+/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t));Quoted 17 lines
Quoted 8 lines
arch/x86/kernel/kprobes/core.c +4
-0+if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 80 lines · Petr Mladek
Quoted 51 lines · Masami Hiramatsu
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Quoted 3 lines
Acked-by Masami Hiramatsu <[email protected]>Ingo, could you merge this as an urgent fix?
Please, wait a bit, see below.
Quoted 22 lines
Thank you!
Quoted 20 lines
Signed-off-by Petr Mladek <[email protected]>--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)Changes against v1:
+ always use 5-byte NOP for ftrace location
+ fix indentation of the touched commentarch/x86/kernel/kprobes/core.c +1
-0@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr;
Quoted 9 lines
Quoted 4 lines
arch/x86/kernel/kprobes/core.c static unsigned long · line 229
Quoted 4 lines
arch/x86/kernel/kprobes/core.c +1
-2kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr);I have just realized that
ftrace_location()might return another address if the given one points inside theftrace_location. This situation is not checked by this patch. I am going to work on v3.
Well, it should not happen after all because __recover_probed_insn() is called only for already approved Kprobe locations and therefore only for the first byte of the ftrace location. Any address inside the ftrace location is refused earlier by check_kprobe_address_safe() that is called from register_kprobe.
It means that it will never return another address here and the patch can be used as is unless you want to be paranoid.
I am sorry for the rumor.
Best Regards,
Petr
Quoted 56 lines
Quoted 56 lines
Quoted 47 lines
arch/x86/kernel/kprobes/core.c +26
-12+/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); +if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 52 lines · Petr Mladek
Quoted 51 lines · Masami Hiramatsu
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Quoted 5 lines
Quoted 3 lines
Acked-by Masami Hiramatsu <[email protected]>Ingo, could you merge this as an urgent fix?
Please, wait a bit, see below.
Please [re]send the final patch with Masami's Ack added and me Cc:-ed.
Thanks,
Ingo
Quoted 55 lines · Ingo Molnar
Quoted 52 lines · Petr Mladek
Quoted 51 lines · Masami Hiramatsu
Quoted 40 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.There is a bug that
__recover_probed_insn()tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference betweenarch_prepare_kprobe()andarch_prepare_kprobe_ftrace(). The opcode is stored byarch_copy_kprobe()only fromarch_prepare_kprobe().This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see
ftrace_make_nop()andftrace_nop_replace().Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on
x86_64with gcc-4.8.3 that supported -mfentry. I modifiedsamples/kprobes/kprobe_example.cand added offset 5 to put the probe right after the fentry area:--- cut ---
static struct kprobe kp = {(quoted diff) +1
-1.symbol_name = "do_fork", + .offset = 5, }; --- cut ---Then I was able to load
kprobe_examplebeforejprobe_examplebut not the other way around:Quoted 7 lines
$> modprobe
jprobe_example
$> modprobekprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide characterIt did not make much sense and debugging pointed to the bug described above.
This looks good to me :)
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Acked-by Masami Hiramatsu <[email protected]>Ingo, could you merge this as an urgent fix?
Please, wait a bit, see below.
Please [re]send the final patch with Masami's Ack added and me Cc:-ed.
The other problem was solved and acked in a separate patch. I'll rather send it this way. I am sorry for the confusion.
Best Regards,
Petr
PATCH 1/2 kprobes/x86: Use 5-byte NOP when the code might be modified by ftrace
can_probe() checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, and can_probe() analyzes the buffer instead.
There is a bug that __recover_probed_insn() tries to restore the original code even for Kprobes using the ftrace framework. But in this case, the opcode is not stored. See the difference between arch_prepare_kprobe() and arch_prepare_kprobe_ftrace(). The opcode is stored by arch_copy_kprobe() only from arch_prepare_kprobe().
This patch makes Kprobe to use the ideal 5-byte NOP when the code can be modified by ftrace. It is the original instruction, see ftrace_make_nop() and ftrace_nop_replace().
Note that we always need to use the NOP for ftrace locations. Kprobes do not block ftrace and the instruction might get modified at anytime. It might even be in an inconsistent state because it is modified step by step using the int3 breakpoint.
The patch also fixes indentation of the touched comment.
Note that I found this problem when playing with Kprobes. I did it on x86_64 with gcc-4.8.3 that supported -mfentry. I modified samples/kprobes/kprobe_example.c and added offset 5 to put the probe right after the fentry area:
static struct kprobe kp = {
.symbol_name = "do_fork",
+ .offset = 5,
};
Then I was able to load kprobe_example before jprobe_example but not the other way around:
$> modprobe jprobe_example
$> modprobe kprobe_example
modprobe: ERROR: could not insert 'kprobe_example': Invalid or incomplete multibyte or wide character
It did not make much sense and debugging pointed to the bug described above.
--- arch/x86/kernel/kprobes/core.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)
arch/x86/kernel/kprobes/core.c +28 -14
@@ -223,27 +223,41 @@ static unsigned long __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) {struct kprobe *kp; +unsigned long faddr; kp =get_kprobe ((void *)addr); -/* There is no probe, return original address */ -if (!kp) + faddr =ftrace_location (addr); +/* +* Use the current code if it is not modified by Kprobe +* and it cannot be modified by ftrace. +*/ +if (!kp && !faddr)return addr;/* -* Basically, kp->ainsn.insn has an original instruction. -* However, RIP-relative instruction can not do single-stepping -* at different place, __copy_instruction() tweaks the displacement of -* that instruction. In that case, we can't recover the instruction -* from the kp->ainsn.insn. +* Basically, kp->ainsn.insn has an original instruction. +* However, RIP-relative instruction can not do single-stepping +* at different place, __copy_instruction() tweaks the displacement of +* that instruction. In that case, we can't recover the instruction +* from the kp->ainsn.insn. * -* On the other hand, kp->opcode has a copy of the first byte of -* the probed instruction, which is overwritten by int3. And -* the instruction at kp->addr is not modified by kprobes except -* for the first byte, we can recover the original instruction -* from it and kp->opcode. +* On the other hand, in case on normal Kprobe, kp->opcode has a copy +* of the first byte of the probed instruction, which is overwritten +* by int3. And the instruction at kp->addr is not modified by kprobes +* except for the first byte, we can recover the original instruction +* from it and kp->opcode. +* +* In case of Kprobes using ftrace, we do not have a copy of +* the original instruction. In fact, the ftrace location might +* be modified at anytime and even could be in an inconsistent state. +* Fortunately, we know that the original code is the ideal 5-byte +* long NOP. */ -memcpy (buf, kp->addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); - buf[0 ] = kp->opcode; +memcpy (buf, (void *)addr,MAX_INSN_SIZE *sizeof (kprobe_opcode_t)); +if (faddr) +memcpy (buf, ideal_nops[NOP_ATOMIC5 ],MCOUNT_INSN_SIZE ); +else + buf[0 ] = kp->opcode;return (unsigned long )buf; }
Signature · 1 line
1.8.5.6
Quoted 10 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.[...]
Had to drop this patch due to build failures on 32-bit x86:
arch/x86/kernel/kprobes/core.c:258:40: error: ‘MCOUNT_INSN_SIZE’ undeclared (first use in this function)
Thanks,
Ingo
Quoted 12 lines · Ingo Molnar
Quoted 9 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.
Quoted 5 lines
Quoted 1 line
[...]
Had to drop this patch due to build failures on 32-bit x86:
arch/x86/kernel/kprobes/core.c:258:40: error: ‘MCOUNT_INSN_SIZE’ undeclared (first use in this function)
Oops, MCOUNT_INSN_SIZE actually depends on CONFIG_FUNCTION_TRACER... I think we can use 5 instead of that since we are copying NOP_ATOMIC5.
Thank you,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 15 lines · Masami Hiramatsu
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.[...]
Quoted 7 lines
Quoted 3 lines
Had to drop this patch due to build failures on 32-bit x86:
arch/x86/kernel/kprobes/core.c:258:40: error: ‘MCOUNT_INSN_SIZE’ undeclared (first use in this function)Oops,
MCOUNT_INSN_SIZEactually depends onCONFIG_FUNCTION_TRACER... I think we can use 5 instead of that since we are copyingNOP_ATOMIC5.
Or just make the define more widely available? It's not like the size changes from disabling the function tracer.
Thanks,
Ingo
Quoted 20 lines · Ingo Molnar
Quoted 17 lines · Masami Hiramatsu
Quoted 16 lines · Ingo Molnar
Quoted 10 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.[...]
Had to drop this patch due to build failures on 32-bit x86:
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
arch/x86/kernel/kprobes/core.c:258:40: error: ‘MCOUNT_INSN_SIZE’ undeclared (first use in this function)Oops,
MCOUNT_INSN_SIZEactually depends onCONFIG_FUNCTION_TRACER... I think we can use 5 instead of that since we are copyingNOP_ATOMIC5.Or just make the define more widely available? It's not like the size changes from disabling the function tracer.
Yeah, it could be.
Actually, to tell the truth, if CONFIG_FUNCTION_TRACER=n, ftrace_location() always returns 0, so the 2nd memcpy never be executed (it should be disappeared by optimization). Thus *this* issue is very local one, and I thought we'd better fix this locally. :)
Thank you,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 18 lines · Ingo Molnar
Quoted 15 lines · Masami Hiramatsu
Quoted 14 lines · Ingo Molnar
Quoted 10 lines · Petr Mladek
can_probe()checks if the given address points to the beginning of an instruction. It analyzes all the instructions from the beginning of the function until the given address. The code might be modified by another Kprobe. In this case, the current code is read into a buffer, int3 breakpoint is replaced by the saved opcode in the buffer, andcan_probe()analyzes the buffer instead.[...]
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Had to drop this patch due to build failures on 32-bit x86:
arch/x86/kernel/kprobes/core.c:258:40: error: ‘MCOUNT_INSN_SIZE’ undeclared (first use in this function)Oops,
MCOUNT_INSN_SIZEactually depends onCONFIG_FUNCTION_TRACER... I think we can use 5 instead of that since we are copying.
I'll use 5 for now.
Quoted 2 lines
Or just make the define more widely available? It's not like the size changes from disabling the function tracer.
I would do this in a separate patch that would consolidate all NOP_ATOMIC5 users: ftrace, jump_labels, and kprobe. The number 5 is hardcoded more times in arch/x86/kernel/kprobes/core.c. I'll put it into my TODO list.
Best Regards,
Petr
PATCH 2/2 kprobes/x86: Check for invalid ftrace location in __recover_probed_insn()
In reply to Petr Mladek__recover_probed_insn() should always be called from an address where an instructions starts. The check for ftrace_location() might help to discover a potential inconsistency. Something goes terribly wrong when an address inside the ftrace location is checked. Let's BUG() in this case.
--- arch/x86/kernel/kprobes/core.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kernel/kprobes/core.c +6 -0
@@ -228,6 +228,12 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) kp =get_kprobe ((void *)addr); faddr =ftrace_location (addr);/* +* Addresses inside the ftrace location are refused by +* arch_check_ftrace_location(). Something went terribly wrong +* if such an address is checked here. +*/ +BUG_ON (faddr && faddr != addr); +/* * Use the current code if it is not modified by Kprobe * and it cannot be modified by ftrace. */
Signature · 1 line
1.8.5.6
Re: [PATCH 2/2] kprobes/x86: Check for invalid ftrace location in __recover_probed_insn()
Quoted 17 lines · Petr Mladek
__recover_probed_insn()should always be called from an address where an instructions starts. The check forftrace_location()might help to discover a potential inconsistency. Something goes terribly wrong when an address inside the ftrace location is checked. Let'sBUG()in this case.Suggested-by Masami Hiramatsu <[email protected]>Signed-off-by Petr Mladek <[email protected]>Acked-by Masami Hiramatsu <[email protected]>--- arch/x86/kernel/kprobes/core.c | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/kernel/kprobes/core.c +0
-0
arch/x86/kernel/kprobes/core.c __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) · line 235
Quoted 8 lines
arch/x86/kernel/kprobes/core.c +5
-0@@ -228,6 +228,12 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr) kp =get_kprobe ((void *)addr); faddr =ftrace_location (addr);/* +* Addresses inside the ftrace location are refused by +* arch_check_ftrace_location(). Something went terribly wrong +* if such an address is checked here. +*/ +BUG_ON (faddr && faddr != addr);
Crashing the system with a BUG_ON() makes users very sad. Please use a construct like:
if (WARN_ON(faddr && faddr != addr)) return gently;
I've picked up your first patch.
Thanks,
Ingo