3 ms·
Avoiding Retpolines with Static Calls
- saagarjha 7y ago> Changing the target of a static call requires patching the code of the running kernel, which is an expensive operation. Am I misunderstanding, or is this literally patching code pages at runtime? This sounds like it could be dangerous…
- asveikau 7y agoSeems like it could be done safely... Remove X bit and add write bit. Patch. Remove write bit and add X bit. Tricky part: If you get a protection fault trying to jump there, spin until the X bit is re-added. So yeah. That is going to be a hefty performance cost to write.
- barrkel 7y agoSeems safer to me to park CPUs in known safe spots where they can spin until modification is complete. CPUs could check if there's a pending patch when they attempt to reschedule, and when all CPUs apart from the patching CPU are safe, then the patch can go ahead. Trying to retry an operation (so it's transparent from the user's perspective) or reset state to the way it was at the time of the protection fault sounds harder to my mind. PS: I looked at the ftrace doc linked above, and it's architecture dependent of course, but for ftrace's purposes, they use generally use breakpoints on the place being modified.
- ghusbands 7y agoOne technique some use is to map the same page in two locations, so you've got a distinct execution address and writing address, both pointing at the same page. That way, you get no protection faults and you don't remap memory repeatedly.
- rrss 7y agoYes. The kernel does this quite a bit. See https://lwn.net/Articles/620640/ https://lwn.net/Articles/620640/ for the port of the alternative patching framework to arm64, and https://patchwork.kernel.org/patch/6859741/ https://patchwork.kernel.org/patch/6859741/ for an example using it to use the ARM v8.1 LSE atomics when available. Another use case is kprobes.
- rwmj 7y agoDynamic ftrace too (see the explanation for how it works here: https://www.kernel.org/doc/Documentation/trace/ftrace.txt https://www.kernel.org/doc/Documentation/trace/ftrace.txt). I thought that paravirt-ops does patching of the kernel at boot time, but I can't find any reference for it right now.
- vardump 7y agoWhether it is dangerous or not depends entirely on implementation details. There are ways to do runtime patching safely. Obviously code that ends up in production kernel would never use unsafe methods.
- jfkebwjsbx 7y agoWhy would it be dangerous?
- flqn 7y agoI'm assuming concurrency concerns of trying to execute code that is mid-patch
- jfkebwjsbx 7y agoKernel developers have to deal with concurrency, interrupts, preemption and more on a daily basis, so why would this be more dangerous?
- asveikau 7y agoSome people are really against writable executable pages for security reasons. Eg. 20 years ago executable stacks were common, and people like OpenBSD and others made a stink about removing it. The W^X mitigations. More recently, Apple got really strongly into the "no executable writeable pages" stance, banning JITs from the app store and a bunch of other somewhat unreasonable stuff that is really about not letting apps download code without going through an Apple reviewer, rather than a practical security concern. Since that time there seem to be mildly dubious or cargo cult hate for writable code in some circles, who insist it's legit security practices and not a walled garden enforcement mechanism.
- jfkebwjsbx 7y agoThis is kernel space. If you have a way to overwrite arbitrary kernel memory all bets are off, even with no modifiable code. Apple's policies are dictated by commercial reasons, not technical, so not much to discuss there.
- asveikau 7y agoYes. But people rush into a kneejerk defense of superego (Apple is right all the time) and think that rewriting code pages is universally evil. And to be fair it is tricky and slow.