CET/IBT support and live-patches
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Miroslav Benes
maintainer[ adding more CCs ]
Quoted 3 lines · On Mon, 22 Nov 2021, [email protected] wrote:
Hi Miroslav, Petr and Nicolai,
Long time no talk, I hope you are all still doing great :)
Everything great here :)
Quoted 13 lines
So, we have been cooking a few patches for enabling Intel CET/IBT support in the kernel. The way IBT works is -- whenever you have an indirect branch, the control-flow must land in an endbr instruction. The idea is to constrain control-flow in a way to make it harder for attackers to achieve meaningful computation through pointer/memory corruption (as in, an attacker that can corrupt a function pointer by exploiting a memory corruption bug won't be able to execute whatever piece of code, being restricted to jump into endbr instructions). To make the allowed control-flow graph more restrict, we are looking into how to minimize the number of endbrs in the final kernel binary -- meaning that if a function is never called indirectly, it shouldn't have an endbr instruction, thus increasing the security guarantees of the hardware feature.
Quoted 2 lines
Some ref about what is going on --
https://lore.kernel.org/lkml/[email protected]/T/
Yes, I noticed something was happening again. There was a thread on this in February https://lore.kernel.org/all/[email protected]/
and some concerns were raised back then around fentry and int3 patching if I remember correctly. Is this still an issue?
Quoted 9 lines
IIRC, live-patching used kallsyms/kallsyms_lookup_name for grabbing pointers to the symbols in the running kernel and then used these pointers to invoke the functions which reside outside of the live-patch (ie. previously existing functions). With the above IBT support, if these functions were considered non-indirectly-reachable, and were suppressed of an endbr, this would lead into a crash. I remember we were working on klp-convert to fix this through special relocations and that there were other proposals... but I'm not sure where it went.
Quoted 3 lines
So, would you mind giving a quick update on the general state of this? If the IBT support would break this (and anything else) regarding live-patching... and so on?
Right. So, this really depends on how downstream consumers approach this. kpatch-build should be fine if I am not mistaken, because it uses the .klp.rela support we have in the kernel. We (at SUSE) have a problem, because we still exploit kallsyms/kallsyms_lookup_name to cope with this.
Joe, what is the current state of klp-convert? Do we still want to follow that way?
There was an idea a long time ago to actually rewrite all the relocations in a live patch module, so that they are relative to a well defined symbol (both in vmlinux and in modules. It would have to be created, I guess.). It would be easier tooling-wise and kernel module loader would process it like everything else. However, FGKASLR with all its reshuffling would render it useless. So something like klp-convert is needed.
Thanks
Miroslav
Quoted 23 lines · Miroslav Benes
[ adding more CCs ]
Quoted 3 lines · On Mon, 22 Nov 2021, [email protected] wrote:
Hi Miroslav, Petr and Nicolai,
Long time no talk, I hope you are all still doing great :)
Everything great here :)
Quoted 13 lines
So, we have been cooking a few patches for enabling Intel CET/IBT support in the kernel. The way IBT works is -- whenever you have an indirect branch, the control-flow must land in an endbr instruction. The idea is to constrain control-flow in a way to make it harder for attackers to achieve meaningful computation through pointer/memory corruption (as in, an attacker that can corrupt a function pointer by exploiting a memory corruption bug won't be able to execute whatever piece of code, being restricted to jump into endbr instructions). To make the allowed control-flow graph more restrict, we are looking into how to minimize the number of endbrs in the final kernel binary -- meaning that if a function is never called indirectly, it shouldn't have an endbr instruction, thus increasing the security guarantees of the hardware feature.
Quoted 7 lines
Quoted 2 lines
Some ref about what is going on --
https://lore.kernel.org/lkml/[email protected]/T/Yes, I noticed something was happening again. There was a thread on this in February https://lore.kernel.org/all/[email protected]/
and some concerns were raised back then around fentry and int3 patching if I remember correctly. Is this still an issue?
The problem was bpf, and probably still is. I've not come around to looking at it. Asusming fentry is at +0 is silly (although I would like to see that restored for other reasons), but bpf will also need to emit ENDBR at least at the start of every JIT'ed program, because entry into them is through an indirect branch.
If nobody beats me to it, I'll get around to it eventually.
Miroslav Benes
maintainerQuoted 32 lines · Peter Zijlstra
Quoted 23 lines · Miroslav Benes
[ adding more CCs ]
Quoted 3 lines · On Mon, 22 Nov 2021, [email protected] wrote:
Hi Miroslav, Petr and Nicolai,
Long time no talk, I hope you are all still doing great :)
Everything great here :)
Quoted 13 lines
So, we have been cooking a few patches for enabling Intel CET/IBT support in the kernel. The way IBT works is -- whenever you have an indirect branch, the control-flow must land in an endbr instruction. The idea is to constrain control-flow in a way to make it harder for attackers to achieve meaningful computation through pointer/memory corruption (as in, an attacker that can corrupt a function pointer by exploiting a memory corruption bug won't be able to execute whatever piece of code, being restricted to jump into endbr instructions). To make the allowed control-flow graph more restrict, we are looking into how to minimize the number of endbrs in the final kernel binary -- meaning that if a function is never called indirectly, it shouldn't have an endbr instruction, thus increasing the security guarantees of the hardware feature.
Quoted 7 lines
Quoted 2 lines
Some ref about what is going on --
https://lore.kernel.org/lkml/[email protected]/T/Yes, I noticed something was happening again. There was a thread on this in February https://lore.kernel.org/all/[email protected]/
and some concerns were raised back then around fentry and int3 patching if I remember correctly. Is this still an issue?
Quoted 7 lines
The problem was bpf, and probably still is. I've not come around to looking at it. Asusming fentry is at +0 is silly (although I would like to see that restored for other reasons), but bpf will also need to emit ENDBR at least at the start of every JIT'ed program, because entry into them is through an indirect branch.
If nobody beats me to it, I'll get around to it eventually.
Ok. And we would need something like the following for the livepatch (not even compile tested).
---
arch/powerpc/include/asm/livepatch.h +0 -10
@@ -19,16 +19,6 @@ static inline void klp_arch_set_pc(struct ftrace_regs *fregs, unsigned long ip)regs_set_return_ip (regs, ip); } -#define klp_get_ftrace_location klp_get_ftrace_location -static inline unsigned long klp_get_ftrace_location (unsigned long faddr) -{ -/* -* Live patch works only with -mprofile-kernel on PPC. In this case, -* the ftrace location is always within the first 16 bytes. -*/ -return ftrace_location_range (faddr, faddr +16 ); -} -static inline void klp_init_thread_info (struct task_struct *p) {/* + 1 to account for STACK_END_MAGIC */
kernel/livepatch/patch.c +9 -6
@@ -127,15 +127,18 @@ static void notrace klp_ftrace_handler(unsigned long ip,/* * Convert a function address into the appropriate ftrace location. * -* Usually this is just the address of the function, but on some architectures -* it's more complicated so allow them to provide a custom behaviour. +* Usually this is just the address of the function, but there are some +* exceptions. +* +* * PPC - live patch works only with -mprofile-kernel. In this case, +* the ftrace location is always within the first 16 bytes. +* * x86_64 with CET/IBT enabled - there is ENDBR instruction at +0 offset. +* __fentry__ follows it. */ -#ifndef klp_get_ftrace_location -static unsigned long klp_get_ftrace_location (unsigned long faddr) +static inline unsigned long klp_get_ftrace_location (unsigned long faddr) { -return faddr; +return ftrace_location_range (faddr, faddr +16 ); } -#endif static void klp_unpatch_func (struct klp_func *func) {
Quoted 42 lines · Miroslav Benes
Ok. And we would need something like the following for the livepatch (not even compile tested).
---
arch/powerpc/include/asm/livepatch.h +0
-10@@ -19,16 +19,6 @@ static inline void klp_arch_set_pc(struct ftrace_regs *fregs, unsigned long ip)regs_set_return_ip (regs, ip); } -#define klp_get_ftrace_location klp_get_ftrace_location -static inline unsigned long klp_get_ftrace_location (unsigned long faddr) -{ -/* -* Live patch works only with -mprofile-kernel on PPC. In this case, -* the ftrace location is always within the first 16 bytes. -*/ -return ftrace_location_range (faddr, faddr +16 ); -} -static inline void klp_init_thread_info (struct task_struct *p) {/* + 1 to account for STACK_END_MAGIC */ kernel/livepatch/patch.c +6
-2@@ -127,15 +127,18 @@ static void notrace klp_ftrace_handler(unsigned long ip,/* * Convert a function address into the appropriate ftrace location. * -* Usually this is just the address of the function, but on some architectures -* it's more complicated so allow them to provide a custom behaviour. +* Usually this is just the address of the function, but there are some +* exceptions. +* +* * PPC - live patch works only with -mprofile-kernel. In this case, +* the ftrace location is always within the first 16 bytes. +* * x86_64 with CET/IBT enabled - there is ENDBR instruction at +0 offset.
kernel/livepatch/patch.c static void notrace klp_ftrace_handler(unsigned long ip, · line 138
Quoted 10 lines
kernel/livepatch/patch.c +3
-4+* __fentry__ follows it. */ -#ifndef klp_get_ftrace_location -static unsigned long klp_get_ftrace_location (unsigned long faddr) +static inline unsigned long klp_get_ftrace_location (unsigned long faddr) { -return faddr; +return ftrace_location_range (faddr, faddr +16 ); } -#endif
Agreed, in fact, it should have called at least ftrace_location() before, as a sanity check the address is in fact a listed fentry site.
I wonder though, given ftrace_cmp_recs() what the behaviour is if there's two fentry sites within those 16 bytes... I don't think it will uniquely return the leftmost one, so that might need some thinking.
Consider:
foo:
endbr call __fentry__ ret; bar: endbr call __fentry__ ...
then both sites are within 16 bytes of one another.
Quoted 8 lines · On Tue, 23 Nov 2021 12:39:15 +0100 (CET) Miroslav Benes <[email protected]> wrote:
+++
b/kernel/livepatch/patch.ckernel/livepatch/patch.c +1
-2@@ -127,15 +127,18 @@ static void notrace klp_ftrace_handler(unsigned long ip,/* * Convert a function address into the appropriate ftrace location. * -* Usually this is just the address of the function, but on some architectures -* it's more complicated so allow them to provide a custom behaviour. +* Usually this is just the address of the function, but there are some
kernel/livepatch/patch.c static void notrace klp_ftrace_handler(unsigned long ip, · line 138
Quoted 10 lines
kernel/livepatch/patch.c +7
-2+* exceptions. +* +* * PPC - live patch works only with -mprofile-kernel. In this case, +* the ftrace location is always within the first 16 bytes. +* * x86_64 with CET/IBT enabled - there is ENDBR instruction at +0 offset. +* __fentry__ follows it. */ -#ifndef klp_get_ftrace_location -static unsigned long klp_get_ftrace_location (unsigned long faddr) +static inline unsigned long klp_get_ftrace_location (unsigned long faddr)
Why make this the default function? It should only do this for powerpc and x86 *if* CET/IBT is enabled.
-- Steve
Quoted 6 lines
kernel/livepatch/patch.c +1
-2{ -return faddr; +return ftrace_location_range (faddr, faddr +16 ); } -#endif
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · On Tue, 23 Nov 2021 12:39:15 +0100 (CET) Miroslav Benes <[email protected]> wrote:
+++
b/kernel/livepatch/patch.c(quoted diff) +6
-2@@ -127,15 +127,18 @@ static void notrace klp_ftrace_handler(unsigned long ip, /* * Convert a function address into the appropriate ftrace location. * - * Usually this is just the address of the function, but on some architectures - * it's more complicated so allow them to provide a custom behaviour. + * Usually this is just the address of the function, but there are some + * exceptions. + * + * * PPC - live patch works only with -mprofile-kernel. In this case, + * the ftrace location is always within the first 16 bytes. + * * x86_64 with CET/IBT enabled - there is ENDBR instruction at +0 offset.
Quoted 8 lines
Review static void notrace klp_ftrace_handler(unsigned long ip, · line 138
Quoted 5 lines
(quoted diff) +2
-2+ * __fentry__ follows it. */ -#ifndef klp_get_ftrace_location -static unsigned long klp_get_ftrace_location(unsigned long faddr) +static inline unsigned long klp_get_ftrace_location(unsigned long faddr)Why make this the default function? It should only do this for powerpc and x86 *if* CET/IBT is enabled.
Well, only this variant of IBT. Once Joao gets his clang patches together we'll probably have it back at +0.
Something like the below would be more robust, it also gets us something grep-able for when the IBT code-gen changes yet again.
arch/x86/include/asm/livepatch.h +9 -0
@@ -17,4 +17,13 @@ static inline void klp_arch_set_pc(struct ftrace_regs *fregs, unsigned long ip) ftrace_instruction_pointer_set(fregs, ip); } +#define klp_get_ftrace_location klp_get_ftrace_location +static inline unsigned long klp_get_ftrace_location (unsigned long faddr) +{ +unsigned long addr =faddr_location (faddr); +if (!addr &&IS_ENABLED (CONFIG_X86_IBT )) + addr =faddr_location (faddr +4 ); +return addr; +} +#endif /* _ASM_X86_LIVEPATCH_H */
kernel/livepatch/patch.c +1 -1
@@ -133,7 +133,7 @@ static void notrace klp_ftrace_handler(unsigned long ip,#ifndef klp_get_ftrace_locationstatic unsigned long klp_get_ftrace_location (unsigned long faddr) { -return faddr; +return ftrace_location (faddr); }#endif
Miroslav Benes
maintainerQuoted 17 lines · Peter Zijlstra
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · On Tue, 23 Nov 2021 12:39:15 +0100 (CET) Miroslav Benes <[email protected]> wrote:
+++
b/kernel/livepatch/patch.c(quoted diff) +6
-2@@ -127,15 +127,18 @@ static void notrace klp_ftrace_handler(unsigned long ip, /* * Convert a function address into the appropriate ftrace location. * - * Usually this is just the address of the function, but on some architectures - * it's more complicated so allow them to provide a custom behaviour. + * Usually this is just the address of the function, but there are some + * exceptions. + * + * * PPC - live patch works only with -mprofile-kernel. In this case, + * the ftrace location is always within the first 16 bytes. + * * x86_64 with CET/IBT enabled - there is ENDBR instruction at +0 offset.
Quoted 8 lines
Quoted 8 lines
Review static void notrace klp_ftrace_handler(unsigned long ip, · line 138
Quoted 5 lines
(quoted diff) +2
-2+ * __fentry__ follows it. */ -#ifndef klp_get_ftrace_location -static unsigned long klp_get_ftrace_location(unsigned long faddr) +static inline unsigned long klp_get_ftrace_location(unsigned long faddr)Why make this the default function? It should only do this for powerpc and x86 *if* CET/IBT is enabled.
I thought that given it was a slow path anyway, it would be just nice to get rid of the special casing. We support only x86, powerpc and s390x after all.
And as Peter later pointed out, it would have the benefit to have a check even for default version.
Quoted 29 lines
Well, only this variant of IBT. Once Joao gets his clang patches together we'll probably have it back at +0.
Something like the below would be more robust, it also gets us something grep-able for when the IBT code-gen changes yet again.
arch/x86/include/asm/livepatch.h +9
-0@@ -17,4 +17,13 @@ static inline void klp_arch_set_pc(struct ftrace_regs *fregs, unsigned long ip) ftrace_instruction_pointer_set(fregs, ip); } +#define klp_get_ftrace_location klp_get_ftrace_location +static inline unsigned long klp_get_ftrace_location (unsigned long faddr) +{ +unsigned long addr =faddr_location (faddr); +if (!addr &&IS_ENABLED (CONFIG_X86_IBT )) + addr =faddr_location (faddr +4 ); +return addr; +} +#endif /* _ASM_X86_LIVEPATCH_H */ kernel/livepatch/patch.c +0
-0
kernel/livepatch/patch.c static void notrace klp_ftrace_handler(unsigned long ip, · line 138
Quoted 7 lines
kernel/livepatch/patch.c +1
-1@@ -133,7 +133,7 @@ static void notrace klp_ftrace_handler(unsigned long ip,#ifndef klp_get_ftrace_locationstatic unsigned long klp_get_ftrace_location (unsigned long faddr) { -return faddr; +return ftrace_location (faddr); }#endif
Yes, this looks nice.
Thanks
Miroslav
Joe Lawrence
maintainerQuoted 40 lines · Miroslav Benes
[ adding more CCs ]
Quoted 3 lines · On Mon, 22 Nov 2021, [email protected] wrote:
Hi Miroslav, Petr and Nicolai,
Long time no talk, I hope you are all still doing great :)
Everything great here :)
Quoted 13 lines
So, we have been cooking a few patches for enabling Intel CET/IBT support in the kernel. The way IBT works is -- whenever you have an indirect branch, the control-flow must land in an endbr instruction. The idea is to constrain control-flow in a way to make it harder for attackers to achieve meaningful computation through pointer/memory corruption (as in, an attacker that can corrupt a function pointer by exploiting a memory corruption bug won't be able to execute whatever piece of code, being restricted to jump into endbr instructions). To make the allowed control-flow graph more restrict, we are looking into how to minimize the number of endbrs in the final kernel binary -- meaning that if a function is never called indirectly, it shouldn't have an endbr instruction, thus increasing the security guarantees of the hardware feature.
Quoted 2 lines
Some ref about what is going on --
https://lore.kernel.org/lkml/[email protected]/T/Yes, I noticed something was happening again. There was a thread on this in February https://lore.kernel.org/all/[email protected]/
and some concerns were raised back then around fentry and int3 patching if I remember correctly. Is this still an issue?Quoted 9 lines
IIRC, live-patching used kallsyms/kallsyms_lookup_name for grabbing pointers to the symbols in the running kernel and then used these pointers to invoke the functions which reside outside of the live-patch (ie. previously existing functions). With the above IBT support, if these functions were considered non-indirectly-reachable, and were suppressed of an endbr, this would lead into a crash. I remember we were working on klp-convert to fix this through special relocations and that there were other proposals... but I'm not sure where it went.
Quoted 9 lines
Quoted 3 lines
So, would you mind giving a quick update on the general state of this? If the IBT support would break this (and anything else) regarding live-patching... and so on?
Right. So, this really depends on how downstream consumers approach this. kpatch-build should be fine if I am not mistaken, because it uses the .klp.rela support we have in the kernel. We (at SUSE) have a problem, because we still exploit kallsyms/kallsyms_lookup_name to cope with this.
Hi Miroslav, Joao,
Yep, kpatch-build uses its own klp-relocation conversion and not kallsyms.
I'm not familiar with CET/IBT, but it sounds like if a function pointer is not taken at build time (or maybe some other annotation), the compiler won't generate the needed endbr landing spot in said function?
And that would be a problem for modules using kallsyms lookup to get to
said function.
Quoted 3 lines
Joe, what is the current state of klp-convert? Do we still want to follow that way?
kpatch-build relies on klp-relocations and they work well enough across the architectures we support. Its code is slightly different than klp-convert, but concepts the same. Moving all of that into the kernel build would definitely be better from a maintenance POV.
I can rebase the latest klp-convert patchset that I had worked on if we want to test and review. Apologies for losing momentum on that patchset while waiting for .klp.arch stuff to drop out, in particular the subset of static key relocations would still be supported.
Signature · 1 line
Joe
Quoted 5 lines · Joe Lawrence
Yep, kpatch-build uses its own klp-relocation conversion and not kallsyms.
I'm not familiar with CET/IBT, but it sounds like if a function pointer is not taken at build time (or maybe some other annotation), the compiler won't generate the needed endbr landing spot in said function?
Currently it does, but then I'm having objtool scribble it on purpose.
Quoted 2 lines
And that would be a problem for modules using kallsyms lookup to get to
said function.
Which is ofcourse the whole purpose of the exercise. If it's not exported you don't get to call it via a back-door :-) This should kill a whole heap of dodgy modules quite dead I hope.
Joe Lawrence
maintainerQuoted 10 lines · Peter Zijlstra
Quoted 5 lines · Joe Lawrence
Yep, kpatch-build uses its own klp-relocation conversion and not kallsyms.
I'm not familiar with CET/IBT, but it sounds like if a function pointer is not taken at build time (or maybe some other annotation), the compiler won't generate the needed endbr landing spot in said function?
Currently it does, but then I'm having objtool scribble it on purpose.
Hi Peter -- to follow up on the objtool part: what criteria is used to determine that it may scribble out the endbr?
Thanks,
Signature · 1 line
Joe
Quoted 9 lines · Joe Lawrence
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Joe Lawrence
Yep, kpatch-build uses its own klp-relocation conversion and not kallsyms.
I'm not familiar with CET/IBT, but it sounds like if a function pointer is not taken at build time (or maybe some other annotation), the compiler won't generate the needed endbr landing spot in said function?
Quoted 5 lines
Quoted 2 lines
Currently it does, but then I'm having objtool scribble it on purpose.
Hi Peter -- to follow up on the objtool part: what criteria is used to determine that it may scribble out the endbr?
ENDBR is "scribbled" for any function for which no function pointer data relocation exists at vmlinux or module link time.
https://lkml.kernel.org/r/[email protected]
Signature · 1 line
Josh
Miroslav Benes
maintainerHi,
Quoted 8 lines
Quoted 3 lines
Joe, what is the current state of klp-convert? Do we still want to follow that way?
kpatch-build relies on klp-relocations and they work well enough across the architectures we support. Its code is slightly different than klp-convert, but concepts the same. Moving all of that into the kernel build would definitely be better from a maintenance POV.
Ok, then it makes sense to me to pursue this.
Quoted 2 lines
I can rebase the latest klp-convert patchset that I had worked on if we want to test and review.
Yes, please. I think we should take a fresher look and start working on it (again).
Quoted 3 lines
Apologies for losing momentum on that patchset
while waiting for .klp.arch stuff to drop out, in particular the subset of static key relocations would still be supported.
No need to apologize. Thank you for maintaining the patch set.
Miroslav