I was on board until `PROT_READ | PROT_WRITE | PROT_EXEC`
Writing a Self-Mutating x86_64 C Program (2013)
31–40 of 61 posts
Re: Writing a Self-Mutating x86_64 C Program (2013)
#32Earlier quoted context omitted.
Sometimes they will also make small changes to code that was generated earlier, typically by changing a branch instruction to point somewhere else. Dynamic linkers may do that, too, though glibc doesn't normally do it, as far as I know: it prefers to update a pointer to code: same result without needing memory that is both writable and executable and without having to invalidate the instruction cache.
From my delve into such things, in Linux at least the linker is a binary called before the program runs, so its a separate program potentially modifying the code. Interesting philosophical question whether that's still self modifying though.
Assuming its because you think I'm wrong about the separate program. Look up the manpage for ld.so .
If you run the strings program on a dynamically linked program the first thing it spits out should be the path to ld.so
If you run that program without arguments, it even gives you a usage message.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#33Earlier quoted context omitted.
> Are there any languages that could actually make use of self modifying code? Kernel livepatching (and by that ftrace) would come to mind.
Would they not just write a new patched page, and swap out the original? Seems less error prone?
Then to add tracing or change the behavior of the function, the noops are overwritten by a jump to the new code.
Re: Writing a Self-Mutating x86_64 C Program (2013)
#34I once read about a clever use of self-modifying x86 code. The 8086 and 8088 are nearly identical chips, with the difference being that the 8086 has 16-bit I/O and the 8088 has 8-bit. The only way for a program to know which chip it's running on is to write a bit of self modifying code that takes advantage of this difference in I/O size. Both chips use prefetch, but they prefetch words, not bytes, and 8086 words are…
I think you probably read it in Dr Dobbs: http://www.drdobbs.com/embedded-systems/processor-detection-... Not exactly what you described, but very similar: "Differentiating between 8088s and 8086s is trickier. The easiest way I've found to do it is to modify code that's five bytes ahead of IP. Since the prefetch queue of an 8088 is four bytes and the prefetch queue of an 8086 is six bytes, an instruction five bytes a…
Re: Writing a Self-Mutating x86_64 C Program (2013)
#35Re: Writing a Self-Mutating x86_64 C Program (2013)
#36Re: Writing a Self-Mutating x86_64 C Program (2013)
#37Re: Writing a Self-Mutating x86_64 C Program (2013)
#38Re: Writing a Self-Mutating x86_64 C Program (2013)
#39Earlier quoted context omitted.
> Are there any languages that could actually make use of self modifying code? Kernel livepatching (and by that ftrace) would come to mind.
Would they not just write a new patched page, and swap out the original? Seems less error prone?
Re: Writing a Self-Mutating x86_64 C Program (2013)
#40Earlier quoted context omitted.
The real issue with self-modifying code is that it's basically a security bug waiting to happen, plus you have other complications like having to flush the instruction cache on architectures like ARM. Generally, the benefits are not worth it unless except for very specific cases.
I've seen points like these made before, but never really understood how. Can you give an example of self-modifying code becoming a security issue?