4 ms·
The code for xnu_override (the library the author wrote to patch functions in the kernel) is wrong for a number of serious reasons. It assumes (with no verific
by dmitrygr 9y ago
The code for xnu_override (the library the author wrote to patch functions in the kernel) is wrong for a number of serious reasons.
It assumes (with no verification) that the first twelve bytes of any kernel function you could want to patch are all one basic block and do not contain a jump target or a relative branch. These reasons ARE why live patching is hard and why proper support for it is basically required in original code (making sure each func starts with a basic block at least twelve bytes long). Without that, it's quite a dangerous game to play in kernel space.
Furthermore, it assumes RAX is safe to clobber. This may not be so. Compilers are generally free to ignore standard ABI when calling provably-static provably-leaf functions. Who said RAX doesn't have a useful value on entry to patched function?
Additionally, it edits cr0 and cr3 with interrupts enabled, allowing it to be preempted during this action. If said preemption happens, another edit to those registers could happen in between. Scary, since what's being done is a non-atomic read-modify-write on CPU control registers.
Strongly advise anyone from running this on any system.
- munin 9y ago> Strongly advise anyone from running this on any system. Oh come on. Your para 2-4 are serious conceptual defects with xnu_override in general, but this is the kind of hacker shit you'd expect to find in something that will live patch your kernel code. At the point in time you find yourself turning off SIP to load some code to hot patch your kernel, you've long since crossed the Rubicon of caring about running particularly good code. The odds are good that the functions this thing is set up patch do hold with your first two invariants (RAX is scratch, there's 12 bytes in the entry BB). The third is the race, and you'll probably win this race most of the time. "You'll probably win this race most of the time" shouldn't be in any serious engineering design document and is probably grounds for demotion or firing if you bring it up as a defense during a design or code review, but that isn't what's going on here. This is some hacker shit posted to github.
- dmitrygr 9y agoAnd if this project was one monolithic thing, I'd agree. But the page sells this xnu_override as a "reusable library" which might lead someone to try to reuse it in another place. This is scary and hence my warning. Hell, tomorrow's apple 10.13.0.0.0.0.0.0.minor.insignificant.0.1 patch could change these "invariants"
- munin 9y agoYou're right, and I was reacting poorly to the "don't run this on anything" statement. It's some hacker magic off github, caveat empor. You're also right that future updates could break it. It's brittle. I won't be running it myself. I bet, though, that 99% of the time, it works every time, and for its use case, that's "good enough." Hot patching the kernel is scary, though, and hopefully no one walks away from looking at that github thinking "oh yes, I know so many problems that can be solved by hot patching the running kernel" because honestly getting the act of patching the physical code itself is only the start of your troubles.
- rgovostes 9y agoAuthor here. Yes, the issues you pointed out are mostly already captured via // TODO and // FIXME comments. The library was written in about a day. I could probably add the basic block length check (as it is, I just check for 0xC3), but I wasn't able to readily figure out how to disable interrupts during patching. If you have a suggestion, I'll implement it. The nice thing about a reusable library is that safety improvements can be made and shared with other clients. (As an aside, can I suggest posting an issue to GitHub? I might not have seen this thread, and xnu_override users who want to know of its shortcomings should also be able to find your comments easily.) I hope that someone who has the know-how to reverse engineer the kernel, devise a patch to add desired behavior, and write a kernel extension to implement that behavior, will understand that patching the kernel is inherently a dangerous thing to do—and will actually test that their change works. I wrote this as an alternative to other patchers which provide a string search and replace on the system kexts. Reverting these patches, dealing with the code signing fallout, and updating them for a new release are significantly more challenging with this style of patching.
- dmitrygr 9y agoI don't GitHub, but here are some thoughts: you only need to disable ints when you mess with cr0 and cr3 and applying patches. There are apis in the kernel for that. Basic block detection isn't easy. You CAN find branches in the block, and even patch them to work when you copy them elsewhere (you'll need this since most x86 branches are relative). There is provably NO 100% certain way to make sure no branch anywhere else targets those 12 bytes though. You can use heuristics to almost-reliably guess though. If there is a "call", the instruction after it likely is a target of an indirect branch (ret). Also you'll have to disassemble the entire function (breadth first search till every traversal path hits a "ret") and see if any jumps to these 12 bytes exist. This will cover 99.99% of cases. The remaining 0.01% is VERY hard.