bpf: avoid setting bpf insns pages read-only when prog is jited
Subsystems (4)
Daniel Borkmann
maintainerPATCH 1/1 bpf: avoid setting bpf insns pages read-only when prog is jited
For the case where the interpreter is compiled out or when the prog is jited it is completely unnecessary to set the BPF insn pages as read-only. In fact, on frequent churn of BPF programs, it could lead to performance degradation of the system over time since it would break the direct map down to 4k pages when calling set_memory_ro() for the insn buffer on x86-64 / arm64 and there is no reverse operation. Thus, avoid breaking up large pages for data maps, and only limit this to the module range used by the JIT where it is necessary to set the image read-only and executable.
--- include/linux/filter.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/filter.h +6 -2
@@ -776,8 +776,12 @@ bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default)static inline void bpf_prog_lock_ro (struct bpf_prog *fp) { -set_vm_flush_reset_perms (fp); -set_memory_ro ((unsigned long )fp, fp->pages); +#ifndef CONFIG_BPF_JIT_ALWAYS_ON +if (!fp->jited) { +set_vm_flush_reset_perms (fp); +set_memory_ro ((unsigned long )fp, fp->pages); + } +#endif }static inline void bpf_jit_binary_lock_ro (struct bpf_binary_header *hdr)
Signature · 1 line
2.17.1
Quoted 8 lines · Daniel Borkmann
For the case where the interpreter is compiled out or when the prog is jited it is completely unnecessary to set the BPF insn pages as read-only. In fact, on frequent churn of BPF programs, it could lead to performance degradation of the system over time since it would break the direct map down to 4k pages when calling
set_memory_ro()for the insn buffer on x86-64 / arm64 and there is no reverse operation. Thus, avoid breaking up large pages for data maps, and only limit this to the module range used by the JIT where it is necessary to set the image read-only and executable.
Interesting... But why the non JIT case would need RO protection ?
Do you have any performance measures to share ?
Thanks.
Daniel Borkmann
maintainerQuoted 11 lines · Eric Dumazet
Quoted 8 lines · Daniel Borkmann
For the case where the interpreter is compiled out or when the prog is jited it is completely unnecessary to set the BPF insn pages as read-only. In fact, on frequent churn of BPF programs, it could lead to performance degradation of the system over time since it would break the direct map down to 4k pages when calling
set_memory_ro()for the insn buffer on x86-64 / arm64 and there is no reverse operation. Thus, avoid breaking up large pages for data maps, and only limit this to the module range used by the JIT where it is necessary to set the image read-only and executable.Interesting... But why the non JIT case would need RO protection ?
It was done for interpreter around 5 years ago mainly due to concerns from security folks that the BPF insn image could get corrupted (through some other bug in the kernel) in post-verifier stage by an attacker and then there's nothing really that would provide any sort of protection guarantees; pretty much the same reasons why e.g. modules are set to read-only in the kernel.
Quoted 1 line
Do you have any performance measures to share ?
No numbers, and I'm also not aware of any reports from users, but it was recently brought to our attention from mm folks during discussion of a different set:
https://lore.kernel.org/lkml/[email protected]/T/
Thanks,
Daniel
Alexei Starovoitov
AppliedQuoted 20 lines · Daniel Borkmann
Quoted 11 lines · Eric Dumazet
Quoted 8 lines · Daniel Borkmann
For the case where the interpreter is compiled out or when the prog is jited it is completely unnecessary to set the BPF insn pages as read-only. In fact, on frequent churn of BPF programs, it could lead to performance degradation of the system over time since it would break the direct map down to 4k pages when calling
set_memory_ro()for the insn buffer on x86-64 / arm64 and there is no reverse operation. Thus, avoid breaking up large pages for data maps, and only limit this to the module range used by the JIT where it is necessary to set the image read-only and executable.Interesting... But why the non JIT case would need RO protection ?
It was done for interpreter around 5 years ago mainly due to concerns from security folks that the BPF insn image could get corrupted (through some other bug in the kernel) in post-verifier stage by an attacker and then there's nothing really that would provide any sort of protection guarantees; pretty much the same reasons why e.g. modules are set to read-only in the kernel.
Quoted 6 lines
Quoted 1 line
Do you have any performance measures to share ?
No numbers, and I'm also not aware of any reports from users, but it was recently brought to our attention from mm folks during discussion of a different set:
Applied. Thanks
Quoted 19 lines · Daniel Borkmann
Quoted 11 lines · Eric Dumazet
Quoted 8 lines · Daniel Borkmann
For the case where the interpreter is compiled out or when the prog is jited it is completely unnecessary to set the BPF insn pages as read-only. In fact, on frequent churn of BPF programs, it could lead to performance degradation of the system over time since it would break the direct map down to 4k pages when calling
set_memory_ro()for the insn buffer on x86-64 / arm64 and there is no reverse operation. Thus, avoid breaking up large pages for data maps, and only limit this to the module range used by the JIT where it is necessary to set the image read-only and executable.Interesting... But why the non JIT case would need RO protection ?
It was done for interpreter around 5 years ago mainly due to concerns from security folks that the BPF insn image could get corrupted (through some other bug in the kernel) in post-verifier stage by an attacker and then there's nothing really that would provide any sort of protection guarantees; pretty much the same reasons why e.g. modules are set to read-only in the kernel.
Quoted 7 lines
Quoted 1 line
Do you have any performance measures to share ?
No numbers, and I'm also not aware of any reports from users, but it was recently brought to our attention from mm folks during discussion of a different set:
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have CONFIG_STRICT_MODULE_RWX (and CONFIG_STRICT_KERNEL_RWX) for that already.
Or are we saying we also want to get rid of them ?
Quoted 44 lines · Eric Dumazet
Quoted 3 lines · Daniel Borkmann
Quoted 2 lines · Eric Dumazet
Quoted 1 line · Daniel Borkmann
For the case where the interpreter is compiled out or when the prog
is jited
Quoted 1 line
Quoted 1 line
Quoted 1 line
it is completely unnecessary to set the BPF insn pages as
read-only. In fact,
Quoted 1 line
Quoted 1 line
Quoted 1 line
on frequent churn of BPF programs, it could lead to performance
degradation of
Quoted 1 line
Quoted 1 line
Quoted 1 line
the system over time since it would break the direct map down to 4k
pages when
Quoted 1 line
Quoted 1 line
Quoted 1 line
calling
set_memory_ro()for the insn buffer on x86-64 / arm64 andthere is no
Quoted 1 line
Quoted 1 line
Quoted 1 line
reverse operation. Thus, avoid breaking up large pages for data
maps, and only
Quoted 1 line
Quoted 1 line
Quoted 1 line
limit this to the module range used by the JIT where it is
necessary to set
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
the image read-only and executable.
Interesting... But why the non JIT case would need RO protection ?
It was done for interpreter around 5 years ago mainly due to concerns
from security
Quoted 1 line
folks that the BPF insn image could get corrupted (through some other
bug in the
Quoted 1 line
kernel) in post-verifier stage by an attacker and then there's
nothing really that
Quoted 1 line
would provide any sort of protection guarantees; pretty much the same
reasons why
Quoted 5 lines
e.g. modules are set to read-only in the kernel.
Quoted 1 line
Do you have any performance measures to share ?
No numbers, and I'm also not aware of any reports from users, but it
was recently
Quoted 1 line
brought to our attention from mm folks during discussion of a
different set:
Quoted 2 lines
https://lore.kernel.org/lkml/[email protected]/T/
Quoted 1 line
Quoted 8 lines
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have
CONFIG_STRICT_MODULE_RWX(andCONFIG_STRICT_KERNEL_RWX) for that already.Or are we saying we also want to get rid of them ?
The notion is that for security there should never been a page which is both writable and executable at the same time. This makes it harder to inject code.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Quoted 7 lines · Eric Dumazet
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have
CONFIG_STRICT_MODULE_RWX(andCONFIG_STRICT_KERNEL_RWX) for that already.Or are we saying we also want to get rid of them ?
No, in fact I'm working on making that stronger. We currently still have a few cases that violate the W^X rule.
The thing is, when the BPF stuff is JIT'ed, the actual BPF instruction page is not actually executed at all, so making it RO serves no purpose, other than to fragment the direct map.
All actual code lives in the 2G range that x86_64 can directly branch to, but this BPF instruction stuff lives in the general data heap and can thus cause much more fragmentation of the direct map.
Daniel Borkmann
maintainerQuoted 8 lines · Peter Zijlstra
Quoted 6 lines · Eric Dumazet
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have
CONFIG_STRICT_MODULE_RWX(andCONFIG_STRICT_KERNEL_RWX) for that already.
Quoted 8 lines
Quoted 1 line
Or are we saying we also want to get rid of them ?
No, in fact I'm working on making that stronger. We currently still have a few cases that violate the W^X rule.
The thing is, when the BPF stuff is JIT'ed, the actual BPF instruction page is not actually executed at all, so making it RO serves no purpose, other than to fragment the direct map.
Yes exactly, in that case it is only used for dumping the BPF insns back to user space and therefore no need at all to set it RO. (The JITed image however *is* set as RO. - Perhaps there was some confusion given your earlier question.)
Thanks,
Daniel
Quoted 15 lines · Daniel Borkmann
Quoted 13 lines · Peter Zijlstra
Quoted 7 lines · Eric Dumazet
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have
CONFIG_STRICT_MODULE_RWX(andCONFIG_STRICT_KERNEL_RWX) for that already.Or are we saying we also want to get rid of them ?
No, in fact I'm working on making that stronger. We currently still have a few cases that violate the W^X rule.
Quoted 8 lines
Quoted 3 lines
The thing is, when the BPF stuff is JIT'ed, the actual BPF instruction page is not actually executed at all, so making it RO serves no purpose, other than to fragment the direct map.
Yes exactly, in that case it is only used for dumping the BPF insns back to user space and therefore no need at all to set it RO. (The JITed image however *is* set as RO. - Perhaps there was some confusion given your earlier question.)
May be we should also flip the default to net.core.bpf_jit_enable=1 for x86-64 ? and may be arm64 ? These two JITs are well tested and maintained.
Daniel Borkmann
maintainerQuoted 19 lines · Alexei Starovoitov
Quoted 18 lines · Daniel Borkmann
Quoted 16 lines · Peter Zijlstra
Quoted 7 lines · Eric Dumazet
Thanks for the link !
Having RO protection as a debug feature would be useful.
I believe we have
CONFIG_STRICT_MODULE_RWX(andCONFIG_STRICT_KERNEL_RWX) for that already.Or are we saying we also want to get rid of them ?
No, in fact I'm working on making that stronger. We currently still have a few cases that violate the W^X rule.
The thing is, when the BPF stuff is JIT'ed, the actual BPF instruction page is not actually executed at all, so making it RO serves no purpose, other than to fragment the direct map.
Quoted 8 lines
Quoted 4 lines
Yes exactly, in that case it is only used for dumping the BPF insns back to user space and therefore no need at all to set it RO. (The JITed image however *is* set as RO. - Perhaps there was some confusion given your earlier question.)
May be we should also flip the default to net.core.bpf_jit_enable=1 for x86-64 ? and may be arm64 ? These two JITs are well tested and maintained.
Seems reasonable given their status and exposure they've had over the years. I can follow-up on that.
Thanks,
Daniel